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