diff --git a/src/UnicaenAuth/View/Helper/UserCurrent.php b/src/UnicaenAuth/View/Helper/UserCurrent.php index fa10cadfeee5f520ab335bb4d644f699f72950b2..4a32bcdeb82cfb0e2a00414f054e07ee75e677e3 100644 --- a/src/UnicaenAuth/View/Helper/UserCurrent.php +++ b/src/UnicaenAuth/View/Helper/UserCurrent.php @@ -12,10 +12,6 @@ namespace UnicaenAuth\View\Helper; */ class UserCurrent extends UserAbstract { - /** - * @var string - */ - protected $legende; /** * @var bool */ @@ -24,18 +20,16 @@ class UserCurrent extends UserAbstract /** * Point d'entrée. * - * @param string $legende Légende affichée devant l'identité de l'utilisateur connecté éventuel * @param boolean $affectationFineSiDispo Indique s'il faut prendre en compte l'affectation * plus fine (ucbnSousStructure) si elle existe, à la place de l'affectation standard (niveau 2) - * @return UserCurrent + * @return self */ - public function __invoke($legende = null, $affectationFineSiDispo = false) + public function __invoke($affectationFineSiDispo = false) { - $this->setLegende($legende ?: _("Utilisateur connecté : ")) - ->setAffectationFineSiDispo($affectationFineSiDispo); + $this->setAffectationFineSiDispo($affectationFineSiDispo); return $this; } - + /** * Retourne le code HTML généré par cette aide de vue. * @@ -45,19 +39,38 @@ class UserCurrent extends UserAbstract { try { $id = 'user-current-info'; - $status = $this->getView()->userStatus(false); + $userStatusHelper = $this->getView()->plugin('userStatus'); /* @var $userStatusHelper \UnicaenAuth\View\Helper\UserStatus */ + $status = $userStatusHelper(false); if ($this->getIdentity()) { - $content = $this->getView()->userProfile() . $this->getView()->userInfo($this->getAffectationFineSiDispo()); + $userProfileHelper = $this->getView()->plugin('userProfile'); /* @var $userProfileHelper \UnicaenAuth\View\Helper\UserProfile */ + $userInfoHelper = $this->getView()->plugin('userInfo'); /* @var $userInfoHelper \UnicaenAuth\View\Helper\UserInfo */ + $content = $userProfileHelper . $userInfoHelper($this->getAffectationFineSiDispo()); } else { - $content = "Aucun"; + $content = _("Aucun"); + if ($this->getTranslator()) { + $content = $this->getTranslator()->translate($content, $this->getTranslatorTextDomain()); + } } $content = preg_replace('/\r\n|\n|\r/', '', $content); + + $title = _("Utilisateur connecté à l'application"); + if ($this->getTranslator()) { + $title = $this->getTranslator()->translate($title, $this->getTranslatorTextDomain()); + } + $out = <<<EOS -<a class="navbar-link" id="$id" title="Utilisateur connecté" data-placement="bottom" data-toggle="popover" data-content="$content" href="#">$status</a> +<a class="navbar-link" + id="$id" + title="$title" + data-placement="bottom" + data-toggle="popover" + data-content="$content" + href="#">$status</a> EOS; + $out .= PHP_EOL; $out .= <<<EOS <script type="text/javascript"> $(function() { @@ -65,6 +78,7 @@ EOS; }); </script> EOS; + $out .= PHP_EOL; } catch (\Exception $e) { var_dump($e); @@ -74,28 +88,6 @@ EOS; return $out; } - /** - * Retourne la légende affichée devant l'identité de l'utilisateur connecté éventuel. - * - * @return string - */ - public function getLegende() - { - return $this->legende; - } - - /** - * Change la légende affichée devant l'identité de l'utilisateur connecté éventuel. - * - * @param string $legende - * @return UserCurrent - */ - public function setLegende($legende = true) - { - $this->legende = $legende; - return $this; - } - /** * Indique si l'affichage de l'affectation fine éventuelle est activé ou non. * @@ -110,12 +102,11 @@ EOS; * Active ou non l'affichage de l'affectation fine éventuelle. * * @param bool $affectationFineSiDispo - * @return UserCurrent + * @return self */ public function setAffectationFineSiDispo($affectationFineSiDispo = true) { $this->affectationFineSiDispo = $affectationFineSiDispo; return $this; } - -} +} \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/UserCurrentTest.php b/tests/UnicaenAuthTest/View/Helper/UserCurrentTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e53d1c18e7f8442cf4d9f99fe49a6963652f5353 --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/UserCurrentTest.php @@ -0,0 +1,140 @@ +<?php +namespace UnicaenAuthTest\View\Helper; + +use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader; +use UnicaenAuth\View\Helper\UserCurrent; +use Zend\I18n\Translator\Translator; + +/** + * Description of AppConnectionTest + * + * @property UserCurrent $helper Description + * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> + */ +class UserCurrentTest extends AbstractTest +{ + protected $helperClass = 'UnicaenAuth\View\Helper\UserCurrent'; + protected $renderer; + protected $authService; + protected $userStatusHelper; + protected $userProfileHelper; + protected $userInfoHelper; + + /** + * 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->userStatusHelper = $userStatusHelper = $this->getMock('UnicaenAuth\View\Helper\UserStatus', array('__invoke')); + $this->userProfileHelper = $userProfileHelper = $this->getMock('UnicaenAuth\View\Helper\UserProfile', array('__toString')); + $this->userInfoHelper = $userInfoHelper = $this->getMock('UnicaenAuth\View\Helper\UserInfo', array('__invoke')); + + $this->userStatusHelper + ->expects($this->any()) + ->method('__invoke') + ->will($this->returnValue('User Status Helper Markup')); + $this->userProfileHelper + ->expects($this->any()) + ->method('__toString') + ->will($this->returnValue('User Profile Helper Markup')); + $this->userInfoHelper + ->expects($this->any()) + ->method('__invoke') + ->will($this->returnValue('User Info Helper Markup')); + + $this->renderer = $this->getMock('Zend\View\Renderer\PhpRenderer', array('plugin')); + $this->renderer->expects($this->any()) + ->method('plugin') + ->will($this->returnCallback(function ($helper) use ($userStatusHelper, $userProfileHelper, $userInfoHelper) { + if ('userstatus' === strtolower($helper)) { + return $userStatusHelper; + } + if ('userprofile' === strtolower($helper)) { + return $userProfileHelper; + } + if ('userinfo' === strtolower($helper)) { + return $userInfoHelper; + } + return null; + })); + + $this->authService = $this->getMock('Zend\Authentication\AuthenticationService', array('hasIdentity', 'getIdentity')); + + $this->helper->setAuthService($this->authService) + ->setView($this->renderer) + ->setTranslator(new Translator()); + } + + public function testCanConstructWithAuthService() + { + $helper = new UserCurrent($this->authService); + $this->assertSame($this->authService, $helper->getAuthService()); + } + + public function testEntryPointReturnsSelfInstance() + { + $this->assertSame($this->helper, $this->helper->__invoke()); + } + + public function testEntryPointCanSetArgs() + { + $this->helper->__invoke($flag = true); + $this->assertSame($flag, $this->helper->getAffectationFineSiDispo()); + } + + public function testCanRenderIfNoIdentityAvailable() + { + $this->authService->expects($this->any()) + ->method('hasIdentity') + ->will($this->returnValue(false)); + + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected('user_current/logged-out.phtml'), $markup); + + // traduction + $this->helper->setTranslator($this->_getTranslator()); + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected('user_current/logged-out-translated.phtml'), $markup); + } + + public function testCanRenderLogoutLinkIfIdentityAvailable() + { + $this->authService->expects($this->any()) + ->method('hasIdentity') + ->will($this->returnValue(true)); + $this->authService->expects($this->any()) + ->method('getIdentity') + ->will($this->returnValue($identity = 'Auth Service Identity')); + + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected('user_current/logged-in.phtml'), $markup); + + // traduction + $this->helper->setTranslator($this->_getTranslator()); + $markup = (string) $this->helper; + $this->assertEquals($this->getExpected('user_current/logged-in-translated.phtml'), $markup); + } + + /** + * Returns translator + * + * @return Translator + */ + protected function _getTranslator() + { + $loader = new ArrayTranslatorLoader(); + $loader->translations = array( + "Utilisateur connecté à l'application" => "Auth user", + "Aucun" => "None", + ); + + $translator = new Translator(); + $translator->getPluginManager()->setService('default', $loader); + $translator->addTranslationFile('default', null); + + return $translator; + } +} \ No newline at end of file diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-in-translated.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-in-translated.phtml new file mode 100644 index 0000000000000000000000000000000000000000..b9bb7cf4eeb9146f26c8a333ff45d2bbbe049456 --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-in-translated.phtml @@ -0,0 +1,12 @@ +<a class="navbar-link" + id="user-current-info" + title="Auth user" + data-placement="bottom" + data-toggle="popover" + data-content="User Profile Helper MarkupUser Info Helper Markup" + href="#">User Status Helper Markup</a> +<script type="text/javascript"> + $(function() { + $("#user-current-info").popover({ html: true, container: '#navbar' }); + }); +</script> diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-in.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-in.phtml new file mode 100644 index 0000000000000000000000000000000000000000..a213151b0657937b01c022b9a5dd069064d21610 --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-in.phtml @@ -0,0 +1,12 @@ +<a class="navbar-link" + id="user-current-info" + title="Utilisateur connecté à l'application" + data-placement="bottom" + data-toggle="popover" + data-content="User Profile Helper MarkupUser Info Helper Markup" + href="#">User Status Helper Markup</a> +<script type="text/javascript"> + $(function() { + $("#user-current-info").popover({ html: true, container: '#navbar' }); + }); +</script> diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-out-translated.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-out-translated.phtml new file mode 100644 index 0000000000000000000000000000000000000000..d4d926687dec4e9ba25ad8d9b921459158ba210d --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-out-translated.phtml @@ -0,0 +1,12 @@ +<a class="navbar-link" + id="user-current-info" + title="Auth user" + data-placement="bottom" + data-toggle="popover" + data-content="None" + href="#">User Status Helper Markup</a> +<script type="text/javascript"> + $(function() { + $("#user-current-info").popover({ html: true, container: '#navbar' }); + }); +</script> diff --git a/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-out.phtml b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-out.phtml new file mode 100644 index 0000000000000000000000000000000000000000..fc06f5ce74787c508fe2fc5dbde1e3b3dd3871b1 --- /dev/null +++ b/tests/UnicaenAuthTest/View/Helper/_files/expected/user_current/logged-out.phtml @@ -0,0 +1,12 @@ +<a class="navbar-link" + id="user-current-info" + title="Utilisateur connecté à l'application" + data-placement="bottom" + data-toggle="popover" + data-content="Aucun" + href="#">User Status Helper Markup</a> +<script type="text/javascript"> + $(function() { + $("#user-current-info").popover({ html: true, container: '#navbar' }); + }); +</script>