diff --git a/src/UnicaenAuth/View/Helper/UserAbstract.php b/src/UnicaenAuth/View/Helper/UserAbstract.php index a571dca8e41460fa075e9c81363e83e55b469441..94cd8f4860b65f82d0d05b157e0704a9d27ccea4 100644 --- a/src/UnicaenAuth/View/Helper/UserAbstract.php +++ b/src/UnicaenAuth/View/Helper/UserAbstract.php @@ -1,19 +1,25 @@ <?php namespace UnicaenAuth\View\Helper; +use Zend\I18n\View\Helper\AbstractTranslatorHelper; +use Zend\Authentication\AuthenticationService; +use Zend\View\Exception\InvalidArgumentException; + /** * Classe mère des aides de vue concernant l'utilisateur connecté. * * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> */ -abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHelper +abstract class UserAbstract extends AbstractTranslatorHelper { protected $authService; /** * Constructeur. + * + * @param AuthenticationService $authService */ - public function __construct(\Zend\Authentication\AuthenticationService $authService = null) + public function __construct(AuthenticationService $authService = null) { if (null !== $authService) { $this->setAuthService($authService); @@ -48,7 +54,7 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel * @param string $preferedKey * @return mixed */ - public function getIdentity($preferedKey = 'ldap') + public function getIdentity($preferedKey = null) { if (!$this->getAuthService() || !$this->getAuthService()->hasIdentity()) { return null; @@ -57,21 +63,25 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel $identity = $this->getAuthService()->getIdentity(); if (is_array($identity)) { - $preferedKey = $preferedKey ?: 'ldap'; - if (isset($identity[$preferedKey])) { - $identity = $identity[$preferedKey]; - } - elseif (isset($identity['ldap'])) { - $identity = $identity['ldap']; + $keys = array('ldap', 'db'); + if ($preferedKey) { + // on met la clé spécifiée en tête de liste + $keys = array_merge(($tmp = array($preferedKey)), array_diff($keys, $tmp)); } - elseif (isset($identity['db'])) { - $identity = $identity['db']; + $found = null; + foreach ($keys as $key) { + if (isset($identity[$key])) { + $found = $identity[$key]; + break; + } } - else { - throw new \InvalidArgumentException("Données d'identité invalides."); + if (null === $found) { + throw new InvalidArgumentException( + "Aucune des clés suivantes n'a été trouvée dans les données d'identité: " . implode(", ", $keys) . "."); } + $identity = $found; } return $identity; } -} +} \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/UserAbstractTest.php b/tests/UnicaenAuthTest/View/Helper/UserAbstractTest.php index 589381895cdf7083a6e9566a721c6904f4086994..a9c0e0c0a68a284e07712c79a56408c4bf7f0597 100644 --- a/tests/UnicaenAuthTest/View/Helper/UserAbstractTest.php +++ b/tests/UnicaenAuthTest/View/Helper/UserAbstractTest.php @@ -27,6 +27,12 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase $this->helper->setAuthService($this->authService); } + public function testCanConstructWithAuthServiceSpecified() + { + $helper = $this->getMockForAbstractClass('UnicaenAuth\View\Helper\UserAbstract', array($this->authService)); + $this->assertSame($this->authService, $helper->getAuthService()); + } + public function testCanSetAuthService() { $this->assertSame($this->authService, $this->helper->getAuthService()); @@ -61,9 +67,18 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase public function provideValidArrayIdentity() { return array( - array(array('db' => 'Db Identity'), 'Db Identity'), - array(array('ldap' => 'Ldap Identity'), 'Ldap Identity'), - array(array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'), 'Db Identity'), + 'db-only' => array( + array('db' => 'Db Identity'), + 'Db Identity', + ), + 'ldap-only' => array( + array('ldap' => 'Ldap Identity'), + 'Ldap Identity', + ), + 'db-ldap' => array( + array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'), + 'Ldap Identity', + ), ); } @@ -83,6 +98,54 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase $this->assertEquals($expected, $this->helper->getIdentity()); } + public function provideValidArrayIdentityWithPreferedKey() + { + return array( + 'db-only-ldap-prefered' => array( + array('db' => 'Db Identity'), + 'ldap', // clé absente + 'Db Identity', + ), + 'ldap-only-db-prefered' => array( + array('ldap' => 'Ldap Identity'), + 'db', // clé absente + 'Ldap Identity', + ), + 'db-ldap-none-prefered' => array( + array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'), + null, // équivaut à 'ldap' + 'Ldap Identity', + ), + 'db-ldap-ldap-prefered' => array( + array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'), + 'ldap', + 'Ldap Identity', + ), + 'db-ldap-db-prefered' => array( + array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'), + 'db', + 'Db Identity', + ), + ); + } + + /** + * @dataProvider provideValidArrayIdentityWithPreferedKey + * @param array $identity + * @param string $preferedKey + * @param string $expected + */ + public function testGettingIdentityReturnsAuthServiceIdentityFromValidArrayIdentityWithPreferedKey($identity, $preferedKey, $expected) + { + $this->authService->expects($this->once()) + ->method('hasIdentity') + ->will($this->returnValue(true)); + $this->authService->expects($this->once()) + ->method('getIdentity') + ->will($this->returnValue($identity)); + $this->assertEquals($expected, $this->helper->getIdentity($preferedKey)); + } + public function provideInvalidArrayIdentity() { return array(