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);
90 $this->default_deps
= [
99 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
100 $this->adh
->setDependencies(
102 $this->members_fields
,
112 public function testEmpty()
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();
132 $this->variable($adh->fake_prop
)->isNull();
134 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
138 * Test member load dependencies
142 public function testDependencies()
145 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
147 $adh = clone $this->adh
;
148 $adh->disableAllDeps();
157 $this->array($adh->deps
)->isIdenticalTo($expected);
169 ->enableDep('dynamics')
170 ->enableDep('children');
171 $this->array($adh->deps
)->isIdenticalTo($expected);
181 $adh->disableDep('children');
182 $this->array($adh->deps
)->isIdenticalTo($expected);
184 $adh->disableDep('none')->enableDep('anothernone');
185 $this->array($adh->deps
)->isIdenticalTo($expected);
195 $adh->enableAllDeps('children');
196 $this->array($adh->deps
)->isIdenticalTo($expected);
204 public function testGetterWException()
209 function () use ($adh) {
212 )->isInstanceOf('RuntimeException');
216 * Set dependencies from constructor
220 public function testDepsAtConstuct()
230 $adh = new \Galette\Entity\
Adherent(
236 $this->array($adh->deps
)->isIdenticalTo($deps);
238 $adh = new \Galette\Entity\
Adherent(
243 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
247 * Test simple member creation
251 public function testSimpleMember()
253 $this->getMemberOne();
254 $this->checkMemberOneExpected();
256 //load member from db
257 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
258 $this->checkMemberOneExpected($adh);
262 * Test load form login and email
266 public function testLoadForLogin()
268 $this->getMemberOne();
270 $login = $this->adh
->login
;
271 $email = $this->adh
->email
;
273 $this->variable($this->adh
->email
)->isIdenticalTo($this->adh
->getEmail());
275 $adh = new \Galette\Entity\
Adherent($this->zdb
, $login);
276 $this->checkMemberOneExpected($adh);
278 $adh = new \Galette\Entity\
Adherent($this->zdb
, $email);
279 $this->checkMemberOneExpected($adh);
283 * Test password updating
287 public function testUpdatePassword()
289 $this->getMemberOne();
291 $this->checkMemberOneExpected();
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();
299 //reset original password
300 \Galette\Entity\Adherent
::updatePassword($this->zdb
, $this->adh
->id
, 'J^B-()f');
308 public function testCheckErrors()
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);
319 'date_crea_adh' => 'not a date'
321 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
322 $check = $adh->check($data, [], []);
323 $this->array($check)->isIdenticalTo($expected);
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();
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);
337 'url_adh' => 'mywebsite'
339 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
340 $check = $adh->check($data, [], []);
341 $this->array($check)->isIdenticalTo($expected);
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('');
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);
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);
361 'mdp_adh' => 'short',
362 'mdp_adh2' => 'short'
364 $expected = ['Too short (6 characters minimum, 5 found)'];
365 $check = $adh->check($data, [], []);
366 $this->array($check)->isIdenticalTo($expected);
368 $data = ['mdp_adh' => 'mypassword'];
369 $expected = ['- The passwords don\'t match!'];
370 $check = $adh->check($data, [], []);
371 $this->array($check)->isIdenticalTo($expected);
374 'mdp_adh' => 'mypassword',
375 'mdp_adh2' => 'mypasswor'
377 $expected = ['- The passwords don\'t match!'];
378 $check = $adh->check($data, [], []);
379 $this->array($check)->isIdenticalTo($expected);
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);
392 public function testPhoto()
394 $this->getMemberOne();
396 $fakedata = new \Galette\Util\
FakeData($this->zdb
, $this->i18n
);
397 $this->boolean($fakedata->addPhoto($this->adh
))->isTrue();
399 $this->boolean($this->adh
->hasPicture())->isTrue();
402 $this->boolean($this->adh
->picture
->delete())->isTrue();
410 public function testCanEdit()
412 $adh = new \Galette\Entity\
Adherent($this->zdb
);
415 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
);
416 $this->boolean($adh->canEdit($login))->isFalse();
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();
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();
429 $adh = new \mock\Galette\Entity\
Adherent($this->zdb
);
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;
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();
440 $this->calling($login)->isGroupManager
= true;
441 $this->boolean($adh->canEdit($login))->isTrue();
445 * Test member duplication
449 public function testDuplicate()
451 $this->getMemberOne();
453 $this->checkMemberOneExpected();
455 //load member from db
456 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
457 $this->checkMemberOneExpected($adh);
459 $adh->setDuplicate();
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();
474 public function testParents()
476 $this->getMemberOne();
478 $this->checkMemberOneExpected();
480 //load member from db
481 $parent = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
482 $this->checkMemberOneExpected($parent);
484 $this->login
->logAdmin('superadmin', $this->preferences
);
485 $this->boolean($this->login
->isLogged())->isTrue();
486 $this->boolean($this->login
->isSuperAdmin())->isTrue();
490 'prenom_adh' => 'Johny',
491 'parent_id' => $parent->id
,
494 $child = $this->createMember($child_data);
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
);
500 $check = $child->check(['detach_parent' => true], [], []);
501 if (is_array($check)) {
504 $this->boolean($check)->isTrue();
505 $this->boolean($child->store())->isTrue();
506 $this->variable($child->parent
)->isNull();
510 * Test XSS/SQL injection
514 public function testInjection()
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);
525 $this->string($member->sfullname
)->isIdenticalTo('DOE Johny Console.log("anything");');
526 $this->string($member->others_infos
)->isIdenticalTo('Any console.log("useful"); information');
534 public function testCan()
536 $this->getMemberOne();
537 //load member from db
538 $member = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
540 $this->boolean($member->canShow($this->login
))->isFalse();
541 $this->boolean($member->canCreate($this->login
))->isFalse();
542 $this->boolean($member->canEdit($this->login
))->isFalse();
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();
549 $this->boolean($member->canShow($this->login
))->isTrue();
550 $this->boolean($member->canCreate($this->login
))->isTrue();
551 $this->boolean($member->canEdit($this->login
))->isTrue();
554 $this->login
->logOut();
555 $this->boolean($this->login
->isLogged())->isFalse();
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();
564 $this->boolean($member->canShow($this->login
))->isTrue();
565 $this->boolean($member->canCreate($this->login
))->isTrue();
566 $this->boolean($member->canEdit($this->login
))->isTrue();
569 $this->login
->logOut();
570 $this->boolean($this->login
->isLogged())->isFalse();
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();
580 $this->boolean($member->canShow($this->login
))->isFalse();
581 $this->boolean($member->canCreate($this->login
))->isFalse();
582 $this->boolean($member->canEdit($this->login
))->isFalse();
584 //parents can fully change children information
585 $this->getMemberOne();
586 $mdata = $this->dataAdherentOne();
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();
595 'prenom_adh' => 'Johny',
596 'parent_id' => $member->id
,
598 'login_adh' => 'child.johny.doe',
599 'fingerprint' => 'FAKER' . $this->seed
601 $child = $this->createMember($child_data);
603 $this->login
->logOut();
606 $child = new \Galette\Entity\
Adherent($this->zdb
);
607 $child->enableDep('parent');
608 $this->boolean($child->load($cid))->isTrue();
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();
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();
621 $this->boolean($child->canShow($this->login
))->isTrue();
622 $this->boolean($child->canCreate($this->login
))->isFalse();
623 $this->boolean($child->canEdit($this->login
))->isTrue();
626 $this->login
->logOut();
627 $this->boolean($this->login
->isLogged())->isFalse();