Commit 075bdac1 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Aide de vue UserCurrent : tests unitaires + légères modifs.

parent bc72ddd5
...@@ -12,10 +12,6 @@ namespace UnicaenAuth\View\Helper; ...@@ -12,10 +12,6 @@ namespace UnicaenAuth\View\Helper;
*/ */
class UserCurrent extends UserAbstract class UserCurrent extends UserAbstract
{ {
/**
* @var string
*/
protected $legende;
/** /**
* @var bool * @var bool
*/ */
...@@ -24,18 +20,16 @@ class UserCurrent extends UserAbstract ...@@ -24,18 +20,16 @@ class UserCurrent extends UserAbstract
/** /**
* Point d'entrée. * 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 * @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) * 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é : ")) $this->setAffectationFineSiDispo($affectationFineSiDispo);
->setAffectationFineSiDispo($affectationFineSiDispo);
return $this; return $this;
} }
/** /**
* Retourne le code HTML généré par cette aide de vue. * Retourne le code HTML généré par cette aide de vue.
* *
...@@ -45,19 +39,38 @@ class UserCurrent extends UserAbstract ...@@ -45,19 +39,38 @@ class UserCurrent extends UserAbstract
{ {
try { try {
$id = 'user-current-info'; $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()) { 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 { else {
$content = "Aucun"; $content = _("Aucun");
if ($this->getTranslator()) {
$content = $this->getTranslator()->translate($content, $this->getTranslatorTextDomain());
}
} }
$content = preg_replace('/\r\n|\n|\r/', '', $content); $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 $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; EOS;
$out .= PHP_EOL;
$out .= <<<EOS $out .= <<<EOS
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
...@@ -65,6 +78,7 @@ EOS; ...@@ -65,6 +78,7 @@ EOS;
}); });
</script> </script>
EOS; EOS;
$out .= PHP_EOL;
} }
catch (\Exception $e) { catch (\Exception $e) {
var_dump($e); var_dump($e);
...@@ -74,28 +88,6 @@ EOS; ...@@ -74,28 +88,6 @@ EOS;
return $out; 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. * Indique si l'affichage de l'affectation fine éventuelle est activé ou non.
* *
...@@ -110,12 +102,11 @@ EOS; ...@@ -110,12 +102,11 @@ EOS;
* Active ou non l'affichage de l'affectation fine éventuelle. * Active ou non l'affichage de l'affectation fine éventuelle.
* *
* @param bool $affectationFineSiDispo * @param bool $affectationFineSiDispo
* @return UserCurrent * @return self
*/ */
public function setAffectationFineSiDispo($affectationFineSiDispo = true) public function setAffectationFineSiDispo($affectationFineSiDispo = true)
{ {
$this->affectationFineSiDispo = $affectationFineSiDispo; $this->affectationFineSiDispo = $affectationFineSiDispo;
return $this; return $this;
} }
}
} \ No newline at end of file
<?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
<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>
<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>
<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>
<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>
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