]>
git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Login.php
eaf870c4ad1979a97386172e231e436f9db3150d
4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Core\test\units
;
24 use Galette\GaletteTestCase
;
29 * @author Johan Cwiklinski <johan@x-tnd.be>
31 class Login
extends GaletteTestCase
33 protected int $seed = 320112365;
34 private string $login_adh = 'dumas.roger';
35 private string $mdp_adh = 'sd8)AvtE|*';
42 public function tearDown(): void
44 $this->zdb
= new \Galette\Core\
Db();
45 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
46 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
47 $this->zdb
->execute($delete);
57 public function testDefaults()
59 $this->assertFalse($this->login
->isLogged());
60 $this->assertFalse($this->login
->isStaff());
61 $this->assertFalse($this->login
->isAdmin());
62 $this->assertFalse($this->login
->isSuperAdmin());
63 $this->assertFalse($this->login
->isActive());
64 $this->assertFalse($this->login
->isCron());
65 $this->assertFalse($this->login
->isUp2Date());
66 $this->assertFalse($this->login
->isImpersonated());
67 $this->assertNull($this->login
->lang
);
71 * Test not logged-in users Impersonating
75 public function testNotLoggedCantImpersonate()
77 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
78 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
79 ->onlyMethods(array('isLogged'))
81 $login->method('isLogged')->willReturn(false);
83 $this->expectExceptionMessage('Only superadmin can impersonate!');
84 $login->impersonate(1);
88 * Test staff users Impersonating
92 public function testStaffCantImpersonate()
94 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
95 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
96 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin'))
99 $login->method('isLogged')->willReturn(true);
100 $login->method('isStaff')->willReturn(true);
101 $login->method('isAdmin')->willReturn(false);
102 $login->method('isSuperAdmin')->willReturn(false);
104 $this->expectExceptionMessage('Only superadmin can impersonate!');
105 $login->impersonate(1);
109 * Test admin users Impersonating
113 public function testAdminCantImpersonate()
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(true);
123 $login->method('isSuperAdmin')->willReturn(false);
125 $this->expectExceptionMessage('Only superadmin can impersonate!');
126 $login->impersonate(1);
130 * Test Impersonating that throws an exception
134 public function testImpersonateExistsWException()
136 $zdb = $this->getMockBuilder(\Galette\Core\Db
::class)
137 ->onlyMethods(array('execute'))
140 $zdb->method('execute')
141 ->willReturnCallback(
143 throw new \
LogicException('Error executing query!', 123);
147 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
148 ->setConstructorArgs(array($zdb, $this->i18n
))
149 ->onlyMethods(array('isSuperAdmin'))
152 $login->method('isSuperAdmin')->willReturn(true);
154 $this->assertFalse($login->impersonate(1));
158 * Test superadmin users Impersonating
162 public function testSuperadminCanImpersonate()
164 $login = $this->getMockBuilder(\Galette\Core\Login
::class)
165 ->setConstructorArgs(array($this->zdb
, $this->i18n
))
166 ->onlyMethods(array('isSuperAdmin'))
169 $login->method('isSuperAdmin')->willReturn(true);
171 ///We're faking, Impersonating won't work but will not throw any exception
172 $this->assertFalse($login->impersonate(1));
176 * Test return requesting a non-existing property
180 public function testInexistingGetter()
182 $this->expectException('RuntimeException');
183 $this->expectExceptionMessage('Property doesnotexists is not set!');
184 $this->assertFalse($this->login
->doesnotexists
);
192 public function testLoginExists()
194 $this->assertFalse($this->login
->loginExists('exists'));
195 $this->assertFalse($this->login
->loginExists('doesnotexists'));
199 * Test login exists that throws an exception
203 public function testLoginExistsWException()
205 $zdb = $this->getMockBuilder(\Galette\Core\Db
::class)
206 ->onlyMethods(array('execute'))
209 $zdb->method('execute')
210 ->willReturnCallback(
212 if ($o instanceof \Laminas\Db\Sql\Select
) {
213 throw new \
LogicException('Error executing query!', 123);
218 $login = new \Galette\Core\
Login($zdb, $this->i18n
);
219 $this->assertTrue($login->loginExists('doesnotexists'));
223 * Test login as super admin
227 public function testLogAdmin()
229 $this->login
->logAdmin('superadmin', $this->preferences
);
230 $this->assertTrue($this->login
->isLogged());
231 $this->assertFalse($this->login
->isStaff());
232 $this->assertTrue($this->login
->isAdmin());
233 $this->assertTrue($this->login
->isSuperAdmin());
234 $this->assertTrue($this->login
->isActive());
235 $this->assertFalse($this->login
->isCron());
236 $this->assertFalse($this->login
->isUp2Date());
237 $this->assertFalse($this->login
->isImpersonated());
238 $this->assertSame($this->preferences
->pref_lang
, $this->login
->lang
);
241 $this->login
->logOut();
242 $this->testDefaults();
246 * Creates or load test user
250 private function createUser()
252 $select = $this->zdb
->select(\Galette\Entity\Adherent
::TABLE
, 'a');
253 $select->where(array('a.fingerprint' => 'FAKER' . $this->seed
));
254 $results = $this->zdb
->execute($select);
256 global $zdb, $login, $hist, $i18n; // globals :(
258 $login = $this->login
;
259 $hist = $this->history
;
262 if ($results->count() === 0) {
263 $status = new \Galette\Entity\
Status($this->zdb
);
264 if (count($status->getList()) === 0) {
265 $res = $status->installInit();
266 $this->assertTrue($res);
270 'nom_adh' => 'Barre',
271 'prenom_adh' => 'Olivier',
272 'ville_adh' => 'Le GoffVille',
273 'cp_adh' => '05 029',
274 'adresse_adh' => '9, impasse Frédérique Boulanger',
275 'email_adh' => 'bernadette37@hernandez.fr',
276 'login_adh' => 'dumas.roger',
277 'mdp_adh' => 'sd8)AvtE|*',
278 'mdp_adh2' => 'sd8)AvtE|*',
279 'bool_admin_adh' => false,
280 'bool_exempt_adh' => false,
281 'bool_display_info' => true,
283 'prof_adh' => 'Pédologue',
285 'ddn_adh' => '1948-10-23',
286 'lieu_naissance' => 'Lagarde',
287 'pseudo_adh' => 'elisabeth50',
288 'pays_adh' => 'Géorgie',
289 'tel_adh' => '05 05 20 88 04',
290 'activite_adh' => true,
292 'date_crea_adh' => '2019-09-02',
293 'pref_lang' => 'nb_NO',
294 'fingerprint' => 'FAKER' . $this->seed
,
297 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
298 $this->adh
->setDependencies(
300 $this->members_fields
,
304 $check = $this->adh
->check($data, [], []);
305 if (is_array($check)) {
308 $this->assertTrue($check);
310 $store = $this->adh
->store();
311 $this->assertTrue($store);
313 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $results->current());
318 * Look for a login that does exist
322 public function testLoginExistsDb()
325 $this->assertTrue($this->login
->loginExists('dumas.roger'));
333 public function testLogin()
336 $this->assertFalse($this->login
->login('doenotexists', 'empty'));
337 $this->assertTrue($this->login
->login($this->login_adh
, $this->mdp_adh
));
341 * Test logged user name
345 public function testLoggedInAs()
350 $this->assertTrue($this->login
->login($this->login_adh
, $this->mdp_adh
));
352 /** Should get message in the right locale but doesn't... */
353 $this->i18n
->changeLanguage('en_US');
354 $tstring = $translator->translate(
355 "Logged in as:<br/>%login",
362 'Barre Olivier (dumas.roger)',
365 $this->login
->loggedInAs()
367 $this->assertSame('Barre Olivier (dumas.roger)', $this->login
->loggedInAs(true));
371 * Test login from cron
375 public function testLogCron()
377 $this->login
->logCron('reminder', $this->preferences
);
378 $this->assertTrue($this->login
->isLogged());
379 $this->assertFalse($this->login
->isStaff());
380 $this->assertFalse($this->login
->isAdmin());
381 $this->assertFalse($this->login
->isSuperAdmin());
382 $this->assertFalse($this->login
->isActive());
383 $this->assertTrue($this->login
->isCron());
384 $this->assertFalse($this->login
->isUp2Date());
385 $this->assertFalse($this->login
->isImpersonated());
386 $this->assertSame('cron', $this->login
->login
);
387 $this->assertSame($this->preferences
->pref_lang
, $this->login
->lang
);
389 $this->expectException('Exception');
390 $this->expectExceptionMessage('Not authorized!');
391 $this->login
->logCron('filename', $this->preferences
);