3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020 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 2020 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 * @link http://galette.eu
42 * Galette tests case main class
45 * @name GaletteTestCase
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2020 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.eu
53 abstract class GaletteTestCase
extends atoum
56 protected $members_fields;
57 protected $members_fields_cats;
59 protected $preferences;
63 protected $logger_storage = '';
67 protected $adh_ids = [];
68 protected $contrib_ids = [];
69 /** @var \mock\Slim\Router */
70 protected $mocked_router;
72 protected $flash_data;
73 /** @var \Slim\Flash\Messages */
83 * @param stgring $testMethod Method tested
87 public function beforeTestMethod($testMethod)
89 $this->mocked_router
= new \mock\Slim\
Router();
90 $this->calling($this->mocked_router
)->pathFor
= function ($name, $params) {
94 $this->flash_data
= &$flash_data;
95 $this->flash
= new \Slim\Flash\
Messages($flash_data);
97 $app = new \Galette\Core\
SlimApp();
98 $plugins = new \Galette\Core\
Plugins();
99 require GALETTE_BASE_PATH
. '/includes/dependencies.php';
100 $container = $app->getContainer();
101 $_SERVER['HTTP_HOST'] = '';
103 $container->set('flash', $this->flash
);
104 $container->set(Slim\Flash\Messages
::class, $this->flash
);
105 $container->set('router', $this->mocked_router
);
106 $container->set(Slim\Router
::class, $this->mocked_router
);
108 $this->container
= $container;
110 $this->zdb
= $container->get('zdb');
111 $this->i18n
= $container->get('i18n');
112 $this->login
= $container->get('login');
113 $this->preferences
= $container->get('preferences');
114 $this->history
= $container->get('history');
115 $this->members_fields
= $container->get('members_fields');
116 $this->members_fields_cats
= $container->get('members_fields_cats');
117 $this->request
= $container->get('request');
118 $this->response
= $container->get('response');
120 global $zdb, $login, $hist, $i18n, $container, $galette_log_var; // globals :(
122 $login = $this->login
;
123 $hist = $this->history
;
125 $container = $this->container
;
126 $galette_log_var = $this->logger_storage
;
130 * Loads member from a resultset
132 * @param integer $id Id
136 protected function loadAdherent($id)
138 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, (int)$id);
139 $this->adh
->setDependencies(
141 $this->members_fields
,
147 * Get Faker data for one member
151 protected function dataAdherentOne(): array
153 $bdate = new \
DateTime(date('Y') . '-12-26');
154 //member is expected to be 82 years old
156 $now = new \
DateTime();
157 if ($now <= $bdate) {
160 $bdate->sub(new \
DateInterval('P' . $years . 'Y'));
162 'nom_adh' => 'Durand',
163 'prenom_adh' => 'René',
164 'ville_adh' => 'Martel',
165 'cp_adh' => '39 069',
166 'adresse_adh' => '66, boulevard De Oliveira',
167 'email_adh' => 'meunier.josephine@ledoux.com',
168 'login_adh' => 'arthur.hamon',
169 'mdp_adh' => 'J^B-()f',
170 'mdp_adh2' => 'J^B-()f',
171 'bool_admin_adh' => false,
172 'bool_exempt_adh' => false,
173 'bool_display_info' => true,
175 'prof_adh' => 'Chef de fabrication',
177 'ddn_adh' => $bdate->format('Y-m-d'),
178 'lieu_naissance' => 'Gonzalez-sur-Meunier',
179 'pseudo_adh' => 'ubertrand',
180 'pays_adh' => 'Antarctique',
181 'tel_adh' => '0439153432',
182 'url_adh' => 'http://bouchet.com/',
183 'activite_adh' => true,
185 'date_crea_adh' => '2020-06-10',
186 'pref_lang' => 'en_US',
187 'fingerprint' => 'FAKER' . $this->seed
,
193 * Get Faker data for second member
197 protected function dataAdherentTwo(): array
199 $bdate = new \
DateTime(date('Y') . '-09-13');
200 //member is expected to be 28 years old
202 $now = new \
DateTime();
203 if ($now <= $bdate) {
206 $bdate->sub(new \
DateInterval('P' . $years . 'Y'));
209 'nom_adh' => 'Hoarau',
210 'prenom_adh' => 'Lucas',
211 'ville_adh' => 'Reynaudnec',
213 'adresse_adh' => '2, boulevard Legros',
214 'email_adh' => 'phoarau@tele2.fr',
215 'login_adh' => 'nathalie51',
216 'mdp_adh' => 'T.u!IbKOi|06',
217 'mdp_adh2' => 'T.u!IbKOi|06',
218 'bool_admin_adh' => false,
219 'bool_exempt_adh' => false,
220 'bool_display_info' => false,
222 'prof_adh' => 'Extraction',
224 'ddn_adh' => $bdate->format('Y-m-d'),
225 'lieu_naissance' => 'Fischer',
226 'pseudo_adh' => 'vallet.camille',
228 'tel_adh' => '05 59 53 59 43',
229 'url_adh' => 'http://bodin.net/omnis-ratione-sint-dolorem-architecto',
230 'activite_adh' => true,
232 'date_crea_adh' => '2019-05-20',
234 'fingerprint' => 'FAKER' . $this->seed
,
235 'societe_adh' => 'Philippe',
236 'is_company' => true,
242 * Create member from data
244 * @param array $data Data to use to create member
246 * @return \Galette\Entity\Adherent
248 public function createMember(array $data)
250 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
251 $this->adh
->setDependencies(
253 $this->members_fields
,
257 $check = $this->adh
->check($data, [], []);
258 if (is_array($check)) {
261 $this->boolean($check)->isTrue();
263 $store = $this->adh
->store();
264 $this->boolean($store)->isTrue();
270 * Check members expecteds
272 * @param Adherent $adh Member instance, if any
273 * @param array $new_expecteds Changes on expected values
277 protected function checkMemberOneExpected($adh = null, $new_expecteds = [])
284 'nom_adh' => 'Durand',
285 'prenom_adh' => 'René',
286 'ville_adh' => 'Martel',
287 'cp_adh' => '07 926',
288 'adresse_adh' => '66, boulevard De Oliveira',
289 'email_adh' => 'meunier.josephine@ledoux.com',
290 'login_adh' => 'arthur.hamon',
291 'mdp_adh' => 'J^B-()f',
292 'bool_admin_adh' => false,
293 'bool_exempt_adh' => false,
294 'bool_display_info' => true,
296 'prof_adh' => 'Chef de fabrication',
298 'ddn_adh' => 'NOT USED',
299 'lieu_naissance' => 'Gonzalez-sur-Meunier',
300 'pseudo_adh' => 'ubertrand',
301 'cp_adh' => '39 069',
302 'pays_adh' => 'Antarctique',
303 'tel_adh' => '0439153432',
304 'url_adh' => 'http://bouchet.com/',
305 'activite_adh' => true,
307 'pref_lang' => 'en_US',
308 'fingerprint' => 'FAKER95842354',
311 $expecteds = array_merge($expecteds, $new_expecteds);
313 foreach ($expecteds as $key => $value) {
314 $property = $this->members_fields
[$key]['propname'];
316 case 'bool_admin_adh':
317 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
319 case 'bool_exempt_adh':
320 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
322 case 'bool_display_info':
323 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
326 $this->boolean($adh->isActive())->isIdenticalTo($value);
329 $pw_checked = password_verify($value, $adh->password
);
330 $this->boolean($pw_checked)->isTrue();
333 //rely on age, not on birthdate
334 $this->variable($adh->$property)->isNotNull();
335 $this->string($adh->getAge())->isIdenticalTo(' (82 years old)');
338 $this->variable($adh->$property)->isIdenticalTo(
340 "$property expected {$value} got {$adh->$property}"
347 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
349 $expected_str = ' (82 years old)';
350 $this->string($adh->getAge())->isIdenticalTo($expected_str);
351 $this->boolean($adh->hasChildren())->isFalse();
352 $this->boolean($adh->hasParent())->isFalse();
353 $this->boolean($adh->hasPicture())->isFalse();
355 $this->string($adh->sadmin
)->isIdenticalTo('No');
356 $this->string($adh->sdue_free
)->isIdenticalTo('No');
357 $this->string($adh->sappears_in_list
)->isIdenticalTo('Yes');
358 $this->string($adh->sstaff
)->isIdenticalTo('No');
359 $this->string($adh->sactive
)->isIdenticalTo('Active');
360 $this->variable($adh->stitle
)->isNull();
361 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
362 $this->string($adh->sfullname
)->isIdenticalTo('DURAND René');
363 $this->string($adh->saddress
)->isIdenticalTo('66, boulevard De Oliveira');
364 $this->string($adh->sname
)->isIdenticalTo('DURAND René');
366 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
367 $this->string($adh->getAddressContinuation())->isEmpty();
368 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
369 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
370 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
372 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('DURAND René');
373 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
377 * Check members expecteds
379 * @param Adherent $adh Member instance, if any
380 * @param array $new_expecteds Changes on expected values
384 protected function checkMemberTwoExpected($adh = null, $new_expecteds = [])
391 'nom_adh' => 'Hoarau',
392 'prenom_adh' => 'Lucas',
393 'ville_adh' => 'Reynaudnec',
395 'adresse_adh' => '2, boulevard Legros',
396 'email_adh' => 'phoarau@tele2.fr',
397 'login_adh' => 'nathalie51',
398 'mdp_adh' => 'T.u!IbKOi|06',
399 'bool_admin_adh' => false,
400 'bool_exempt_adh' => false,
401 'bool_display_info' => false,
403 'prof_adh' => 'Extraction',
405 'ddn_adh' => 'NOT USED',
406 'lieu_naissance' => 'Fischer',
407 'pseudo_adh' => 'vallet.camille',
409 'tel_adh' => '05 59 53 59 43',
410 'url_adh' => 'http://bodin.net/omnis-ratione-sint-dolorem-architecto',
411 'activite_adh' => true,
414 'fingerprint' => 'FAKER' . $this->seed
,
415 'societe_adh' => 'Philippe'
417 $expecteds = array_merge($expecteds, $new_expecteds);
419 foreach ($expecteds as $key => $value) {
420 $property = $this->members_fields
[$key]['propname'];
422 case 'bool_admin_adh':
423 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
425 case 'bool_exempt_adh':
426 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
428 case 'bool_display_info':
429 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
432 $this->boolean($adh->isActive())->isIdenticalTo($value);
435 $pw_checked = password_verify($value, $adh->password
);
436 $this->boolean($pw_checked)->isTrue();
439 //rely on age, not on birthdate
440 $this->variable($adh->$property)->isNotNull();
441 $this->string($adh->getAge())->isIdenticalTo(' (28 years old)');
444 $this->variable($adh->$property)->isIdenticalTo(
446 "$property expected {$value} got {$adh->$property}"
452 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
454 $expected_str = ' (28 years old)';
455 $this->string($adh->getAge())->isIdenticalTo($expected_str);
456 $this->boolean($adh->hasChildren())->isFalse();
457 $this->boolean($adh->hasParent())->isFalse();
458 $this->boolean($adh->hasPicture())->isFalse();
460 $this->string($adh->sadmin
)->isIdenticalTo('No');
461 $this->string($adh->sdue_free
)->isIdenticalTo('No');
462 $this->string($adh->sappears_in_list
)->isIdenticalTo('No');
463 $this->string($adh->sstaff
)->isIdenticalTo('No');
464 $this->string($adh->sactive
)->isIdenticalTo('Active');
465 $this->variable($adh->stitle
)->isNull();
466 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
467 $this->string($adh->sfullname
)->isIdenticalTo('HOARAU Lucas');
468 $this->string($adh->saddress
)->isIdenticalTo('2, boulevard Legros');
469 $this->string($adh->sname
)->isIdenticalTo('HOARAU Lucas');
471 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
472 $this->string($adh->getAddressContinuation())->isEmpty();
473 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
474 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
475 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
477 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('HOARAU Lucas');
478 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
482 * Look in database if test member already exists
484 * @return false|ResultSet
486 protected function adhOneExists()
488 $mdata = $this->dataAdherentOne();
489 $select = $this->zdb
->select(\Galette\Entity\Adherent
::TABLE
, 'a');
492 'a.fingerprint' => 'FAKER' . $this->seed
,
493 'a.login_adh' => $mdata['login_adh']
497 $results = $this->zdb
->execute($select);
498 if ($results->count() === 0) {
506 * Look in database if test member already exists
508 * @return false|ResultSet
510 protected function adhTwoExists()
512 $mdata = $this->dataAdherentTwo();
513 $select = $this->zdb
->select(\Galette\Entity\Adherent
::TABLE
, 'a');
516 'a.fingerprint' => 'FAKER' . $this->seed
,
517 'a.login_adh' => $mdata['login_adh']
521 $results = $this->zdb
->execute($select);
522 if ($results->count() === 0) {
532 * @return \Galette\Entity\Adherent
534 protected function getMemberOne()
536 $rs = $this->adhOneExists();
538 $this->createMember($this->dataAdherentOne());
540 $this->loadAdherent($rs->current()->id_adh
);
547 * @return \Galette\Entity\Adherent
549 protected function getMemberTwo()
551 $rs = $this->adhTwoExists();
553 $this->createMember($this->dataAdherentTwo());
555 $this->loadAdherent($rs->current()->id_adh
);
560 * Create contribution from data
562 * @param array $data Data to use to create contribution
564 * @return \Galette\Entity\Contribution
566 public function createContrib(array $data)
568 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
569 $contrib = $this->contrib
;
570 $check = $contrib->check($data, [], []);
571 if (is_array($check)) {
574 $this->boolean($check)->isTrue();
576 $store = $contrib->store();
577 $this->boolean($store)->isTrue();
583 * Create test contribution in database
587 protected function createContribution()
589 $bdate = new \
DateTime(); // 2020-11-07
590 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
591 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
593 $edate = clone $bdate;
594 $edate->add(new \
DateInterval('P1Y'));
597 'id_adh' => $this->adh
->id
,
598 'id_type_cotis' => 1,
599 'montant_cotis' => 92,
600 'type_paiement_cotis' => 3,
601 'info_cotis' => 'FAKER' . $this->seed
,
602 'date_enreg' => $bdate->format('Y-m-d'),
603 'date_debut_cotis' => $bdate->format('Y-m-d'),
604 'date_fin_cotis' => $edate->format('Y-m-d'),
606 $this->createContrib($data);
607 $this->checkContribExpected();
611 * Check contributions expected
613 * @param Contribution $contrib Contribution instance, if any
614 * @param array $new_expecteds Changes on expected values
618 protected function checkContribExpected($contrib = null, $new_expecteds = [])
620 if ($contrib === null) {
621 $contrib = $this->contrib
;
624 $date_begin = $contrib->raw_begin_date
;
625 $date_end = clone $date_begin;
626 $date_end->add(new \
DateInterval('P1Y'));
628 $this->object($contrib->raw_date
)->isInstanceOf('DateTime');
629 $this->object($contrib->raw_begin_date
)->isInstanceOf('DateTime');
630 $this->object($contrib->raw_end_date
)->isInstanceOf('DateTime');
633 'id_adh' => "{$this->adh->id}",
634 'id_type_cotis' => 1,
635 'montant_cotis' => '92',
636 'type_paiement_cotis' => '3',
637 'info_cotis' => 'FAKER' . $this->seed
,
638 'date_fin_cotis' => $date_end->format('Y-m-d'),
640 $expecteds = array_merge($expecteds, $new_expecteds);
642 $this->string($contrib->raw_end_date
->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
644 foreach ($expecteds as $key => $value) {
645 $property = $this->contrib
->fields
[$key]['propname'];
647 case \Galette\Entity\ContributionsTypes
::PK
:
648 $ct = $this->contrib
->type
;
649 if ($ct instanceof \Galette\Entity\ContributionsTypes
) {
650 $this->integer((int)$ct->id
)->isIdenticalTo($value);
652 $this->integer($ct)->isIdenticalTo($value);
656 $this->variable($contrib->$property)->isEqualTo($value, $property);
661 //load member from db
662 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
663 //member is now up-to-date
664 $this->string($this->adh
->getRowClass())->isIdenticalTo('active cotis-ok');
665 $this->string($this->adh
->due_date
)->isIdenticalTo($this->contrib
->end_date
);
666 $this->boolean($this->adh
->isUp2Date())->isTrue();
670 * Initialize default status in database
674 protected function initStatus(): void
676 $status = new \Galette\Entity\
Status($this->zdb
);
677 if (count($status->getList()) === 0) {
678 //status are not yet instantiated.
679 $res = $status->installInit();
680 $this->boolean($res)->isTrue();
685 * Initialize default contributions types in database
689 protected function initContributionsTypes(): void
691 $ct = new \Galette\Entity\
ContributionsTypes($this->zdb
);
692 if (count($ct->getCompleteList()) === 0) {
693 //status are not yet instanciated.
694 $res = $ct->installInit();
695 $this->boolean($res)->isTrue();
700 * Initialize default titles in database
704 protected function initTitles(): void
706 $titles = new \Galette\Repository\
Titles($this->zdb
);
707 if (count($titles->getList($this->zdb
)) === 0) {
708 $res = $titles->installInit($this->zdb
);
709 $this->boolean($res)->isTrue();