UserProfileTest.php 3.82 KB
Newer Older
1
2
3
4
5
<?php
namespace UnicaenAuthTest\View\Helper;

use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader;
use UnicaenAuth\View\Helper\UserProfile;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
6
use Laminas\I18n\Translator\Translator;
7
8
9
10
11
12
13
14
15
16
17

/**
 * 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;
18

19
20
21
22
23
24
25
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        parent::setUp();
26
27
28

        $this->identityProvider = $this->getMockForAbstractClass('BjyAuthorize\Provider\Identity\ProviderInterface', ['getIdentityRoles']);

29
30
31
        $this->helper->setIdentityProvider($this->identityProvider)
                     ->setTranslator(new Translator());
    }
32

33
34
35
36
    public function testEntryPointReturnsSelfInstance()
    {
        $this->assertSame($this->helper, $this->helper->__invoke());
    }
37

38
39
    public function getIdentityRolesAndExpectedScript()
    {
40
41
42
        return [
            'none' => [
                [],
43
44
                'user_profile/none.phtml',
                'user_profile/none-translated.phtml',
45
46
            ],
            'role' => [
Bertrand Gauthier's avatar
Bertrand Gauthier committed
47
                [new \Laminas\Permissions\Acl\Role\GenericRole('Invité')],
48
49
                'user_profile/role.phtml',
                'user_profile/role-translated.phtml',
50
51
52
            ],
            'named-role' => [
                [new \UnicaenAuth\Acl\NamedRole('admin', null, "Administrateur")],
53
54
                'user_profile/named-role.phtml',
                'user_profile/named-role-translated.phtml',
55
56
57
            ],
            'stringable-role' => [
                ['Opérateur'], // (string) 'Opérateur' renverra 'Opérateur'
58
59
                'user_profile/stringable-role.phtml',
                'user_profile/stringable-role-translated.phtml',
60
61
62
            ],
            'non-stringable-role' => [
                [new \stdClass()], // (string) new \stdClass() lèvera l'erreur "Object of class stdClass could not be converted to string"
63
64
                'user_profile/non-stringable-role.phtml',
                'user_profile/non-stringable-role-translated.phtml',
65
66
            ],
        ];
67
    }
68

69
70
71
72
73
74
75
76
77
78
79
80
    /**
     * @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));
81

82
83
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected($expectedScript), $markup);
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
        // 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();
99
        $loader->translations = [
100
101
102
103
104
105
            "Profil utilisateur" => "User profile",
            "Inconnu"            => "Unknown",
            "Aucun"              => "None",
            "Invité"             => "Guest",
            "Administrateur"     => "Administrator",
            "Opérateur"          => "Operator"
106
        ];
107
108
109
110

        $translator = new Translator();
        $translator->getPluginManager()->setService('default', $loader);
        $translator->addTranslationFile('default', null);
111

112
113
114
        return $translator;
    }
}