3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2017 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 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
;
43 * Adherent tests class
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2017 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 atoum
57 private $members_fields;
58 private $members_fields_cats;
64 private $seed = 95842354;
65 private $default_deps;
74 public function setUp()
76 $this->zdb
= new \Galette\Core\
Db();
77 $status = new \Galette\Entity\
Status($this->zdb
);
78 if (count($status->getList()) === 0) {
79 //status are not yet instanciated.
80 $res = $status->installInit();
81 $this->boolean($res)->isTrue();
90 public function tearDown()
92 $this->zdb
= new \Galette\Core\
Db();
93 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
94 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
95 $this->zdb
->execute($delete);
101 * @param string $testMethod Calling method
105 public function beforeTestMethod($testMethod)
107 $this->zdb
= new \Galette\Core\
Db();
109 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
110 $this->members_fields
= $members_fields;
111 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields_cats.php';
112 $this->members_fields_cats
= $members_fields_cats;
114 $this->i18n
= new \Galette\Core\
I18n(
115 \Galette\Core\I18n
::DEFAULT_LANG
118 $this->preferences
= new \Galette\Core\
Preferences(
121 $this->session
= new \RKA\
Session();
122 $this->login
= new \Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
123 $this->history
= new \Galette\Core\
History($this->zdb
, $this->login
);
125 $this->default_deps
= [
134 global $zdb, $login, $hist, $i18n; // globals :(
136 $login = $this->login
;
137 $hist = $this->history
;
140 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
141 $this->adh
->setDependencies(
143 $this->members_fields
,
149 * Look in database if test member already exists
151 * @return false|ResultSet
153 private function adhExists()
155 $select = $this->zdb
->select(\Galette\Entity\Adherent
::TABLE
, 'a');
156 $select->where(array('a.fingerprint' => 'FAKER' . $this->seed
));
158 $results = $this->zdb
->execute($select);
159 if ($results->count() === 0) {
167 * Get Faker data for one member
171 private function dataAdherent(): array
173 $fakedata = new \Galette\Util\
FakeData($this->zdb
, $this->i18n
);
175 ->setSeed($this->seed
)
178 $this->members_fields
,
183 $data = $fakedata->fakeMember();
188 * Create test user in database
192 private function createAdherent()
194 $this->createMember($this->dataAdherent());
198 * Loads member from a resultset
200 * @param integer $id Id
204 private function loadAdherent($id)
206 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, (int)$id);
207 $this->adh
->setDependencies(
209 $this->members_fields
,
219 public function testEmpty()
222 $this->boolean($adh->isAdmin())->isFalse();
223 $this->boolean($adh->admin
)->isFalse();
224 $this->boolean($adh->isStaff())->isFalse();
225 $this->boolean($adh->staff
)->isFalse();
226 $this->boolean($adh->isDueFree())->isFalse();
227 $this->boolean($adh->due_free
)->isFalse();
228 $this->boolean($adh->isGroupMember('any'))->isFalse();
229 $this->boolean($adh->isGroupManager('any'))->isFalse();
230 $this->boolean($adh->isCompany())->isFalse();
231 $this->boolean($adh->isMan())->isFalse();
232 $this->boolean($adh->isWoman())->isFalse();
233 $this->boolean($adh->isActive())->isTrue();
234 $this->boolean($adh->active
)->isTrue();
235 $this->boolean($adh->isUp2Date())->isFalse();
236 $this->boolean($adh->appearsInMembersList())->isFalse();
237 $this->boolean($adh->appears_in_list
)->isFalse();
239 $this->variable($adh->fake_prop
)->isNull();
241 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
249 public function testGetterWException()
254 function () use ($adh) {
257 )->isInstanceOf('RuntimeException');
261 * Create member from data
263 * @param array $data Data to use to create member
265 * @return \Galette\Entity\Adherent
267 public function createMember(array $data)
270 $check = $adh->check($data, [], []);
271 if (is_array($check)) {
274 $this->boolean($check)->isTrue();
276 $store = $adh->store();
277 $this->boolean($store)->isTrue();
279 $this->ids
[] = $adh->id
;
284 * Set dependencies from constructor
288 public function testDepsAtConstuct()
298 $adh = new \Galette\Entity\
Adherent(
304 $this->array($adh->deps
)->isIdenticalTo($deps);
306 $adh = new \Galette\Entity\
Adherent(
311 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
315 * Check members expecteds
317 * @param Adherent $adh Member instance, if any
318 * @param array $new_expecteds Changes on expected values
322 private function checkMemberExpected($adh = null, $new_expecteds = [])
329 'nom_adh' => 'Durand',
330 'prenom_adh' => 'René',
331 'ville_adh' => 'Martel',
332 'cp_adh' => '07 926',
333 'adresse_adh' => '66, boulevard De Oliveira',
334 'email_adh' => 'meunier.josephine@ledoux.com',
335 'login_adh' => 'arthur.hamon',
336 'mdp_adh' => 'J^B-()f',
337 'bool_admin_adh' => false,
338 'bool_exempt_adh' => false,
339 'bool_display_info' => true,
341 'prof_adh' => 'Chef de fabrication',
343 'ddn_adh' => '1934-06-08',
344 'lieu_naissance' => 'Gonzalez-sur-Meunier',
345 'pseudo_adh' => 'ubertrand',
346 'cp_adh' => '39 069',
347 'pays_adh' => 'Antarctique',
348 'tel_adh' => '0439153432',
349 'url_adh' => 'http://bouchet.com/',
350 'activite_adh' => true,
352 'pref_lang' => 'en_US',
353 'fingerprint' => 'FAKER95842354',
356 $expecteds = array_merge($expecteds, $new_expecteds);
358 foreach ($expecteds as $key => $value) {
359 $property = $this->members_fields
[$key]['propname'];
361 case 'bool_admin_adh':
362 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
364 case 'bool_exempt_adh':
365 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
367 case 'bool_display_info':
368 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
371 $this->boolean($adh->isActive())->isIdenticalTo($value);
374 $pw_checked = password_verify($value, $adh->password
);
375 $this->boolean($pw_checked)->isTrue();
378 //rely on age, not on birthdate
379 $this->variable($adh->$property)->isNotNull();
380 $this->string($adh->getAge())->isIdenticalTo(' (82 years old)');
383 $this->variable($adh->$property)->isIdenticalTo($value, $property);
388 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
390 $expected_str = ' (82 years old)';
391 $this->string($adh->getAge())->isIdenticalTo($expected_str);
392 $this->boolean($adh->hasChildren())->isFalse();
393 $this->boolean($adh->hasParent())->isFalse();
394 $this->boolean($adh->hasPicture())->isFalse();
396 $this->string($adh->sadmin
)->isIdenticalTo('No');
397 $this->string($adh->sdue_free
)->isIdenticalTo('No');
398 $this->string($adh->sappears_in_list
)->isIdenticalTo('Yes');
399 $this->string($adh->sstaff
)->isIdenticalTo('No');
400 $this->string($adh->sactive
)->isIdenticalTo('Active');
401 $this->variable($adh->stitle
)->isNull();
402 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
403 $this->string($adh->sfullname
)->isIdenticalTo('DURAND René');
404 $this->string($adh->saddress
)->isIdenticalTo('66, boulevard De Oliveira');
405 $this->string($adh->sname
)->isIdenticalTo('DURAND René');
407 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
408 $this->string($adh->getAddressContinuation())->isEmpty();
409 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
410 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
411 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
413 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('DURAND René');
414 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
418 * Test simple member creation
422 public function testSimpleMember()
424 $rs = $this->adhExists();
426 $this->createAdherent();
428 $this->loadAdherent($rs->current()->id_adh
);
431 $this->checkMemberExpected();
433 //load member from db
434 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
435 $this->checkMemberExpected($adh);
439 * Test load form login and email
443 public function testLoadForLogin()
445 $rs = $this->adhExists();
447 $this->createAdherent();
449 $this->loadAdherent($rs->current()->id_adh
);
452 $login = $this->adh
->login
;
453 $email = $this->adh
->email
;
455 $this->variable($this->adh
->email
)->isIdenticalTo($this->adh
->getEmail());
457 $adh = new \Galette\Entity\
Adherent($this->zdb
, $login);
458 $this->checkMemberExpected($adh);
460 $adh = new \Galette\Entity\
Adherent($this->zdb
, $email);
461 $this->checkMemberExpected($adh);
465 * Test password updating
469 public function testUpdatePassword()
471 $rs = $this->adhExists();
473 $this->createAdherent();
475 $this->loadAdherent($rs->current()->id_adh
);
478 $this->checkMemberExpected();
481 \Galette\Entity\Adherent
::updatePassword($this->zdb
, $this->adh
->id
, $newpass);
482 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
483 $pw_checked = password_verify($newpass, $adh->password
);
484 $this->boolean($pw_checked)->isTrue();
486 //reset original password
487 \Galette\Entity\Adherent
::updatePassword($this->zdb
, $this->adh
->id
, 'J^B-()f');
495 public function testCheckErrors()
499 $data = ['ddn_adh' => 'not a date'];
500 $expected = ['- Wrong date format (Y-m-d) for Birth date!'];
501 $check = $adh->check($data, [], []);
502 $this->array($check)->isIdenticalTo($expected);
506 'date_crea_adh' => 'not a date'
508 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
509 $check = $adh->check($data, [], []);
510 $this->array($check)->isIdenticalTo($expected);
512 //reste creation date to its default value
513 $data = ['date_crea_adh' => date('Y-m-d')];
514 $check = $adh->check($data, [], []);
515 $this->boolean($check)->isTrue();
517 $data = ['email_adh' => 'not an email'];
518 $expected = ['- Non-valid E-Mail address! (E-Mail)'];
519 $check = $adh->check($data, [], []);
520 $this->array($check)->isIdenticalTo($expected);
524 'url_adh' => 'mywebsite'
526 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
527 $check = $adh->check($data, [], []);
528 $this->array($check)->isIdenticalTo($expected);
530 $data = ['url_adh' => 'http://'];
531 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
532 $check = $adh->check($data, [], []);
533 $this->boolean($check)->isTrue($expected);
534 $this->variable($adh->_website
)->isIdenticalTo('');
536 $data = ['login_adh' => 'a'];
537 $expected = ['- The username must be composed of at least 2 characters!'];
538 $check = $adh->check($data, [], []);
539 $this->array($check)->isIdenticalTo($expected);
541 $data = ['login_adh' => 'login@galette'];
542 $expected = ['- The username cannot contain the @ character'];
543 $check = $adh->check($data, [], []);
544 $this->array($check)->isIdenticalTo($expected);
548 'mdp_adh' => 'short',
549 'mdp_adh2' => 'short'
551 $expected = ['Too short (6 characters minimum, 5 found)'];
552 $check = $adh->check($data, [], []);
553 $this->array($check)->isIdenticalTo($expected);
555 $data = ['mdp_adh' => 'mypassword'];
556 $expected = ['- The passwords don\'t match!'];
557 $check = $adh->check($data, [], []);
558 $this->array($check)->isIdenticalTo($expected);
561 'mdp_adh' => 'mypassword',
562 'mdp_adh2' => 'mypasswor'
564 $expected = ['- The passwords don\'t match!'];
565 $check = $adh->check($data, [], []);
566 $this->array($check)->isIdenticalTo($expected);
568 $data = ['id_statut' => 256];
569 $expected = ['Status #256 does not exists in database.'];
570 $check = $adh->check($data, [], []);
571 $this->array($check)->isIdenticalTo($expected);
579 public function testPhoto()
581 $rs = $this->adhExists();
583 $this->createAdherent();
585 $this->loadAdherent($rs->current()->id_adh
);
588 $fakedata = new \Galette\Util\
FakeData($this->zdb
, $this->i18n
);
590 ->setSeed($this->seed
)
593 $this->members_fields
,
597 $this->boolean($fakedata->addPhoto($this->adh
))->isTrue();
599 $this->boolean($this->adh
->hasPicture())->isTrue();
602 $this->boolean($this->adh
->picture
->delete())->isTrue();
610 public function testCanEdit()
612 $adh = new \Galette\Entity\
Adherent($this->zdb
);
615 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
616 $this->boolean($adh->canEdit($login))->isFalse();
618 //admin => authorized
619 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
620 $this->calling($login)->isAdmin
= true;
621 $this->boolean($adh->canEdit($login))->isTrue();
623 //staff => authorized
624 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
625 $this->calling($login)->isStaff
= true;
626 $this->boolean($adh->canEdit($login))->isTrue();
629 $adh = new \mock\Galette\Entity\
Adherent($this->zdb
);
631 $g1 = new \mock\Galette\Entity\
Group();
632 $this->calling($g1)->getId
= 1;
633 $g2 = new \mock\Galette\Entity\
Group();
634 $this->calling($g1)->getId
= 2;
636 $this->calling($adh)->getGroups
= [$g1, $g2];
637 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
638 $this->boolean($adh->canEdit($login))->isFalse();
640 $this->calling($login)->isGroupManager
= true;
641 $this->boolean($adh->canEdit($login))->isTrue();
645 * Test member duplication
649 public function testDuplicate()
651 $adh = new \Galette\Entity\
Adherent($this->zdb
);
653 $rs = $this->adhExists();
655 $this->createAdherent();
657 $this->loadAdherent($rs->current()->id_adh
);
660 $this->checkMemberExpected();
662 //load member from db
663 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
664 $this->checkMemberExpected($adh);
666 $adh->setDuplicate();
668 $this->string($adh->others_infos_admin
)->contains('Duplicated from');
669 $this->variable($adh->email
)->isNull();
670 $this->variable($adh->id
)->isNull();
671 $this->variable($adh->login
)->isNull();
672 $this->variable($adh->birthdate
)->isNull();
673 $this->variable($adh->surname
)->isNull();
681 public function testParents()
683 $rs = $this->adhExists();
685 $this->createAdherent();
687 $this->loadAdherent($rs->current()->id_adh
);
689 $this->checkMemberExpected();
691 //load member from db
692 $parent = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
693 $this->checkMemberExpected($parent);
695 $child_data = $this->dataAdherent() +
[
697 'prenom_adh' => 'Johny',
698 'parent_id' => $parent->id
,
700 $child = $this->createMember($child_data);
702 $this->string($child->name
)->isIdenticalTo($child_data['nom_adh']);
703 $this->object($child->parent
)->isInstanceOf('\Galette\Entity\Adherent');
704 $this->integer($child->parent
->id
)->isIdenticalTo($parent->id
);
706 $check = $child->check(['detach_parent' => true], [], []);
707 if (is_array($check)) {
710 $this->boolean($check)->isTrue();
711 $this->boolean($child->store())->isTrue();
712 $this->variable($child->parent
)->isNull();