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