]>
git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Login.php
7cc0ce20e558adebdcec7629510755abd79c5844
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
33 * @link http://galette.tuxfamily.org
37 namespace Galette\Core\test\units
;
39 use Galette\GaletteTestCase
;
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
53 class Login
extends GaletteTestCase
55 protected int $seed = 320112365;
56 private string $login_adh = 'dumas.roger';
57 private string $mdp_adh = 'sd8)AvtE|*';
64 public function tearDown(): void
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);
79 public function testDefaults()
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());
92 * Test not logged-in users Impersonating
96 public function testNotLoggedCantImpersonate()
98 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
99 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
100 ->onlyMethods(array('isLogged'))
102 $login->method('isLogged')->willReturn(false);
104 $this->expectExceptionMessage('Only superadmin can impersonate!');
105 $login->impersonate(1);
109 * Test staff users Impersonating
113 public function testStaffCantImpersonate()
115 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
116 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
117 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin'))
120 $login->method('isLogged')->willReturn(true);
121 $login->method('isStaff')->willReturn(true);
122 $login->method('isAdmin')->willReturn(false);
123 $login->method('isSuperAdmin')->willReturn(false);
125 $this->expectExceptionMessage('Only superadmin can impersonate!');
126 $login->impersonate(1);
130 * Test admin users Impersonating
134 public function testAdminCantImpersonate()
136 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
137 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
138 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin'))
141 $login->method('isLogged')->willReturn(true);
142 $login->method('isStaff')->willReturn(true);
143 $login->method('isAdmin')->willReturn(true);
144 $login->method('isSuperAdmin')->willReturn(false);
146 $this->expectExceptionMessage('Only superadmin can impersonate!');
147 $login->impersonate(1);
151 * Test Impersonating that throws an exception
155 public function testImpersonateExistsWException()
157 $zdb = $this->getMockBuilder(\Galette\Core\Db
::class)
158 ->onlyMethods(array('execute'))
161 $zdb->method('execute')
163 $this->returnCallback(
165 throw new \
LogicException('Error executing query!', 123);
170 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
171 ->setConstructorArgs(array($zdb, $this->i18n
))
172 ->onlyMethods(array('isSuperAdmin'))
175 $login->method('isSuperAdmin')->willReturn(true);
177 $this->assertFalse($login->impersonate(1));
181 * Test superadmin users Impersonating
185 public function testSuperadminCanImpersonate()
187 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
188 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
189 ->onlyMethods(array('isSuperAdmin'))
192 $login->method('isSuperAdmin')->willReturn(true);
194 ///We're faking, Impersonating won't work but will not throw any exception
195 $this->assertFalse($login->impersonate(1));
199 * Test return requesting a non-existing property
203 public function testInexistingGetter()
205 $this->assertFalse($this->login
->doesnotexists
);
213 public function testLoginExists()
215 $this->assertFalse($this->login
->loginExists('exists'));
216 $this->assertFalse($this->login
->loginExists('doesnotexists'));
220 * Test login exists that throws an exception
224 public function testLoginExistsWException()
226 $zdb = $this->getMockBuilder(\Galette\Core\Db
::class)
227 ->onlyMethods(array('execute'))
230 $zdb->method('execute')
232 $this->returnCallback(
234 if ($o instanceof \Laminas\Db\Sql\Select
) {
235 throw new \
LogicException('Error executing query!', 123);
241 $login = new \Galette\Core\
Login($zdb, $this->i18n
);
242 $this->assertTrue($login->loginExists('doesnotexists'));
246 * Test login as super admin
250 public function testLogAdmin()
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());
263 $this->login
->logOut();
264 $this->testDefaults();
268 * Creates or load test user
272 private function createUser()
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);
278 global $zdb, $login, $hist, $i18n; // globals :(
280 $login = $this->login
;
281 $hist = $this->history
;
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);
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,
305 'prof_adh' => 'Pédologue',
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,
314 'date_crea_adh' => '2019-09-02',
315 'pref_lang' => 'nb_NO',
316 'fingerprint' => 'FAKER' . $this->seed
,
319 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
320 $this->adh
->setDependencies(
322 $this->members_fields
,
326 $check = $this->adh
->check($data, [], []);
327 if (is_array($check)) {
330 $this->assertTrue($check);
332 $store = $this->adh
->store();
333 $this->assertTrue($store);
335 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $results->current());
340 * Look for a login that does exist
344 public function testLoginExistsDb()
347 $this->assertTrue($this->login
->loginExists($this->login
));
355 public function testLogin()
358 $this->assertFalse($this->login
->login('doenotexists', 'empty'));
359 $this->assertTrue($this->login
->login($this->login_adh
, $this->mdp_adh
));
363 * Test logged user name
367 public function testLoggedInAs()
372 $this->assertTrue($this->login
->login($this->login_adh
, $this->mdp_adh
));
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",
384 'Barre Olivier (dumas.roger)',
387 $this->login
->loggedInAs()
389 $this->assertSame('Barre Olivier (dumas.roger)', $this->login
->loggedInAs(true));
393 * Test login from cron
397 public function testLogCron()
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
);
410 $this->expectException('Exception');
411 $this->expectExceptionMessage('Not authorized!');
412 $this->login
->logCron('filename');