]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Adherent.php
c4e5f64c45110d140cf56164a6e3197e96c84242
[galette.git] / tests / Galette / Entity / tests / units / Adherent.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Adherent tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2017-2021 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 Entity
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2017-2021 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 2017-04-17
36 */
37
38 namespace Galette\Entity\test\units;
39
40 use Galette\GaletteTestCase;
41
42 /**
43 * Adherent tests class
44 *
45 * @category Entity
46 * @name Adherent
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2017-2021 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 2017-04-17
53 */
54 class Adherent extends GaletteTestCase
55 {
56 protected $seed = 95842354;
57 private $default_deps;
58
59 /**
60 * Cleanup after tests
61 *
62 * @return void
63 */
64 public function tearDown()
65 {
66 $this->zdb = new \Galette\Core\Db();
67
68 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
69 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
70 $delete->where('parent_id IS NOT NULL');
71 $this->zdb->execute($delete);
72
73 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
74 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
75 $this->zdb->execute($delete);
76 }
77
78 /**
79 * Set up tests
80 *
81 * @param string $testMethod Calling method
82 *
83 * @return void
84 */
85 public function beforeTestMethod($testMethod)
86 {
87 parent::beforeTestMethod($testMethod);
88 $this->initStatus();
89
90 $this->default_deps = [
91 'picture' => true,
92 'groups' => true,
93 'dues' => true,
94 'parent' => false,
95 'children' => false,
96 'dynamics' => false
97 ];
98
99 $this->adh = new \Galette\Entity\Adherent($this->zdb);
100 $this->adh->setDependencies(
101 $this->preferences,
102 $this->members_fields,
103 $this->history
104 );
105 }
106
107 /**
108 * Test empty member
109 *
110 * @return void
111 */
112 public function testEmpty()
113 {
114 $adh = $this->adh;
115 $this->boolean($adh->isAdmin())->isFalse();
116 $this->boolean($adh->admin)->isFalse();
117 $this->boolean($adh->isStaff())->isFalse();
118 $this->boolean($adh->staff)->isFalse();
119 $this->boolean($adh->isDueFree())->isFalse();
120 $this->boolean($adh->due_free)->isFalse();
121 $this->boolean($adh->isGroupMember('any'))->isFalse();
122 $this->boolean($adh->isGroupManager('any'))->isFalse();
123 $this->boolean($adh->isCompany())->isFalse();
124 $this->boolean($adh->isMan())->isFalse();
125 $this->boolean($adh->isWoman())->isFalse();
126 $this->boolean($adh->isActive())->isTrue();
127 $this->boolean($adh->active)->isTrue();
128 $this->boolean($adh->isUp2Date())->isFalse();
129 $this->boolean($adh->appearsInMembersList())->isFalse();
130 $this->boolean($adh->appears_in_list)->isFalse();
131
132 $this->variable($adh->fake_prop)->isNull();
133
134 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
135 }
136
137 /**
138 * Test member load dependencies
139 *
140 * @return void
141 */
142 public function testDependencies()
143 {
144 $adh = $this->adh;
145 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
146
147 $adh = clone $this->adh;
148 $adh->disableAllDeps();
149 $expected = [
150 'picture' => false,
151 'groups' => false,
152 'dues' => false,
153 'parent' => false,
154 'children' => false,
155 'dynamics' => false
156 ];
157 $this->array($adh->deps)->isIdenticalTo($expected);
158
159 $expected = [
160 'picture' => false,
161 'groups' => false,
162 'dues' => true,
163 'parent' => false,
164 'children' => true,
165 'dynamics' => true
166 ];
167 $adh
168 ->enableDep('dues')
169 ->enableDep('dynamics')
170 ->enableDep('children');
171 $this->array($adh->deps)->isIdenticalTo($expected);
172
173 $expected = [
174 'picture' => false,
175 'groups' => false,
176 'dues' => true,
177 'parent' => false,
178 'children' => false,
179 'dynamics' => true
180 ];
181 $adh->disableDep('children');
182 $this->array($adh->deps)->isIdenticalTo($expected);
183
184 $adh->disableDep('none')->enableDep('anothernone');
185 $this->array($adh->deps)->isIdenticalTo($expected);
186
187 $expected = [
188 'picture' => true,
189 'groups' => true,
190 'dues' => true,
191 'parent' => true,
192 'children' => true,
193 'dynamics' => true
194 ];
195 $adh->enableAllDeps('children');
196 $this->array($adh->deps)->isIdenticalTo($expected);
197 }
198
199 /**
200 * Tests getter
201 *
202 * @return void
203 */
204 public function testGetterWException()
205 {
206 $adh = $this->adh;
207
208 $this->exception(
209 function () use ($adh) {
210 $adh->row_classes;
211 }
212 )->isInstanceOf('RuntimeException');
213 }
214
215 /**
216 * Set dependencies from constructor
217 *
218 * @return void
219 */
220 public function testDepsAtConstuct()
221 {
222 $deps = [
223 'picture' => false,
224 'groups' => false,
225 'dues' => false,
226 'parent' => false,
227 'children' => false,
228 'dynamics' => false
229 ];
230 $adh = new \Galette\Entity\Adherent(
231 $this->zdb,
232 null,
233 $deps
234 );
235
236 $this->array($adh->deps)->isIdenticalTo($deps);
237
238 $adh = new \Galette\Entity\Adherent(
239 $this->zdb,
240 null,
241 'not an array'
242 );
243 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
244 }
245
246 /**
247 * Test simple member creation
248 *
249 * @return void
250 */
251 public function testSimpleMember()
252 {
253 $this->getMemberOne();
254 $this->checkMemberOneExpected();
255
256 //load member from db
257 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
258 $this->checkMemberOneExpected($adh);
259 }
260
261 /**
262 * Test load form login and email
263 *
264 * @return void
265 */
266 public function testLoadForLogin()
267 {
268 $this->getMemberOne();
269
270 $login = $this->adh->login;
271 $email = $this->adh->email;
272
273 $this->variable($this->adh->email)->isIdenticalTo($this->adh->getEmail());
274
275 $adh = new \Galette\Entity\Adherent($this->zdb, $login);
276 $this->checkMemberOneExpected($adh);
277
278 $adh = new \Galette\Entity\Adherent($this->zdb, $email);
279 $this->checkMemberOneExpected($adh);
280 }
281
282 /**
283 * Test password updating
284 *
285 * @return void
286 */
287 public function testUpdatePassword()
288 {
289 $this->getMemberOne();
290
291 $this->checkMemberOneExpected();
292
293 $newpass = 'aezrty';
294 \Galette\Entity\Adherent::updatePassword($this->zdb, $this->adh->id, $newpass);
295 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
296 $pw_checked = password_verify($newpass, $adh->password);
297 $this->boolean($pw_checked)->isTrue();
298
299 //reset original password
300 \Galette\Entity\Adherent::updatePassword($this->zdb, $this->adh->id, 'J^B-()f');
301 }
302
303 /**
304 * Tests check errors
305 *
306 * @return void
307 */
308 public function testCheckErrors()
309 {
310 $adh = $this->adh;
311
312 $data = ['ddn_adh' => 'not a date'];
313 $expected = ['- Wrong date format (Y-m-d) for Birth date!'];
314 $check = $adh->check($data, [], []);
315 $this->array($check)->isIdenticalTo($expected);
316
317 $data = [
318 'ddn_adh' => '',
319 'date_crea_adh' => 'not a date'
320 ];
321 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
322 $check = $adh->check($data, [], []);
323 $this->array($check)->isIdenticalTo($expected);
324
325 //reste creation date to its default value
326 $data = ['date_crea_adh' => date('Y-m-d')];
327 $check = $adh->check($data, [], []);
328 $this->boolean($check)->isTrue();
329
330 $data = ['email_adh' => 'not an email'];
331 $expected = ['- Non-valid E-Mail address! (E-Mail)'];
332 $check = $adh->check($data, [], []);
333 $this->array($check)->isIdenticalTo($expected);
334
335 $data = [
336 'email_adh' => '',
337 'url_adh' => 'mywebsite'
338 ];
339 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
340 $check = $adh->check($data, [], []);
341 $this->array($check)->isIdenticalTo($expected);
342
343 $data = ['url_adh' => 'http://'];
344 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
345 $check = $adh->check($data, [], []);
346 $this->boolean($check)->isTrue($expected);
347 $this->variable($adh->_website)->isIdenticalTo('');
348
349 $data = ['login_adh' => 'a'];
350 $expected = ['- The username must be composed of at least 2 characters!'];
351 $check = $adh->check($data, [], []);
352 $this->array($check)->isIdenticalTo($expected);
353
354 $data = ['login_adh' => 'login@galette'];
355 $expected = ['- The username cannot contain the @ character'];
356 $check = $adh->check($data, [], []);
357 $this->array($check)->isIdenticalTo($expected);
358
359 $data = [
360 'login_adh' => '',
361 'mdp_adh' => 'short',
362 'mdp_adh2' => 'short'
363 ];
364 $expected = ['Too short (6 characters minimum, 5 found)'];
365 $check = $adh->check($data, [], []);
366 $this->array($check)->isIdenticalTo($expected);
367
368 $data = ['mdp_adh' => 'mypassword'];
369 $expected = ['- The passwords don\'t match!'];
370 $check = $adh->check($data, [], []);
371 $this->array($check)->isIdenticalTo($expected);
372
373 $data = [
374 'mdp_adh' => 'mypassword',
375 'mdp_adh2' => 'mypasswor'
376 ];
377 $expected = ['- The passwords don\'t match!'];
378 $check = $adh->check($data, [], []);
379 $this->array($check)->isIdenticalTo($expected);
380
381 $data = ['id_statut' => 256];
382 $expected = ['Status #256 does not exists in database.'];
383 $check = $adh->check($data, [], []);
384 $this->array($check)->isIdenticalTo($expected);
385 }
386
387 /**
388 * Test picture
389 *
390 * @return void
391 */
392 public function testPhoto()
393 {
394 $this->getMemberOne();
395
396 $fakedata = new \Galette\Util\FakeData($this->zdb, $this->i18n);
397 $this->boolean($fakedata->addPhoto($this->adh))->isTrue();
398
399 $this->boolean($this->adh->hasPicture())->isTrue();
400
401 //remove photo
402 $this->boolean($this->adh->picture->delete())->isTrue();
403 }
404
405 /**
406 * Test canEdit
407 *
408 * @return void
409 */
410 public function testCanEdit()
411 {
412 $adh = new \Galette\Entity\Adherent($this->zdb);
413
414 //non authorized
415 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
416 $this->boolean($adh->canEdit($login))->isFalse();
417
418 //admin => authorized
419 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
420 $this->calling($login)->isAdmin = true;
421 $this->boolean($adh->canEdit($login))->isTrue();
422
423 //staff => authorized
424 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
425 $this->calling($login)->isStaff = true;
426 $this->boolean($adh->canEdit($login))->isTrue();
427
428 //group managers
429 $adh = new \mock\Galette\Entity\Adherent($this->zdb);
430
431 $g1 = new \mock\Galette\Entity\Group();
432 $this->calling($g1)->getId = 1;
433 $g2 = new \mock\Galette\Entity\Group();
434 $this->calling($g1)->getId = 2;
435
436 $this->calling($adh)->getGroups = [$g1, $g2];
437 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
438 $this->boolean($adh->canEdit($login))->isFalse();
439
440 $this->calling($login)->isGroupManager = true;
441 $this->boolean($adh->canEdit($login))->isTrue();
442 }
443
444 /**
445 * Test member duplication
446 *
447 * @return void
448 */
449 public function testDuplicate()
450 {
451 $this->getMemberOne();
452
453 $this->checkMemberOneExpected();
454
455 //load member from db
456 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
457 $this->checkMemberOneExpected($adh);
458
459 $adh->setDuplicate();
460
461 $this->string($adh->others_infos_admin)->contains('Duplicated from');
462 $this->variable($adh->email)->isNull();
463 $this->variable($adh->id)->isNull();
464 $this->variable($adh->login)->isNull();
465 $this->variable($adh->birthdate)->isNull();
466 $this->variable($adh->surname)->isNull();
467 }
468
469 /**
470 * Test parents
471 *
472 * @return void
473 */
474 public function testParents()
475 {
476 $this->getMemberOne();
477
478 $this->checkMemberOneExpected();
479
480 //load member from db
481 $parent = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
482 $this->checkMemberOneExpected($parent);
483
484 $this->login->logAdmin('superadmin', $this->preferences);
485 $this->boolean($this->login->isLogged())->isTrue();
486 $this->boolean($this->login->isSuperAdmin())->isTrue();
487
488 $child_data = [
489 'nom_adh' => 'Doe',
490 'prenom_adh' => 'Johny',
491 'parent_id' => $parent->id,
492 'attach' => true
493 ];
494 $child = $this->createMember($child_data);
495
496 $this->string($child->name)->isIdenticalTo($child_data['nom_adh']);
497 $this->object($child->parent)->isInstanceOf('\Galette\Entity\Adherent');
498 $this->integer($child->parent->id)->isIdenticalTo($parent->id);
499
500 $check = $child->check(['detach_parent' => true], [], []);
501 if (is_array($check)) {
502 var_dump($check);
503 }
504 $this->boolean($check)->isTrue();
505 $this->boolean($child->store())->isTrue();
506 $this->variable($child->parent)->isNull();
507 }
508
509 /**
510 * Test XSS/SQL injection
511 *
512 * @return void
513 */
514 public function testInjection()
515 {
516 $data = [
517 'nom_adh' => 'Doe',
518 'prenom_adh' => 'Johny <script>console.log("anything");</script>',
519 'email_adh' => 'jdoe@doe.com',
520 'login_adh' => 'jdoe',
521 'info_public_adh' => 'Any <script>console.log("useful");</script> information'
522 ] + $this->dataAdherentOne();
523 $member = $this->createMember($data);
524
525 $this->string($member->sfullname)->isIdenticalTo('DOE Johny Console.log("anything");');
526 $this->string($member->others_infos)->isIdenticalTo('Any console.log("useful"); information');
527 }
528
529 /**
530 * Test can* methods
531 *
532 * @return void
533 */
534 public function testCan()
535 {
536 $this->getMemberOne();
537 //load member from db
538 $member = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
539
540 $this->boolean($member->canShow($this->login))->isFalse();
541 $this->boolean($member->canCreate($this->login))->isFalse();
542 $this->boolean($member->canEdit($this->login))->isFalse();
543
544 //Superadmin can fully change members
545 $this->login->logAdmin('superadmin', $this->preferences);
546 $this->boolean($this->login->isLogged())->isTrue();
547 $this->boolean($this->login->isSuperAdmin())->isTrue();
548
549 $this->boolean($member->canShow($this->login))->isTrue();
550 $this->boolean($member->canCreate($this->login))->isTrue();
551 $this->boolean($member->canEdit($this->login))->isTrue();
552
553 //logout
554 $this->login->logOut();
555 $this->boolean($this->login->isLogged())->isFalse();
556
557 //Member can fully change its own information
558 $mdata = $this->dataAdherentOne();
559 $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
560 $this->boolean($this->login->isLogged())->isTrue();
561 $this->boolean($this->login->isAdmin())->isFalse();
562 $this->boolean($this->login->isStaff())->isFalse();
563
564 $this->boolean($member->canShow($this->login))->isTrue();
565 $this->boolean($member->canCreate($this->login))->isTrue();
566 $this->boolean($member->canEdit($this->login))->isTrue();
567
568 //logout
569 $this->login->logOut();
570 $this->boolean($this->login->isLogged())->isFalse();
571
572 //Another member has no access
573 $this->getMemberTwo();
574 $mdata = $this->dataAdherentTwo();
575 $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
576 $this->boolean($this->login->isLogged())->isTrue();
577 $this->boolean($this->login->isAdmin())->isFalse();
578 $this->boolean($this->login->isStaff())->isFalse();
579
580 $this->boolean($member->canShow($this->login))->isFalse();
581 $this->boolean($member->canCreate($this->login))->isFalse();
582 $this->boolean($member->canEdit($this->login))->isFalse();
583
584 //parents can fully change children information
585 $this->getMemberOne();
586 $mdata = $this->dataAdherentOne();
587 global $login;
588 $login = $this->login;
589 $this->login->logAdmin('superadmin', $this->preferences);
590 $this->boolean($this->login->isLogged())->isTrue();
591 $this->boolean($this->login->isSuperAdmin())->isTrue();
592
593 $child_data = [
594 'nom_adh' => 'Doe',
595 'prenom_adh' => 'Johny',
596 'parent_id' => $member->id,
597 'attach' => true,
598 'login_adh' => 'child.johny.doe',
599 'fingerprint' => 'FAKER' . $this->seed
600 ];
601 $child = $this->createMember($child_data);
602 $cid = $child->id;
603 $this->login->logOut();
604
605 //load child from db
606 $child = new \Galette\Entity\Adherent($this->zdb);
607 $child->enableDep('parent');
608 $this->boolean($child->load($cid))->isTrue();
609
610 $this->string($child->name)->isIdenticalTo($child_data['nom_adh']);
611 $this->object($child->parent)->isInstanceOf('\Galette\Entity\Adherent');
612 $this->integer($child->parent->id)->isIdenticalTo($member->id);
613 $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
614
615 $mdata = $this->dataAdherentOne();
616 $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
617 $this->boolean($this->login->isLogged())->isTrue();
618 $this->boolean($this->login->isAdmin())->isFalse();
619 $this->boolean($this->login->isStaff())->isFalse();
620
621 $this->boolean($child->canShow($this->login))->isTrue();
622 $this->boolean($child->canCreate($this->login))->isFalse();
623 $this->boolean($child->canEdit($this->login))->isTrue();
624
625 //logout
626 $this->login->logOut();
627 $this->boolean($this->login->isLogged())->isFalse();
628 }
629 }