3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2017-2021 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Entity\test\units
;
40 use Galette\GaletteTestCase
;
43 * Adherent tests class
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
54 class Adherent
extends GaletteTestCase
56 protected $seed = 95842354;
57 private $default_deps;
64 public function tearDown()
66 $this->zdb
= new \Galette\Core\
Db();
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);
73 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
74 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
75 $this->zdb
->execute($delete);
77 $this->cleanHistory();
83 * @param string $method Calling method
87 public function beforeTestMethod($method)
89 parent
::beforeTestMethod($method);
93 $this->default_deps
= [
103 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
104 $this->adh
->setDependencies(
106 $this->members_fields
,
116 public function testEmpty()
119 $this->boolean($adh->isAdmin())->isFalse();
120 $this->boolean($adh->admin
)->isFalse();
121 $this->boolean($adh->isStaff())->isFalse();
122 $this->boolean($adh->staff
)->isFalse();
123 $this->boolean($adh->isDueFree())->isFalse();
124 $this->boolean($adh->due_free
)->isFalse();
125 $this->boolean($adh->isGroupMember('any'))->isFalse();
126 $this->boolean($adh->isGroupManager('any'))->isFalse();
127 $this->boolean($adh->isCompany())->isFalse();
128 $this->boolean($adh->isMan())->isFalse();
129 $this->boolean($adh->isWoman())->isFalse();
130 $this->boolean($adh->isActive())->isTrue();
131 $this->boolean($adh->active
)->isTrue();
132 $this->boolean($adh->isUp2Date())->isFalse();
133 $this->boolean($adh->appearsInMembersList())->isFalse();
134 $this->boolean($adh->appears_in_list
)->isFalse();
136 $this->variable($adh->fake_prop
)->isNull();
138 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
142 * Test member load dependencies
146 public function testDependencies()
149 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
151 $adh = clone $this->adh
;
152 $adh->disableAllDeps();
162 $this->array($adh->deps
)->isIdenticalTo($expected);
175 ->enableDep('dynamics')
176 ->enableDep('children');
177 $this->array($adh->deps
)->isIdenticalTo($expected);
188 $adh->disableDep('children');
189 $this->array($adh->deps
)->isIdenticalTo($expected);
191 $adh->disableDep('none')->enableDep('anothernone');
192 $this->array($adh->deps
)->isIdenticalTo($expected);
203 $adh->enableAllDeps('children');
204 $this->array($adh->deps
)->isIdenticalTo($expected);
212 public function testGetterWException()
217 function () use ($adh) {
220 )->isInstanceOf('RuntimeException');
224 * Set dependencies from constructor
228 public function testDepsAtConstuct()
239 $adh = new \Galette\Entity\
Adherent(
245 $this->array($adh->deps
)->isIdenticalTo($deps);
247 $adh = new \Galette\Entity\
Adherent(
252 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
256 * Test simple member creation
260 public function testSimpleMember()
262 $this->getMemberOne();
263 $this->checkMemberOneExpected();
265 //load member from db
266 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
267 $this->checkMemberOneExpected($adh);
271 * Test load form login and email
275 public function testLoadForLogin()
277 $this->getMemberOne();
279 $login = $this->adh
->login
;
280 $email = $this->adh
->email
;
282 $this->variable($this->adh
->email
)->isIdenticalTo($this->adh
->getEmail());
284 $adh = new \Galette\Entity\
Adherent($this->zdb
, $login);
285 $this->checkMemberOneExpected($adh);
287 $adh = new \Galette\Entity\
Adherent($this->zdb
, $email);
288 $this->checkMemberOneExpected($adh);
292 * Test password updating
296 public function testUpdatePassword()
298 $this->getMemberOne();
300 $this->checkMemberOneExpected();
303 \Galette\Entity\Adherent
::updatePassword($this->zdb
, $this->adh
->id
, $newpass);
304 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
305 $pw_checked = password_verify($newpass, $adh->password
);
306 $this->boolean($pw_checked)->isTrue();
308 //reset original password
309 \Galette\Entity\Adherent
::updatePassword($this->zdb
, $this->adh
->id
, 'J^B-()f');
317 public function testCheckErrors()
321 $data = ['ddn_adh' => 'not a date'];
322 $expected = ['- Wrong date format (Y-m-d) for Birth date!'];
323 $check = $adh->check($data, [], []);
324 $this->array($check)->isIdenticalTo($expected);
328 'date_crea_adh' => 'not a date'
330 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
331 $check = $adh->check($data, [], []);
332 $this->array($check)->isIdenticalTo($expected);
334 //reste creation date to its default value
335 $data = ['date_crea_adh' => date('Y-m-d')];
336 $check = $adh->check($data, [], []);
337 $this->boolean($check)->isTrue();
339 $data = ['email_adh' => 'not an email'];
340 $expected = ['- Non-valid E-Mail address! (E-Mail)'];
341 $check = $adh->check($data, [], []);
342 $this->array($check)->isIdenticalTo($expected);
344 $data = ['login_adh' => 'a'];
345 $expected = ['- The username must be composed of at least 2 characters!'];
346 $check = $adh->check($data, [], []);
347 $this->array($check)->isIdenticalTo($expected);
349 $data = ['login_adh' => 'login@galette'];
350 $expected = ['- The username cannot contain the @ character'];
351 $check = $adh->check($data, [], []);
352 $this->array($check)->isIdenticalTo($expected);
356 'mdp_adh' => 'short',
357 'mdp_adh2' => 'short'
359 $expected = ['Too short (6 characters minimum, 5 found)'];
360 $check = $adh->check($data, [], []);
361 $this->array($check)->isIdenticalTo($expected);
363 $data = ['mdp_adh' => 'mypassword'];
364 $expected = ['- The passwords don\'t match!'];
365 $check = $adh->check($data, [], []);
366 $this->array($check)->isIdenticalTo($expected);
369 'mdp_adh' => 'mypassword',
370 'mdp_adh2' => 'mypasswor'
372 $expected = ['- The passwords don\'t match!'];
373 $check = $adh->check($data, [], []);
374 $this->array($check)->isIdenticalTo($expected);
376 $data = ['id_statut' => 256];
377 $expected = ['Status #256 does not exists in database.'];
378 $check = $adh->check($data, [], []);
379 $this->array($check)->isIdenticalTo($expected);
387 public function testPhoto()
389 $this->getMemberOne();
391 $fakedata = new \Galette\Util\
FakeData($this->zdb
, $this->i18n
);
392 $this->boolean($fakedata->addPhoto($this->adh
))->isTrue();
394 $this->boolean($this->adh
->hasPicture())->isTrue();
397 $this->boolean($this->adh
->picture
->delete())->isTrue();
405 public function testCanEdit()
407 $adh = new \Galette\Entity\
Adherent($this->zdb
);
410 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
);
411 $this->boolean($adh->canEdit($login))->isFalse();
413 //admin => authorized
414 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
);
415 $this->calling($login)->isAdmin
= true;
416 $this->boolean($adh->canEdit($login))->isTrue();
418 //staff => authorized
419 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
);
420 $this->calling($login)->isStaff
= true;
421 $this->boolean($adh->canEdit($login))->isTrue();
424 $adh = new \mock\Galette\Entity\
Adherent($this->zdb
);
426 $g1 = new \mock\Galette\Entity\
Group();
427 $this->calling($g1)->getId
= 1;
428 $g2 = new \mock\Galette\Entity\
Group();
429 $this->calling($g1)->getId
= 2;
431 $this->calling($adh)->getGroups
= [$g1, $g2];
432 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
);
433 $this->boolean($adh->canEdit($login))->isFalse();
435 $this->calling($login)->isGroupManager
= true;
436 $this->boolean($adh->canEdit($login))->isTrue();
440 * Test member duplication
444 public function testDuplicate()
446 $this->getMemberOne();
448 $this->checkMemberOneExpected();
450 //load member from db
451 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
452 $this->checkMemberOneExpected($adh);
454 $adh->setDuplicate();
456 $this->string($adh->others_infos_admin
)->contains('Duplicated from');
457 $this->variable($adh->email
)->isNull();
458 $this->variable($adh->id
)->isNull();
459 $this->variable($adh->login
)->isNull();
460 $this->variable($adh->birthdate
)->isNull();
461 $this->variable($adh->surname
)->isNull();
469 public function testParents()
471 $this->getMemberOne();
473 $this->checkMemberOneExpected();
475 //load member from db
476 $parent = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
477 $this->checkMemberOneExpected($parent);
479 $this->logSuperAdmin();
483 'prenom_adh' => 'Johny',
484 'parent_id' => $parent->id
,
487 $child = $this->createMember($child_data);
489 $this->string($child->name
)->isIdenticalTo($child_data['nom_adh']);
490 $this->object($child->parent
)->isInstanceOf('\Galette\Entity\Adherent');
491 $this->integer($child->parent
->id
)->isIdenticalTo($parent->id
);
493 $check = $child->check(['detach_parent' => true], [], []);
494 if (is_array($check)) {
497 $this->boolean($check)->isTrue();
498 $this->boolean($child->store())->isTrue();
499 $this->variable($child->parent
)->isNull();
503 * Test XSS/SQL injection
507 public function testInjection()
511 'prenom_adh' => 'Johny <script>console.log("anything");</script>',
512 'email_adh' => 'jdoe@doe.com',
513 'login_adh' => 'jdoe',
514 'info_public_adh' => 'Any <script>console.log("useful");</script> information'
515 ] +
$this->dataAdherentOne();
516 $member = $this->createMember($data);
518 $this->string($member->sfullname
)->isIdenticalTo('DOE Johny Console.log("anything");');
519 $this->string($member->others_infos
)->isIdenticalTo('Any console.log("useful"); information');
527 public function testCan()
529 $this->getMemberOne();
530 //load member from db
531 $member = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
533 $this->boolean($member->canShow($this->login
))->isFalse();
534 $this->boolean($member->canCreate($this->login
))->isFalse();
535 $this->boolean($member->canEdit($this->login
))->isFalse();
537 //Superadmin can fully change members
538 $this->logSuperAdmin();
540 $this->boolean($member->canShow($this->login
))->isTrue();
541 $this->boolean($member->canCreate($this->login
))->isTrue();
542 $this->boolean($member->canEdit($this->login
))->isTrue();
545 $this->login
->logOut();
546 $this->boolean($this->login
->isLogged())->isFalse();
548 //Member can fully change its own information
549 $mdata = $this->dataAdherentOne();
550 $this->boolean($this->login
->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
551 $this->boolean($this->login
->isLogged())->isTrue();
552 $this->boolean($this->login
->isAdmin())->isFalse();
553 $this->boolean($this->login
->isStaff())->isFalse();
555 $this->boolean($member->canShow($this->login
))->isTrue();
556 $this->boolean($member->canCreate($this->login
))->isTrue();
557 $this->boolean($member->canEdit($this->login
))->isTrue();
560 $this->login
->logOut();
561 $this->boolean($this->login
->isLogged())->isFalse();
563 //Another member has no access
564 $this->getMemberTwo();
565 $mdata = $this->dataAdherentTwo();
566 $this->boolean($this->login
->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
567 $this->boolean($this->login
->isLogged())->isTrue();
568 $this->boolean($this->login
->isAdmin())->isFalse();
569 $this->boolean($this->login
->isStaff())->isFalse();
571 $this->boolean($member->canShow($this->login
))->isFalse();
572 $this->boolean($member->canCreate($this->login
))->isFalse();
573 $this->boolean($member->canEdit($this->login
))->isFalse();
575 //parents can fully change children information
576 $this->getMemberOne();
577 $mdata = $this->dataAdherentOne();
579 $login = $this->login
;
580 $this->logSuperAdmin();
584 'prenom_adh' => 'Johny',
585 'parent_id' => $member->id
,
587 'login_adh' => 'child.johny.doe',
588 'fingerprint' => 'FAKER' . $this->seed
590 $child = $this->createMember($child_data);
592 $this->login
->logOut();
595 $child = new \Galette\Entity\
Adherent($this->zdb
);
596 $child->enableDep('parent');
597 $this->boolean($child->load($cid))->isTrue();
599 $this->string($child->name
)->isIdenticalTo($child_data['nom_adh']);
600 $this->object($child->parent
)->isInstanceOf('\Galette\Entity\Adherent');
601 $this->integer($child->parent
->id
)->isIdenticalTo($member->id
);
602 $this->boolean($this->login
->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
604 $mdata = $this->dataAdherentOne();
605 $this->boolean($this->login
->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
606 $this->boolean($this->login
->isLogged())->isTrue();
607 $this->boolean($this->login
->isAdmin())->isFalse();
608 $this->boolean($this->login
->isStaff())->isFalse();
610 $this->boolean($child->canShow($this->login
))->isTrue();
611 $this->boolean($child->canCreate($this->login
))->isFalse();
612 $this->boolean($child->canEdit($this->login
))->isTrue();
615 $this->login
->logOut();
616 $this->boolean($this->login
->isLogged())->isFalse();
624 protected function nameCaseProvider(): array
633 'expected' => 'DOE John'
641 'expected' => 'DOÉÈ John'
646 'title' => new \Galette\Entity\
Title(\Galette\Entity\Title
::MR
),
649 'expected' => 'Mr. DOE John'
657 'expected' => 'DOE John (foo)'
665 'expected' => 'DOE John (42)'
670 'title' => new \Galette\Entity\
Title(\Galette\Entity\Title
::MR
),
673 'expected' => 'Mr. DOE John (foo, 42)'
679 * Test getNameWithCase
681 * @dataProvider nameCaseProvider
683 * @param string $name Name
684 * @param string $surname Surname
685 * @param \Galette\Entity\Title|false $title Title
686 * @param string|false $id ID
687 * @param string|false $nick Nick
688 * @param string $expected Expected result
692 public function testsGetNameWithCase(string $name, string $surname, $title, $id, $nick, string $expected)
695 \Galette\Entity\Adherent
::getNameWithCase(
702 )->isIdenticalTo($expected);