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

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

/**
 * Description of AppConnectionTest
 *
 * @property UserInfo $helper Description
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UserInfoTest extends AbstractTest
{
    protected $helperClass = 'UnicaenAuth\View\Helper\UserInfo';
    protected $renderer;
    protected $authService;
    protected $mapperStructure;
20

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

Bertrand Gauthier's avatar
Bertrand Gauthier committed
29
        $this->authService = $this->createMock('Laminas\Authentication\AuthenticationService'/*, ['hasIdentity', 'getIdentity']*/);
30

31
32
        $this->mapperStructure = $this->createMock('UnicaenApp\Mapper\Ldap\Structure'/*,
                ['findOneByDn', 'findOnePathByCodeStructure', 'findAllPathByCodeStructure', 'findOneByCodeEntite']*/);
33

34
35
36
37
        $this->helper->setMapperStructure($this->mapperStructure)
                     ->setAuthService($this->authService)
                     ->setTranslator(new Translator());
    }
38

39
40
41
42
43
    public function testCanConstructWithAuthService()
    {
        $helper = new UserInfo($this->authService);
        $this->assertSame($this->authService, $helper->getAuthService());
    }
44

45
46
47
48
    public function testEntryPointReturnsSelfInstance()
    {
        $this->assertSame($this->helper, $this->helper->__invoke());
    }
49

50
51
52
53
54
    public function testEntryPointCanSetArgs()
    {
        $this->helper->__invoke($flag = true);
        $this->assertSame($flag, $this->helper->getAffectationPrincipale());
    }
55

56
57
58
59
60
    public function testRenderingReturnsEmptyStringIfNoIdentityAvailable()
    {
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(false));
61

62
63
64
        $markup = (string) $this->helper;
        $this->assertEquals('', $markup);
    }
65

66
67
68
69
70
71
72
73
    public function testRenderingReturnsInfoMessageIfUnexpectedIdentityAvailable()
    {
        $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'));
74

75
76
        $markup = (string) $this->helper;
        $this->assertEquals("Aucune information disponible.", $markup);
77

78
79
80
81
82
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
        $markup = (string) $this->helper;
        $this->assertEquals("No info available.", $markup);
    }
83

84
85
    public function getInfosAndExpectedScript()
    {
86
87
88
89
90
        return [
            'rien' => [
                [],
                [],
                [],
91
92
                'user_info/rien.phtml',
                'user_info/rien-translated.phtml',
93
94
95
96
97
            ],
            'aff-admin-seule' => [
                ["Chemin > Structure > Affectation"],
                [],
                [],
98
99
                'user_info/aff-admin-seule.phtml',
                'user_info/aff-admin-seule-translated.phtml',
100
101
102
103
104
            ],
            'aff-rech-seule' => [
                [],
                ["Chemin > Structure > Recherche"],
                [],
105
106
                'user_info/aff-rech-seule.phtml',
                'user_info/aff-rech-seule-translated.phtml',
107
108
109
110
111
            ],
            'fonct-seule' => [
                [],
                [],
                ["Responsable (DSI)"],
112
113
                'user_info/fonct-seule.phtml',
                'user_info/fonct-seule-translated.phtml',
114
115
116
117
118
            ],
            'pas-fonct' => [
                ["Chemin > Structure > Affectation"],
                ["Chemin > Structure > Recherche"],
                [],
119
120
                'user_info/pas-fonct.phtml',
                'user_info/pas-fonct-translated.phtml',
121
122
123
124
125
            ],
            'pas-aff-admin' => [
                [],
                ["Chemin > Structure > Recherche"],
                ["Responsable (DSI)"],
126
127
                'user_info/pas-aff-admin.phtml',
                'user_info/pas-aff-admin-translated.phtml',
128
129
130
131
132
            ],
            'pas-aff-rech' => [
                ["Chemin > Structure > Affectation"],
                [],
                ["Responsable (DSI)"],
133
134
                'user_info/pas-aff-rech.phtml',
                'user_info/pas-aff-rech-translated.phtml',
135
136
137
138
139
            ],
            'tout' => [
                ["Chemin > Structure > Affectation"],
                ["Chemin > Structure > Recherche"],
                ["Responsable (DSI)"],
140
141
                'user_info/tout.phtml',
                'user_info/tout-translated.phtml',
142
143
            ],
        ];
144
    }
145

146
147
148
149
150
151
152
153
154
    /**
     * @dataProvider getInfosAndExpectedScript
     * @param array $affectationsAdmin
     * @param array $affectationsRecherche
     * @param array $fonctionsStructurelles
     * @param string $expectedScript
     * @param string $expectedScriptTranslated
     */
    public function testCanRenderLogoutLinkIfIdentityAvailable(
155
156
157
158
            $affectationsAdmin,
            $affectationsRecherche,
            $fonctionsStructurelles,
            $expectedScript,
159
160
            $expectedScriptTranslated)
    {
161
162
        $identity = $this->createMock(
                'UnicaenApp\Entity\Ldap\People'/*,
163
164
                ['getAffectationsAdmin', 'getAffectationsRecherche', 'getFonctionsStructurelles'],
                [],
165
                '',
166
                false*/);
167
168
169
170
171
172
173
174
175
        $identity->expects($this->any())
                 ->method('getAffectationsAdmin')
                 ->will($this->returnValue($affectationsAdmin));
        $identity->expects($this->any())
                 ->method('getAffectationsRecherche')
                 ->will($this->returnValue($affectationsRecherche));
        $identity->expects($this->any())
                 ->method('getFonctionsStructurelles')
                 ->will($this->returnValue($fonctionsStructurelles));
176

177
178
179
180
181
182
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(true));
        $this->authService->expects($this->any())
                          ->method('getIdentity')
                          ->will($this->returnValue($identity));
183

184
185
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected($expectedScript), $markup);
186

187
188
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
189
        $markup = (string) $this->helper;
190
191
192
193
194
195
196
197
198
199
200
        $this->assertEquals($this->getExpected($expectedScriptTranslated), $markup);
    }

    /**
     * Returns translator
     *
     * @return Translator
     */
    protected function _getTranslator()
    {
        $loader = new ArrayTranslatorLoader();
201
        $loader->translations = [
202
203
204
205
206
            "Aucune information disponible." => "No info available.",
            "Affectations administratives"   => "Administrative affectations",
            "Affectations recherche"         => "Research affectations",
            "Responsabilités"                => "Responsabilities",
            "Aucune affectation trouvée."    => "No affectation found.",
207
        ];
208
209
210
211

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

213
214
215
        return $translator;
    }
}