]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Adherent.php
Instanciate titles, models, status, ...
[galette.git] / tests / Galette / Entity / tests / units / Adherent.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\Entity\test\units;
23
24 use Galette\GaletteTestCase;
25
26 /**
27 * Adherent tests class
28 *
29 * @author Johan Cwiklinski <johan@x-tnd.be>
30 */
31 class Adherent extends GaletteTestCase
32 {
33 protected int $seed = 95842354;
34 private array $default_deps;
35
36 /**
37 * Cleanup after tests
38 *
39 * @return void
40 */
41 public function tearDown(): void
42 {
43 parent::tearDown();
44 $this->zdb = new \Galette\Core\Db();
45
46 $this->cleanContributions();
47
48 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
49 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
50 $delete->where('parent_id IS NOT NULL');
51 $this->zdb->execute($delete);
52
53 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
54 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
55 $this->zdb->execute($delete);
56 }
57
58 /**
59 * Cleanup after class
60 *
61 * @return void
62 */
63 public static function tearDownAfterClass(): void
64 {
65 $self = new self(__METHOD__);
66 $self->setUp();
67 $self->tearDown();
68 }
69
70 /**
71 * Set up tests
72 *
73 * @return void
74 */
75 public function setUp(): void
76 {
77 parent::setUp();
78
79 $this->default_deps = [
80 'picture' => true,
81 'groups' => true,
82 'dues' => true,
83 'parent' => false,
84 'children' => false,
85 'dynamics' => false,
86 'socials' => false
87 ];
88
89 $this->adh = new \Galette\Entity\Adherent($this->zdb);
90 $this->adh->setDependencies(
91 $this->preferences,
92 $this->members_fields,
93 $this->history
94 );
95 }
96
97 /**
98 * Test empty member
99 *
100 * @return void
101 */
102 public function testEmpty()
103 {
104 $adh = $this->adh;
105 $this->assertFalse($adh->isAdmin());
106 $this->assertFalse($adh->admin);
107 $this->assertFalse($adh->isStaff());
108 $this->assertFalse($adh->staff);
109 $this->assertFalse($adh->isDueFree());
110 $this->assertFalse($adh->due_free);
111 $this->assertFalse($adh->isGroupMember('any'));
112 $this->assertFalse($adh->isGroupManager('any'));
113 $this->assertFalse($adh->isCompany());
114 $this->assertFalse($adh->isMan());
115 $this->assertFalse($adh->isWoman());
116 $this->assertTrue($adh->isActive());
117 $this->assertTrue($adh->active);
118 $this->assertFalse($adh->isUp2Date());
119 $this->assertFalse($adh->appearsInMembersList());
120 $this->assertFalse($adh->appears_in_list);
121
122 $this->assertNull($adh->fake_prop);
123
124 $this->assertSame($this->default_deps, $adh->deps);
125 }
126
127 /**
128 * Test member load dependencies
129 *
130 * @return void
131 */
132 public function testDependencies()
133 {
134 $adh = $this->adh;
135 $this->assertSame($this->default_deps, $adh->deps);
136
137 $adh = clone $this->adh;
138 $adh->disableAllDeps();
139 $expected = [
140 'picture' => false,
141 'groups' => false,
142 'dues' => false,
143 'parent' => false,
144 'children' => false,
145 'dynamics' => false,
146 'socials' => false
147 ];
148 $this->assertSame($expected, $adh->deps);
149
150 $expected = [
151 'picture' => false,
152 'groups' => false,
153 'dues' => true,
154 'parent' => false,
155 'children' => true,
156 'dynamics' => true,
157 'socials' => false
158 ];
159 $adh
160 ->enableDep('dues')
161 ->enableDep('dynamics')
162 ->enableDep('children');
163 $this->assertSame($expected, $adh->deps);
164
165 $expected = [
166 'picture' => false,
167 'groups' => false,
168 'dues' => true,
169 'parent' => false,
170 'children' => false,
171 'dynamics' => true,
172 'socials' => false
173 ];
174 $adh->disableDep('children');
175 $this->assertSame($expected, $adh->deps);
176
177 $adh->disableDep('none')->enableDep('anothernone');
178 $this->assertSame($expected, $adh->deps);
179
180 $expected = [
181 'picture' => true,
182 'groups' => true,
183 'dues' => true,
184 'parent' => true,
185 'children' => true,
186 'dynamics' => true,
187 'socials' => true
188 ];
189 $adh->enableAllDeps('children');
190 $this->assertSame($expected, $adh->deps);
191 }
192
193 /**
194 * Tests getter
195 *
196 * @return void
197 */
198 public function testGetterWException()
199 {
200 $adh = $this->adh;
201
202 $this->expectException('RuntimeException');
203 $adh->row_classes;
204 }
205
206 /**
207 * Set dependencies from constructor
208 *
209 * @return void
210 */
211 public function testDepsAtConstuct()
212 {
213 $deps = [
214 'picture' => false,
215 'groups' => false,
216 'dues' => false,
217 'parent' => false,
218 'children' => false,
219 'dynamics' => false,
220 'socials' => false
221 ];
222 $adh = new \Galette\Entity\Adherent(
223 $this->zdb,
224 null,
225 $deps
226 );
227
228 $this->assertSame($deps, $adh->deps);
229 }
230
231 /**
232 * Test simple member creation
233 *
234 * @return void
235 */
236 public function testSimpleMember()
237 {
238 $this->getMemberOne();
239 $this->checkMemberOneExpected();
240
241 //load member from db
242 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
243 $this->checkMemberOneExpected($adh);
244 }
245
246 /**
247 * Test load form login and email
248 *
249 * @return void
250 */
251 public function testLoadForLogin()
252 {
253 $this->getMemberOne();
254
255 $login = $this->adh->login;
256 $email = $this->adh->email;
257
258 $this->assertSame($this->adh->getEmail(), $this->adh->email);
259
260 $adh = new \Galette\Entity\Adherent($this->zdb, $login);
261 $this->checkMemberOneExpected($adh);
262
263 $adh = new \Galette\Entity\Adherent($this->zdb, $email);
264 $this->checkMemberOneExpected($adh);
265 }
266
267 /**
268 * Test password updating
269 *
270 * @return void
271 */
272 public function testUpdatePassword()
273 {
274 $this->getMemberOne();
275
276 $this->checkMemberOneExpected();
277
278 $newpass = 'aezrty';
279 \Galette\Entity\Adherent::updatePassword($this->zdb, $this->adh->id, $newpass);
280 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
281 $pw_checked = password_verify($newpass, $adh->password);
282 $this->assertTrue($pw_checked);
283
284 //reset original password
285 \Galette\Entity\Adherent::updatePassword($this->zdb, $this->adh->id, 'J^B-()f');
286 }
287
288 /**
289 * Tests check errors
290 *
291 * @return void
292 */
293 public function testCheckErrors()
294 {
295 $adh = $this->adh;
296
297 $data = ['ddn_adh' => 'not a date'];
298 $expected = ['- Wrong date format (Y-m-d) for Birth date!'];
299 $check = $adh->check($data, [], []);
300 $this->assertSame($expected, $check);
301
302 $data = [
303 'ddn_adh' => '',
304 'date_crea_adh' => 'not a date'
305 ];
306 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
307 $check = $adh->check($data, [], []);
308 $this->assertSame($expected, $check);
309
310 //reste creation date to its default value
311 $data = ['date_crea_adh' => date('Y-m-d')];
312 $check = $adh->check($data, [], []);
313 $this->assertTrue($check);
314
315 $data = ['email_adh' => 'not an email'];
316 $expected = ['- Non-valid E-Mail address! (E-Mail)'];
317 $check = $adh->check($data, [], []);
318 $this->assertSame($expected, $check);
319
320 $data = ['login_adh' => 'a'];
321 $expected = ['- The username must be composed of at least 2 characters!'];
322 $check = $adh->check($data, [], []);
323 $this->assertSame($expected, $check);
324
325 $data = ['login_adh' => 'login@galette'];
326 $expected = ['- The username cannot contain the @ character'];
327 $check = $adh->check($data, [], []);
328 $this->assertSame($expected, $check);
329
330 $data = [
331 'login_adh' => '',
332 'mdp_adh' => 'short',
333 'mdp_adh2' => 'short'
334 ];
335 $expected = ['Too short (6 characters minimum, 5 found)'];
336 $check = $adh->check($data, [], []);
337 $this->assertSame($expected, $check);
338
339 $data = ['mdp_adh' => 'mypassword'];
340 $expected = ['- The passwords don\'t match!'];
341 $check = $adh->check($data, [], []);
342 $this->assertSame($expected, $check);
343
344 $data = [
345 'mdp_adh' => 'mypassword',
346 'mdp_adh2' => 'mypasswor'
347 ];
348 $expected = ['- The passwords don\'t match!'];
349 $check = $adh->check($data, [], []);
350 $this->assertSame($expected, $check);
351
352 $data = ['id_statut' => 256];
353 $expected = ['Status #256 does not exists in database.'];
354 $check = $adh->check($data, [], []);
355 $this->assertSame($expected, $check);
356
357 //tests for group managers
358 //test insert failing
359 $g1 = $this->getMockBuilder(\Galette\Entity\Group::class)
360 ->onlyMethods(array('getId'))
361 ->getMock();
362 $g1->method('getId')->willReturn(1);
363
364 $g2 = $this->getMockBuilder(\Galette\Entity\Group::class)
365 ->onlyMethods(array('getId'))
366 ->getMock();
367 $g2->method('getId')->willReturn(2);
368
369 //groups managers must specify a group they manage
370 global $login;
371 $login = $this->getMockBuilder(\Galette\Core\Login::class)
372 ->setConstructorArgs(array($this->zdb, $this->i18n))
373 ->onlyMethods(array('isGroupManager'))
374 ->getMock();
375 $login->method('isGroupManager')->willReturnCallback(
376 function ($gid) use ($g1) {
377 return $gid === null || $gid == $g1->getId();
378 }
379 );
380
381 $data = ['id_statut' => \Galette\Entity\Status::DEFAULT_STATUS];
382 $check = $adh->check($data, [], []);
383 $expected = ['You have to select a group you own!'];
384 $this->assertSame($expected, $check);
385
386 $data = ['groups_adh' => [$g2->getId()]];
387 $check = $adh->check($data, [], []);
388 $expected = ['You have to select a group you own!'];
389 $this->assertSame($expected, $check);
390
391 $data = ['groups_adh' => [$g1->getId()]];
392 $check = $adh->check($data, [], []);
393 $this->assertTrue($check);
394 }
395
396 /**
397 * Test picture
398 *
399 * @return void
400 */
401 public function testPhoto()
402 {
403 $this->getMemberOne();
404
405 $fakedata = new \Galette\Util\FakeData($this->zdb, $this->i18n);
406 $this->assertTrue($fakedata->addPhoto($this->adh));
407
408 $this->assertTrue($this->adh->hasPicture());
409
410 //remove photo
411 $this->assertTrue($this->adh->picture->delete());
412 }
413
414 /**
415 * Test canEdit
416 *
417 * @return void
418 */
419 public function testCanEdit()
420 {
421 $adh = new \Galette\Entity\Adherent($this->zdb);
422
423 //non authorized
424 $login = $this->getMockBuilder(\Galette\Core\Login::class)
425 ->setConstructorArgs(array($this->zdb, $this->i18n))
426 ->onlyMethods(array('isGroupManager'))
427 ->getMock();
428 $this->assertFalse($adh->canEdit($login));
429
430 //admin => authorized
431 $login = $this->getMockBuilder(\Galette\Core\Login::class)
432 ->setConstructorArgs(array($this->zdb, $this->i18n))
433 ->onlyMethods(array('isAdmin'))
434 ->getMock();
435 $login->method('isAdmin')->willReturn(true);
436 $this->assertTrue($adh->canEdit($login));
437
438 //staff => authorized
439 $login = $this->getMockBuilder(\Galette\Core\Login::class)
440 ->setConstructorArgs(array($this->zdb, $this->i18n))
441 ->onlyMethods(array('isStaff'))
442 ->getMock();
443 $login->method('isStaff')->willReturn(true);
444 $this->assertTrue($adh->canEdit($login));
445
446 //group managers
447 $adh = $this->getMockBuilder(\Galette\Entity\Adherent::class)
448 ->setConstructorArgs(array($this->zdb))
449 ->onlyMethods(array('getGroups'))
450 ->getMock();
451
452 $g1 = $this->getMockBuilder(\Galette\Entity\Group::class)
453 ->onlyMethods(array('getId'))
454 ->getMock();
455 $g1->method('getId')->willReturn(1);
456
457 $g2 = $this->getMockBuilder(\Galette\Entity\Group::class)
458 ->onlyMethods(array('getId'))
459 ->getMock();
460 $g2->method('getId')->willReturn(2);
461
462 $adh->method('getGroups')->willReturn([$g1, $g2]);
463
464 $login = $this->getMockBuilder(\Galette\Core\Login::class)
465 ->setConstructorArgs(array($this->zdb, $this->i18n))
466 ->onlyMethods(array('isGroupManager'))
467 ->getMock();
468
469 $this->assertFalse($adh->canEdit($login));
470
471 $login->method('isGroupManager')->willReturnCallback(
472 function ($gid) use ($g1) {
473 return $gid === null || $gid == $g1->getId();
474 }
475 );
476 $this->assertFalse($adh->canEdit($login));
477
478 $this->preferences->pref_bool_groupsmanagers_edit_member = true;
479 $canEdit = $adh->canEdit($login);
480 $this->preferences->pref_bool_groupsmanagers_edit_member = false; //reset
481 $this->assertTrue($canEdit);
482
483 //groups managers cannot edit members of the groups they do not own
484 $adh->method('getGroups')->willReturn([$g2]);
485 $this->assertFalse($adh->canEdit($login));
486 }
487
488 /**
489 * Test member duplication
490 *
491 * @return void
492 */
493 public function testDuplicate()
494 {
495 $this->getMemberOne();
496
497 $this->checkMemberOneExpected();
498
499 //load member from db
500 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
501 $this->checkMemberOneExpected($adh);
502
503 $adh->setDuplicate();
504
505 $this->assertStringContainsString('Duplicated from', $adh->others_infos_admin);
506 $this->assertNull($adh->email);
507 $this->assertNull($adh->id);
508 $this->assertNull($adh->login);
509 $this->assertNull($adh->birthdate);
510 $this->assertNull($adh->surname);
511 }
512
513 /**
514 * Test parents
515 *
516 * @return void
517 */
518 public function testParents()
519 {
520 $this->getMemberOne();
521
522 $this->checkMemberOneExpected();
523
524 //load member from db
525 $parent = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
526 $this->checkMemberOneExpected($parent);
527
528 $this->logSuperAdmin();
529
530 $child_data = [
531 'nom_adh' => 'Doe',
532 'prenom_adh' => 'Johny',
533 'parent_id' => $parent->id,
534 'attach' => true,
535 'fingerprint' => 'FAKER' . $this->seed
536 ];
537 $child = $this->createMember($child_data);
538
539 $this->assertSame($child_data['nom_adh'], $child->name);
540 $this->assertInstanceOf('\Galette\Entity\Adherent', $child->parent);
541 $this->assertSame($parent->id, $child->parent->id);
542
543 $check = $child->check(['detach_parent' => true], [], []);
544 if (is_array($check)) {
545 var_dump($check);
546 }
547 $this->assertTrue($check);
548 $this->assertTrue($child->store());
549 $this->assertNull($child->parent);
550 }
551
552 /**
553 * Test XSS/SQL injection
554 *
555 * @return void
556 */
557 public function testInjection()
558 {
559 $data = [
560 'nom_adh' => 'Doe',
561 'prenom_adh' => 'Johny <script>console.log("anything");</script>',
562 'email_adh' => 'jdoe@doe.com',
563 'login_adh' => 'jdoe',
564 'info_public_adh' => 'Any <script>console.log("useful");</script> information',
565 'fingerprint' => 'FAKER' . $this->seed
566 ] + $this->dataAdherentOne();
567 $member = $this->createMember($data);
568
569 $this->assertSame('DOE Johny Console.log("anything");', $member->sfullname);
570 $this->assertSame('Any console.log("useful"); information', $member->others_infos);
571 }
572
573 /**
574 * Test can* methods
575 *
576 * @return void
577 */
578 public function testCan()
579 {
580 $this->getMemberOne();
581 //load member from db
582 $member = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
583
584 $this->assertFalse($member->canShow($this->login));
585 $this->assertFalse($member->canCreate($this->login));
586 $this->assertFalse($member->canEdit($this->login));
587
588 //Superadmin can fully change members
589 $this->logSuperAdmin();
590
591 $this->assertTrue($member->canShow($this->login));
592 $this->assertTrue($member->canCreate($this->login));
593 $this->assertTrue($member->canEdit($this->login));
594
595 //logout
596 $this->login->logOut();
597 $this->assertFalse($this->login->isLogged());
598
599 //Member can fully change its own information
600 $mdata = $this->dataAdherentOne();
601 $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh']));
602 $this->assertTrue($this->login->isLogged());
603 $this->assertFalse($this->login->isAdmin());
604 $this->assertFalse($this->login->isStaff());
605
606 $this->assertTrue($member->canShow($this->login));
607 $this->assertTrue($member->canCreate($this->login));
608 $this->assertTrue($member->canEdit($this->login));
609
610 //logout
611 $this->login->logOut();
612 $this->assertFalse($this->login->isLogged());
613
614 //Another member has no access
615 $this->getMemberTwo();
616 $mdata = $this->dataAdherentTwo();
617 $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh']));
618 $this->assertTrue($this->login->isLogged());
619 $this->assertFalse($this->login->isAdmin());
620 $this->assertFalse($this->login->isStaff());
621
622 $this->assertFalse($member->canShow($this->login));
623 $this->assertFalse($member->canCreate($this->login));
624 $this->assertFalse($member->canEdit($this->login));
625
626 //parents can fully change children information
627 $this->getMemberOne();
628 $mdata = $this->dataAdherentOne();
629 global $login;
630 $login = $this->login;
631 $this->logSuperAdmin();
632
633 $child_data = [
634 'nom_adh' => 'Doe',
635 'prenom_adh' => 'Johny',
636 'parent_id' => $member->id,
637 'attach' => true,
638 'login_adh' => 'child.johny.doe',
639 'fingerprint' => 'FAKER' . $this->seed
640 ];
641 $child = $this->createMember($child_data);
642 $cid = $child->id;
643 $this->login->logOut();
644
645 //load child from db
646 $child = new \Galette\Entity\Adherent($this->zdb);
647 $child->enableDep('parent');
648 $this->assertTrue($child->load($cid));
649
650 $this->assertSame($child_data['nom_adh'], $child->name);
651 $this->assertInstanceOf('\Galette\Entity\Adherent', $child->parent);
652 $this->assertSame($member->id, $child->parent->id);
653 $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh']));
654
655 $mdata = $this->dataAdherentOne();
656 $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh']));
657 $this->assertTrue($this->login->isLogged());
658 $this->assertFalse($this->login->isAdmin());
659 $this->assertFalse($this->login->isStaff());
660
661 $this->assertTrue($child->canShow($this->login));
662 $this->assertFalse($child->canCreate($this->login));
663 $this->assertTrue($child->canEdit($this->login));
664
665 //logout
666 $this->login->logOut();
667 $this->assertFalse($this->login->isLogged());
668
669 //tests for group managers
670 $adh = $this->getMockBuilder('\Galette\Entity\Adherent')
671 ->setConstructorArgs([$this->zdb])
672 ->onlyMethods(['getGroups'])
673 ->getMock();
674
675 $g1 = $this->getMockBuilder('\Galette\Entity\Group')
676 ->onlyMethods(['getId'])
677 ->getMock();
678 $g1->method('getId')->willReturn(1);
679
680 $g2 = $this->getMockBuilder('\Galette\Entity\Group')
681 ->onlyMethods(['getId'])
682 ->getMock();
683 $g2->method('getId')->willReturn(2);
684
685 //groups managers can show members of the groups they own
686 $adh->method('getGroups')->willReturn([$g1, $g2]);
687
688 $login = $this->getMockBuilder('\Galette\Core\Login')
689 ->setConstructorArgs([$this->zdb, $this->i18n])
690 ->onlyMethods(['isGroupManager'])
691 ->getMock();
692 $this->assertFalse($adh->canShow($login));
693
694 $login->method('isGroupManager')->willReturnCallback(function ($gid) use ($g1) {
695 return $gid === null || $gid == $g1->getId();
696 });
697 $this->assertTrue($adh->canShow($login));
698
699 //groups managers cannot show members of the groups they do not own
700 $adh = $this->getMockBuilder('\Galette\Entity\Adherent')
701 ->setConstructorArgs([$this->zdb])
702 ->onlyMethods(['getGroups'])
703 ->getMock();
704 $adh->method('getGroups')->willReturn([$g2]);
705 $this->assertFalse($adh->canShow($login));
706 }
707
708 /**
709 * Names provider
710 *
711 * @return array[]
712 */
713 public static function nameCaseProvider(): array
714 {
715 return [
716 [
717 'name' => 'Doe',
718 'surname' => 'John',
719 'title' => false,
720 'id' => false,
721 'nick' => false,
722 'expected' => 'DOE John'
723 ],
724 [
725 'name' => 'Doéè',
726 'surname' => 'John',
727 'title' => false,
728 'id' => false,
729 'nick' => false,
730 'expected' => 'DOÉÈ John'
731 ],
732 [
733 'name' => 'Doe',
734 'surname' => 'John',
735 'title' => new \Galette\Entity\Title(\Galette\Entity\Title::MR),
736 'id' => false,
737 'nick' => false,
738 'expected' => 'Mr. DOE John'
739 ],
740 [
741 'name' => 'Doe',
742 'surname' => 'John',
743 'title' => false,
744 'id' => false,
745 'nick' => 'foo',
746 'expected' => 'DOE John (foo)'
747 ],
748 [
749 'name' => 'Doe',
750 'surname' => 'John',
751 'title' => false,
752 'id' => 42,
753 'nick' => false,
754 'expected' => 'DOE John (42)'
755 ],
756 [
757 'name' => 'Doe',
758 'surname' => 'John',
759 'title' => new \Galette\Entity\Title(\Galette\Entity\Title::MR),
760 'id' => 42,
761 'nick' => 'foo',
762 'expected' => 'Mr. DOE John (foo, 42)'
763 ],
764 ];
765 }
766
767 /**
768 * Test getNameWithCase
769 *
770 * @dataProvider nameCaseProvider
771 *
772 * @param string $name Name
773 * @param string $surname Surname
774 * @param \Galette\Entity\Title|false $title Title
775 * @param string|false $id ID
776 * @param string|false $nick Nick
777 * @param string $expected Expected result
778 *
779 * @return void
780 */
781 public function testsGetNameWithCase(string $name, string $surname, $title, $id, $nick, string $expected)
782 {
783 $this->assertSame(
784 $expected,
785 \Galette\Entity\Adherent::getNameWithCase(
786 $name,
787 $surname,
788 $title,
789 $id,
790 $nick,
791 )
792 );
793 }
794
795 /**
796 * Change member active status
797 *
798 * @param bool $active Activation status
799 *
800 * @return void
801 */
802 private function changeMemberActivation(bool $active): void
803 {
804 $check = $this->adh->check(['activite_adh' => $active], [], []);
805 if (is_array($check)) {
806 var_dump($check);
807 }
808 $this->assertTrue($check);
809 $this->assertTrue($this->adh->store());
810 $this->assertTrue($this->adh->load($this->adh->id));
811 }
812
813 /**
814 * Test getDueStatus
815 *
816 * @return void
817 */
818 public function testGetDueStatus()
819 {
820 $now = new \DateTime();
821 $member = new \Galette\Entity\Adherent($this->zdb);
822 $this->assertSame(\Galette\Entity\Contribution::STATUS_UNKNOWN, $member->getDueStatus());
823
824 $this->getMemberOne();
825
826 $this->assertTrue($this->adh->isActive());
827 $this->assertSame(\Galette\Entity\Contribution::STATUS_NEVER, $this->adh->getDueStatus());
828
829 //non-active members always have OLD due status
830 $this->changeMemberActivation(false);
831 $this->assertSame(\Galette\Entity\Contribution::STATUS_OLD, $this->adh->getDueStatus());
832 $this->changeMemberActivation(true);
833
834 //create a close to be expired contribution
835 $due_date = clone $now;
836 $due_date->add(new \DateInterval('P30D'));
837 $begin_date = clone $due_date;
838 $begin_date->add(new \DateInterval('P1D'));
839 $begin_date->sub(new \DateInterval('P1Y'));
840
841 $this->cleanContributions();
842 $this->createContrib([
843 'id_adh' => $this->adh->id,
844 'id_type_cotis' => 3,
845 'montant_cotis' => '111',
846 'type_paiement_cotis' => '6',
847 'info_cotis' => 'FAKER' . $this->seed,
848 'date_fin_cotis' => $due_date->format('Y-m-d'),
849 'date_enreg' => $begin_date->format('Y-m-d'),
850 'date_debut_cotis' => $begin_date->format('Y-m-d')
851 ]);
852
853 //member is up-to-date, close to be expired
854 $this->assertTrue($this->adh->load($this->adh->id));
855 $this->assertTrue($this->adh->isActive());
856 $this->assertTrue($this->adh->isUp2Date());
857 $this->assertSame(\Galette\Entity\Contribution::STATUS_IMPENDING, $this->adh->getDueStatus());
858
859 //non-active members always have OLD due status
860 $this->changeMemberActivation(false);
861 $this->assertSame(\Galette\Entity\Contribution::STATUS_OLD, $this->adh->getDueStatus());
862 $this->changeMemberActivation(true);
863
864 //create an expired contribution, 29 days ago
865 $due_date = clone $now;
866 $due_date->sub(new \DateInterval('P29D'));
867 $begin_date = clone $due_date;
868 $begin_date->add(new \DateInterval('P1D'));
869 $begin_date->sub(new \DateInterval('P1Y'));
870
871 $this->cleanContributions();
872 $this->createContrib([
873 'id_adh' => $this->adh->id,
874 'id_type_cotis' => 3,
875 'montant_cotis' => '111',
876 'type_paiement_cotis' => '6',
877 'info_cotis' => 'FAKER' . $this->seed,
878 'date_fin_cotis' => $due_date->format('Y-m-d'),
879 'date_enreg' => $begin_date->format('Y-m-d'),
880 'date_debut_cotis' => $begin_date->format('Y-m-d')
881 ]);
882
883 //member is late, but for less than 30 days, no reminder to send
884 $this->assertTrue($this->adh->load($this->adh->id));
885 $this->assertTrue($this->adh->isActive());
886 $this->assertFalse($this->adh->isUp2Date());
887 $this->assertSame(\Galette\Entity\Contribution::STATUS_LATE, $this->adh->getDueStatus());
888
889 //non-active members always have OLD due status
890 $this->changeMemberActivation(false);
891 $this->assertSame(\Galette\Entity\Contribution::STATUS_OLD, $this->adh->getDueStatus());
892 $this->changeMemberActivation(true);
893 }
894
895 /**
896 * Clean created contributions
897 *
898 * @return void
899 */
900 private function cleanContributions(): void
901 {
902 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
903 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
904 $this->zdb->execute($delete);
905 }
906 }