]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Adherent.php
Fix tests
[galette.git] / tests / Galette / Entity / tests / units / Adherent.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Adherent tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2017 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Entity
28 * @package GaletteTests
29 *
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
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2017-04-17
36 */
37
38 namespace Galette\Entity\test\units;
39
40 use atoum;
41
42 /**
43 * Adherent tests class
44 *
45 * @category Entity
46 * @name Adherent
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
52 * @since 2017-04-17
53 */
54 class Adherent extends atoum
55 {
56 private $zdb;
57 private $members_fields;
58 private $members_fields_cats;
59 private $i18n;
60 private $preferences;
61 private $session;
62 private $login;
63 private $history;
64 private $seed = 95842354;
65 private $default_deps;
66 private $adh;
67 private $ids = [];
68
69 /**
70 * Set up tests
71 *
72 * @return void
73 */
74 public function setUp()
75 {
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();
82 }
83 }
84
85 /**
86 * Cleanup after tests
87 *
88 * @return void
89 */
90 public function tearDown()
91 {
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);
96 }
97
98 /**
99 * Set up tests
100 *
101 * @param string $testMethod Calling method
102 *
103 * @return void
104 */
105 public function beforeTestMethod($testMethod)
106 {
107 $this->zdb = new \Galette\Core\Db();
108
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;
113
114 $this->i18n = new \Galette\Core\I18n(
115 \Galette\Core\I18n::DEFAULT_LANG
116 );
117
118 $this->preferences = new \Galette\Core\Preferences(
119 $this->zdb
120 );
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);
124
125 $this->default_deps = [
126 'picture' => true,
127 'groups' => true,
128 'dues' => true,
129 'parent' => false,
130 'children' => false,
131 'dynamics' => false
132 ];
133
134 global $zdb, $login, $hist, $i18n; // globals :(
135 $zdb = $this->zdb;
136 $login = $this->login;
137 $hist = $this->history;
138 $i18n = $this->i18n;
139
140 $this->adh = new \Galette\Entity\Adherent($this->zdb);
141 $this->adh->setDependencies(
142 $this->preferences,
143 $this->members_fields,
144 $this->history
145 );
146 }
147
148 /**
149 * Look in database if test member already exists
150 *
151 * @return false|ResultSet
152 */
153 private function adhExists()
154 {
155 $select = $this->zdb->select(\Galette\Entity\Adherent::TABLE, 'a');
156 $select->where(array('a.fingerprint' => 'FAKER' . $this->seed));
157
158 $results = $this->zdb->execute($select);
159 if ($results->count() === 0) {
160 return false;
161 } else {
162 return $results;
163 }
164 }
165
166 /**
167 * Get Faker data for one member
168 *
169 * @return array
170 */
171 private function dataAdherent(): array
172 {
173 $fakedata = new \Galette\Util\FakeData($this->zdb, $this->i18n);
174 $fakedata
175 ->setSeed($this->seed)
176 ->setDependencies(
177 $this->preferences,
178 $this->members_fields,
179 $this->history,
180 $this->login
181 );
182
183 $data = $fakedata->fakeMember();
184 return $data;
185 }
186
187 /**
188 * Create test user in database
189 *
190 * @return void
191 */
192 private function createAdherent()
193 {
194 $this->createMember($this->dataAdherent());
195 }
196
197 /**
198 * Loads member from a resultset
199 *
200 * @param integer $id Id
201 *
202 * @return void
203 */
204 private function loadAdherent($id)
205 {
206 $this->adh = new \Galette\Entity\Adherent($this->zdb, (int)$id);
207 $this->adh->setDependencies(
208 $this->preferences,
209 $this->members_fields,
210 $this->history
211 );
212 }
213
214 /**
215 * Test empty member
216 *
217 * @return void
218 */
219 public function testEmpty()
220 {
221 $adh = $this->adh;
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();
238
239 $this->variable($adh->fake_prop)->isNull();
240
241 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
242 }
243
244 /**
245 * Tests getter
246 *
247 * @return void
248 */
249 public function testGetterWException()
250 {
251 $adh = $this->adh;
252
253 $this->exception(
254 function () use ($adh) {
255 $adh->row_classes;
256 }
257 )->isInstanceOf('RuntimeException');
258 }
259
260 /**
261 * Create member from data
262 *
263 * @param array $data Data to use to create member
264 *
265 * @return \Galette\Entity\Adherent
266 */
267 public function createMember(array $data)
268 {
269 $adh = $this->adh;
270 $check = $adh->check($data, [], []);
271 if (is_array($check)) {
272 var_dump($check);
273 }
274 $this->boolean($check)->isTrue();
275
276 $store = $adh->store();
277 $this->boolean($store)->isTrue();
278
279 $this->ids[] = $adh->id;
280 return $adh;
281 }
282
283 /**
284 * Set dependencies from constructor
285 *
286 * @return void
287 */
288 public function testDepsAtConstuct()
289 {
290 $deps = [
291 'picture' => false,
292 'groups' => false,
293 'dues' => false,
294 'parent' => false,
295 'children' => false,
296 'dynamics' => false
297 ];
298 $adh = new \Galette\Entity\Adherent(
299 $this->zdb,
300 null,
301 $deps
302 );
303
304 $this->array($adh->deps)->isIdenticalTo($deps);
305
306 $adh = new \Galette\Entity\Adherent(
307 $this->zdb,
308 null,
309 'not an array'
310 );
311 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
312 }
313
314 /**
315 * Check members expecteds
316 *
317 * @param Adherent $adh Member instance, if any
318 * @param array $new_expecteds Changes on expected values
319 *
320 * @return void
321 */
322 private function checkMemberExpected($adh = null, $new_expecteds = [])
323 {
324 if ($adh === null) {
325 $adh = $this->adh;
326 }
327
328 $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,
340 'sexe_adh' => 0,
341 'prof_adh' => 'Chef de fabrication',
342 'titre_adh' => null,
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,
351 'id_statut' => 9,
352 'pref_lang' => 'en_US',
353 'fingerprint' => 'FAKER95842354',
354 'societe_adh' => ''
355 ];
356 $expecteds = array_merge($expecteds, $new_expecteds);
357
358 foreach ($expecteds as $key => $value) {
359 $property = $this->members_fields[$key]['propname'];
360 switch ($key) {
361 case 'bool_admin_adh':
362 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
363 break;
364 case 'bool_exempt_adh':
365 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
366 break;
367 case 'bool_display_info':
368 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
369 break;
370 case 'activite_adh':
371 $this->boolean($adh->isActive())->isIdenticalTo($value);
372 break;
373 case 'mdp_adh':
374 $pw_checked = password_verify($value, $adh->password);
375 $this->boolean($pw_checked)->isTrue();
376 break;
377 case 'ddn_adh':
378 //rely on age, not on birthdate
379 $this->variable($adh->$property)->isNotNull();
380 $this->string($adh->getAge())->isIdenticalTo(' (82 years old)');
381 break;
382 default:
383 $this->variable($adh->$property)->isIdenticalTo($value, $property);
384 break;
385 }
386 }
387
388 $d = \DateTime::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
389
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();
395
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é');
406
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']);
412
413 $this->string($adh::getSName($this->zdb, $adh->id))->isIdenticalTo('DURAND René');
414 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
415 }
416
417 /**
418 * Test simple member creation
419 *
420 * @return void
421 */
422 public function testSimpleMember()
423 {
424 $rs = $this->adhExists();
425 if ($rs === false) {
426 $this->createAdherent();
427 } else {
428 $this->loadAdherent($rs->current()->id_adh);
429 }
430
431 $this->checkMemberExpected();
432
433 //load member from db
434 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
435 $this->checkMemberExpected($adh);
436 }
437
438 /**
439 * Test load form login and email
440 *
441 * @return void
442 */
443 public function testLoadForLogin()
444 {
445 $rs = $this->adhExists();
446 if ($rs === false) {
447 $this->createAdherent();
448 } else {
449 $this->loadAdherent($rs->current()->id_adh);
450 }
451
452 $login = $this->adh->login;
453 $email = $this->adh->email;
454
455 $this->variable($this->adh->email)->isIdenticalTo($this->adh->getEmail());
456
457 $adh = new \Galette\Entity\Adherent($this->zdb, $login);
458 $this->checkMemberExpected($adh);
459
460 $adh = new \Galette\Entity\Adherent($this->zdb, $email);
461 $this->checkMemberExpected($adh);
462 }
463
464 /**
465 * Test password updating
466 *
467 * @return void
468 */
469 public function testUpdatePassword()
470 {
471 $rs = $this->adhExists();
472 if ($rs === false) {
473 $this->createAdherent();
474 } else {
475 $this->loadAdherent($rs->current()->id_adh);
476 }
477
478 $this->checkMemberExpected();
479
480 $newpass = 'aezrty';
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();
485
486 //reset original password
487 \Galette\Entity\Adherent::updatePassword($this->zdb, $this->adh->id, 'J^B-()f');
488 }
489
490 /**
491 * Tests check errors
492 *
493 * @return void
494 */
495 public function testCheckErrors()
496 {
497 $adh = $this->adh;
498
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);
503
504 $data = [
505 'ddn_adh' => '',
506 'date_crea_adh' => 'not a date'
507 ];
508 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
509 $check = $adh->check($data, [], []);
510 $this->array($check)->isIdenticalTo($expected);
511
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();
516
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);
521
522 $data = [
523 'email_adh' => '',
524 'url_adh' => 'mywebsite'
525 ];
526 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
527 $check = $adh->check($data, [], []);
528 $this->array($check)->isIdenticalTo($expected);
529
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('');
535
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);
540
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);
545
546 $data = [
547 'login_adh' => '',
548 'mdp_adh' => 'short',
549 'mdp_adh2' => 'short'
550 ];
551 $expected = ['Too short (6 characters minimum, 5 found)'];
552 $check = $adh->check($data, [], []);
553 $this->array($check)->isIdenticalTo($expected);
554
555 $data = ['mdp_adh' => 'mypassword'];
556 $expected = ['- The passwords don\'t match!'];
557 $check = $adh->check($data, [], []);
558 $this->array($check)->isIdenticalTo($expected);
559
560 $data = [
561 'mdp_adh' => 'mypassword',
562 'mdp_adh2' => 'mypasswor'
563 ];
564 $expected = ['- The passwords don\'t match!'];
565 $check = $adh->check($data, [], []);
566 $this->array($check)->isIdenticalTo($expected);
567
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);
572 }
573
574 /**
575 * Test picture
576 *
577 * @return void
578 */
579 public function testPhoto()
580 {
581 $rs = $this->adhExists();
582 if ($rs === false) {
583 $this->createAdherent();
584 } else {
585 $this->loadAdherent($rs->current()->id_adh);
586 }
587
588 $fakedata = new \Galette\Util\FakeData($this->zdb, $this->i18n);
589 $fakedata
590 ->setSeed($this->seed)
591 ->setDependencies(
592 $this->preferences,
593 $this->members_fields,
594 $this->history,
595 $this->login
596 );
597 $this->boolean($fakedata->addPhoto($this->adh))->isTrue();
598
599 $this->boolean($this->adh->hasPicture())->isTrue();
600
601 //remove photo
602 $this->boolean($this->adh->picture->delete())->isTrue();
603 }
604
605 /**
606 * Test canEdit
607 *
608 * @return void
609 */
610 public function testCanEdit()
611 {
612 $adh = new \Galette\Entity\Adherent($this->zdb);
613
614 //non authorized
615 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n, $this->session);
616 $this->boolean($adh->canEdit($login))->isFalse();
617
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();
622
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();
627
628 //group managers
629 $adh = new \mock\Galette\Entity\Adherent($this->zdb);
630
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;
635
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();
639
640 $this->calling($login)->isGroupManager = true;
641 $this->boolean($adh->canEdit($login))->isTrue();
642 }
643
644 /**
645 * Test member duplication
646 *
647 * @return void
648 */
649 public function testDuplicate()
650 {
651 $adh = new \Galette\Entity\Adherent($this->zdb);
652
653 $rs = $this->adhExists();
654 if ($rs === false) {
655 $this->createAdherent();
656 } else {
657 $this->loadAdherent($rs->current()->id_adh);
658 }
659
660 $this->checkMemberExpected();
661
662 //load member from db
663 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
664 $this->checkMemberExpected($adh);
665
666 $adh->setDuplicate();
667
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();
674 }
675
676 /**
677 * Test parents
678 *
679 * @return void
680 */
681 public function testParents()
682 {
683 $rs = $this->adhExists();
684 if ($rs === false) {
685 $this->createAdherent();
686 } else {
687 $this->loadAdherent($rs->current()->id_adh);
688 }
689 $this->checkMemberExpected();
690
691 //load member from db
692 $parent = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
693 $this->checkMemberExpected($parent);
694
695 $child_data = $this->dataAdherent() + [
696 'nom_adh' => 'Doe',
697 'prenom_adh' => 'Johny',
698 'parent_id' => $parent->id,
699 ];
700 $child = $this->createMember($child_data);
701
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);
705
706 $check = $child->check(['detach_parent' => true], [], []);
707 if (is_array($check)) {
708 var_dump($check);
709 }
710 $this->boolean($check)->isTrue();
711 $this->boolean($child->store())->isTrue();
712 $this->variable($child->parent)->isNull();
713 }
714 }