]> git.agnieray.net Git - galette.git/blob - tests/Galette/Repository/tests/units/Reminders.php
aacd78b8087cc93492c057b6673d551e4fbd142e
[galette.git] / tests / Galette / Repository / tests / units / Reminders.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Reminders repository tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020-2023 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 Repository
28 * @package GaletteTests
29 *
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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2020-09-14
36 */
37
38 namespace Galette\Repository\test\units;
39
40 use PHPUnit\Framework\TestCase;
41 use Galette\GaletteTestCase;
42
43 /**
44 * Reminders repository tests
45 *
46 * @category Repository
47 * @name Remoinders
48 * @package GaletteTests
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2020-2023 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since 2020-09-14
54 */
55 class Reminders extends GaletteTestCase
56 {
57 protected int $seed = 95842355;
58 private array $ids = [];
59
60 /**
61 * Set up tests
62 *
63 * @return void
64 */
65 public function setUp(): void
66 {
67 parent::setUp();
68 $this->initStatus();
69 $this->initContributionsTypes();
70
71 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
72
73 $this->adh = new \Galette\Entity\Adherent($this->zdb);
74 $this->adh->setDependencies(
75 $this->preferences,
76 $this->members_fields,
77 $this->history
78 );
79 }
80
81 /**
82 * Tear down tests
83 *
84 * @return void
85 */
86 public function tearDown(): void
87 {
88 parent::tearDown();
89 $this->cleanContributions();
90
91 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
92 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
93 $this->zdb->execute($delete);
94
95 $delete = $this->zdb->delete(\Galette\Entity\Reminder::TABLE);
96 $this->zdb->execute($delete);
97 }
98
99 /**
100 * Clean created contributions
101 *
102 * @return void
103 */
104 private function cleanContributions(): void
105 {
106 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
107 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
108 $this->zdb->execute($delete);
109 }
110
111 /**
112 * Test getList
113 *
114 * @return void
115 */
116 public function testGetList()
117 {
118 //impendings
119 $ireminders = new \Galette\Repository\Reminders([\Galette\Entity\Reminder::IMPENDING]);
120 $this->assertSame([], $ireminders->getList($this->zdb));
121
122 //lates
123 $lreminders = new \Galette\Repository\Reminders([\Galette\Entity\Reminder::LATE]);
124 $this->assertSame([], $lreminders->getList($this->zdb));
125
126 //all
127 $reminders = new \Galette\Repository\Reminders();
128 $this->assertSame([], $reminders->getList($this->zdb));
129
130 //create member
131 $this->getMemberTwo();
132 $id = $this->adh->id;
133
134 //create a contribution, just before being a close to be expired contribution
135 $now = new \DateTime();
136 $due_date = clone $now;
137 $due_date->add(new \DateInterval('P30D'));
138 $due_date->add(new \DateInterval('P1D'));
139 $begin_date = clone $due_date;
140 $begin_date->add(new \DateInterval('P1D'));
141 $begin_date->sub(new \DateInterval('P1Y'));
142
143 $this->createContrib([
144 'id_adh' => $id,
145 'id_type_cotis' => 3,
146 'montant_cotis' => '111',
147 'type_paiement_cotis' => '6',
148 'info_cotis' => 'FAKER' . $this->seed,
149 'date_fin_cotis' => $due_date->format('Y-m-d'),
150 'date_enreg' => $begin_date->format('Y-m-d'),
151 'date_debut_cotis' => $begin_date->format('Y-m-d')
152 ]);
153
154 $adh = $this->adh;
155 $this->assertTrue($adh->load($id));
156
157 //member is up to date, but not yet close to be expired, no reminder to send
158 $this->assertTrue($this->adh->isUp2Date());
159 $this->assertCount(0, $reminders->getList($this->zdb));
160 $this->assertCount(0, $lreminders->getList($this->zdb));
161 $this->assertCount(0, $ireminders->getList($this->zdb));
162
163
164 //create a close to be expired contribution
165 $due_date = clone $now;
166 $due_date->add(new \DateInterval('P30D'));
167 $begin_date = clone $due_date;
168 $begin_date->add(new \DateInterval('P1D'));
169 $begin_date->sub(new \DateInterval('P1Y'));
170
171 $this->cleanContributions();
172 $this->createContrib([
173 'id_adh' => $id,
174 'id_type_cotis' => 3,
175 'montant_cotis' => '111',
176 'type_paiement_cotis' => '6',
177 'info_cotis' => 'FAKER' . $this->seed,
178 'date_fin_cotis' => $due_date->format('Y-m-d'),
179 'date_enreg' => $begin_date->format('Y-m-d'),
180 'date_debut_cotis' => $begin_date->format('Y-m-d')
181 ]);
182
183 $adh = $this->adh;
184 $this->assertTrue($adh->load($id));
185
186 //member is up-to-date, and close to be expired, one impending reminder to send
187 $this->assertTrue($this->adh->isUp2Date());
188 $this->assertCount(1, $reminders->getList($this->zdb));
189 $this->assertCount(0, $lreminders->getList($this->zdb));
190 $this->assertCount(1, $ireminders->getList($this->zdb));
191
192
193 //create a close to be expired contribution, 7 days before expiration
194 $due_date = clone $now;
195 $due_date->add(new \DateInterval('P7D'));
196 $begin_date = clone $due_date;
197 $begin_date->add(new \DateInterval('P1D'));
198 $begin_date->sub(new \DateInterval('P1Y'));
199
200 $this->cleanContributions();
201 $this->createContrib([
202 'id_adh' => $id,
203 'id_type_cotis' => 3,
204 'montant_cotis' => '111',
205 'type_paiement_cotis' => '6',
206 'info_cotis' => 'FAKER' . $this->seed,
207 'date_fin_cotis' => $due_date->format('Y-m-d'),
208 'date_enreg' => $begin_date->format('Y-m-d'),
209 'date_debut_cotis' => $begin_date->format('Y-m-d')
210 ]);
211
212 $adh = $this->adh;
213 $this->assertTrue($adh->load($id));
214
215 //member is up to date, and close to be expired, one impending reminder to send
216 $this->assertTrue($this->adh->isUp2Date());
217 $this->assertCount(1, $reminders->getList($this->zdb));
218 $this->assertCount(0, $lreminders->getList($this->zdb));
219 $this->assertCount(1, $ireminders->getList($this->zdb));
220
221 //create a close to be expired contribution, the last day before expiration
222 $due_date = clone $now;
223 $begin_date = clone $due_date;
224 $begin_date->add(new \DateInterval('P1D'));
225 $begin_date->sub(new \DateInterval('P1Y'));
226
227 $this->cleanContributions();
228 $this->createContrib([
229 'id_adh' => $id,
230 'id_type_cotis' => 3,
231 'montant_cotis' => '111',
232 'type_paiement_cotis' => '6',
233 'info_cotis' => 'FAKER' . $this->seed,
234 'date_fin_cotis' => $due_date->format('Y-m-d'),
235 'date_enreg' => $begin_date->format('Y-m-d'),
236 'date_debut_cotis' => $begin_date->format('Y-m-d')
237 ]);
238
239 $adh = $this->adh;
240 $this->assertTrue($adh->load($id));
241
242 //member is up-to-date, and close to be expired, one impending reminder to send
243 $this->assertTrue($this->adh->isUp2Date());
244 $this->assertCount(1, $reminders->getList($this->zdb));
245 $this->assertCount(0, $lreminders->getList($this->zdb));
246 $this->assertCount(1, $ireminders->getList($this->zdb));
247
248 //add a first close to be expired contribution reminder
249 $send = new \DateTime();
250 $send->sub(new \DateInterval('P30D'));
251 $data = array(
252 'reminder_type' => \Galette\Entity\Reminder::IMPENDING,
253 'reminder_dest' => $id,
254 'reminder_date' => $send->format('Y-m-d'),
255 'reminder_success' => true,
256 'reminder_nomail' => ($this->zdb->isPostgres() ? 'false' : 0)
257 );
258
259 $insert = $this->zdb->insert(\Galette\Entity\Reminder::TABLE);
260 $insert->values($data);
261
262 $add = $this->zdb->execute($insert);
263 $this->assertGreaterThan(0, $add->count());
264
265 //there is still one impending reminder to send
266 $this->assertTrue($this->adh->isUp2Date());
267 $this->assertCount(1, $reminders->getList($this->zdb));
268 $this->assertCount(0, $lreminders->getList($this->zdb));
269 $this->assertCount(1, $ireminders->getList($this->zdb));
270
271 //add a second close to be expired contribution reminder, yesterday
272 $send = new \DateTime();
273 $send->sub(new \DateInterval('P1D'));
274 $data = array(
275 'reminder_type' => \Galette\Entity\Reminder::IMPENDING,
276 'reminder_dest' => $id,
277 'reminder_date' => $send->format('Y-m-d'),
278 'reminder_success' => true,
279 'reminder_nomail' => ($this->zdb->isPostgres() ? 'false' : 0)
280 );
281
282 $insert = $this->zdb->insert(\Galette\Entity\Reminder::TABLE);
283 $insert->values($data);
284
285 $add = $this->zdb->execute($insert);
286 $this->assertGreaterThan(0, $add->count());
287
288 //nothing to send!
289 $this->assertTrue($this->adh->isUp2Date());
290 $this->assertCount(0, $reminders->getList($this->zdb));
291 $this->assertCount(0, $lreminders->getList($this->zdb));
292 $this->assertCount(0, $ireminders->getList($this->zdb));
293
294 //create an expired contribution, today
295 $due_date = clone $now;
296 $due_date->sub(new \DateInterval('P1D'));
297 $begin_date = clone $due_date;
298 $begin_date->add(new \DateInterval('P1D'));
299 $begin_date->sub(new \DateInterval('P1Y'));
300
301 $this->cleanContributions();
302 $this->createContrib([
303 'id_adh' => $id,
304 'id_type_cotis' => 3,
305 'montant_cotis' => '111',
306 'type_paiement_cotis' => '6',
307 'info_cotis' => 'FAKER' . $this->seed,
308 'date_fin_cotis' => $due_date->format('Y-m-d'),
309 'date_enreg' => $begin_date->format('Y-m-d'),
310 'date_debut_cotis' => $begin_date->format('Y-m-d')
311 ]);
312
313 $adh = $this->adh;
314 $this->assertTrue($adh->load($id));
315
316 //member late, but for less than 30 days, no reminder to send
317 $this->assertFalse($this->adh->isUp2Date());
318 $this->assertCount(0, $reminders->getList($this->zdb));
319 $this->assertCount(0, $lreminders->getList($this->zdb));
320 $this->assertCount(0, $ireminders->getList($this->zdb));
321
322 //create an expired contribution, 29 days ago
323 $due_date = clone $now;
324 $due_date->sub(new \DateInterval('P29D'));
325 $begin_date = clone $due_date;
326 $begin_date->add(new \DateInterval('P1D'));
327 $begin_date->sub(new \DateInterval('P1Y'));
328
329 $this->cleanContributions();
330 $this->createContrib([
331 'id_adh' => $id,
332 'id_type_cotis' => 3,
333 'montant_cotis' => '111',
334 'type_paiement_cotis' => '6',
335 'info_cotis' => 'FAKER' . $this->seed,
336 'date_fin_cotis' => $due_date->format('Y-m-d'),
337 'date_enreg' => $begin_date->format('Y-m-d'),
338 'date_debut_cotis' => $begin_date->format('Y-m-d')
339 ]);
340
341 $adh = $this->adh;
342 $this->assertTrue($adh->load($id));
343
344 //member is late, but for less than 30 days, no reminder to send
345 $this->assertFalse($this->adh->isUp2Date());
346 $this->assertCount(0, $reminders->getList($this->zdb));
347 $this->assertCount(0, $lreminders->getList($this->zdb));
348 $this->assertCount(0, $ireminders->getList($this->zdb));
349
350 //create an expired contribution, late by 30 days
351 $due_date = clone $now;
352 $due_date->sub(new \DateInterval('P30D'));
353 $begin_date = clone $due_date;
354 $begin_date->add(new \DateInterval('P1D'));
355 $begin_date->sub(new \DateInterval('P1Y'));
356
357 $this->cleanContributions();
358 $this->createContrib([
359 'id_adh' => $id,
360 'id_type_cotis' => 3,
361 'montant_cotis' => '111',
362 'type_paiement_cotis' => '6',
363 'info_cotis' => 'FAKER' . $this->seed,
364 'date_fin_cotis' => $due_date->format('Y-m-d'),
365 'date_enreg' => $begin_date->format('Y-m-d'),
366 'date_debut_cotis' => $begin_date->format('Y-m-d')
367 ]);
368
369 $adh = $this->adh;
370 $this->assertTrue($adh->load($id));
371
372 //member is late, one late reminder to send
373 $this->assertFalse($this->adh->isUp2Date());
374 $this->assertCount(1, $reminders->getList($this->zdb));
375 $this->assertCount(1, $lreminders->getList($this->zdb));
376 $this->assertCount(0, $ireminders->getList($this->zdb));
377
378 //create an expired contribution, late by 40 days
379 $due_date = clone $now;
380 $due_date->sub(new \DateInterval('P40D'));
381 $begin_date = clone $due_date;
382 $begin_date->add(new \DateInterval('P1D'));
383 $begin_date->sub(new \DateInterval('P1Y'));
384
385 $this->cleanContributions();
386 $this->createContrib([
387 'id_adh' => $id,
388 'id_type_cotis' => 3,
389 'montant_cotis' => '111',
390 'type_paiement_cotis' => '6',
391 'info_cotis' => 'FAKER' . $this->seed,
392 'date_fin_cotis' => $due_date->format('Y-m-d'),
393 'date_enreg' => $begin_date->format('Y-m-d'),
394 'date_debut_cotis' => $begin_date->format('Y-m-d')
395 ]);
396
397 $adh = $this->adh;
398 $this->assertTrue($adh->load($id));
399
400 //member is late, one late reminder to send
401 $this->assertFalse($this->adh->isUp2Date());
402 $this->assertCount(1, $reminders->getList($this->zdb));
403 $this->assertCount(1, $lreminders->getList($this->zdb));
404 $this->assertCount(0, $ireminders->getList($this->zdb));
405
406 //add a sent late reminder, as it should have been
407 $send = clone $now;
408 $send->sub(new \DateInterval('P5D'));
409 $data = array(
410 'reminder_type' => \Galette\Entity\Reminder::LATE,
411 'reminder_dest' => $id,
412 'reminder_date' => $send->format('Y-m-d'),
413 'reminder_success' => true,
414 'reminder_nomail' => ($this->zdb->isPostgres() ? 'false' : 0)
415 );
416
417 $insert = $this->zdb->insert(\Galette\Entity\Reminder::TABLE);
418 $insert->values($data);
419
420 $add = $this->zdb->execute($insert);
421 $this->assertGreaterThan(0, $add->count());
422
423 //nothing to send!
424 $this->assertFalse($this->adh->isUp2Date());
425 $this->assertCount(0, $reminders->getList($this->zdb));
426 $this->assertCount(0, $lreminders->getList($this->zdb));
427 $this->assertCount(0, $ireminders->getList($this->zdb));
428
429 //create an expired contribution, 60 days ago
430 $due_date = clone $now;
431 $due_date->sub(new \DateInterval('P60D'));
432 $begin_date = clone $due_date;
433 $begin_date->add(new \DateInterval('P1D'));
434 $begin_date->sub(new \DateInterval('P1Y'));
435
436 $this->cleanContributions();
437 $this->createContrib([
438 'id_adh' => $id,
439 'id_type_cotis' => 3,
440 'montant_cotis' => '111',
441 'type_paiement_cotis' => '6',
442 'info_cotis' => 'FAKER' . $this->seed,
443 'date_fin_cotis' => $due_date->format('Y-m-d'),
444 'date_enreg' => $begin_date->format('Y-m-d'),
445 'date_debut_cotis' => $begin_date->format('Y-m-d')
446 ]);
447
448 $adh = $this->adh;
449 $this->assertTrue($adh->load($id));
450
451 //member has been late for two months, one late reminder to send
452 $this->assertFalse($this->adh->isUp2Date());
453 $this->assertCount(1, $reminders->getList($this->zdb));
454 $this->assertCount(1, $lreminders->getList($this->zdb));
455 $this->assertCount(0, $ireminders->getList($this->zdb));
456 }
457 }