Commit 3d7cdb9b authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Storage d'authentification LdapDb : tests unitaires ; légères améliorations.

parent 4005d2a4
<?php
namespace UnicaenAuth\Authentication\Storage;
use UnicaenAuth\Entity\Ldap\PeopleAdapter;
......@@ -72,13 +71,19 @@ class LdapDb implements Storage\StorageInterface, ServiceManagerAwareInterface
$userId = $dbIdentity->getId();
$identity = $dbIdentity;
}
// moyen de savoir si l'utilisateur a été créé à partir de l'annuaire LDAP: mdp = 'ldap' dans la table
if (!$dbIdentity || $dbIdentity->getPassword() == 'ldap') {
$userExistsInDb = (bool) $dbIdentity;
$userExistsInLdap = $dbIdentity && $dbIdentity->getPassword() === 'ldap';
// NB: si le mot de passe est 'ldap', alors l'utilisateur a été créé à partir de l'annuaire LDAP
if (!$userExistsInDb || $userExistsInLdap) {
// on s'attend à ce que les données en session soient l'entité LDAP correspondant à l'utilisateur connecté
$ldapIdentity = $this->getLdapStorage()->read();
if ($ldapIdentity) {
$identity = new PeopleAdapter($ldapIdentity->getData(), $userId);
}
}
return $identity;
}
......@@ -195,4 +200,4 @@ class LdapDb implements Storage\StorageInterface, ServiceManagerAwareInterface
}
return $this->options;
}
}
}
\ No newline at end of file
<?php
namespace UnicaenAuthTest\Authentication\Storage;
use PHPUnit_Framework_TestCase;
use UnicaenApp\Entity\Ldap\People;
use UnicaenAppTest\Entity\Ldap\TestAsset\People as PeopleTestAsset;
use UnicaenAuth\Authentication\Storage\Db;
use UnicaenAuth\Authentication\Storage\Ldap;
use UnicaenAuth\Authentication\Storage\LdapDb;
use Zend\ServiceManager\ServiceManager;
use ZfcUser\Entity\User;
/**
* Description of LdapDbTest
*
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class LdapDbTest extends PHPUnit_Framework_TestCase
{
protected $storage;
protected $serviceManager;
protected $options;
protected $dbStorage;
protected $ldapStorage;
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->options = $options = new \UnicaenAuth\Options\ModuleOptions();
$this->serviceManager = new ServiceManager();
$this->serviceManager->setFactory('unicaen-auth_module_options', function() use ($options) {
return $options;
});
$this->dbStorage = $this->getMock('UnicaenAuth\Authentication\Storage\Db', array('read'));
$this->ldapStorage = $this->getMock('UnicaenAuth\Authentication\Storage\Ldap', array('read'));
$this->storage = new LdapDb();
$this->storage->setDbStorage($this->dbStorage)
->setLdapStorage($this->ldapStorage)
->setServiceManager($this->serviceManager);
}
public function testInnerStoragesAreNullByDefault()
{
$storage = new LdapDb();
$this->assertNull($storage->getDbStorage());
$this->assertNull($storage->getLdapStorage());
}
public function testStorageIsEmptyOnlyIfInnerStoragesAreBothEmpty()
{
$this->storage->getDbStorage()->write('content');
$this->assertFalse($this->storage->isEmpty());
$this->storage->getDbStorage()->clear();
$this->storage->getLdapStorage()->write('content');
$this->assertFalse($this->storage->isEmpty());
$this->storage->getLdapStorage()->clear();
$this->assertTrue($this->storage->isEmpty());
}
public function testCanWriteToInnerStorages()
{
$entity = new User();
$this->storage->setDbStorage(new Db());
$this->storage->setLdapStorage(new Ldap());
$this->storage->write($entity);
$this->assertFalse($this->storage->isEmpty());
$this->assertFalse($this->storage->getDbStorage()->isEmpty());
$this->assertFalse($this->storage->getLdapStorage()->isEmpty());
$this->assertEquals($entity, $this->storage->getDbStorage()->read());
$this->assertEquals($entity, $this->storage->getLdapStorage()->read());
$this->storage->clear();
$this->assertTrue($this->storage->isEmpty());
$this->assertTrue($this->storage->getDbStorage()->isEmpty());
$this->assertTrue($this->storage->getLdapStorage()->isEmpty());
}
public function testReadingReturnsStoredDbUser()
{
$entity = new User();
$this->dbStorage->expects($this->once())
->method('read')
->will($this->returnValue($entity));
$result = $this->storage->read();
$this->assertSame($entity, $result);
}
public function testReadingReturnsLdapUserAdapterIfNoDbUserFound()
{
$entity = new People(PeopleTestAsset::$data1);
$this->dbStorage->expects($this->once())
->method('read')
->will($this->returnValue(null));
$this->ldapStorage->expects($this->once())
->method('read')
->will($this->returnValue($entity));
$result = $this->storage->read();
$this->assertInstanceOf('UnicaenAuth\Entity\Ldap\PeopleAdapter', $result);
$this->assertInstanceOf('UnicaenApp\Entity\Ldap\People', $result);
$this->assertNull($result->getId());
}
public function testReadingReturnsNullIfNoUserFoundInDbOrLdap()
{
$this->dbStorage->expects($this->once())
->method('read')
->will($this->returnValue(null));
$this->ldapStorage->expects($this->once())
->method('read')
->will($this->returnValue(null));
$result = $this->storage->read();
$this->assertNull($result);
}
public function testReadingReturnsLdapUserAdapterIfDUserFoundInDbAndLdap()
{
$dbEntity = new User();
$dbEntity->setId(12)
->setPassword('ldap'); // requis
$ldapEntity = new People(PeopleTestAsset::$data1);
$this->dbStorage->expects($this->once())
->method('read')
->will($this->returnValue($dbEntity));
$this->ldapStorage->expects($this->once())
->method('read')
->will($this->returnValue($ldapEntity));
$result = $this->storage->read();
$this->assertInstanceOf('UnicaenAuth\Entity\Ldap\PeopleAdapter', $result);
$this->assertInstanceOf('UnicaenApp\Entity\Ldap\People', $result);
$this->assertEquals($result->getId(), $dbEntity->getId());
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment