]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Adherent.php
37637f5d96e248b3a25c885ebe4ca9ef9fb323b7
[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 $this->initTitles();
90
91 $this->default_deps = [
92 'picture' => true,
93 'groups' => true,
94 'dues' => true,
95 'parent' => false,
96 'children' => false,
97 'dynamics' => false
98 ];
99
100 $this->adh = new \Galette\Entity\Adherent($this->zdb);
101 $this->adh->setDependencies(
102 $this->preferences,
103 $this->members_fields,
104 $this->history
105 );
106 }
107
108 /**
109 * Test empty member
110 *
111 * @return void
112 */
113 public function testEmpty()
114 {
115 $adh = $this->adh;
116 $this->boolean($adh->isAdmin())->isFalse();
117 $this->boolean($adh->admin)->isFalse();
118 $this->boolean($adh->isStaff())->isFalse();
119 $this->boolean($adh->staff)->isFalse();
120 $this->boolean($adh->isDueFree())->isFalse();
121 $this->boolean($adh->due_free)->isFalse();
122 $this->boolean($adh->isGroupMember('any'))->isFalse();
123 $this->boolean($adh->isGroupManager('any'))->isFalse();
124 $this->boolean($adh->isCompany())->isFalse();
125 $this->boolean($adh->isMan())->isFalse();
126 $this->boolean($adh->isWoman())->isFalse();
127 $this->boolean($adh->isActive())->isTrue();
128 $this->boolean($adh->active)->isTrue();
129 $this->boolean($adh->isUp2Date())->isFalse();
130 $this->boolean($adh->appearsInMembersList())->isFalse();
131 $this->boolean($adh->appears_in_list)->isFalse();
132
133 $this->variable($adh->fake_prop)->isNull();
134
135 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
136 }
137
138 /**
139 * Test member load dependencies
140 *
141 * @return void
142 */
143 public function testDependencies()
144 {
145 $adh = $this->adh;
146 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
147
148 $adh = clone $this->adh;
149 $adh->disableAllDeps();
150 $expected = [
151 'picture' => false,
152 'groups' => false,
153 'dues' => false,
154 'parent' => false,
155 'children' => false,
156 'dynamics' => false
157 ];
158 $this->array($adh->deps)->isIdenticalTo($expected);
159
160 $expected = [
161 'picture' => false,
162 'groups' => false,
163 'dues' => true,
164 'parent' => false,
165 'children' => true,
166 'dynamics' => true
167 ];
168 $adh
169 ->enableDep('dues')
170 ->enableDep('dynamics')
171 ->enableDep('children');
172 $this->array($adh->deps)->isIdenticalTo($expected);
173
174 $expected = [
175 'picture' => false,
176 'groups' => false,
177 'dues' => true,
178 'parent' => false,
179 'children' => false,
180 'dynamics' => true
181 ];
182 $adh->disableDep('children');
183 $this->array($adh->deps)->isIdenticalTo($expected);
184
185 $adh->disableDep('none')->enableDep('anothernone');
186 $this->array($adh->deps)->isIdenticalTo($expected);
187
188 $expected = [
189 'picture' => true,
190 'groups' => true,
191 'dues' => true,
192 'parent' => true,
193 'children' => true,
194 'dynamics' => true
195 ];
196 $adh->enableAllDeps('children');
197 $this->array($adh->deps)->isIdenticalTo($expected);
198 }
199
200 /**
201 * Tests getter
202 *
203 * @return void
204 */
205 public function testGetterWException()
206 {
207 $adh = $this->adh;
208
209 $this->exception(
210 function () use ($adh) {
211 $adh->row_classes;
212 }
213 )->isInstanceOf('RuntimeException');
214 }
215
216 /**
217 * Set dependencies from constructor
218 *
219 * @return void
220 */
221 public function testDepsAtConstuct()
222 {
223 $deps = [
224 'picture' => false,
225 'groups' => false,
226 'dues' => false,
227 'parent' => false,
228 'children' => false,
229 'dynamics' => false
230 ];
231 $adh = new \Galette\Entity\Adherent(
232 $this->zdb,
233 null,
234 $deps
235 );
236
237 $this->array($adh->deps)->isIdenticalTo($deps);
238
239 $adh = new \Galette\Entity\Adherent(
240 $this->zdb,
241 null,
242 'not an array'
243 );
244 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
245 }
246
247 /**
248 * Test simple member creation
249 *
250 * @return void
251 */
252 public function testSimpleMember()
253 {
254 $this->getMemberOne();
255 $this->checkMemberOneExpected();
256
257 //load member from db
258 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
259 $this->checkMemberOneExpected($adh);
260 }
261
262 /**
263 * Test load form login and email
264 *
265 * @return void
266 */
267 public function testLoadForLogin()
268 {
269 $this->getMemberOne();
270
271 $login = $this->adh->login;
272 $email = $this->adh->email;
273
274 $this->variable($this->adh->email)->isIdenticalTo($this->adh->getEmail());
275
276 $adh = new \Galette\Entity\Adherent($this->zdb, $login);
277 $this->checkMemberOneExpected($adh);
278
279 $adh = new \Galette\Entity\Adherent($this->zdb, $email);
280 $this->checkMemberOneExpected($adh);
281 }
282
283 /**
284 * Test password updating
285 *
286 * @return void
287 */
288 public function testUpdatePassword()
289 {
290 $this->getMemberOne();
291
292 $this->checkMemberOneExpected();
293
294 $newpass = 'aezrty';
295 \Galette\Entity\Adherent::updatePassword($this->zdb, $this->adh->id, $newpass);
296 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
297 $pw_checked = password_verify($newpass, $adh->password);
298 $this->boolean($pw_checked)->isTrue();
299
300 //reset original password
301 \Galette\Entity\Adherent::updatePassword($this->zdb, $this->adh->id, 'J^B-()f');
302 }
303
304 /**
305 * Tests check errors
306 *
307 * @return void
308 */
309 public function testCheckErrors()
310 {
311 $adh = $this->adh;
312
313 $data = ['ddn_adh' => 'not a date'];
314 $expected = ['- Wrong date format (Y-m-d) for Birth date!'];
315 $check = $adh->check($data, [], []);
316 $this->array($check)->isIdenticalTo($expected);
317
318 $data = [
319 'ddn_adh' => '',
320 'date_crea_adh' => 'not a date'
321 ];
322 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
323 $check = $adh->check($data, [], []);
324 $this->array($check)->isIdenticalTo($expected);
325
326 //reste creation date to its default value
327 $data = ['date_crea_adh' => date('Y-m-d')];
328 $check = $adh->check($data, [], []);
329 $this->boolean($check)->isTrue();
330
331 $data = ['email_adh' => 'not an email'];
332 $expected = ['- Non-valid E-Mail address! (E-Mail)'];
333 $check = $adh->check($data, [], []);
334 $this->array($check)->isIdenticalTo($expected);
335
336 $data = [
337 'email_adh' => '',
338 'url_adh' => 'mywebsite'
339 ];
340 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
341 $check = $adh->check($data, [], []);
342 $this->array($check)->isIdenticalTo($expected);
343
344 $data = ['url_adh' => 'http://'];
345 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
346 $check = $adh->check($data, [], []);
347 $this->boolean($check)->isTrue($expected);
348 $this->variable($adh->_website)->isIdenticalTo('');
349
350 $data = ['login_adh' => 'a'];
351 $expected = ['- The username must be composed of at least 2 characters!'];
352 $check = $adh->check($data, [], []);
353 $this->array($check)->isIdenticalTo($expected);
354
355 $data = ['login_adh' => 'login@galette'];
356 $expected = ['- The username cannot contain the @ character'];
357 $check = $adh->check($data, [], []);
358 $this->array($check)->isIdenticalTo($expected);
359
360 $data = [
361 'login_adh' => '',
362 'mdp_adh' => 'short',
363 'mdp_adh2' => 'short'
364 ];
365 $expected = ['Too short (6 characters minimum, 5 found)'];
366 $check = $adh->check($data, [], []);
367 $this->array($check)->isIdenticalTo($expected);
368
369 $data = ['mdp_adh' => 'mypassword'];
370 $expected = ['- The passwords don\'t match!'];
371 $check = $adh->check($data, [], []);
372 $this->array($check)->isIdenticalTo($expected);
373
374 $data = [
375 'mdp_adh' => 'mypassword',
376 'mdp_adh2' => 'mypasswor'
377 ];
378 $expected = ['- The passwords don\'t match!'];
379 $check = $adh->check($data, [], []);
380 $this->array($check)->isIdenticalTo($expected);
381
382 $data = ['id_statut' => 256];
383 $expected = ['Status #256 does not exists in database.'];
384 $check = $adh->check($data, [], []);
385 $this->array($check)->isIdenticalTo($expected);
386 }
387
388 /**
389 * Test picture
390 *
391 * @return void
392 */
393 public function testPhoto()
394 {
395 $this->getMemberOne();
396
397 $fakedata = new \Galette\Util\FakeData($this->zdb, $this->i18n);
398 $this->boolean($fakedata->addPhoto($this->adh))->isTrue();
399
400 $this->boolean($this->adh->hasPicture())->isTrue();
401
402 //remove photo
403 $this->boolean($this->adh->picture->delete())->isTrue();
404 }
405
406 /**
407 * Test canEdit
408 *
409 * @return void
410 */
411 public function testCanEdit()
412 {
413 $adh = new \Galette\Entity\Adherent($this->zdb);
414
415 //non authorized
416 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
417 $this->boolean($adh->canEdit($login))->isFalse();
418
419 //admin => authorized
420 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
421 $this->calling($login)->isAdmin = true;
422 $this->boolean($adh->canEdit($login))->isTrue();
423
424 //staff => authorized
425 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
426 $this->calling($login)->isStaff = true;
427 $this->boolean($adh->canEdit($login))->isTrue();
428
429 //group managers
430 $adh = new \mock\Galette\Entity\Adherent($this->zdb);
431
432 $g1 = new \mock\Galette\Entity\Group();
433 $this->calling($g1)->getId = 1;
434 $g2 = new \mock\Galette\Entity\Group();
435 $this->calling($g1)->getId = 2;
436
437 $this->calling($adh)->getGroups = [$g1, $g2];
438 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
439 $this->boolean($adh->canEdit($login))->isFalse();
440
441 $this->calling($login)->isGroupManager = true;
442 $this->boolean($adh->canEdit($login))->isTrue();
443 }
444
445 /**
446 * Test member duplication
447 *
448 * @return void
449 */
450 public function testDuplicate()
451 {
452 $this->getMemberOne();
453
454 $this->checkMemberOneExpected();
455
456 //load member from db
457 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
458 $this->checkMemberOneExpected($adh);
459
460 $adh->setDuplicate();
461
462 $this->string($adh->others_infos_admin)->contains('Duplicated from');
463 $this->variable($adh->email)->isNull();
464 $this->variable($adh->id)->isNull();
465 $this->variable($adh->login)->isNull();
466 $this->variable($adh->birthdate)->isNull();
467 $this->variable($adh->surname)->isNull();
468 }
469
470 /**
471 * Test parents
472 *
473 * @return void
474 */
475 public function testParents()
476 {
477 $this->getMemberOne();
478
479 $this->checkMemberOneExpected();
480
481 //load member from db
482 $parent = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
483 $this->checkMemberOneExpected($parent);
484
485 $this->login->logAdmin('superadmin', $this->preferences);
486 $this->boolean($this->login->isLogged())->isTrue();
487 $this->boolean($this->login->isSuperAdmin())->isTrue();
488
489 $child_data = [
490 'nom_adh' => 'Doe',
491 'prenom_adh' => 'Johny',
492 'parent_id' => $parent->id,
493 'attach' => true
494 ];
495 $child = $this->createMember($child_data);
496
497 $this->string($child->name)->isIdenticalTo($child_data['nom_adh']);
498 $this->object($child->parent)->isInstanceOf('\Galette\Entity\Adherent');
499 $this->integer($child->parent->id)->isIdenticalTo($parent->id);
500
501 $check = $child->check(['detach_parent' => true], [], []);
502 if (is_array($check)) {
503 var_dump($check);
504 }
505 $this->boolean($check)->isTrue();
506 $this->boolean($child->store())->isTrue();
507 $this->variable($child->parent)->isNull();
508 }
509
510 /**
511 * Test XSS/SQL injection
512 *
513 * @return void
514 */
515 public function testInjection()
516 {
517 $data = [
518 'nom_adh' => 'Doe',
519 'prenom_adh' => 'Johny <script>console.log("anything");</script>',
520 'email_adh' => 'jdoe@doe.com',
521 'login_adh' => 'jdoe',
522 'info_public_adh' => 'Any <script>console.log("useful");</script> information'
523 ] + $this->dataAdherentOne();
524 $member = $this->createMember($data);
525
526 $this->string($member->sfullname)->isIdenticalTo('DOE Johny Console.log("anything");');
527 $this->string($member->others_infos)->isIdenticalTo('Any console.log("useful"); information');
528 }
529
530 /**
531 * Test can* methods
532 *
533 * @return void
534 */
535 public function testCan()
536 {
537 $this->getMemberOne();
538 //load member from db
539 $member = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
540
541 $this->boolean($member->canShow($this->login))->isFalse();
542 $this->boolean($member->canCreate($this->login))->isFalse();
543 $this->boolean($member->canEdit($this->login))->isFalse();
544
545 //Superadmin can fully change members
546 $this->login->logAdmin('superadmin', $this->preferences);
547 $this->boolean($this->login->isLogged())->isTrue();
548 $this->boolean($this->login->isSuperAdmin())->isTrue();
549
550 $this->boolean($member->canShow($this->login))->isTrue();
551 $this->boolean($member->canCreate($this->login))->isTrue();
552 $this->boolean($member->canEdit($this->login))->isTrue();
553
554 //logout
555 $this->login->logOut();
556 $this->boolean($this->login->isLogged())->isFalse();
557
558 //Member can fully change its own information
559 $mdata = $this->dataAdherentOne();
560 $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
561 $this->boolean($this->login->isLogged())->isTrue();
562 $this->boolean($this->login->isAdmin())->isFalse();
563 $this->boolean($this->login->isStaff())->isFalse();
564
565 $this->boolean($member->canShow($this->login))->isTrue();
566 $this->boolean($member->canCreate($this->login))->isTrue();
567 $this->boolean($member->canEdit($this->login))->isTrue();
568
569 //logout
570 $this->login->logOut();
571 $this->boolean($this->login->isLogged())->isFalse();
572
573 //Another member has no access
574 $this->getMemberTwo();
575 $mdata = $this->dataAdherentTwo();
576 $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
577 $this->boolean($this->login->isLogged())->isTrue();
578 $this->boolean($this->login->isAdmin())->isFalse();
579 $this->boolean($this->login->isStaff())->isFalse();
580
581 $this->boolean($member->canShow($this->login))->isFalse();
582 $this->boolean($member->canCreate($this->login))->isFalse();
583 $this->boolean($member->canEdit($this->login))->isFalse();
584
585 //parents can fully change children information
586 $this->getMemberOne();
587 $mdata = $this->dataAdherentOne();
588 global $login;
589 $login = $this->login;
590 $this->login->logAdmin('superadmin', $this->preferences);
591 $this->boolean($this->login->isLogged())->isTrue();
592 $this->boolean($this->login->isSuperAdmin())->isTrue();
593
594 $child_data = [
595 'nom_adh' => 'Doe',
596 'prenom_adh' => 'Johny',
597 'parent_id' => $member->id,
598 'attach' => true,
599 'login_adh' => 'child.johny.doe',
600 'fingerprint' => 'FAKER' . $this->seed
601 ];
602 $child = $this->createMember($child_data);
603 $cid = $child->id;
604 $this->login->logOut();
605
606 //load child from db
607 $child = new \Galette\Entity\Adherent($this->zdb);
608 $child->enableDep('parent');
609 $this->boolean($child->load($cid))->isTrue();
610
611 $this->string($child->name)->isIdenticalTo($child_data['nom_adh']);
612 $this->object($child->parent)->isInstanceOf('\Galette\Entity\Adherent');
613 $this->integer($child->parent->id)->isIdenticalTo($member->id);
614 $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
615
616 $mdata = $this->dataAdherentOne();
617 $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
618 $this->boolean($this->login->isLogged())->isTrue();
619 $this->boolean($this->login->isAdmin())->isFalse();
620 $this->boolean($this->login->isStaff())->isFalse();
621
622 $this->boolean($child->canShow($this->login))->isTrue();
623 $this->boolean($child->canCreate($this->login))->isFalse();
624 $this->boolean($child->canEdit($this->login))->isTrue();
625
626 //logout
627 $this->login->logOut();
628 $this->boolean($this->login->isLogged())->isFalse();
629 }
630
631 /**
632 * Names provider
633 *
634 * @return array[]
635 */
636 protected function nameCaseProvider(): array
637 {
638 return [
639 [
640 'name' => 'Doe',
641 'surname' => 'John',
642 'title' => false,
643 'id' => false,
644 'nick' => false,
645 'expected' => 'DOE John'
646 ],
647 [
648 'name' => 'Doéè',
649 'surname' => 'John',
650 'title' => false,
651 'id' => false,
652 'nick' => false,
653 'expected' => 'DOÉÈ John'
654 ],
655 [
656 'name' => 'Doe',
657 'surname' => 'John',
658 'title' => new \Galette\Entity\Title(\Galette\Entity\Title::MR),
659 'id' => false,
660 'nick' => false,
661 'expected' => 'Mr. DOE John'
662 ],
663 [
664 'name' => 'Doe',
665 'surname' => 'John',
666 'title' => false,
667 'id' => false,
668 'nick' => 'foo',
669 'expected' => 'DOE John (foo)'
670 ],
671 [
672 'name' => 'Doe',
673 'surname' => 'John',
674 'title' => false,
675 'id' => 42,
676 'nick' => false,
677 'expected' => 'DOE John (42)'
678 ],
679 [
680 'name' => 'Doe',
681 'surname' => 'John',
682 'title' => new \Galette\Entity\Title(\Galette\Entity\Title::MR),
683 'id' => 42,
684 'nick' => 'foo',
685 'expected' => 'Mr. DOE John (foo, 42)'
686 ],
687 ];
688 }
689
690 /**
691 * Test getNameWithCase
692 *
693 * @dataProvider nameCaseProvider
694 *
695 * @param string $name Name
696 * @param string $surname Surname
697 * @param \Galette\Entity\Title|false $title Title
698 * @param string|false $id ID
699 * @param string|false $nick Nick
700 * @param string $expected Expected result
701 *
702 * @return void
703 */
704 public function testsGetNameWithCase(string $name, string $surname, $title, $id, $nick, string $expected)
705 {
706 $this->string(
707 \Galette\Entity\Adherent::getNameWithCase(
708 $name,
709 $surname,
710 $title,
711 $id,
712 $nick,
713 )
714 )->isIdenticalTo($expected);
715 }
716 }