]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Adherent.php
0c4e5493630a5763ba6c41d57240ced721aab598
[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 * Create test user in database
168 *
169 * @return void
170 */
171 private function createAdherent()
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 $this->createMember($data);
185 }
186
187 /**
188 * Loads member from a resultset
189 *
190 * @param integer $id Id
191 *
192 * @return void
193 */
194 private function loadAdherent($id)
195 {
196 $this->adh = new \Galette\Entity\Adherent($this->zdb, (int)$id);
197 }
198
199 /**
200 * Test empty member
201 *
202 * @return void
203 */
204 public function testEmpty()
205 {
206 $adh = $this->adh;
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();
223
224 $this->variable($adh->fake_prop)->isNull();
225
226 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
227 }
228
229 /**
230 * Tests getter
231 *
232 * @return void
233 */
234 public function testGetterWException()
235 {
236 $adh = $this->adh;
237
238 $this->exception(
239 function () use ($adh) {
240 $adh->row_classes;
241 }
242 )->isInstanceOf('RuntimeException');
243 }
244
245 /**
246 * Create member from data
247 *
248 * @param array $data Data to use to create member
249 *
250 * @return \Galette\Entity\Adherent
251 */
252 public function createMember(array $data)
253 {
254 $adh = $this->adh;
255 $check = $adh->check($data, [], []);
256 if (is_array($check)) {
257 var_dump($check);
258 }
259 $this->boolean($check)->isTrue();
260
261 $store = $adh->store();
262 $this->boolean($store)->isTrue();
263
264 $this->ids[] = $adh->id;
265 }
266
267 /**
268 * Set dependencies from constructor
269 *
270 * @return void
271 */
272 public function testDepsAtConstuct()
273 {
274 $deps = [
275 'picture' => false,
276 'groups' => false,
277 'dues' => false,
278 'parent' => false,
279 'children' => false,
280 'dynamics' => false
281 ];
282 $adh = new \Galette\Entity\Adherent(
283 $this->zdb,
284 null,
285 $deps
286 );
287
288 $this->array($adh->deps)->isIdenticalTo($deps);
289
290 $adh = new \Galette\Entity\Adherent(
291 $this->zdb,
292 null,
293 'not an array'
294 );
295 $this->array($adh->deps)->isIdenticalTo($this->default_deps);
296 }
297
298 /**
299 * Check members expecteds
300 *
301 * @param Adherent $adh Member instance, if any
302 * @param array $new_expecteds Changes on expected values
303 *
304 * @return void
305 */
306 private function checkMemberExpected($adh = null, $new_expecteds = [])
307 {
308 if ($adh === null) {
309 $adh = $this->adh;
310 }
311
312 $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,
324 'sexe_adh' => 0,
325 'prof_adh' => 'Chef de fabrication',
326 'titre_adh' => null,
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,
335 'id_statut' => 9,
336 'pref_lang' => 'de_DE',
337 'fingerprint' => 'FAKER95842354',
338 'societe_adh' => ''
339 ];
340 $expecteds = array_merge($expecteds, $new_expecteds);
341
342 foreach ($expecteds as $key => $value) {
343 $property = $this->members_fields[$key]['propname'];
344 switch ($key) {
345 case 'bool_admin_adh':
346 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
347 break;
348 case 'bool_exempt_adh':
349 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
350 break;
351 case 'bool_display_info':
352 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
353 break;
354 case 'activite_adh':
355 $this->boolean($adh->isActive())->isIdenticalTo($value);
356 break;
357 case 'mdp_adh':
358 $pw_checked = password_verify($value, $adh->password);
359 $this->boolean($pw_checked)->isTrue();
360 break;
361 case 'ddn_adh':
362 //rely on age, not on birthdate
363 $this->variable($adh->$property)->isNotNull();
364 $this->string($adh->getAge())->isIdenticalTo(' (82 years old)');
365 break;
366 default:
367 $this->variable($adh->$property)->isIdenticalTo($value, $property);
368 break;
369 }
370 }
371
372 $d = \DateTime::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
373
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();
379
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é');
390
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']);
396
397 $this->string($adh::getSName($this->zdb, $adh->id))->isIdenticalTo('DURAND René');
398 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
399 }
400
401 /**
402 * Test simple member creation
403 *
404 * @return void
405 */
406 public function testSimpleMember()
407 {
408 $rs = $this->adhExists();
409 if ($rs === false) {
410 $this->createAdherent();
411 } else {
412 $this->loadAdherent($rs->current()->id_adh);
413 }
414
415 $this->checkMemberExpected();
416
417 //load member from db
418 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
419 $this->checkMemberExpected($adh);
420 }
421
422 /**
423 * Test load form login and email
424 *
425 * @return void
426 */
427 public function testLoadForLogin()
428 {
429 $rs = $this->adhExists();
430 if ($rs === false) {
431 $this->createAdherent();
432 } else {
433 $this->loadAdherent($rs->current()->id_adh);
434 }
435
436 $login = $this->adh->login;
437 $email = $this->adh->email;
438
439 $this->variable($this->adh->email)->isIdenticalTo($this->adh->getEmail());
440
441 $adh = new \Galette\Entity\Adherent($this->zdb, $login);
442 $this->checkMemberExpected($adh);
443
444 $adh = new \Galette\Entity\Adherent($this->zdb, $email);
445 $this->checkMemberExpected($adh);
446 }
447
448 /**
449 * Test password updating
450 *
451 * @return void
452 */
453 public function testUpdatePassword()
454 {
455 $rs = $this->adhExists();
456 if ($rs === false) {
457 $this->createAdherent();
458 } else {
459 $this->loadAdherent($rs->current()->id_adh);
460 }
461
462 $this->checkMemberExpected();
463
464 $newpass = 'aezrty';
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();
469
470 //reset original password
471 \Galette\Entity\Adherent::updatePassword($this->zdb, $this->adh->id, 'J^B-()f');
472 }
473
474 /**
475 * Tests check errors
476 *
477 * @return void
478 */
479 public function testCheckErrors()
480 {
481 $adh = $this->adh;
482
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);
487
488 $data = [
489 'ddn_adh' => '',
490 'date_crea_adh' => 'not a date'
491 ];
492 $expected = ['- Wrong date format (Y-m-d) for Creation date!'];
493 $check = $adh->check($data, [], []);
494 $this->array($check)->isIdenticalTo($expected);
495
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();
500
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);
505
506 $data = [
507 'email_adh' => '',
508 'url_adh' => 'mywebsite'
509 ];
510 $expected = ['- Non-valid Website address! Maybe you\'ve skipped the http://?'];
511 $check = $adh->check($data, [], []);
512 $this->array($check)->isIdenticalTo($expected);
513
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('');
519
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);
524
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);
529
530 $data = [
531 'login_adh' => '',
532 'mdp_adh' => 'short',
533 'mdp_adh2' => 'short'
534 ];
535 $expected = ['Too short (6 characters minimum, 5 found)'];
536 $check = $adh->check($data, [], []);
537 $this->array($check)->isIdenticalTo($expected);
538
539 $data = ['mdp_adh' => 'mypassword'];
540 $expected = ['- The passwords don\'t match!'];
541 $check = $adh->check($data, [], []);
542 $this->array($check)->isIdenticalTo($expected);
543
544 $data = [
545 'mdp_adh' => 'mypassword',
546 'mdp_adh2' => 'mypasswor'
547 ];
548 $expected = ['- The passwords don\'t match!'];
549 $check = $adh->check($data, [], []);
550 $this->array($check)->isIdenticalTo($expected);
551
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);
556 }
557
558 /**
559 * Test picture
560 *
561 * @return void
562 */
563 public function testPhoto()
564 {
565 $rs = $this->adhExists();
566 if ($rs === false) {
567 $this->createAdherent();
568 } else {
569 $this->loadAdherent($rs->current()->id_adh);
570 }
571
572 $fakedata = new \Galette\Util\FakeData($this->zdb, $this->i18n);
573 $fakedata
574 ->setSeed($this->seed)
575 ->setDependencies(
576 $this->preferences,
577 $this->members_fields,
578 $this->history,
579 $this->login
580 );
581 $this->boolean($fakedata->addPhoto($this->adh))->isTrue();
582
583 $this->boolean($this->adh->hasPicture())->isTrue();
584
585 //remove photo
586 $this->boolean($this->adh->picture->delete())->isTrue();
587 }
588
589 /**
590 * Test canEdit
591 *
592 * @return void
593 */
594 public function testCanEdit()
595 {
596 $adh = new \Galette\Entity\Adherent($this->zdb);
597
598 //non authorized
599 $login = new \mock\Galette\Core\Login($this->zdb, $this->i18n, $this->session);
600 $this->boolean($adh->canEdit($login))->isFalse();
601
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();
606
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();
611
612 //group managers
613 $adh = new \mock\Galette\Entity\Adherent($this->zdb);
614
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;
619
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();
623
624 $this->calling($login)->isGroupManager = true;
625 $this->boolean($adh->canEdit($login))->isTrue();
626 }
627
628 /**
629 * Test member duplication
630 *
631 * @return void
632 */
633 public function testDuplicate()
634 {
635 $adh = new \Galette\Entity\Adherent($this->zdb);
636
637 $rs = $this->adhExists();
638 if ($rs === false) {
639 $this->createAdherent();
640 } else {
641 $this->loadAdherent($rs->current()->id_adh);
642 }
643
644 $this->checkMemberExpected();
645
646 //load member from db
647 $adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
648 $this->checkMemberExpected($adh);
649
650 $adh->setDuplicate();
651
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();
658 }
659 }