]>
git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Login.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2016-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Core\test\units
;
40 use Galette\GaletteTestCase
;
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
54 class Login
extends GaletteTestCase
56 protected int $seed = 320112365;
57 private string $login_adh = 'dumas.roger';
58 private string $mdp_adh = 'sd8)AvtE|*';
65 public function tearDown(): void
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);
80 public function testDefaults()
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());
93 * Test not logged-in users Impersonating
97 public function testNotLoggedCantImpersonate()
99 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
100 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
101 ->onlyMethods(array('isLogged'))
103 $login->method('isLogged')->willReturn(false);
105 $this->expectExceptionMessage('Only superadmin can impersonate!');
106 $login->impersonate(1);
110 * Test staff users Impersonating
114 public function testStaffCantImpersonate()
116 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
117 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
118 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin'))
121 $login->method('isLogged')->willReturn(true);
122 $login->method('isStaff')->willReturn(true);
123 $login->method('isAdmin')->willReturn(false);
124 $login->method('isSuperAdmin')->willReturn(false);
126 $this->expectExceptionMessage('Only superadmin can impersonate!');
127 $login->impersonate(1);
131 * Test admin users Impersonating
135 public function testAdminCantImpersonate()
137 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
138 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
139 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin'))
142 $login->method('isLogged')->willReturn(true);
143 $login->method('isStaff')->willReturn(true);
144 $login->method('isAdmin')->willReturn(true);
145 $login->method('isSuperAdmin')->willReturn(false);
147 $this->expectExceptionMessage('Only superadmin can impersonate!');
148 $login->impersonate(1);
152 * Test Impersonating that throws an exception
156 public function testImpersonateExistsWException()
158 $zdb = $this->getMockBuilder(\Galette\Core\Db
::class)
159 ->onlyMethods(array('execute'))
162 $zdb->method('execute')
164 $this->returnCallback(
166 throw new \
LogicException('Error executing query!', 123);
171 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
172 ->setConstructorArgs(array($zdb, $this->i18n
))
173 ->onlyMethods(array('isSuperAdmin'))
176 $login->method('isSuperAdmin')->willReturn(true);
178 $this->assertFalse($login->impersonate(1));
182 * Test superadmin users Impersonating
186 public function testSuperadminCanImpersonate()
188 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
189 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
190 ->onlyMethods(array('isSuperAdmin'))
193 $login->method('isSuperAdmin')->willReturn(true);
195 ///We're faking, Impersonating won't work but will not throw any exception
196 $this->assertFalse($login->impersonate(1));
200 * Test return requesting a non-existing property
204 public function testInexistingGetter()
206 $this->assertFalse($this->login
->doesnotexists
);
214 public function testLoginExists()
216 $this->assertFalse($this->login
->loginExists('exists'));
217 $this->assertFalse($this->login
->loginExists('doesnotexists'));
221 * Test login exists that throws an exception
225 public function testLoginExistsWException()
227 $zdb = $this->getMockBuilder(\Galette\Core\Db
::class)
228 ->onlyMethods(array('execute'))
231 $zdb->method('execute')
233 $this->returnCallback(
235 if ($o instanceof \Laminas\Db\Sql\Select
) {
236 throw new \
LogicException('Error executing query!', 123);
242 $login = new \Galette\Core\
Login($zdb, $this->i18n
);
243 $this->assertTrue($login->loginExists('doesnotexists'));
247 * Test login as super admin
251 public function testLogAdmin()
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());
264 $this->login
->logOut();
265 $this->testDefaults();
269 * Creates or load test user
273 private function createUser()
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);
279 global $zdb, $login, $hist, $i18n; // globals :(
281 $login = $this->login
;
282 $hist = $this->history
;
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);
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,
306 'prof_adh' => 'Pédologue',
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,
315 'date_crea_adh' => '2019-09-02',
316 'pref_lang' => 'nb_NO',
317 'fingerprint' => 'FAKER' . $this->seed
,
320 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
321 $this->adh
->setDependencies(
323 $this->members_fields
,
327 $check = $this->adh
->check($data, [], []);
328 if (is_array($check)) {
331 $this->assertTrue($check);
333 $store = $this->adh
->store();
334 $this->assertTrue($store);
336 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $results->current());
341 * Look for a login that does exist
345 public function testLoginExistsDb()
348 $this->assertTrue($this->login
->loginExists($this->login
));
356 public function testLogin()
359 $this->assertFalse($this->login
->login('doenotexists', 'empty'));
360 $this->assertTrue($this->login
->login($this->login_adh
, $this->mdp_adh
));
364 * Test logged user name
368 public function testLoggedInAs()
373 $this->assertTrue($this->login
->login($this->login_adh
, $this->mdp_adh
));
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",
385 'Barre Olivier (dumas.roger)',
388 $this->login
->loggedInAs()
390 $this->assertSame('Barre Olivier (dumas.roger)', $this->login
->loggedInAs(true));
394 * Test login from cron
398 public function testLogCron()
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
);
411 $this->expectException('Exception');
412 $this->expectExceptionMessage('Not authorized!');
413 $this->login
->logCron('filename');