]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Login.php
eaf870c4ad1979a97386172e231e436f9db3150d
[galette.git] / tests / Galette / Core / tests / units / Login.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 namespace Galette\Core\test\units;
23
24 use Galette\GaletteTestCase;
25
26 /**
27 * Login tests class
28 *
29 * @author Johan Cwiklinski <johan@x-tnd.be>
30 */
31 class Login extends GaletteTestCase
32 {
33 protected int $seed = 320112365;
34 private string $login_adh = 'dumas.roger';
35 private string $mdp_adh = 'sd8)AvtE|*';
36
37 /**
38 * Cleanup after tests
39 *
40 * @return void
41 */
42 public function tearDown(): void
43 {
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);
48
49 parent::tearDown();
50 }
51
52 /**
53 * Test defaults
54 *
55 * @return void
56 */
57 public function testDefaults()
58 {
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);
68 }
69
70 /**
71 * Test not logged-in users Impersonating
72 *
73 * @return void
74 */
75 public function testNotLoggedCantImpersonate()
76 {
77 $login = $this->getMockBuilder(\Galette\Core\Login::class)
78 ->setConstructorArgs(array($this->zdb, $this->i18n))
79 ->onlyMethods(array('isLogged'))
80 ->getMock();
81 $login->method('isLogged')->willReturn(false);
82
83 $this->expectExceptionMessage('Only superadmin can impersonate!');
84 $login->impersonate(1);
85 }
86
87 /**
88 * Test staff users Impersonating
89 *
90 * @return void
91 */
92 public function testStaffCantImpersonate()
93 {
94 $login = $this->getMockBuilder(\Galette\Core\Login::class)
95 ->setConstructorArgs(array($this->zdb, $this->i18n))
96 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin'))
97 ->getMock();
98
99 $login->method('isLogged')->willReturn(true);
100 $login->method('isStaff')->willReturn(true);
101 $login->method('isAdmin')->willReturn(false);
102 $login->method('isSuperAdmin')->willReturn(false);
103
104 $this->expectExceptionMessage('Only superadmin can impersonate!');
105 $login->impersonate(1);
106 }
107
108 /**
109 * Test admin users Impersonating
110 *
111 * @return void
112 */
113 public function testAdminCantImpersonate()
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(true);
123 $login->method('isSuperAdmin')->willReturn(false);
124
125 $this->expectExceptionMessage('Only superadmin can impersonate!');
126 $login->impersonate(1);
127 }
128
129 /**
130 * Test Impersonating that throws an exception
131 *
132 * @return void
133 */
134 public function testImpersonateExistsWException()
135 {
136 $zdb = $this->getMockBuilder(\Galette\Core\Db::class)
137 ->onlyMethods(array('execute'))
138 ->getMock();
139
140 $zdb->method('execute')
141 ->willReturnCallback(
142 function ($o) {
143 throw new \LogicException('Error executing query!', 123);
144 }
145 );
146
147 $login = $this->getMockBuilder(\Galette\Core\Login::class)
148 ->setConstructorArgs(array($zdb, $this->i18n))
149 ->onlyMethods(array('isSuperAdmin'))
150 ->getMock();
151
152 $login->method('isSuperAdmin')->willReturn(true);
153
154 $this->assertFalse($login->impersonate(1));
155 }
156
157 /**
158 * Test superadmin users Impersonating
159 *
160 * @return void
161 */
162 public function testSuperadminCanImpersonate()
163 {
164 $login = $this->getMockBuilder(\Galette\Core\Login::class)
165 ->setConstructorArgs(array($this->zdb, $this->i18n))
166 ->onlyMethods(array('isSuperAdmin'))
167 ->getMock();
168
169 $login->method('isSuperAdmin')->willReturn(true);
170
171 ///We're faking, Impersonating won't work but will not throw any exception
172 $this->assertFalse($login->impersonate(1));
173 }
174
175 /**
176 * Test return requesting a non-existing property
177 *
178 * @return void
179 */
180 public function testInexistingGetter()
181 {
182 $this->expectException('RuntimeException');
183 $this->expectExceptionMessage('Property doesnotexists is not set!');
184 $this->assertFalse($this->login->doesnotexists);
185 }
186
187 /**
188 * Test login exists
189 *
190 * @return void
191 */
192 public function testLoginExists()
193 {
194 $this->assertFalse($this->login->loginExists('exists'));
195 $this->assertFalse($this->login->loginExists('doesnotexists'));
196 }
197
198 /**
199 * Test login exists that throws an exception
200 *
201 * @return void
202 */
203 public function testLoginExistsWException()
204 {
205 $zdb = $this->getMockBuilder(\Galette\Core\Db::class)
206 ->onlyMethods(array('execute'))
207 ->getMock();
208
209 $zdb->method('execute')
210 ->willReturnCallback(
211 function ($o) {
212 if ($o instanceof \Laminas\Db\Sql\Select) {
213 throw new \LogicException('Error executing query!', 123);
214 }
215 }
216 );
217
218 $login = new \Galette\Core\Login($zdb, $this->i18n);
219 $this->assertTrue($login->loginExists('doesnotexists'));
220 }
221
222 /**
223 * Test login as super admin
224 *
225 * @return void
226 */
227 public function testLogAdmin()
228 {
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);
239
240 //test logout
241 $this->login->logOut();
242 $this->testDefaults();
243 }
244
245 /**
246 * Creates or load test user
247 *
248 * @return void
249 */
250 private function createUser()
251 {
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);
255
256 global $zdb, $login, $hist, $i18n; // globals :(
257 $zdb = $this->zdb;
258 $login = $this->login;
259 $hist = $this->history;
260 $i18n = $this->i18n;
261
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);
267 }
268
269 $data = [
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,
282 'sexe_adh' => 1,
283 'prof_adh' => 'Pédologue',
284 'titre_adh' => null,
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,
291 'id_statut' => 6,
292 'date_crea_adh' => '2019-09-02',
293 'pref_lang' => 'nb_NO',
294 'fingerprint' => 'FAKER' . $this->seed,
295 ];
296
297 $this->adh = new \Galette\Entity\Adherent($this->zdb);
298 $this->adh->setDependencies(
299 $this->preferences,
300 $this->members_fields,
301 $this->history
302 );
303
304 $check = $this->adh->check($data, [], []);
305 if (is_array($check)) {
306 var_dump($check);
307 }
308 $this->assertTrue($check);
309
310 $store = $this->adh->store();
311 $this->assertTrue($store);
312 } else {
313 $this->adh = new \Galette\Entity\Adherent($this->zdb, $results->current());
314 }
315 }
316
317 /**
318 * Look for a login that does exist
319 *
320 * @return void
321 */
322 public function testLoginExistsDb()
323 {
324 $this->createUser();
325 $this->assertTrue($this->login->loginExists('dumas.roger'));
326 }
327
328 /**
329 * Test user login
330 *
331 * @return void
332 */
333 public function testLogin()
334 {
335 $this->createUser();
336 $this->assertFalse($this->login->login('doenotexists', 'empty'));
337 $this->assertTrue($this->login->login($this->login_adh, $this->mdp_adh));
338 }
339
340 /**
341 * Test logged user name
342 *
343 * @return void
344 */
345 public function testLoggedInAs()
346 {
347 global $translator;
348
349 $this->createUser();
350 $this->assertTrue($this->login->login($this->login_adh, $this->mdp_adh));
351
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",
356 'galette',
357 $this->login->lang
358 );
359 $this->assertSame(
360 str_replace(
361 '%login',
362 'Barre Olivier (dumas.roger)',
363 $tstring
364 ),
365 $this->login->loggedInAs()
366 );
367 $this->assertSame('Barre Olivier (dumas.roger)', $this->login->loggedInAs(true));
368 }
369
370 /**
371 * Test login from cron
372 *
373 * @return void
374 */
375 public function testLogCron()
376 {
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);
388
389 $this->expectException('Exception');
390 $this->expectExceptionMessage('Not authorized!');
391 $this->login->logCron('filename', $this->preferences);
392 }
393 }