Skip to content
Snippets Groups Projects
Commit 1d060a74 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Aide de vue UserStatus : améliorations légères et tests unitaires.

parent 9b5c704d
No related branches found
No related tags found
No related merge requests found
Showing
with 269 additions and 39 deletions
......@@ -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
<?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
<strong>User identity</strong> | UserConnection Helper Markup
\ No newline at end of file
<strong>User identity</strong>
\ No newline at end of file
<strong>None</strong> | UserConnection Helper Markup
\ No newline at end of file
<strong>Aucun</strong> | UserConnection Helper Markup
\ No newline at end of file
<strong>None</strong>
\ No newline at end of file
<strong>Aucun</strong>
\ No newline at end of file
<strong><span title="Erreur: identité inattendue (DateTime)">???</span></strong> | UserConnection Helper Markup
\ No newline at end of file
<strong><span title="Erreur: identité inattendue (DateTime)">???</span></strong>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment