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 * Create test user in database
171 private function createAdherent()
173 $fakedata = new \Galette\Util\
FakeData($this->zdb
, $this->i18n
);
175 ->setSeed($this->seed
)
178 $this->members_fields
,
183 $data = $fakedata->fakeMember();
184 $this->createMember($data);
188 * Loads member from a resultset
190 * @param integer $id Id
194 private function loadAdherent($id)
196 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, (int)$id);
204 public function testEmpty()
207 $this->boolean($adh->isAdmin())->isFalse();
208 $this->boolean($adh->admin
)->isFalse();
209 $this->boolean($adh->isStaff())->isFalse();
210 $this->boolean($adh->staff
)->isFalse();
211 $this->boolean($adh->isDueFree())->isFalse();
212 $this->boolean($adh->due_free
)->isFalse();
213 $this->boolean($adh->isGroupMember('any'))->isFalse();
214 $this->boolean($adh->isGroupManager('any'))->isFalse();
215 $this->boolean($adh->isCompany())->isFalse();
216 $this->boolean($adh->isMan())->isFalse();
217 $this->boolean($adh->isWoman())->isFalse();
218 $this->boolean($adh->isActive())->isTrue();
219 $this->boolean($adh->active
)->isTrue();
220 $this->boolean($adh->isUp2Date())->isFalse();
221 $this->boolean($adh->appearsInMembersList())->isFalse();
222 $this->boolean($adh->appears_in_list
)->isFalse();
224 $this->variable($adh->fake_prop
)->isNull();
226 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
234 public function testGetterWException()
239 function () use ($adh) {
242 )->isInstanceOf('RuntimeException');
246 * Create member from data
248 * @param array $data Data to use to create member
250 * @return \Galette\Entity\Adherent
252 public function createMember(array $data)
255 $check = $adh->check($data, [], []);
256 if (is_array($check)) {
259 $this->boolean($check)->isTrue();
261 $store = $adh->store();
262 $this->boolean($store)->isTrue();
264 $this->ids
[] = $adh->id
;
268 * Set dependencies from constructor
272 public function testDepsAtConstuct()
282 $adh = new \Galette\Entity\
Adherent(
288 $this->array($adh->deps
)->isIdenticalTo($deps);
290 $adh = new \Galette\Entity\
Adherent(
295 $this->array($adh->deps
)->isIdenticalTo($this->default_deps
);
299 * Check members expecteds
301 * @param Adherent $adh Member instance, if any
302 * @param array $new_expecteds Changes on expected values
306 private function checkMemberExpected($adh = null, $new_expecteds = [])
313 'nom_adh' => 'Durand',
314 'prenom_adh' => 'René',
315 'ville_adh' => 'Martel',
316 'cp_adh' => '07 926',
317 'adresse_adh' => '66, boulevard De Oliveira',
318 'email_adh' => 'meunier.josephine@ledoux.com',
319 'login_adh' => 'arthur.hamon',
320 'mdp_adh' => 'J^B-()f',
321 'bool_admin_adh' => false,
322 'bool_exempt_adh' => false,
323 'bool_display_info' => true,
325 'prof_adh' => 'Chef de fabrication',
327 'ddn_adh' => '1934-06-08',
328 'lieu_naissance' => 'Gonzalez-sur-Meunier',
329 'pseudo_adh' => 'ubertrand',
330 'cp_adh' => '39 069',
331 'pays_adh' => 'Antarctique',
332 'tel_adh' => '0439153432',
333 'url_adh' => 'http://bouchet.com/',
334 'activite_adh' => true,
336 'pref_lang' => 'de_DE',
337 'fingerprint' => 'FAKER95842354',
340 $expecteds = array_merge($expecteds, $new_expecteds);
342 foreach ($expecteds as $key => $value) {
343 $property = $this->members_fields
[$key]['propname'];
345 case 'bool_admin_adh':
346 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
348 case 'bool_exempt_adh':
349 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
351 case 'bool_display_info':
352 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
355 $this->boolean($adh->isActive())->isIdenticalTo($value);
358 $pw_checked = password_verify($value, $adh->password
);
359 $this->boolean($pw_checked)->isTrue();
362 //rely on age, not on birthdate
363 $this->variable($adh->$property)->isNotNull();
364 $this->string($adh->getAge())->isIdenticalTo(' (82 years old)');
367 $this->variable($adh->$property)->isIdenticalTo($value, $property);
372 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
374 $expected_str = ' (82 years old)';
375 $this->string($adh->getAge())->isIdenticalTo($expected_str);
376 $this->boolean($adh->hasChildren())->isFalse();
377 $this->boolean($adh->hasParent())->isFalse();
378 $this->boolean($adh->hasPicture())->isFalse();
380 $this->string($adh->sadmin
)->isIdenticalTo('No');
381 $this->string($adh->sdue_free
)->isIdenticalTo('No');
382 $this->string($adh->sappears_in_list
)->isIdenticalTo('Yes');
383 $this->string($adh->sstaff
)->isIdenticalTo('No');
384 $this->string($adh->sactive
)->isIdenticalTo('Active');
385 $this->variable($adh->stitle
)->isNull();
386 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
387 $this->string($adh->sfullname
)->isIdenticalTo('DURAND René');
388 $this->string($adh->saddress
)->isIdenticalTo('66, boulevard De Oliveira');
389 $this->string($adh->sname
)->isIdenticalTo('DURAND René');
391 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
392 $this->string($adh->getAddressContinuation())->isEmpty();
393 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
394 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
395 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
397 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('DURAND René');
398 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
402 * Test simple member creation
406 public function testSimpleMember()
408 $rs = $this->adhExists();
410 $this->createAdherent();
412 $this->loadAdherent($rs->current()->id_adh
);
415 $this->checkMemberExpected();
417 //load member from db
418 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
419 $this->checkMemberExpected($adh);
423 * Test load form login and email
427 public function testLoadForLogin()
429 $rs = $this->adhExists();
431 $this->createAdherent();
433 $this->loadAdherent($rs->current()->id_adh
);
436 $login = $this->adh
->login
;
437 $email = $this->adh
->email
;
439 $this->variable($this->adh
->email
)->isIdenticalTo($this->adh
->getEmail());
441 $adh = new \Galette\Entity\
Adherent($this->zdb
, $login);
442 $this->checkMemberExpected($adh);
444 $adh = new \Galette\Entity\
Adherent($this->zdb
, $email);
445 $this->checkMemberExpected($adh);
449 * Test password updating
453 public function testUpdatePassword()
455 $rs = $this->adhExists();
457 $this->createAdherent();
459 $this->loadAdherent($rs->current()->id_adh
);
462 $this->checkMemberExpected();
465 \Galette\Entity\Adherent
::updatePassword($this->zdb
, $this->adh
->id
, $newpass);
466 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
467 $pw_checked = password_verify($newpass, $adh->password
);
468 $this->boolean($pw_checked)->isTrue();
470 //reset original password
471 \Galette\Entity\Adherent
::updatePassword($this->zdb
, $this->adh
->id
, 'J^B-()f');
479 public function testCheckErrors()
483 $data = ['ddn_adh' => 'not a date'];
484 $expected = ['- Wrong date format (Y-m-d) for Birth date!'];
485 $check = $adh->check($data, [], []);
486 $this->array($check)->isIdenticalTo($expected);
490 'date_crea_adh' => 'not a date'
492 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
493 $check = $adh->check($data, [], []);
494 $this->array($check)->isIdenticalTo($expected);
496 //reste creation date to its default value
497 $data = ['date_crea_adh' => date('Y-m-d')];
498 $check = $adh->check($data, [], []);
499 $this->boolean($check)->isTrue();
501 $data = ['email_adh' => 'not an email'];
502 $expected = ['- Non-valid E-Mail address! (E-Mail)'];
503 $check = $adh->check($data, [], []);
504 $this->array($check)->isIdenticalTo($expected);
508 'url_adh' => 'mywebsite'
510 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
511 $check = $adh->check($data, [], []);
512 $this->array($check)->isIdenticalTo($expected);
514 $data = ['url_adh' => 'http://'];
515 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
516 $check = $adh->check($data, [], []);
517 $this->boolean($check)->isTrue($expected);
518 $this->variable($adh->_website
)->isIdenticalTo('');
520 $data = ['login_adh' => 'a'];
521 $expected = ['- The username must be composed of at least 2 characters!'];
522 $check = $adh->check($data, [], []);
523 $this->array($check)->isIdenticalTo($expected);
525 $data = ['login_adh' => 'login@galette'];
526 $expected = ['- The username cannot contain the @ character'];
527 $check = $adh->check($data, [], []);
528 $this->array($check)->isIdenticalTo($expected);
532 'mdp_adh' => 'short',
533 'mdp_adh2' => 'short'
535 $expected = ['Too short (6 characters minimum, 5 found)'];
536 $check = $adh->check($data, [], []);
537 $this->array($check)->isIdenticalTo($expected);
539 $data = ['mdp_adh' => 'mypassword'];
540 $expected = ['- The passwords don\'t match!'];
541 $check = $adh->check($data, [], []);
542 $this->array($check)->isIdenticalTo($expected);
545 'mdp_adh' => 'mypassword',
546 'mdp_adh2' => 'mypasswor'
548 $expected = ['- The passwords don\'t match!'];
549 $check = $adh->check($data, [], []);
550 $this->array($check)->isIdenticalTo($expected);
552 $data = ['id_statut' => 256];
553 $expected = ['Status #256 does not exists in database.'];
554 $check = $adh->check($data, [], []);
555 $this->array($check)->isIdenticalTo($expected);
563 public function testPhoto()
565 $rs = $this->adhExists();
567 $this->createAdherent();
569 $this->loadAdherent($rs->current()->id_adh
);
572 $fakedata = new \Galette\Util\
FakeData($this->zdb
, $this->i18n
);
574 ->setSeed($this->seed
)
577 $this->members_fields
,
581 $this->boolean($fakedata->addPhoto($this->adh
))->isTrue();
583 $this->boolean($this->adh
->hasPicture())->isTrue();
586 $this->boolean($this->adh
->picture
->delete())->isTrue();
594 public function testCanEdit()
596 $adh = new \Galette\Entity\
Adherent($this->zdb
);
599 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
600 $this->boolean($adh->canEdit($login))->isFalse();
602 //admin => authorized
603 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
604 $this->calling($login)->isAdmin
= true;
605 $this->boolean($adh->canEdit($login))->isTrue();
607 //staff => authorized
608 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
609 $this->calling($login)->isStaff
= true;
610 $this->boolean($adh->canEdit($login))->isTrue();
613 $adh = new \mock\Galette\Entity\
Adherent($this->zdb
);
615 $g1 = new \mock\Galette\Entity\
Group();
616 $this->calling($g1)->getId
= 1;
617 $g2 = new \mock\Galette\Entity\
Group();
618 $this->calling($g1)->getId
= 2;
620 $this->calling($adh)->getGroups
= [$g1, $g2];
621 $login = new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
622 $this->boolean($adh->canEdit($login))->isFalse();
624 $this->calling($login)->isGroupManager
= true;
625 $this->boolean($adh->canEdit($login))->isTrue();
629 * Test member duplication
633 public function testDuplicate()
635 $adh = new \Galette\Entity\
Adherent($this->zdb
);
637 $rs = $this->adhExists();
639 $this->createAdherent();
641 $this->loadAdherent($rs->current()->id_adh
);
644 $this->checkMemberExpected();
646 //load member from db
647 $adh = new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
648 $this->checkMemberExpected($adh);
650 $adh->setDuplicate();
652 $this->string($adh->others_infos_admin
)->contains('Duplicated from');
653 $this->variable($adh->email
)->isNull();
654 $this->variable($adh->id
)->isNull();
655 $this->variable($adh->login
)->isNull();
656 $this->variable($adh->birthdate
)->isNull();
657 $this->variable($adh->surname
)->isNull();