3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020-2021 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-2021 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
41 use Galette\Core\History
;
42 use Galette\Core\I18n
;
43 use Galette\Core\Login
;
44 use Galette\Core\Preferences
;
45 use Galette\Entity\Adherent
;
46 use Galette\Entity\Contribution
;
49 * Galette tests case main class
52 * @name GaletteTestCase
53 * @package GaletteTests
54 * @author Johan Cwiklinski <johan@x-tnd.be>
55 * @copyright 2020 The Galette Team
56 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
57 * @link http://galette.eu
60 abstract class GaletteTestCase
extends atoum
64 protected $members_fields;
65 protected $members_fields_cats;
68 /** @var Preferences */
69 protected $preferences;
75 protected $logger_storage = '';
79 /** @var Contribution */
81 protected $adh_ids = [];
82 protected $contrib_ids = [];
83 /** @var \mock\Slim\Router */
84 protected $mocked_router;
86 protected $flash_data;
87 /** @var \Slim\Flash\Messages */
93 protected $excluded_after_methods = [];
98 * @param stgring $method Method tested
102 public function beforeTestMethod($method)
104 $this->mocked_router
= new \mock\Slim\
Router();
105 $this->calling($this->mocked_router
)->pathFor
= function ($name, $params) {
109 $this->flash_data
= &$flash_data;
110 $this->flash
= new \Slim\Flash\
Messages($flash_data);
112 $app = new \Galette\Core\
SlimApp();
113 $plugins = new \Galette\Core\
Plugins();
114 require GALETTE_BASE_PATH
. '/includes/dependencies.php';
115 $container = $app->getContainer();
116 $_SERVER['HTTP_HOST'] = '';
118 $container->set('flash', $this->flash
);
119 $container->set(Slim\Flash\Messages
::class, $this->flash
);
120 $container->set('router', $this->mocked_router
);
121 $container->set(Slim\Router
::class, $this->mocked_router
);
123 $this->container
= $container;
125 $this->zdb
= $container->get('zdb');
126 $this->i18n
= $container->get('i18n');
127 $this->login
= $container->get('login');
128 $this->preferences
= $container->get('preferences');
129 $this->history
= $container->get('history');
130 $this->members_fields
= $container->get('members_fields');
131 $this->members_fields_cats
= $container->get('members_fields_cats');
132 $this->request
= $container->get('request');
133 $this->response
= $container->get('response');
134 $this->session
= $container->get('session');
136 global $zdb, $login, $hist, $i18n, $container, $galette_log_var; // globals :(
138 $login = $this->login
;
139 $hist = $this->history
;
141 $container = $this->container
;
142 $galette_log_var = $this->logger_storage
;
148 * @param string $method Calling method
152 public function afterTestMethod($method)
154 if (TYPE_DB
=== 'mysql' && !in_array($method, $this->excluded_after_methods
)) {
155 $this->array($this->zdb
->getWarnings())->isIdenticalTo([]);
160 * Loads member from a resultset
162 * @param integer $id Id
166 protected function loadAdherent($id)
168 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, (int)$id);
169 $this->adh
->setDependencies(
171 $this->members_fields
,
177 * Get Faker data for one member
181 protected function dataAdherentOne(): array
183 $bdate = new \
DateTime(date('Y') . '-12-26');
184 //member is expected to be 82 years old
186 $now = new \
DateTime();
187 if ($now <= $bdate) {
190 $bdate->sub(new \
DateInterval('P' . $years . 'Y'));
192 'nom_adh' => 'Durand',
193 'prenom_adh' => 'René',
194 'ville_adh' => 'Martel',
195 'cp_adh' => '39 069',
196 'adresse_adh' => '66, boulevard De Oliveira',
197 'email_adh' => 'meunier.josephine@ledoux.com',
198 'login_adh' => 'arthur.hamon',
199 'mdp_adh' => 'J^B-()f',
200 'mdp_adh2' => 'J^B-()f',
201 'bool_admin_adh' => false,
202 'bool_exempt_adh' => false,
203 'bool_display_info' => true,
205 'prof_adh' => 'Chef de fabrication',
207 'ddn_adh' => $bdate->format('Y-m-d'),
208 'lieu_naissance' => 'Gonzalez-sur-Meunier',
209 'pseudo_adh' => 'ubertrand',
210 'pays_adh' => 'Antarctique',
211 'tel_adh' => '0439153432',
212 'activite_adh' => true,
214 'date_crea_adh' => '2020-06-10',
215 'pref_lang' => 'en_US',
216 'fingerprint' => 'FAKER' . $this->seed
,
222 * Get Faker data for second member
226 protected function dataAdherentTwo(): array
228 $bdate = new \
DateTime(date('Y') . '-09-13');
229 //member is expected to be 28 years old
231 $now = new \
DateTime();
232 if ($now <= $bdate) {
235 $bdate->sub(new \
DateInterval('P' . $years . 'Y'));
238 'nom_adh' => 'Hoarau',
239 'prenom_adh' => 'Lucas',
240 'ville_adh' => 'Reynaudnec',
242 'adresse_adh' => '2, boulevard Legros',
243 'email_adh' => 'phoarau@tele2.fr',
244 'login_adh' => 'nathalie51',
245 'mdp_adh' => 'T.u!IbKOi|06',
246 'mdp_adh2' => 'T.u!IbKOi|06',
247 'bool_admin_adh' => false,
248 'bool_exempt_adh' => false,
249 'bool_display_info' => false,
251 'prof_adh' => 'Extraction',
253 'ddn_adh' => $bdate->format('Y-m-d'),
254 'lieu_naissance' => 'Fischer',
255 'pseudo_adh' => 'vallet.camille',
257 'tel_adh' => '05 59 53 59 43',
258 'activite_adh' => true,
260 'date_crea_adh' => '2019-05-20',
262 'fingerprint' => 'FAKER' . $this->seed
,
263 'societe_adh' => 'Philippe',
264 'is_company' => true,
270 * Create member from data
272 * @param array $data Data to use to create member
274 * @return \Galette\Entity\Adherent
276 public function createMember(array $data)
278 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
279 $this->adh
->setDependencies(
281 $this->members_fields
,
285 $check = $this->adh
->check($data, [], []);
286 if (is_array($check)) {
289 $this->boolean($check)->isTrue();
291 $store = $this->adh
->store();
292 $this->boolean($store)->isTrue();
298 * Check members expecteds
300 * @param Adherent $adh Member instance, if any
301 * @param array $new_expecteds Changes on expected values
305 protected function checkMemberOneExpected($adh = null, $new_expecteds = [])
312 'nom_adh' => 'Durand',
313 'prenom_adh' => 'René',
314 'ville_adh' => 'Martel',
315 'cp_adh' => '07 926',
316 'adresse_adh' => '66, boulevard De Oliveira',
317 'email_adh' => 'meunier.josephine@ledoux.com',
318 'login_adh' => 'arthur.hamon',
319 'mdp_adh' => 'J^B-()f',
320 'bool_admin_adh' => false,
321 'bool_exempt_adh' => false,
322 'bool_display_info' => true,
324 'prof_adh' => 'Chef de fabrication',
326 'ddn_adh' => 'NOT USED',
327 'lieu_naissance' => 'Gonzalez-sur-Meunier',
328 'pseudo_adh' => 'ubertrand',
329 'cp_adh' => '39 069',
330 'pays_adh' => 'Antarctique',
331 'tel_adh' => '0439153432',
332 'activite_adh' => true,
334 'pref_lang' => 'en_US',
335 'fingerprint' => 'FAKER95842354',
338 $expecteds = array_merge($expecteds, $new_expecteds);
340 foreach ($expecteds as $key => $value) {
341 $property = $this->members_fields
[$key]['propname'];
343 case 'bool_admin_adh':
344 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
346 case 'bool_exempt_adh':
347 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
349 case 'bool_display_info':
350 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
353 $this->boolean($adh->isActive())->isIdenticalTo($value);
356 $pw_checked = password_verify($value, $adh->password
);
357 $this->boolean($pw_checked)->isTrue();
360 //rely on age, not on birthdate
361 $this->variable($adh->$property)->isNotNull();
362 $this->string($adh->getAge())->isIdenticalTo(' (82 years old)');
365 $this->variable($adh->$property)->isIdenticalTo(
367 "$property expected {$value} got {$adh->$property}"
374 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
376 $expected_str = ' (82 years old)';
377 $this->string($adh->getAge())->isIdenticalTo($expected_str);
378 $this->boolean($adh->hasChildren())->isFalse();
379 $this->boolean($adh->hasParent())->isFalse();
380 $this->boolean($adh->hasPicture())->isFalse();
382 $this->string($adh->sadmin
)->isIdenticalTo('No');
383 $this->string($adh->sdue_free
)->isIdenticalTo('No');
384 $this->string($adh->sappears_in_list
)->isIdenticalTo('Yes');
385 $this->string($adh->sstaff
)->isIdenticalTo('No');
386 $this->string($adh->sactive
)->isIdenticalTo('Active');
387 $this->variable($adh->stitle
)->isNull();
388 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
389 $this->string($adh->sfullname
)->isIdenticalTo('DURAND René');
390 $this->string($adh->saddress
)->isIdenticalTo('66, boulevard De Oliveira');
391 $this->string($adh->sname
)->isIdenticalTo('DURAND René');
393 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
394 $this->string($adh->getAddressContinuation())->isEmpty();
395 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
396 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
397 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
399 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('DURAND René');
400 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
404 * Check members expecteds
406 * @param Adherent $adh Member instance, if any
407 * @param array $new_expecteds Changes on expected values
411 protected function checkMemberTwoExpected($adh = null, $new_expecteds = [])
418 'nom_adh' => 'Hoarau',
419 'prenom_adh' => 'Lucas',
420 'ville_adh' => 'Reynaudnec',
422 'adresse_adh' => '2, boulevard Legros',
423 'email_adh' => 'phoarau@tele2.fr',
424 'login_adh' => 'nathalie51',
425 'mdp_adh' => 'T.u!IbKOi|06',
426 'bool_admin_adh' => false,
427 'bool_exempt_adh' => false,
428 'bool_display_info' => false,
430 'prof_adh' => 'Extraction',
432 'ddn_adh' => 'NOT USED',
433 'lieu_naissance' => 'Fischer',
434 'pseudo_adh' => 'vallet.camille',
436 'tel_adh' => '05 59 53 59 43',
437 'activite_adh' => true,
440 'fingerprint' => 'FAKER' . $this->seed
,
441 'societe_adh' => 'Philippe'
443 $expecteds = array_merge($expecteds, $new_expecteds);
445 foreach ($expecteds as $key => $value) {
446 $property = $this->members_fields
[$key]['propname'];
448 case 'bool_admin_adh':
449 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
451 case 'bool_exempt_adh':
452 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
454 case 'bool_display_info':
455 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
458 $this->boolean($adh->isActive())->isIdenticalTo($value);
461 $pw_checked = password_verify($value, $adh->password
);
462 $this->boolean($pw_checked)->isTrue();
465 //rely on age, not on birthdate
466 $this->variable($adh->$property)->isNotNull();
467 $this->string($adh->getAge())->isIdenticalTo(' (28 years old)');
470 $this->variable($adh->$property)->isIdenticalTo(
472 "$property expected {$value} got {$adh->$property}"
478 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
480 $expected_str = ' (28 years old)';
481 $this->string($adh->getAge())->isIdenticalTo($expected_str);
482 $this->boolean($adh->hasChildren())->isFalse();
483 $this->boolean($adh->hasParent())->isFalse();
484 $this->boolean($adh->hasPicture())->isFalse();
486 $this->string($adh->sadmin
)->isIdenticalTo('No');
487 $this->string($adh->sdue_free
)->isIdenticalTo('No');
488 $this->string($adh->sappears_in_list
)->isIdenticalTo('No');
489 $this->string($adh->sstaff
)->isIdenticalTo('No');
490 $this->string($adh->sactive
)->isIdenticalTo('Active');
491 $this->variable($adh->stitle
)->isNull();
492 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
493 $this->string($adh->sfullname
)->isIdenticalTo('HOARAU Lucas');
494 $this->string($adh->saddress
)->isIdenticalTo('2, boulevard Legros');
495 $this->string($adh->sname
)->isIdenticalTo('HOARAU Lucas');
497 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
498 $this->string($adh->getAddressContinuation())->isEmpty();
499 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
500 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
501 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
503 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('HOARAU Lucas');
504 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
508 * Look in database if test member already exists
510 * @return false|ResultSet
512 protected function adhOneExists()
514 $mdata = $this->dataAdherentOne();
515 $select = $this->zdb
->select(\Galette\Entity\Adherent
::TABLE
, 'a');
518 'a.fingerprint' => 'FAKER' . $this->seed
,
519 'a.login_adh' => $mdata['login_adh']
523 $results = $this->zdb
->execute($select);
524 if ($results->count() === 0) {
532 * Look in database if test member already exists
534 * @return false|ResultSet
536 protected function adhTwoExists()
538 $mdata = $this->dataAdherentTwo();
539 $select = $this->zdb
->select(\Galette\Entity\Adherent
::TABLE
, 'a');
542 'a.fingerprint' => 'FAKER' . $this->seed
,
543 'a.login_adh' => $mdata['login_adh']
547 $results = $this->zdb
->execute($select);
548 if ($results->count() === 0) {
558 * @return \Galette\Entity\Adherent
560 protected function getMemberOne()
562 $rs = $this->adhOneExists();
564 $this->createMember($this->dataAdherentOne());
566 $this->loadAdherent($rs->current()->id_adh
);
573 * @return \Galette\Entity\Adherent
575 protected function getMemberTwo()
577 $rs = $this->adhTwoExists();
579 $this->createMember($this->dataAdherentTwo());
581 $this->loadAdherent($rs->current()->id_adh
);
586 * Create contribution from data
588 * @param array $data Data to use to create contribution
590 * @return \Galette\Entity\Contribution
592 public function createContrib(array $data)
594 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
595 $contrib = $this->contrib
;
596 $check = $contrib->check($data, [], []);
597 if (is_array($check)) {
600 $this->boolean($check)->isTrue();
602 $store = $contrib->store();
603 $this->boolean($store)->isTrue();
609 * Create test contribution in database
613 protected function createContribution()
615 $bdate = new \
DateTime(); // 2020-11-07
616 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
617 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
619 $edate = clone $bdate;
620 $edate->add(new \
DateInterval('P1Y'));
623 'id_adh' => $this->adh
->id
,
624 'id_type_cotis' => 1, //annual fee
625 'montant_cotis' => 92,
626 'type_paiement_cotis' => 3,
627 'info_cotis' => 'FAKER' . $this->seed
,
628 'date_enreg' => $bdate->format('Y-m-d'),
629 'date_debut_cotis' => $bdate->format('Y-m-d'),
630 'date_fin_cotis' => $edate->format('Y-m-d'),
632 $this->createContrib($data);
633 $this->checkContribExpected();
637 * Check contributions expected
639 * @param Contribution $contrib Contribution instance, if any
640 * @param array $new_expecteds Changes on expected values
644 protected function checkContribExpected($contrib = null, $new_expecteds = [])
646 if ($contrib === null) {
647 $contrib = $this->contrib
;
650 $date_begin = $contrib->raw_begin_date
;
651 $date_end = clone $date_begin;
652 $date_end->add(new \
DateInterval('P1Y'));
654 $this->object($contrib->raw_date
)->isInstanceOf('DateTime');
655 $this->object($contrib->raw_begin_date
)->isInstanceOf('DateTime');
656 $this->object($contrib->raw_end_date
)->isInstanceOf('DateTime');
659 'id_adh' => "{$this->adh->id}",
660 'id_type_cotis' => 1, //annual fee
661 'montant_cotis' => '92',
662 'type_paiement_cotis' => '3',
663 'info_cotis' => 'FAKER' . $this->seed
,
664 'date_fin_cotis' => $date_end->format('Y-m-d'),
666 $expecteds = array_merge($expecteds, $new_expecteds);
668 $this->string($contrib->raw_end_date
->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
670 foreach ($expecteds as $key => $value) {
671 $property = $this->contrib
->fields
[$key]['propname'];
673 case \Galette\Entity\ContributionsTypes
::PK
:
674 $ct = $this->contrib
->type
;
675 if ($ct instanceof \Galette\Entity\ContributionsTypes
) {
676 $this->integer((int)$ct->id
)->isIdenticalTo($value);
678 $this->integer($ct)->isIdenticalTo($value);
682 $this->variable($contrib->$property)->isEqualTo($value, $property);
687 //load member from db
688 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
689 //member is now up-to-date
690 $this->string($this->adh
->getRowClass())->isIdenticalTo('active cotis-ok');
691 $this->string($this->adh
->due_date
)->isIdenticalTo($this->contrib
->end_date
);
692 $this->boolean($this->adh
->isUp2Date())->isTrue();
693 $this->boolean($contrib->isFee())->isTrue();
694 $this->string($contrib->getTypeLabel())->isIdenticalTo('Membership');
695 $this->string($contrib->getRawType())->isIdenticalTo('membership');
696 $this->array($this->contrib
->getRequired())->isIdenticalTo([
697 'id_type_cotis' => 1,
700 'date_debut_cotis' => 1,
701 'date_fin_cotis' => 1,
707 * Initialize default status in database
711 protected function initStatus(): void
713 $status = new \Galette\Entity\
Status($this->zdb
);
714 if (count($status->getList()) === 0) {
715 //status are not yet instantiated.
716 $res = $status->installInit();
717 $this->boolean($res)->isTrue();
722 * Initialize default contributions types in database
726 protected function initContributionsTypes(): void
728 $ct = new \Galette\Entity\
ContributionsTypes($this->zdb
);
729 if (count($ct->getCompleteList()) === 0) {
730 //status are not yet instanciated.
731 $res = $ct->installInit();
732 $this->boolean($res)->isTrue();
737 * Initialize default titles in database
741 protected function initTitles(): void
743 $titles = new \Galette\Repository\
Titles($this->zdb
);
744 if (count($titles->getList($this->zdb
)) === 0) {
745 $res = $titles->installInit($this->zdb
);
746 $this->boolean($res)->isTrue();
755 protected function cleanHistory(): void
757 $this->zdb
->db
->query(
758 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Core\History
::TABLE
,
759 \Zend\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
764 * Log-in as super administrator
768 protected function logSuperAdmin(): void
770 $this->login
->logAdmin('superadmin', $this->preferences
);
771 $this->boolean($this->login
->isLogged())->isTrue();
772 $this->boolean($this->login
->isSuperAdmin())->isTrue();