diff --git a/src/UnicaenAuth/View/Helper/UserStatus.php b/src/UnicaenAuth/View/Helper/UserStatus.php index 53afbcd56d9e4c4593889e0b21e2f6ea711b6cda..35956493a5a09d54d2b163b28c04dd674a0482f3 100644 --- a/src/UnicaenAuth/View/Helper/UserStatus.php +++ b/src/UnicaenAuth/View/Helper/UserStatus.php @@ -2,11 +2,14 @@ namespace UnicaenAuth\View\Helper; /** - * View helper de rendu des éléments concernant le statut de connexion à l'appli. - * A savoir : - * - si un utilisateur est connecté: l'identité de l'utilisateur connecté et un - * lien pointant vers la demande de déconnexion - * - sinon: un lien pointant vers le formulaire de connexion + * Aide de vue générant les éléments concernant le statut de connexion à l'appli + * de l'utilisateur. + * + * À savoir : + * - Si un utilisateur est connecté : l'identité de l'utilisateur connecté et + * éventuellement le lien pointant vers l'URL de déconnexion. + * - Si aucun utilisateur n'est connecté : le libellé "Aucun" et éventuellement + * le lien pointant vers l'URL de connexion. * * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr> */ @@ -21,7 +24,7 @@ class UserStatus extends UserAbstract * Retourne l'instance de ce view helper. * * @param boolean $displayConnectionLink Inclure ou pas le lien de connexion/déconnexion ? - * @return UserStatus + * @return self */ public function __invoke($displayConnectionLink = true) { @@ -29,28 +32,6 @@ class UserStatus extends UserAbstract return $this; } - /** - * Indique si le lien de connexion/déconnexion est affiché ou non - * - * @return boolean - */ - public function getDisplayConnectionLink() - { - return $this->displayConnectionLink; - } - - /** - * Affiche ou non le lien de connexion/déconnexion - * - * @param boolean $displayConnectionLink - * @return UserStatus - */ - public function setDisplayConnectionLink($displayConnectionLink) - { - $this->displayConnectionLink = $displayConnectionLink; - return $this; - } - /** * Retourne le code HTML généré par cette aide de vue. * @@ -58,12 +39,17 @@ class UserStatus extends UserAbstract */ public function __toString() { - $out = $this->createStatusContainer(); + $parts = array(); + + $parts[] = $this->createStatusContainer(); if ($this->getDisplayConnectionLink()) { - $out .= " | " . $this->getView()->userConnection(); + $userConnectionHelper = $this->getView()->plugin('userConnection'); /* @var $userConnectionHelper UserConnection */ + $parts[] = (string) $userConnectionHelper; } + $out = implode(' | ', $parts); + return $out; } @@ -73,10 +59,9 @@ class UserStatus extends UserAbstract */ protected function createStatusContainer() { - $template = '<strong>%s</strong>'; if (($identity = $this->getIdentity())) { if (method_exists($identity, '__toString')) { - $name = "" . $identity; + $name = (string) $identity; } elseif (method_exists($identity, 'getDisplayName')) { $name = $identity->getDisplayName(); @@ -88,17 +73,41 @@ class UserStatus extends UserAbstract $name = $identity->getId(); } else { - $title = $this->getView()->translate("Erreur: identité inattendue"); - $name = sprintf('<span title="' . $title . '%s">???</span>', - is_object($identity) ? " (classe " . get_class($identity) . ")" : null); + $name = sprintf('<span title="Erreur: identité inattendue (%s)">???</span>', + is_object($identity) ? get_class($identity) : gettype($identity)); } } - if (!isset($name) || !$name) { - $name = $this->getView()->translate("Aucun"); + else { + $name = _("Aucun"); + if ($this->getTranslator()) { + $name = $this->getTranslator()->translate($name, $this->getTranslatorTextDomain()); + } } - $out = sprintf($template, $name); + + $out = sprintf('<strong>%s</strong>', $name); return $out; } -} + /** + * Indique si le lien de connexion/déconnexion est affiché ou non + * + * @return boolean + */ + public function getDisplayConnectionLink() + { + return $this->displayConnectionLink; + } + + /** + * Affiche ou non le lien de connexion/déconnexion + * + * @param boolean $displayConnectionLink + * @return self + */ + public function setDisplayConnectionLink($displayConnectionLink = true) + { + $this->displayConnectionLink = $displayConnectionLink; + return $this; + } +} \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/UserStatusTest.php b/tests/UnicaenAuthTest/View/Helper/UserStatusTest.php new file mode 100644 index 0000000000000000000000000000000000000000..67759d8b644a0dc522ac4416d3343bf9bd56680d --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/UserStatusTest.php @@ -0,0 +1,213 @@ +<?php +namespace UnicaenAuthTest\View\Helper; + +use UnicaenApp\Entity\Ldap\People as LdapPeopleEntity; +use UnicaenAppTest\Entity\Ldap\TestAsset\People as LdapPeopleTestAsset; +use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader; +use UnicaenAuth\View\Helper\UserStatus; +use Zend\I18n\Translator\Translator; + +/** + * Description of UserProfileTest + * + * @property UserStatus $helper Description + * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> + */ +class UserStatusTest extends AbstractTest +{ + protected $helperClass = 'UnicaenAuth\View\Helper\UserStatus'; + + /** + * Sets up the fixture, for example, open a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + parent::setUp(); + + $this->authService = $this->getMock('Zend\Authentication\AuthenticationService', array('hasIdentity', 'getIdentity')); + + $userConnectionHelper = $this->getMock('UnicaenAuth\View\Helper\UserConnection', array('__toString')); + $userConnectionHelper->expects($this->any()) + ->method('__toString') + ->will($this->returnValue('UserConnection Helper Markup')); + + $this->helper->getView()->getHelperPluginManager()->setService('userConnection', $userConnectionHelper); + + $this->helper->setDisplayConnectionLink() + ->setAuthService($this->authService); + } + + public function testEntryPointReturnsSelfInstance() + { + $this->assertSame($this->helper, $this->helper->__invoke()); + } + + public function testEntryPointCanSetArgs() + { + $this->helper->__invoke($flag = true); + $this->assertSame($flag, $this->helper->getDisplayConnectionLink()); + } + + public function testRenderingWithoutConnectionLinkReturnsNoneIfNoIdentityAvailable() + { + $this->authService->expects($this->any()) + ->method('hasIdentity') + ->will($this->returnValue(false)); + + $this->helper->setDisplayConnectionLink(false); + + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected('user_status/no-identity-without-link.phtml'), $markup); + + // traduction + $this->helper->setTranslator($this->_getTranslator()); + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected('user_status/no-identity-without-link-translated.phtml'), $markup); + } + + public function getIdentityAndExpectedScript() + { + return array( + 'identity-to-string' => array( + new IdentityTestAsset1(), + 'user_status/identity-without-link.phtml', + 'user_status/identity-with-link.phtml', + ), + 'identity-get-displayname' => array( + new IdentityTestAsset2(), + 'user_status/identity-without-link.phtml', + 'user_status/identity-with-link.phtml', + ), + 'identity-get-username' => array( + new IdentityTestAsset3(), + 'user_status/identity-without-link.phtml', + 'user_status/identity-with-link.phtml', + ), + 'identity-get-id' => array( + new IdentityTestAsset4(), + 'user_status/identity-without-link.phtml', + 'user_status/identity-with-link.phtml', + ), + 'unexpected-identity' => array( + new \DateTime(), + 'user_status/unexpected-identity-without-link.phtml', + 'user_status/unexpected-identity-with-link.phtml', + ), + ); + } + + /** + * @dataProvider getIdentityAndExpectedScript + * @param mixed $identity + * @param string $expectedScriptWithoutLink + */ + public function testRenderingWithoutConnectLinkReturnsCorrectMarkupIfIdentityAvailable( + $identity, + $expectedScriptWithoutLink) + { + $this->authService->expects($this->any()) + ->method('hasIdentity') + ->will($this->returnValue(true)); + $this->authService->expects($this->any()) + ->method('getIdentity') + ->will($this->returnValue($identity)); + + $this->helper->setDisplayConnectionLink(false); + + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected($expectedScriptWithoutLink), $markup); + } + + public function testRenderingWithConnectionLinkReturnsNoneIfNoIdentityAvailable() + { + $this->authService->expects($this->any()) + ->method('hasIdentity') + ->will($this->returnValue(false)); + + $this->helper->setDisplayConnectionLink(true); + + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected('user_status/no-identity-with-link.phtml'), $markup); + + // traduction + $this->helper->setTranslator($this->_getTranslator()); + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected('user_status/no-identity-with-link-translated.phtml'), $markup); + } + + /** + * @dataProvider getIdentityAndExpectedScript + * @param mixed $identity + * @param string $expectedScriptWithoutLink + * @param string $expectedScriptWithLink + */ + public function testRenderingWithConnectLinkReturnsCorrectMarkupIfIdentityAvailable( + $identity, + $expectedScriptWithoutLink, + $expectedScriptWithLink) + { + $this->authService->expects($this->any()) + ->method('hasIdentity') + ->will($this->returnValue(true)); + $this->authService->expects($this->any()) + ->method('getIdentity') + ->will($this->returnValue($identity)); + + $this->helper->setDisplayConnectionLink(true); + + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected($expectedScriptWithLink), $markup); + } + + /** + * Returns translator + * + * @return Translator + */ + protected function _getTranslator() + { + $loader = new ArrayTranslatorLoader(); + $loader->translations = array( + "Aucun" => "None", + ); + + $translator = new Translator(); + $translator->getPluginManager()->setService('default', $loader); + $translator->addTranslationFile('default', null); + + return $translator; + } +} + +class IdentityTestAsset1 +{ + public function __toString() + { + return 'User identity'; + } +} + +class IdentityTestAsset2 +{ + public function getDisplayName() + { + return 'User identity'; + } +} + +class IdentityTestAsset3 +{ + public function getUsername() + { + return 'User identity'; + } +} + +class IdentityTestAsset4 +{ + public function getId() + { + return 'User identity'; + } +} \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/identity-with-link.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/identity-with-link.phtml new file mode 100644 index 0000000000000000000000000000000000000000..4e4c13fe0d0b139bfe4d059e08484736904c509f --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/identity-with-link.phtml @@ -0,0 +1 @@ +<strong>User identity</strong> | UserConnection Helper Markup \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/identity-without-link.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/identity-without-link.phtml new file mode 100644 index 0000000000000000000000000000000000000000..4822e43bc757cbef41aaa52755909f4eaadf8f8b --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/identity-without-link.phtml @@ -0,0 +1 @@ +<strong>User identity</strong> \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-with-link-translated.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-with-link-translated.phtml new file mode 100644 index 0000000000000000000000000000000000000000..65c92411cadcac917da2bffd7f00d1a66926e790 --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-with-link-translated.phtml @@ -0,0 +1 @@ +<strong>None</strong> | UserConnection Helper Markup \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-with-link.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-with-link.phtml new file mode 100644 index 0000000000000000000000000000000000000000..cc4916f21464842b72e929ab4b6b366bc8328d1c --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-with-link.phtml @@ -0,0 +1 @@ +<strong>Aucun</strong> | UserConnection Helper Markup \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-without-link-translated.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-without-link-translated.phtml new file mode 100644 index 0000000000000000000000000000000000000000..40e774aa874f9956080b823c6ef0285a8c94eddb --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-without-link-translated.phtml @@ -0,0 +1 @@ +<strong>None</strong> \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-without-link.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-without-link.phtml new file mode 100644 index 0000000000000000000000000000000000000000..81988694f51dab1c79e1ca339d21693cdd909ae6 --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/no-identity-without-link.phtml @@ -0,0 +1 @@ +<strong>Aucun</strong> \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/unexpected-identity-with-link.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/unexpected-identity-with-link.phtml new file mode 100644 index 0000000000000000000000000000000000000000..f1aeed8715830adf702eb0f425de17e5805c7427 --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/unexpected-identity-with-link.phtml @@ -0,0 +1 @@ +<strong><span title="Erreur: identité inattendue (DateTime)">???</span></strong> | UserConnection Helper Markup \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/unexpected-identity-without-link.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/unexpected-identity-without-link.phtml new file mode 100644 index 0000000000000000000000000000000000000000..a6ad93913f83f6ec8eaab427dca078877c9ac632 --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_status/unexpected-identity-without-link.phtml @@ -0,0 +1 @@ +<strong><span title="Erreur: identité inattendue (DateTime)">???</span></strong> \ No newline at end of file