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);
81 * @param string $testMethod Calling method
85 public function beforeTestMethod($testMethod)
87 parent
::beforeTestMethod($testMethod);
91 $this->default_deps
= [
100 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
101 $this->adh
->setDependencies(
103 $this->members_fields
,
113 public function testEmpty()
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();
133 $this->variable($adh->fake_prop
)->isNull();
135 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
139 * Test member load dependencies
143 public function testDependencies()
146 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
148 $adh = clone $this->adh
;
149 $adh->disableAllDeps();
158 $this->array($adh->deps
)->isIdenticalTo($expected);
170 ->enableDep('dynamics')
171 ->enableDep('children');
172 $this->array($adh->deps
)->isIdenticalTo($expected);
182 $adh->disableDep('children');
183 $this->array($adh->deps
)->isIdenticalTo($expected);
185 $adh->disableDep('none')->enableDep('anothernone');
186 $this->array($adh->deps
)->isIdenticalTo($expected);
196 $adh->enableAllDeps('children');
197 $this->array($adh->deps
)->isIdenticalTo($expected);
205 public function testGetterWException()
210 function () use ($adh) {
213 )->isInstanceOf('RuntimeException');
217 * Set dependencies from constructor
221 public function testDepsAtConstuct()
231 $adh = new \Galette\Entity\
Adherent(
237 $this->array($adh->deps
)->isIdenticalTo($deps);
239 $adh = new \Galette\Entity\
Adherent(
244 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
248 * Test simple member creation
252 public function testSimpleMember()
254 $this->getMemberOne();
255 $this->checkMemberOneExpected();
257 //load member from db
258 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
259 $this->checkMemberOneExpected($adh);
263 * Test load form login and email
267 public function testLoadForLogin()
269 $this->getMemberOne();
271 $login = $this->adh
->login
;
272 $email = $this->adh
->email
;
274 $this->variable($this->adh
->email
)->isIdenticalTo($this->adh
->getEmail());
276 $adh = new \Galette\Entity\
Adherent($this->zdb
, $login);
277 $this->checkMemberOneExpected($adh);
279 $adh = new \Galette\Entity\
Adherent($this->zdb
, $email);
280 $this->checkMemberOneExpected($adh);
284 * Test password updating
288 public function testUpdatePassword()
290 $this->getMemberOne();
292 $this->checkMemberOneExpected();
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();
300 //reset original password
301 \Galette\Entity\Adherent
::updatePassword($this->zdb
, $this->adh
->id
, 'J^B-()f');
309 public function testCheckErrors()
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);
320 'date_crea_adh' => 'not a date'
322 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
323 $check = $adh->check($data, [], []);
324 $this->array($check)->isIdenticalTo($expected);
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();
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);
338 'url_adh' => 'mywebsite'
340 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
341 $check = $adh->check($data, [], []);
342 $this->array($check)->isIdenticalTo($expected);
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('');
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);
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);
362 'mdp_adh' => 'short',
363 'mdp_adh2' => 'short'
365 $expected = ['Too short (6 characters minimum, 5 found)'];
366 $check = $adh->check($data, [], []);
367 $this->array($check)->isIdenticalTo($expected);
369 $data = ['mdp_adh' => 'mypassword'];
370 $expected = ['- The passwords don\'t match!'];
371 $check = $adh->check($data, [], []);
372 $this->array($check)->isIdenticalTo($expected);
375 'mdp_adh' => 'mypassword',
376 'mdp_adh2' => 'mypasswor'
378 $expected = ['- The passwords don\'t match!'];
379 $check = $adh->check($data, [], []);
380 $this->array($check)->isIdenticalTo($expected);
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);
393 public function testPhoto()
395 $this->getMemberOne();
397 $fakedata = new \Galette\Util\
FakeData($this->zdb
, $this->i18n
);
398 $this->boolean($fakedata->addPhoto($this->adh
))->isTrue();
400 $this->boolean($this->adh
->hasPicture())->isTrue();
403 $this->boolean($this->adh
->picture
->delete())->isTrue();
411 public function testCanEdit()
413 $adh = new \Galette\Entity\
Adherent($this->zdb
);
416 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
);
417 $this->boolean($adh->canEdit($login))->isFalse();
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();
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();
430 $adh = new \mock\Galette\Entity\
Adherent($this->zdb
);
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;
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();
441 $this->calling($login)->isGroupManager
= true;
442 $this->boolean($adh->canEdit($login))->isTrue();
446 * Test member duplication
450 public function testDuplicate()
452 $this->getMemberOne();
454 $this->checkMemberOneExpected();
456 //load member from db
457 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
458 $this->checkMemberOneExpected($adh);
460 $adh->setDuplicate();
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();
475 public function testParents()
477 $this->getMemberOne();
479 $this->checkMemberOneExpected();
481 //load member from db
482 $parent = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
483 $this->checkMemberOneExpected($parent);
485 $this->login
->logAdmin('superadmin', $this->preferences
);
486 $this->boolean($this->login
->isLogged())->isTrue();
487 $this->boolean($this->login
->isSuperAdmin())->isTrue();
491 'prenom_adh' => 'Johny',
492 'parent_id' => $parent->id
,
495 $child = $this->createMember($child_data);
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
);
501 $check = $child->check(['detach_parent' => true], [], []);
502 if (is_array($check)) {
505 $this->boolean($check)->isTrue();
506 $this->boolean($child->store())->isTrue();
507 $this->variable($child->parent
)->isNull();
511 * Test XSS/SQL injection
515 public function testInjection()
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);
526 $this->string($member->sfullname
)->isIdenticalTo('DOE Johny Console.log("anything");');
527 $this->string($member->others_infos
)->isIdenticalTo('Any console.log("useful"); information');
535 public function testCan()
537 $this->getMemberOne();
538 //load member from db
539 $member = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
541 $this->boolean($member->canShow($this->login
))->isFalse();
542 $this->boolean($member->canCreate($this->login
))->isFalse();
543 $this->boolean($member->canEdit($this->login
))->isFalse();
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();
550 $this->boolean($member->canShow($this->login
))->isTrue();
551 $this->boolean($member->canCreate($this->login
))->isTrue();
552 $this->boolean($member->canEdit($this->login
))->isTrue();
555 $this->login
->logOut();
556 $this->boolean($this->login
->isLogged())->isFalse();
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();
565 $this->boolean($member->canShow($this->login
))->isTrue();
566 $this->boolean($member->canCreate($this->login
))->isTrue();
567 $this->boolean($member->canEdit($this->login
))->isTrue();
570 $this->login
->logOut();
571 $this->boolean($this->login
->isLogged())->isFalse();
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();
581 $this->boolean($member->canShow($this->login
))->isFalse();
582 $this->boolean($member->canCreate($this->login
))->isFalse();
583 $this->boolean($member->canEdit($this->login
))->isFalse();
585 //parents can fully change children information
586 $this->getMemberOne();
587 $mdata = $this->dataAdherentOne();
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();
596 'prenom_adh' => 'Johny',
597 'parent_id' => $member->id
,
599 'login_adh' => 'child.johny.doe',
600 'fingerprint' => 'FAKER' . $this->seed
602 $child = $this->createMember($child_data);
604 $this->login
->logOut();
607 $child = new \Galette\Entity\
Adherent($this->zdb
);
608 $child->enableDep('parent');
609 $this->boolean($child->load($cid))->isTrue();
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();
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();
622 $this->boolean($child->canShow($this->login
))->isTrue();
623 $this->boolean($child->canCreate($this->login
))->isFalse();
624 $this->boolean($child->canEdit($this->login
))->isTrue();
627 $this->login
->logOut();
628 $this->boolean($this->login
->isLogged())->isFalse();
636 protected function nameCaseProvider(): array
645 'expected' => 'DOE John'
653 'expected' => 'DOÉÈ John'
658 'title' => new \Galette\Entity\
Title(\Galette\Entity\Title
::MR
),
661 'expected' => 'Mr. DOE John'
669 'expected' => 'DOE John (foo)'
677 'expected' => 'DOE John (42)'
682 'title' => new \Galette\Entity\
Title(\Galette\Entity\Title
::MR
),
685 'expected' => 'Mr. DOE John (foo, 42)'
691 * Test getNameWithCase
693 * @dataProvider nameCaseProvider
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
704 public function testsGetNameWithCase(string $name, string $surname, $title, $id, $nick, string $expected)
707 \Galette\Entity\Adherent
::getNameWithCase(
714 )->isIdenticalTo($expected);