3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020-2023 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-2023 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
;
47 use Galette\Middleware\Authenticate
;
50 * Galette tests case main class
53 * @name GaletteTestCase
54 * @package GaletteTests
55 * @author Johan Cwiklinski <johan@x-tnd.be>
56 * @copyright 2020-2023 The Galette Team
57 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
58 * @link http://galette.eu
61 abstract class GaletteTestCase
extends atoum
64 protected \Galette\Core\Db
$zdb;
65 protected array $members_fields;
66 protected array $members_fields_cats;
68 protected \Galette\Core\I18n
$i18n;
69 /** @var Preferences */
70 protected \Galette\Core\Preferences
$preferences;
71 protected \RKA\Session
$session;
73 protected \Galette\Core\Login
$login;
75 protected \Galette\Core\History
$history;
76 protected $logger_storage = '';
79 protected Adherent
$adh;
80 /** @var Contribution */
81 protected Contribution
$contrib;
82 protected array $adh_ids = [];
83 protected array $contrib_ids = [];
85 protected array $flash_data;
86 /** @var \Slim\Flash\Messages */
87 protected \Slim\Flash\Messages
$flash;
88 protected \DI\Container
$container;
90 protected array $excluded_after_methods = [];
95 * @param string $method Method tested
99 public function beforeTestMethod($method)
102 $this->flash_data
= &$flash_data;
103 $this->flash
= new \Slim\Flash\
Messages($flash_data);
105 $gapp = new \Galette\Core\
SlimApp();
106 $app = $gapp->getApp();
107 $plugins = new \Galette\Core\
Plugins();
108 require GALETTE_BASE_PATH
. '/includes/dependencies.php';
109 $container = $app->getContainer();
110 $_SERVER['HTTP_HOST'] = '';
112 $container->set('flash', $this->flash
);
113 $container->set(Slim\Flash\Messages
::class, $this->flash
);
115 $app->addRoutingMiddleware();
117 $this->container
= $container;
119 $this->zdb
= $container->get('zdb');
120 $this->i18n
= $container->get('i18n');
121 $this->login
= $container->get('login');
122 $this->preferences
= $container->get('preferences');
123 $this->history
= $container->get('history');
124 $this->members_fields
= $container->get('members_fields');
125 $this->members_fields_cats
= $container->get('members_fields_cats');
126 $this->session
= $container->get('session');
128 global $zdb, $login, $hist, $i18n, $container, $galette_log_var; // globals :(
130 $login = $this->login
;
131 $hist = $this->history
;
133 $container = $this->container
;
134 $galette_log_var = $this->logger_storage
;
136 $authenticate = new Authenticate($container);
137 $showPublicPages = function (\Slim\Psr7\Request
$request, \Psr\Http\Server\RequestHandlerInterface
$handler) {
138 return $handler->handle($request);
141 require_once GALETTE_ROOT
. 'includes/routes/main.routes.php';
142 require_once GALETTE_ROOT
. 'includes/routes/authentication.routes.php';
143 require_once GALETTE_ROOT
. 'includes/routes/management.routes.php';
144 require_once GALETTE_ROOT
. 'includes/routes/members.routes.php';
145 require_once GALETTE_ROOT
. 'includes/routes/groups.routes.php';
146 require_once GALETTE_ROOT
. 'includes/routes/contributions.routes.php';
147 require_once GALETTE_ROOT
. 'includes/routes/public_pages.routes.php';
148 require_once GALETTE_ROOT
. 'includes/routes/ajax.routes.php';
149 require_once GALETTE_ROOT
. 'includes/routes/plugins.routes.php';
155 * @param string $method Calling method
159 public function afterTestMethod($method)
161 if (TYPE_DB
=== 'mysql' && !in_array($method, $this->excluded_after_methods
)) {
162 $this->array($this->zdb
->getWarnings())->isIdenticalTo([]);
167 * Loads member from a resultset
169 * @param integer $id Id
173 protected function loadAdherent($id)
175 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, (int)$id);
176 $this->adh
->setDependencies(
178 $this->members_fields
,
184 * Get Faker data for one member
188 protected function dataAdherentOne(): array
190 $bdate = new \
DateTime(date('Y') . '-12-26');
191 //member is expected to be 82 years old
193 $now = new \
DateTime();
194 if ($now <= $bdate) {
197 $bdate->sub(new \
DateInterval('P' . $years . 'Y'));
199 'nom_adh' => 'Durand',
200 'prenom_adh' => 'René',
201 'ville_adh' => 'Martel',
202 'cp_adh' => '39 069',
203 'adresse_adh' => '66, boulevard De Oliveira',
204 'email_adh' => 'meunier.josephine@ledoux.com',
205 'login_adh' => 'arthur.hamon',
206 'mdp_adh' => 'J^B-()f',
207 'mdp_adh2' => 'J^B-()f',
208 'bool_admin_adh' => false,
209 'bool_exempt_adh' => false,
210 'bool_display_info' => true,
212 'prof_adh' => 'Chef de fabrication',
214 'ddn_adh' => $bdate->format('Y-m-d'),
215 'lieu_naissance' => 'Gonzalez-sur-Meunier',
216 'pseudo_adh' => 'ubertrand',
217 'pays_adh' => 'Antarctique',
218 'tel_adh' => '0439153432',
219 'activite_adh' => true,
221 'date_crea_adh' => '2020-06-10',
222 'pref_lang' => 'en_US',
223 'fingerprint' => 'FAKER' . $this->seed
,
229 * Get Faker data for second member
233 protected function dataAdherentTwo(): array
235 $bdate = new \
DateTime(date('Y') . '-09-13');
236 //member is expected to be 28 years old
238 $now = new \
DateTime();
239 if ($now <= $bdate) {
242 $bdate->sub(new \
DateInterval('P' . $years . 'Y'));
245 'nom_adh' => 'Hoarau',
246 'prenom_adh' => 'Lucas',
247 'ville_adh' => 'Reynaudnec',
249 'adresse_adh' => '2, boulevard Legros',
250 'email_adh' => 'phoarau@tele2.fr',
251 'login_adh' => 'nathalie51',
252 'mdp_adh' => 'T.u!IbKOi|06',
253 'mdp_adh2' => 'T.u!IbKOi|06',
254 'bool_admin_adh' => false,
255 'bool_exempt_adh' => false,
256 'bool_display_info' => false,
258 'prof_adh' => 'Extraction',
260 'ddn_adh' => $bdate->format('Y-m-d'),
261 'lieu_naissance' => 'Fischer',
262 'pseudo_adh' => 'vallet.camille',
264 'tel_adh' => '05 59 53 59 43',
265 'activite_adh' => true,
267 'date_crea_adh' => '2019-05-20',
269 'fingerprint' => 'FAKER' . $this->seed
,
270 'societe_adh' => 'Philippe',
271 'is_company' => true,
277 * Create member from data
279 * @param array $data Data to use to create member
281 * @return \Galette\Entity\Adherent
283 public function createMember(array $data)
285 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
286 $this->adh
->setDependencies(
288 $this->members_fields
,
292 $check = $this->adh
->check($data, [], []);
293 if (is_array($check)) {
296 $this->boolean($check)->isTrue();
298 $store = $this->adh
->store();
299 $this->boolean($store)->isTrue();
305 * Check members expecteds
307 * @param Adherent $adh Member instance, if any
308 * @param array $new_expecteds Changes on expected values
312 protected function checkMemberOneExpected($adh = null, $new_expecteds = [])
319 'nom_adh' => 'Durand',
320 'prenom_adh' => 'René',
321 'ville_adh' => 'Martel',
322 'cp_adh' => '07 926',
323 'adresse_adh' => '66, boulevard De Oliveira',
324 'email_adh' => 'meunier.josephine@ledoux.com',
325 'login_adh' => 'arthur.hamon',
326 'mdp_adh' => 'J^B-()f',
327 'bool_admin_adh' => false,
328 'bool_exempt_adh' => false,
329 'bool_display_info' => true,
331 'prof_adh' => 'Chef de fabrication',
333 'ddn_adh' => 'NOT USED',
334 'lieu_naissance' => 'Gonzalez-sur-Meunier',
335 'pseudo_adh' => 'ubertrand',
336 'cp_adh' => '39 069',
337 'pays_adh' => 'Antarctique',
338 'tel_adh' => '0439153432',
339 'activite_adh' => true,
341 'pref_lang' => 'en_US',
342 'fingerprint' => 'FAKER95842354',
345 $expecteds = array_merge($expecteds, $new_expecteds);
347 foreach ($expecteds as $key => $value) {
348 $property = $this->members_fields
[$key]['propname'];
350 case 'bool_admin_adh':
351 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
353 case 'bool_exempt_adh':
354 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
356 case 'bool_display_info':
357 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
360 $this->boolean($adh->isActive())->isIdenticalTo($value);
363 $pw_checked = password_verify($value, $adh->password
);
364 $this->boolean($pw_checked)->isTrue();
367 //rely on age, not on birthdate
368 $this->variable($adh->$property)->isNotNull();
369 $this->string($adh->getAge())->isIdenticalTo(' (82 years old)');
372 $this->variable($adh->$property)->isIdenticalTo(
374 "$property expected {$value} got {$adh->$property}"
381 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
383 $expected_str = ' (82 years old)';
384 $this->string($adh->getAge())->isIdenticalTo($expected_str);
385 $this->boolean($adh->hasChildren())->isFalse();
386 $this->boolean($adh->hasParent())->isFalse();
387 $this->boolean($adh->hasPicture())->isFalse();
389 $this->string($adh->sadmin
)->isIdenticalTo('No');
390 $this->string($adh->sdue_free
)->isIdenticalTo('No');
391 $this->string($adh->sappears_in_list
)->isIdenticalTo('Yes');
392 $this->string($adh->sstaff
)->isIdenticalTo('No');
393 $this->string($adh->sactive
)->isIdenticalTo('Active');
394 $this->variable($adh->stitle
)->isNull();
395 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
396 $this->string($adh->sfullname
)->isIdenticalTo('DURAND René');
397 $this->string($adh->saddress
)->isIdenticalTo('66, boulevard De Oliveira');
398 $this->string($adh->sname
)->isIdenticalTo('DURAND René');
400 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
401 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
402 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
403 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
405 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('DURAND René');
406 $this->string($adh->getRowClass())->isIdenticalTo('active-account cotis-never');
410 * Check members expecteds
412 * @param Adherent $adh Member instance, if any
413 * @param array $new_expecteds Changes on expected values
417 protected function checkMemberTwoExpected($adh = null, $new_expecteds = [])
424 'nom_adh' => 'Hoarau',
425 'prenom_adh' => 'Lucas',
426 'ville_adh' => 'Reynaudnec',
428 'adresse_adh' => '2, boulevard Legros',
429 'email_adh' => 'phoarau@tele2.fr',
430 'login_adh' => 'nathalie51',
431 'mdp_adh' => 'T.u!IbKOi|06',
432 'bool_admin_adh' => false,
433 'bool_exempt_adh' => false,
434 'bool_display_info' => false,
436 'prof_adh' => 'Extraction',
438 'ddn_adh' => 'NOT USED',
439 'lieu_naissance' => 'Fischer',
440 'pseudo_adh' => 'vallet.camille',
442 'tel_adh' => '05 59 53 59 43',
443 'activite_adh' => true,
446 'fingerprint' => 'FAKER' . $this->seed
,
447 'societe_adh' => 'Philippe'
449 $expecteds = array_merge($expecteds, $new_expecteds);
451 foreach ($expecteds as $key => $value) {
452 $property = $this->members_fields
[$key]['propname'];
454 case 'bool_admin_adh':
455 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
457 case 'bool_exempt_adh':
458 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
460 case 'bool_display_info':
461 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
464 $this->boolean($adh->isActive())->isIdenticalTo($value);
467 $pw_checked = password_verify($value, $adh->password
);
468 $this->boolean($pw_checked)->isTrue();
471 //rely on age, not on birthdate
472 $this->variable($adh->$property)->isNotNull();
473 $this->string($adh->getAge())->isIdenticalTo(' (28 years old)');
476 $this->variable($adh->$property)->isIdenticalTo(
478 "$property expected {$value} got {$adh->$property}"
484 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
486 $expected_str = ' (28 years old)';
487 $this->string($adh->getAge())->isIdenticalTo($expected_str);
488 $this->boolean($adh->hasChildren())->isFalse();
489 $this->boolean($adh->hasParent())->isFalse();
490 $this->boolean($adh->hasPicture())->isFalse();
492 $this->string($adh->sadmin
)->isIdenticalTo('No');
493 $this->string($adh->sdue_free
)->isIdenticalTo('No');
494 $this->string($adh->sappears_in_list
)->isIdenticalTo('No');
495 $this->string($adh->sstaff
)->isIdenticalTo('No');
496 $this->string($adh->sactive
)->isIdenticalTo('Active');
497 $this->variable($adh->stitle
)->isNull();
498 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
499 $this->string($adh->sfullname
)->isIdenticalTo('HOARAU Lucas');
500 $this->string($adh->saddress
)->isIdenticalTo('2, boulevard Legros');
501 $this->string($adh->sname
)->isIdenticalTo('HOARAU Lucas');
503 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
504 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
505 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
506 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
508 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('HOARAU Lucas');
509 $this->string($adh->getRowClass())->isIdenticalTo('active-account cotis-never');
513 * Look in database if test member already exists
515 * @return false|ResultSet
517 protected function adhOneExists()
519 $mdata = $this->dataAdherentOne();
520 $select = $this->zdb
->select(\Galette\Entity\Adherent
::TABLE
, 'a');
523 'a.fingerprint' => 'FAKER' . $this->seed
,
524 'a.login_adh' => $mdata['login_adh']
528 $results = $this->zdb
->execute($select);
529 if ($results->count() === 0) {
537 * Look in database if test member already exists
539 * @return false|ResultSet
541 protected function adhTwoExists()
543 $mdata = $this->dataAdherentTwo();
544 $select = $this->zdb
->select(\Galette\Entity\Adherent
::TABLE
, 'a');
547 'a.fingerprint' => 'FAKER' . $this->seed
,
548 'a.login_adh' => $mdata['login_adh']
552 $results = $this->zdb
->execute($select);
553 if ($results->count() === 0) {
563 * @return \Galette\Entity\Adherent
565 protected function getMemberOne()
567 $rs = $this->adhOneExists();
569 $this->createMember($this->dataAdherentOne());
571 $this->loadAdherent($rs->current()->id_adh
);
578 * @return \Galette\Entity\Adherent
580 protected function getMemberTwo()
582 $rs = $this->adhTwoExists();
584 $this->createMember($this->dataAdherentTwo());
586 $this->loadAdherent($rs->current()->id_adh
);
591 * Create contribution from data
593 * @param array $data Data to use to create contribution
595 * @return \Galette\Entity\Contribution
597 public function createContrib(array $data)
599 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
600 $contrib = $this->contrib
;
601 $check = $contrib->check($data, [], []);
602 if (is_array($check)) {
605 $this->boolean($check)->isTrue();
607 $store = $contrib->store();
608 $this->boolean($store)->isTrue();
614 * Create test contribution in database
618 protected function createContribution()
620 $now = new \
DateTime(); // 2020-11-07
621 $begin_date = clone $now;
622 $begin_date->sub(new \
DateInterval('P5M')); // 2020-06-08
623 $begin_date->add(new \
DateInterval('P3D')); // 2020-06-11
625 $due_date = clone $begin_date;
626 $due_date->sub(new \
DateInterval('P1D'));
627 $due_date->add(new \
DateInterval('P1Y'));
630 'id_adh' => $this->adh
->id
,
631 'id_type_cotis' => 1, //annual fee
632 'montant_cotis' => 92,
633 'type_paiement_cotis' => 3,
634 'info_cotis' => 'FAKER' . $this->seed
,
635 'date_enreg' => $begin_date->format('Y-m-d'),
636 'date_debut_cotis' => $begin_date->format('Y-m-d'),
637 'date_fin_cotis' => $due_date->format('Y-m-d'),
639 $this->createContrib($data);
640 $this->checkContribExpected();
644 * Check contributions expected
646 * @param Contribution $contrib Contribution instance, if any
647 * @param array $new_expecteds Changes on expected values
651 protected function checkContribExpected($contrib = null, $new_expecteds = [])
653 if ($contrib === null) {
654 $contrib = $this->contrib
;
657 $begin_date = $contrib->raw_begin_date
;
659 $due_date = clone $begin_date;
660 $due_date->sub(new \
DateInterval('P1D'));
661 $due_date->add(new \
DateInterval('P1Y'));
663 $this->object($contrib->raw_date
)->isInstanceOf('DateTime');
664 $this->object($contrib->raw_begin_date
)->isInstanceOf('DateTime');
665 $this->object($contrib->raw_end_date
)->isInstanceOf('DateTime');
668 'id_adh' => "{$this->adh->id}",
669 'id_type_cotis' => 1, //annual fee
670 'montant_cotis' => '92',
671 'type_paiement_cotis' => '3',
672 'info_cotis' => 'FAKER' . $this->seed
,
673 'date_fin_cotis' => $due_date->format('Y-m-d'),
675 $expecteds = array_merge($expecteds, $new_expecteds);
677 $this->string($contrib->raw_end_date
->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
679 foreach ($expecteds as $key => $value) {
680 $property = $this->contrib
->fields
[$key]['propname'];
682 case \Galette\Entity\ContributionsTypes
::PK
:
683 $ct = $this->contrib
->type
;
684 if ($ct instanceof \Galette\Entity\ContributionsTypes
) {
685 $this->integer((int)$ct->id
)->isIdenticalTo($value);
687 $this->integer($ct)->isIdenticalTo($value);
691 $this->variable($contrib->$property)->isEqualTo($value, $property);
696 //load member from db
697 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
698 //member is now up-to-date
699 $this->string($this->adh
->getRowClass())->isIdenticalTo('active-account cotis-ok');
700 $this->string($this->adh
->due_date
)->isIdenticalTo($this->contrib
->end_date
);
701 $this->boolean($this->adh
->isUp2Date())->isTrue();
702 $this->boolean($contrib->isFee())->isTrue();
703 $this->string($contrib->getTypeLabel())->isIdenticalTo('Membership');
704 $this->string($contrib->getRawType())->isIdenticalTo('membership');
705 $this->array($this->contrib
->getRequired())->isIdenticalTo([
706 'id_type_cotis' => 1,
709 'date_debut_cotis' => 1,
710 'date_fin_cotis' => 1,
716 * Initialize default status in database
720 protected function initStatus(): void
722 $status = new \Galette\Entity\
Status($this->zdb
);
723 if (count($status->getList()) === 0) {
724 //status are not yet instantiated.
725 $res = $status->installInit();
726 $this->boolean($res)->isTrue();
731 * Initialize default contributions types in database
735 protected function initContributionsTypes(): void
737 $ct = new \Galette\Entity\
ContributionsTypes($this->zdb
);
738 if (count($ct->getCompleteList()) === 0) {
739 //status are not yet instanciated.
740 $res = $ct->installInit();
741 $this->boolean($res)->isTrue();
746 * Initialize default titles in database
750 protected function initTitles(): void
752 $titles = new \Galette\Repository\
Titles($this->zdb
);
753 if (count($titles->getList($this->zdb
)) === 0) {
754 $res = $titles->installInit($this->zdb
);
755 $this->boolean($res)->isTrue();
760 * Initialize default PDF models in database
764 protected function initModels(): void
766 $models = new \Galette\Repository\
PdfModels($this->zdb
, $this->preferences
, $this->login
);
767 $res = $models->installInit(false);
768 $this->boolean($res)->isTrue();
776 protected function cleanHistory(): void
778 $this->zdb
->db
->query(
779 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Core\History
::TABLE
,
780 \Laminas\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
785 * Log-in as super administrator
789 protected function logSuperAdmin(): void
791 $this->login
->logAdmin('superadmin', $this->preferences
);
792 $this->boolean($this->login
->isLogged())->isTrue();
793 $this->boolean($this->login
->isSuperAdmin())->isTrue();