]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Login.php
7cc0ce20e558adebdcec7629510755abd79c5844
[galette.git] / tests / Galette / Core / tests / units / Login.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Login tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2016-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Core
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2016-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since 2016-12-05
35 */
36
37 namespace Galette\Core\test\units;
38
39 use Galette\GaletteTestCase;
40
41 /**
42 * Login tests class
43 *
44 * @category Core
45 * @name Login
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2016-2023 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
51 * @since 2016-12-05
52 */
53 class Login extends GaletteTestCase
54 {
55 protected int $seed = 320112365;
56 private string $login_adh = 'dumas.roger';
57 private string $mdp_adh = 'sd8)AvtE|*';
58
59 /**
60 * Cleanup after tests
61 *
62 * @return void
63 */
64 public function tearDown(): void
65 {
66 $this->zdb = new \Galette\Core\Db();
67 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
68 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
69 $this->zdb->execute($delete);
70
71 parent::tearDown();
72 }
73
74 /**
75 * Test defaults
76 *
77 * @return void
78 */
79 public function testDefaults()
80 {
81 $this->assertFalse($this->login->isLogged());
82 $this->assertFalse($this->login->isStaff());
83 $this->assertFalse($this->login->isAdmin());
84 $this->assertFalse($this->login->isSuperAdmin());
85 $this->assertFalse($this->login->isActive());
86 $this->assertFalse($this->login->isCron());
87 $this->assertFalse($this->login->isUp2Date());
88 $this->assertFalse($this->login->isImpersonated());
89 }
90
91 /**
92 * Test not logged-in users Impersonating
93 *
94 * @return void
95 */
96 public function testNotLoggedCantImpersonate()
97 {
98 $login = $this->getMockBuilder(\Galette\Core\Login::class)
99 ->setConstructorArgs(array($this->zdb, $this->i18n))
100 ->onlyMethods(array('isLogged'))
101 ->getMock();
102 $login->method('isLogged')->willReturn(false);
103
104 $this->expectExceptionMessage('Only superadmin can impersonate!');
105 $login->impersonate(1);
106 }
107
108 /**
109 * Test staff users Impersonating
110 *
111 * @return void
112 */
113 public function testStaffCantImpersonate()
114 {
115 $login = $this->getMockBuilder(\Galette\Core\Login::class)
116 ->setConstructorArgs(array($this->zdb, $this->i18n))
117 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin'))
118 ->getMock();
119
120 $login->method('isLogged')->willReturn(true);
121 $login->method('isStaff')->willReturn(true);
122 $login->method('isAdmin')->willReturn(false);
123 $login->method('isSuperAdmin')->willReturn(false);
124
125 $this->expectExceptionMessage('Only superadmin can impersonate!');
126 $login->impersonate(1);
127 }
128
129 /**
130 * Test admin users Impersonating
131 *
132 * @return void
133 */
134 public function testAdminCantImpersonate()
135 {
136 $login = $this->getMockBuilder(\Galette\Core\Login::class)
137 ->setConstructorArgs(array($this->zdb, $this->i18n))
138 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin'))
139 ->getMock();
140
141 $login->method('isLogged')->willReturn(true);
142 $login->method('isStaff')->willReturn(true);
143 $login->method('isAdmin')->willReturn(true);
144 $login->method('isSuperAdmin')->willReturn(false);
145
146 $this->expectExceptionMessage('Only superadmin can impersonate!');
147 $login->impersonate(1);
148 }
149
150 /**
151 * Test Impersonating that throws an exception
152 *
153 * @return void
154 */
155 public function testImpersonateExistsWException()
156 {
157 $zdb = $this->getMockBuilder(\Galette\Core\Db::class)
158 ->onlyMethods(array('execute'))
159 ->getMock();
160
161 $zdb->method('execute')
162 ->will(
163 $this->returnCallback(
164 function ($o) {
165 throw new \LogicException('Error executing query!', 123);
166 }
167 )
168 );
169
170 $login = $this->getMockBuilder(\Galette\Core\Login::class)
171 ->setConstructorArgs(array($zdb, $this->i18n))
172 ->onlyMethods(array('isSuperAdmin'))
173 ->getMock();
174
175 $login->method('isSuperAdmin')->willReturn(true);
176
177 $this->assertFalse($login->impersonate(1));
178 }
179
180 /**
181 * Test superadmin users Impersonating
182 *
183 * @return void
184 */
185 public function testSuperadminCanImpersonate()
186 {
187 $login = $this->getMockBuilder(\Galette\Core\Login::class)
188 ->setConstructorArgs(array($this->zdb, $this->i18n))
189 ->onlyMethods(array('isSuperAdmin'))
190 ->getMock();
191
192 $login->method('isSuperAdmin')->willReturn(true);
193
194 ///We're faking, Impersonating won't work but will not throw any exception
195 $this->assertFalse($login->impersonate(1));
196 }
197
198 /**
199 * Test return requesting a non-existing property
200 *
201 * @return void
202 */
203 public function testInexistingGetter()
204 {
205 $this->assertFalse($this->login->doesnotexists);
206 }
207
208 /**
209 * Test login exists
210 *
211 * @return void
212 */
213 public function testLoginExists()
214 {
215 $this->assertFalse($this->login->loginExists('exists'));
216 $this->assertFalse($this->login->loginExists('doesnotexists'));
217 }
218
219 /**
220 * Test login exists that throws an exception
221 *
222 * @return void
223 */
224 public function testLoginExistsWException()
225 {
226 $zdb = $this->getMockBuilder(\Galette\Core\Db::class)
227 ->onlyMethods(array('execute'))
228 ->getMock();
229
230 $zdb->method('execute')
231 ->will(
232 $this->returnCallback(
233 function ($o) {
234 if ($o instanceof \Laminas\Db\Sql\Select) {
235 throw new \LogicException('Error executing query!', 123);
236 }
237 }
238 )
239 );
240
241 $login = new \Galette\Core\Login($zdb, $this->i18n);
242 $this->assertTrue($login->loginExists('doesnotexists'));
243 }
244
245 /**
246 * Test login as super admin
247 *
248 * @return void
249 */
250 public function testLogAdmin()
251 {
252 $this->login->logAdmin('superadmin', $this->preferences);
253 $this->assertTrue($this->login->isLogged());
254 $this->assertFalse($this->login->isStaff());
255 $this->assertTrue($this->login->isAdmin());
256 $this->assertTrue($this->login->isSuperAdmin());
257 $this->assertTrue($this->login->isActive());
258 $this->assertFalse($this->login->isCron());
259 $this->assertFalse($this->login->isUp2Date());
260 $this->assertFalse($this->login->isImpersonated());
261
262 //test logout
263 $this->login->logOut();
264 $this->testDefaults();
265 }
266
267 /**
268 * Creates or load test user
269 *
270 * @return void
271 */
272 private function createUser()
273 {
274 $select = $this->zdb->select(\Galette\Entity\Adherent::TABLE, 'a');
275 $select->where(array('a.fingerprint' => 'FAKER' . $this->seed));
276 $results = $this->zdb->execute($select);
277
278 global $zdb, $login, $hist, $i18n; // globals :(
279 $zdb = $this->zdb;
280 $login = $this->login;
281 $hist = $this->history;
282 $i18n = $this->i18n;
283
284 if ($results->count() === 0) {
285 $status = new \Galette\Entity\Status($this->zdb);
286 if (count($status->getList()) === 0) {
287 $res = $status->installInit();
288 $this->assertTrue($res);
289 }
290
291 $data = [
292 'nom_adh' => 'Barre',
293 'prenom_adh' => 'Olivier',
294 'ville_adh' => 'Le GoffVille',
295 'cp_adh' => '05 029',
296 'adresse_adh' => '9, impasse Frédérique Boulanger',
297 'email_adh' => 'bernadette37@hernandez.fr',
298 'login_adh' => 'dumas.roger',
299 'mdp_adh' => 'sd8)AvtE|*',
300 'mdp_adh2' => 'sd8)AvtE|*',
301 'bool_admin_adh' => false,
302 'bool_exempt_adh' => false,
303 'bool_display_info' => true,
304 'sexe_adh' => 1,
305 'prof_adh' => 'Pédologue',
306 'titre_adh' => null,
307 'ddn_adh' => '1948-10-23',
308 'lieu_naissance' => 'Lagarde',
309 'pseudo_adh' => 'elisabeth50',
310 'pays_adh' => 'Géorgie',
311 'tel_adh' => '05 05 20 88 04',
312 'activite_adh' => true,
313 'id_statut' => 6,
314 'date_crea_adh' => '2019-09-02',
315 'pref_lang' => 'nb_NO',
316 'fingerprint' => 'FAKER' . $this->seed,
317 ];
318
319 $this->adh = new \Galette\Entity\Adherent($this->zdb);
320 $this->adh->setDependencies(
321 $this->preferences,
322 $this->members_fields,
323 $this->history
324 );
325
326 $check = $this->adh->check($data, [], []);
327 if (is_array($check)) {
328 var_dump($check);
329 }
330 $this->assertTrue($check);
331
332 $store = $this->adh->store();
333 $this->assertTrue($store);
334 } else {
335 $this->adh = new \Galette\Entity\Adherent($this->zdb, $results->current());
336 }
337 }
338
339 /**
340 * Look for a login that does exist
341 *
342 * @return void
343 */
344 public function testLoginExistsDb()
345 {
346 $this->createUser();
347 $this->assertTrue($this->login->loginExists($this->login));
348 }
349
350 /**
351 * Test user login
352 *
353 * @return void
354 */
355 public function testLogin()
356 {
357 $this->createUser();
358 $this->assertFalse($this->login->login('doenotexists', 'empty'));
359 $this->assertTrue($this->login->login($this->login_adh, $this->mdp_adh));
360 }
361
362 /**
363 * Test logged user name
364 *
365 * @return void
366 */
367 public function testLoggedInAs()
368 {
369 global $translator;
370
371 $this->createUser();
372 $this->assertTrue($this->login->login($this->login_adh, $this->mdp_adh));
373
374 /** Should get message in the right locale but doesn't... */
375 $this->i18n->changeLanguage('en_US');
376 $tstring = $translator->translate(
377 "Logged in as:<br/>%login",
378 'galette',
379 $this->login->lang
380 );
381 $this->assertSame(
382 str_replace(
383 '%login',
384 'Barre Olivier (dumas.roger)',
385 $tstring
386 ),
387 $this->login->loggedInAs()
388 );
389 $this->assertSame('Barre Olivier (dumas.roger)', $this->login->loggedInAs(true));
390 }
391
392 /**
393 * Test login from cron
394 *
395 * @return void
396 */
397 public function testLogCron()
398 {
399 $this->login->logCron('reminder');
400 $this->assertTrue($this->login->isLogged());
401 $this->assertFalse($this->login->isStaff());
402 $this->assertFalse($this->login->isAdmin());
403 $this->assertFalse($this->login->isSuperAdmin());
404 $this->assertFalse($this->login->isActive());
405 $this->assertTrue($this->login->isCron());
406 $this->assertFalse($this->login->isUp2Date());
407 $this->assertFalse($this->login->isImpersonated());
408 $this->assertSame('cron', $this->login->login);
409
410 $this->expectException('Exception');
411 $this->expectExceptionMessage('Not authorized!');
412 $this->login->logCron('filename');
413 }
414 }