Commit 9b5c704d authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

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

parent d9761ec2
<?php
namespace UnicaenAuth\View\Helper;
use BjyAuthorize\Provider\Identity\ProviderInterface;
use UnicaenAuth\Acl\NamedRole;
use Zend\Permissions\Acl\Role\RoleInterface;
/**
* Aide de vue permettant d'afficher le profil de l'utilisateur connecté.
*
......@@ -9,7 +13,7 @@ namespace UnicaenAuth\View\Helper;
class UserProfile extends UserAbstract
{
/**
* @var \BjyAuthorize\Provider\Identity\ProviderInterface
* @var ProviderInterface
*/
protected $identityProvider;
......@@ -30,28 +34,52 @@ class UserProfile extends UserAbstract
*/
public function __toString()
{
$title = _("Profil utilisateur");
$unknown = _("Inconnu");
$none = _("Aucun");
if ($this->getTranslator()) {
$title = $this->getTranslator()->translate($title, $this->getTranslatorTextDomain());
$unknown = $this->getTranslator()->translate($unknown, $this->getTranslatorTextDomain());
$none = $this->getTranslator()->translate($none, $this->getTranslatorTextDomain());
}
$roles = array();
foreach ($this->getIdentityProvider()->getIdentityRoles() as $role) {
if ($role instanceof \UnicaenAuth\Acl\NamedRole) {
$roles[] = $role->getRoleName();
$lib = '';
if ($role instanceof NamedRole) {
$lib = $role->getRoleName();
}
elseif ($role instanceof \BjyAuthorize\Acl\Role) {
$roles[] = $role->getRoleId();
elseif ($role instanceof RoleInterface) {
$lib = $role->getRoleId();
}
else {
$roles[] = (string)$role;
$fallback = null;
set_error_handler(function() use (&$fallback, $unknown) { $fallback = $unknown; });
$lib = (string) $role;
restore_error_handler();
$lib = $lib ?: $fallback;
}
if ($lib) {
if ($this->getTranslator()) {
$lib = $this->getTranslator()->translate($lib, $this->getTranslatorTextDomain());
}
$html = "<strong>Profil utilisateur :</strong>";
if ($roles) {
$html .= $this->getView()->htmlList($roles);
$roles[] = $lib;
}
}
if (!$roles) {
$roles[] = $none;
}
$html = "<strong>$title :</strong>" . PHP_EOL;
$html .= $this->getView()->htmlList($roles);
return $html;
}
/**
*
* @return \BjyAuthorize\Provider\Identity\ProviderInterface
* @return ProviderInterface
*/
public function getIdentityProvider()
{
......@@ -60,10 +88,10 @@ class UserProfile extends UserAbstract
/**
*
* @param \BjyAuthorize\Provider\Identity\ProviderInterface $identityProvider
* @param ProviderInterface $identityProvider
* @return self
*/
public function setIdentityProvider(\BjyAuthorize\Provider\Identity\ProviderInterface $identityProvider)
public function setIdentityProvider(ProviderInterface $identityProvider)
{
$this->identityProvider = $identityProvider;
return $this;
......
<?php
namespace UnicaenAuthTest\View\Helper;
use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader;
use UnicaenAuth\View\Helper\UserProfile;
use Zend\I18n\Translator\Translator;
/**
* Description of UserProfileTest
*
* @property UserProfile $helper Description
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class UserProfileTest extends AbstractTest
{
protected $helperClass = 'UnicaenAuth\View\Helper\UserProfile';
protected $identityProvider;
/**
* 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->identityProvider = $this->getMockForAbstractClass('BjyAuthorize\Provider\Identity\ProviderInterface', array('getIdentityRoles'));
$this->helper->setIdentityProvider($this->identityProvider)
->setTranslator(new Translator());
}
public function testEntryPointReturnsSelfInstance()
{
$this->assertSame($this->helper, $this->helper->__invoke());
}
public function getIdentityRolesAndExpectedScript()
{
return array(
'none' => array(
array(),
'user_profile/none.phtml',
'user_profile/none-translated.phtml',
),
'role' => array(
array(new \Zend\Permissions\Acl\Role\GenericRole('Invité')),
'user_profile/role.phtml',
'user_profile/role-translated.phtml',
),
'named-role' => array(
array(new \UnicaenAuth\Acl\NamedRole('admin', null, "Administrateur")),
'user_profile/named-role.phtml',
'user_profile/named-role-translated.phtml',
),
'stringable-role' => array(
array('Opérateur'), // (string) 'Opérateur' renverra 'Opérateur'
'user_profile/stringable-role.phtml',
'user_profile/stringable-role-translated.phtml',
),
'non-stringable-role' => array(
array(new \stdClass()), // (string) new \stdClass() lèvera l'erreur "Object of class stdClass could not be converted to string"
'user_profile/non-stringable-role.phtml',
'user_profile/non-stringable-role-translated.phtml',
),
);
}
/**
* @dataProvider getIdentityRolesAndExpectedScript
* @param array $roles
* @param string $expectedScript
* @param string $expectedScriptTranslated
*/
public function testRenderingReturnsNoneIfIdentityProviderProvidesNoRole($roles, $expectedScript, $expectedScriptTranslated)
{
$this->identityProvider
->expects($this->any())
->method('getIdentityRoles')
->will($this->returnValue($roles));
$markup = (string) $this->helper;
$this->assertEquals($this->getExpected($expectedScript), $markup);
// traduction
$this->helper->setTranslator($this->_getTranslator());
$markup = (string) $this->helper;
$this->assertEquals($this->getExpected($expectedScriptTranslated), $markup);
}
/**
* Returns translator
*
* @return Translator
*/
protected function _getTranslator()
{
$loader = new ArrayTranslatorLoader();
$loader->translations = array(
"Profil utilisateur" => "User profile",
"Inconnu" => "Unknown",
"Aucun" => "None",
"Invité" => "Guest",
"Administrateur" => "Administrator",
"Opérateur" => "Operator"
);
$translator = new Translator();
$translator->getPluginManager()->setService('default', $loader);
$translator->addTranslationFile('default', null);
return $translator;
}
}
\ No newline at end of file
<strong>User profile :</strong>
<ul>
<li>Administrator</li>
</ul>
<strong>Profil utilisateur :</strong>
<ul>
<li>Administrateur</li>
</ul>
<strong>Profil utilisateur :</strong>
<ul>
<li>Inconnu</li>
</ul>
<strong>Profil utilisateur :</strong>
<ul>
<li>Aucun</li>
</ul>
<strong>Profil utilisateur :</strong>
<ul>
<li>Invité</li>
</ul>
<strong>Profil utilisateur :</strong>
<ul>
<li>Opérateur</li>
</ul>
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