3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Reminders repository tests
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/>.
27 * @category Repository
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
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Repository\test\units
;
43 * Reminders repository tests
45 * @category Repository
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2020 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
54 class Reminders
extends atoum
57 private $seed = 95842355;
62 //private $remove = [];
65 private $members_fields;
68 private $contribs_ids = [];
75 public function setUp()
77 $this->zdb
= new \Galette\Core\
Db();
78 $ct = new \Galette\Entity\
ContributionsTypes($this->zdb
);
79 if (count($ct->getCompleteList()) === 0) {
80 //status are not yet instanciated.
81 $res = $ct->installInit();
82 $this->boolean($res)->isTrue();
88 * @param string $testMethod Calling method
92 public function beforeTestMethod($testMethod)
94 $this->zdb
= new \Galette\Core\
Db();
96 $this->i18n
= new \Galette\Core\
I18n(
97 \Galette\Core\I18n
::DEFAULT_LANG
100 $this->preferences
= new \Galette\Core\
Preferences(
103 $this->session
= new \RKA\
Session();
104 $this->login
= new \Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
105 $this->history
= new \Galette\Core\
History($this->zdb
, $this->login
, $this->preferences
);
107 global $zdb, $login, $hist, $i18n; // globals :(
109 $login = $this->login
;
110 $hist = $this->history
;
113 $status = new \Galette\Entity\
Status($this->zdb
);
114 if (count($status->getList()) === 0) {
115 $res = $status->installInit();
116 $this->boolean($res)->isTrue();
119 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
121 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
122 $this->members_fields
= $members_fields;
123 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
124 $this->adh
->setDependencies(
126 $this->members_fields
,
134 * @param string $testMethod Calling method
138 public function afterTestMethod($testMethod)
140 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
141 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
142 $this->zdb
->execute($delete);
144 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
145 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
146 $this->zdb
->execute($delete);
148 $delete = $this->zdb
->delete(\Galette\Entity\Reminder
::TABLE
);
149 $this->zdb
->execute($delete);
157 public function testGetList()
160 $ireminders = new \Galette\Repository\
Reminders([\Galette\Entity\Reminder
::IMPENDING
]);
161 $this->array($ireminders->getList($this->zdb
))->isIdenticalTo([]);
164 $lreminders = new \Galette\Repository\
Reminders([\Galette\Entity\Reminder
::LATE
]);
165 $this->array($lreminders->getList($this->zdb
))->isIdenticalTo([]);
168 $reminders = new \Galette\Repository\
Reminders();
169 $this->array($reminders->getList($this->zdb
))->isIdenticalTo([]);
172 $this->createAdherent();
173 $id = current($this->ids
);
175 //create contribution, just about to be impending
176 $now = new \
DateTime();
177 $date_begin = clone $now;
178 $date_begin->sub(new \
DateInterval('P1Y'));
179 $date_begin->add(new \
DateInterval('P1M'));
180 $date_end = clone $date_begin;
181 $date_end->add(new \
DateInterval('P1Y'));
183 $this->createContrib([
185 'id_type_cotis' => 3,
186 'montant_cotis' => '111',
187 'type_paiement_cotis' => '6',
188 'info_cotis' => 'FAKER' . $this->seed
,
189 'date_fin_cotis' => $date_end->format('Y-m-d'),
190 'date_enreg' => $now->format('Y-m-d'),
191 'date_debut_cotis' => $now->format('Y-m-d')
193 $cid = current($this->contribs_ids
);
196 $this->boolean($adh->load($id))->isTrue();
198 //member is up to date, no reminder to send
199 $this->boolean($this->adh
->isUp2Date())->isTrue();
200 $this->array($reminders->getList($this->zdb
))->isIdenticalTo([]);
202 //create contribution, just impending
203 $date_begin = clone $now;
204 $date_begin->sub(new \
DateInterval('P1Y'));
205 $date_begin->add(new \
DateInterval('P1M'));
206 $date_begin->sub(new \
DateInterval('P1D'));
207 $date_end = clone $date_begin;
208 $date_end->add(new \
DateInterval('P1Y'));
210 $this->createContrib([
212 'id_type_cotis' => 3,
213 'montant_cotis' => '111',
214 'type_paiement_cotis' => '6',
215 'info_cotis' => 'FAKER' . $this->seed
,
216 'date_fin_cotis' => $date_end->format('Y-m-d'),
217 'date_enreg' => $now->format('Y-m-d'),
218 'date_debut_cotis' => $now->format('Y-m-d')
220 $cid = current($this->contribs_ids
);
223 $this->boolean($adh->load($id))->isTrue();
225 //member is up to date, there is one impending reminder to send
226 $this->boolean($this->adh
->isUp2Date())->isTrue();
227 $this->array($reminders->getList($this->zdb
))->hasSize(1);
228 $this->array($lreminders->getList($this->zdb
))->hasSize(0);
229 $this->array($ireminders->getList($this->zdb
))->hasSize(1);
231 //create contribution, just impending less than 7 days
232 $date_begin = clone $now;
233 $date_begin->sub(new \
DateInterval('P1Y'));
234 $date_begin->add(new \
DateInterval('P4D'));
235 $date_end = clone $date_begin;
236 $date_end->add(new \
DateInterval('P1Y'));
238 $this->createContrib([
240 'id_type_cotis' => 3,
241 'montant_cotis' => '111',
242 'type_paiement_cotis' => '6',
243 'info_cotis' => 'FAKER' . $this->seed
,
244 'date_fin_cotis' => $date_end->format('Y-m-d'),
245 'date_enreg' => $now->format('Y-m-d'),
246 'date_debut_cotis' => $now->format('Y-m-d')
248 $cid = current($this->contribs_ids
);
251 $this->boolean($adh->load($id))->isTrue();
253 //member is up to date, there is one impending reminder to send
254 $this->boolean($this->adh
->isUp2Date())->isTrue();
255 $this->array($reminders->getList($this->zdb
))->hasSize(1);
256 $this->array($lreminders->getList($this->zdb
))->hasSize(0);
257 $this->array($ireminders->getList($this->zdb
))->hasSize(1);
259 //add a sent first impending reminder
261 $send->sub(new \
DateInterval('P1M'));
263 'reminder_type' => \Galette\Entity\Reminder
::IMPENDING
,
264 'reminder_dest' => $id,
265 'reminder_date' => $send->format('Y-m-d'),
266 'reminder_success' => true,
267 'reminder_nomail' => ($this->zdb
->isPostgres() ?
'false' : 0)
270 $insert = $this->zdb
->insert(\Galette\Entity\Reminder
::TABLE
);
271 $insert->values($data);
273 $add = $this->zdb
->execute($insert);
274 $this->integer($add->count())->isGreaterThan(0);
276 //there is still one reminder to send
277 $this->boolean($this->adh
->isUp2Date())->isTrue();
278 $this->array($reminders->getList($this->zdb
))->hasSize(1);
279 $this->array($lreminders->getList($this->zdb
))->hasSize(0);
280 $this->array($ireminders->getList($this->zdb
))->hasSize(1);
282 //add a sent second impending reminder, yesterday
284 $send->sub(new \
DateInterval('P1D'));
286 'reminder_type' => \Galette\Entity\Reminder
::IMPENDING
,
287 'reminder_dest' => $id,
288 'reminder_date' => $send->format('Y-m-d'),
289 'reminder_success' => true,
290 'reminder_nomail' => ($this->zdb
->isPostgres() ?
'false' : 0)
293 $insert = $this->zdb
->insert(\Galette\Entity\Reminder
::TABLE
);
294 $insert->values($data);
296 $add = $this->zdb
->execute($insert);
297 $this->integer($add->count())->isGreaterThan(0);
300 $this->boolean($this->adh
->isUp2Date())->isTrue();
301 $this->array($reminders->getList($this->zdb
))->hasSize(0);
302 $this->array($lreminders->getList($this->zdb
))->hasSize(0);
303 $this->array($ireminders->getList($this->zdb
))->hasSize(0);
305 //create contribution, expiration day
306 $now = new \
DateTime();
307 $date_begin = clone $now;
308 $date_begin->sub(new \
DateInterval('P1Y'));
309 $date_end = clone $date_begin;
310 $date_end->add(new \
DateInterval('P1Y'));
312 $this->createContrib([
314 'id_type_cotis' => 3,
315 'montant_cotis' => '111',
316 'type_paiement_cotis' => '6',
317 'info_cotis' => 'FAKER' . $this->seed
,
318 'date_fin_cotis' => $date_end->format('Y-m-d'),
319 'date_enreg' => $now->format('Y-m-d'),
320 'date_debut_cotis' => $now->format('Y-m-d')
322 $cid = current($this->contribs_ids
);
325 $this->boolean($adh->load($id))->isTrue();
327 //member is up to date, no reminder to send
328 $this->boolean($this->adh
->isUp2Date())->isTrue();
329 $this->array($reminders->getList($this->zdb
))->isIdenticalTo([]);
331 //create contribution, just late
332 $now = new \
DateTime();
333 $date_begin = clone $now;
334 $date_begin->sub(new \
DateInterval('P1Y'));
335 $date_begin->sub(new \
DateInterval('P1D'));
336 $date_end = clone $date_begin;
337 $date_end->add(new \
DateInterval('P1Y'));
339 $this->createContrib([
341 'id_type_cotis' => 3,
342 'montant_cotis' => '111',
343 'type_paiement_cotis' => '6',
344 'info_cotis' => 'FAKER' . $this->seed
,
345 'date_fin_cotis' => $date_end->format('Y-m-d'),
346 'date_enreg' => $now->format('Y-m-d'),
347 'date_debut_cotis' => $now->format('Y-m-d')
349 $cid = current($this->contribs_ids
);
352 $this->boolean($adh->load($id))->isTrue();
354 //member is not up to date, but less than one month, no reminder to send
355 $this->boolean($this->adh
->isUp2Date())->isFalse();
356 $this->array($reminders->getList($this->zdb
))->hasSize(0);
357 $this->array($lreminders->getList($this->zdb
))->hasSize(0);
358 $this->array($ireminders->getList($this->zdb
))->hasSize(0);
360 //create contribution, late by 1 month minus 1 day
361 $now = new \
DateTime();
362 $date_begin = clone $now;
363 $date_begin->sub(new \
DateInterval('P1Y'));
364 $date_begin->sub(new \
DateInterval('P1M'));
365 $date_begin->add(new \
DateInterval('P1D'));
366 $date_end = clone $date_begin;
367 $date_end->add(new \
DateInterval('P1Y'));
369 $this->createContrib([
371 'id_type_cotis' => 3,
372 'montant_cotis' => '111',
373 'type_paiement_cotis' => '6',
374 'info_cotis' => 'FAKER' . $this->seed
,
375 'date_fin_cotis' => $date_end->format('Y-m-d'),
376 'date_enreg' => $now->format('Y-m-d'),
377 'date_debut_cotis' => $now->format('Y-m-d')
379 $cid = current($this->contribs_ids
);
382 $this->boolean($adh->load($id))->isTrue();
384 //member is not up to date, but less than one month, no reminder to send
385 $this->boolean($this->adh
->isUp2Date())->isFalse();
386 $this->array($reminders->getList($this->zdb
))->hasSize(0);
387 $this->array($lreminders->getList($this->zdb
))->hasSize(0);
388 $this->array($ireminders->getList($this->zdb
))->hasSize(0);
390 //create contribution, late by 31 days
391 $now = new \
DateTime();
392 $date_begin = clone $now;
393 $date_begin->sub(new \
DateInterval('P1Y'));
394 $date_begin->sub(new \
DateInterval('P31D'));
395 $date_end = clone $date_begin;
396 $date_end->add(new \
DateInterval('P1Y'));
398 $this->createContrib([
400 'id_type_cotis' => 3,
401 'montant_cotis' => '111',
402 'type_paiement_cotis' => '6',
403 'info_cotis' => 'FAKER' . $this->seed
,
404 'date_fin_cotis' => $date_end->format('Y-m-d'),
405 'date_enreg' => $now->format('Y-m-d'),
406 'date_debut_cotis' => $now->format('Y-m-d')
408 $cid = current($this->contribs_ids
);
411 $this->boolean($adh->load($id))->isTrue();
413 //member is not up to date for one month, one late reminder to send
414 $this->boolean($this->adh
->isUp2Date())->isFalse();
415 $this->array($reminders->getList($this->zdb
))->hasSize(1);
416 $this->array($lreminders->getList($this->zdb
))->hasSize(1);
417 $this->array($ireminders->getList($this->zdb
))->hasSize(0);
419 //create contribution, late by 40 days
420 $now = new \
DateTime();
421 $date_begin = clone $now;
422 $date_begin->sub(new \
DateInterval('P1Y'));
423 $date_begin->sub(new \
DateInterval('P40D'));
424 $date_end = clone $date_begin;
425 $date_end->add(new \
DateInterval('P1Y'));
427 $this->createContrib([
429 'id_type_cotis' => 3,
430 'montant_cotis' => '111',
431 'type_paiement_cotis' => '6',
432 'info_cotis' => 'FAKER' . $this->seed
,
433 'date_fin_cotis' => $date_end->format('Y-m-d'),
434 'date_enreg' => $now->format('Y-m-d'),
435 'date_debut_cotis' => $now->format('Y-m-d')
437 $cid = current($this->contribs_ids
);
440 $this->boolean($adh->load($id))->isTrue();
442 //member is not up to date for one month, one late reminder to send
443 $this->boolean($this->adh
->isUp2Date())->isFalse();
444 $this->array($reminders->getList($this->zdb
))->hasSize(1);
445 $this->array($lreminders->getList($this->zdb
))->hasSize(1);
446 $this->array($ireminders->getList($this->zdb
))->hasSize(0);
448 //add a sent late reminder, at it should have been
450 $send->sub(new \
DateInterval('P5D'));
452 'reminder_type' => \Galette\Entity\Reminder
::LATE
,
453 'reminder_dest' => $id,
454 'reminder_date' => $send->format('Y-m-d'),
455 'reminder_success' => true,
456 'reminder_nomail' => ($this->zdb
->isPostgres() ?
'false' : 0)
459 $insert = $this->zdb
->insert(\Galette\Entity\Reminder
::TABLE
);
460 $insert->values($data);
462 $add = $this->zdb
->execute($insert);
463 $this->integer($add->count())->isGreaterThan(0);
466 $this->boolean($this->adh
->isUp2Date())->isFalse();
467 $this->array($reminders->getList($this->zdb
))->hasSize(0);
468 $this->array($lreminders->getList($this->zdb
))->hasSize(0);
469 $this->array($ireminders->getList($this->zdb
))->hasSize(0);
471 //create contribution, late by 2 months
472 $now = new \
DateTime();
473 $date_begin = clone $now;
474 $date_begin->sub(new \
DateInterval('P1Y'));
475 $date_begin->sub(new \
DateInterval('P2M'));
476 $date_end = clone $date_begin;
477 $date_end->add(new \
DateInterval('P1Y'));
479 $this->createContrib([
481 'id_type_cotis' => 3,
482 'montant_cotis' => '111',
483 'type_paiement_cotis' => '6',
484 'info_cotis' => 'FAKER' . $this->seed
,
485 'date_fin_cotis' => $date_end->format('Y-m-d'),
486 'date_enreg' => $now->format('Y-m-d'),
487 'date_debut_cotis' => $now->format('Y-m-d')
489 $cid = current($this->contribs_ids
);
492 $this->boolean($adh->load($id))->isTrue();
494 //member is not up to date for one month, one late reminder to send
495 $this->boolean($this->adh
->isUp2Date())->isFalse();
496 $this->array($reminders->getList($this->zdb
))->hasSize(1);
497 $this->array($lreminders->getList($this->zdb
))->hasSize(1);
498 $this->array($ireminders->getList($this->zdb
))->hasSize(0);
502 * Create test user in database
506 private function createAdherent()
508 $fakedata = new \Galette\Util\
FakeData($this->zdb
, $this->i18n
);
510 ->setSeed($this->seed
)
513 $this->members_fields
,
518 $data = $fakedata->fakeMember();
519 $this->createMember($data);
520 $this->checkMemberExpected();
524 * Create member from data
526 * @param array $data Data to use to create member
528 * @return \Galette\Entity\Adherent
530 public function createMember(array $data)
533 $check = $adh->check($data, [], []);
534 if (is_array($check)) {
537 $this->boolean($check)->isTrue();
539 $store = $adh->store();
540 $this->boolean($store)->isTrue();
542 $this->ids
[] = $adh->id
;
546 * Check members expecteds
548 * @param Adherent $adh Member instance, if any
549 * @param array $new_expecteds Changes on expected values
553 private function checkMemberExpected($adh = null, $new_expecteds = [])
560 'nom_adh' => 'Hoarau',
561 'prenom_adh' => 'Lucas',
562 'ville_adh' => 'Reynaudnec',
564 'adresse_adh' => '2, boulevard Legros',
565 'email_adh' => 'phoarau@tele2.fr',
566 'login_adh' => 'nathalie51',
567 'mdp_adh' => 'T.u!IbKOi|06',
568 'bool_admin_adh' => false,
569 'bool_exempt_adh' => false,
570 'bool_display_info' => false,
572 'prof_adh' => 'Extraction',
574 'ddn_adh' => '1992-02-22',
575 'lieu_naissance' => 'Fischer',
576 'pseudo_adh' => 'vallet.camille',
578 'tel_adh' => '05 59 53 59 43',
579 'url_adh' => 'http://bodin.net/omnis-ratione-sint-dolorem-architecto',
580 'activite_adh' => true,
583 'fingerprint' => 'FAKER' . $this->seed
,
584 'societe_adh' => 'Philippe'
586 $expecteds = array_merge($expecteds, $new_expecteds);
588 foreach ($expecteds as $key => $value) {
589 $property = $this->members_fields
[$key]['propname'];
591 case 'bool_admin_adh':
592 $this->boolean($adh->isAdmin())->isIdenticalTo($value);
594 case 'bool_exempt_adh':
595 $this->boolean($adh->isDueFree())->isIdenticalTo($value);
597 case 'bool_display_info':
598 $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
601 $this->boolean($adh->isActive())->isIdenticalTo($value);
604 $pw_checked = password_verify($value, $adh->password
);
605 $this->boolean($pw_checked)->isTrue();
608 //rely on age, not on birthdate
609 $this->variable($adh->$property)->isNotNull();
610 $this->string($adh->getAge())->isIdenticalTo(' (28 years old)');
613 $this->variable($adh->$property)->isIdenticalTo(
615 "$property expected {$value} got {$adh->$property}"
621 $d = \DateTime
::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
623 $expected_str = ' (28 years old)';
624 $this->string($adh->getAge())->isIdenticalTo($expected_str);
625 $this->boolean($adh->hasChildren())->isFalse();
626 $this->boolean($adh->hasParent())->isFalse();
627 $this->boolean($adh->hasPicture())->isFalse();
629 $this->string($adh->sadmin
)->isIdenticalTo('No');
630 $this->string($adh->sdue_free
)->isIdenticalTo('No');
631 $this->string($adh->sappears_in_list
)->isIdenticalTo('No');
632 $this->string($adh->sstaff
)->isIdenticalTo('No');
633 $this->string($adh->sactive
)->isIdenticalTo('Active');
634 $this->variable($adh->stitle
)->isNull();
635 $this->string($adh->sstatus
)->isIdenticalTo('Non-member');
636 $this->string($adh->sfullname
)->isIdenticalTo('HOARAU Lucas');
637 $this->string($adh->saddress
)->isIdenticalTo('2, boulevard Legros');
638 $this->string($adh->sname
)->isIdenticalTo('HOARAU Lucas');
640 $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
641 $this->string($adh->getAddressContinuation())->isEmpty();
642 $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
643 $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
644 $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
646 $this->string($adh::getSName($this->zdb
, $adh->id
))->isIdenticalTo('HOARAU Lucas');
647 $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
651 * Create contribution from data
653 * @param array $data Data to use to create contribution
655 * @return \Galette\Entity\Contribution
657 public function createContrib(array $data)
659 $contrib = $this->contrib
;
660 $check = $contrib->check($data, [], []);
661 if (is_array($check)) {
664 $this->boolean($check)->isTrue();
666 $store = $contrib->store();
667 $this->boolean($store)->isTrue($store);
669 $this->contribs_ids
[] = (int)$contrib->id
;