]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Contribution.php
6a368086d1c6028564fa65f107d747f2bc7791b3
[galette.git] / tests / Galette / Entity / tests / units / Contribution.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\Entity\test\units;
23
24 use Galette\GaletteTestCase;
25
26 /**
27 * Contribution tests class
28 *
29 * @author Johan Cwiklinski <johan@x-tnd.be>
30 */
31 class Contribution extends GaletteTestCase
32 {
33 protected int $seed = 95842354;
34
35 /**
36 * Cleanup after each test method
37 *
38 * @return void
39 */
40 public function tearDown(): void
41 {
42 parent::tearDown();
43
44 $this->zdb = new \Galette\Core\Db();
45 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
46 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
47 $this->zdb->execute($delete);
48
49 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
50 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
51 $delete->where('parent_id IS NOT NULL');
52 $this->zdb->execute($delete);
53
54 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
55 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
56 $this->zdb->execute($delete);
57 }
58
59 /**
60 * Set up tests
61 *
62 * @return void
63 */
64 public function setUp(): void
65 {
66 parent::setUp();
67 $this->initContributionsTypes();
68
69 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
70
71 $this->adh = new \Galette\Entity\Adherent($this->zdb);
72 $this->adh->setDependencies(
73 $this->preferences,
74 $this->members_fields,
75 $this->history
76 );
77 }
78
79 /**
80 * Test empty contribution
81 *
82 * @return void
83 */
84 public function testEmpty()
85 {
86 $contrib = $this->contrib;
87 $this->assertNull($contrib->id);
88 $this->assertNull($contrib->date);
89 $this->assertNull($contrib->begin_date);
90 $this->assertNull($contrib->end_date);
91 $this->assertNull($contrib->raw_date);
92 $this->assertNull($contrib->raw_begin_date);
93 $this->assertNull($contrib->raw_end_date);
94 $this->assertEmpty($contrib->duration);
95 $this->assertSame((int)$this->preferences->pref_default_paymenttype, $contrib->payment_type);
96 $this->assertSame('Check', $contrib->getPaymentType());
97 $this->assertNull($contrib->model);
98 $this->assertNull($contrib->member);
99 $this->assertNull($contrib->type);
100 $this->assertNull($contrib->amount);
101 $this->assertNull($contrib->orig_amount);
102 $this->assertNull($contrib->info);
103 $this->assertNull($contrib->transaction);
104 $this->assertCount(11, $contrib->fields);
105 $this->assertTrue(isset($contrib->fields[\Galette\Entity\Contribution::PK]));
106 $this->assertTrue(isset($contrib->fields[\Galette\Entity\Adherent::PK]));
107 $this->assertTrue(isset($contrib->fields[\Galette\Entity\ContributionsTypes::PK]));
108 $this->assertTrue(isset($contrib->fields['montant_cotis']));
109 $this->assertTrue(isset($contrib->fields['type_paiement_cotis']));
110 $this->assertTrue(isset($contrib->fields['info_cotis']));
111 $this->assertTrue(isset($contrib->fields['date_debut_cotis']));
112
113 $this->assertSame('cotis-give', $contrib->getRowClass());
114 $this->assertNull($contrib::getDueDate($this->zdb, 1));
115 $this->assertFalse($contrib->isTransactionPart());
116 $this->assertFalse($contrib->isTransactionPartOf(1));
117 $this->assertSame('Check', $contrib->getPaymentType());
118 $this->assertNull($contrib->unknown_property);
119 }
120
121 /**
122 * Test getter and setter special cases
123 *
124 * @return void
125 */
126 public function testGetterSetter()
127 {
128 $contrib = $this->contrib;
129
130 //set a bad date
131 $contrib->begin_date = 'not a date';
132 $this->assertNull($contrib->raw_begin_date);
133 $this->assertNull($contrib->begin_date);
134
135 $contrib->begin_date = '2017-06-17';
136 $this->assertInstanceOf('DateTime', $contrib->raw_begin_date);
137 $this->assertSame('2017-06-17', $contrib->begin_date);
138
139 $contrib->amount = 'not an amount';
140 $this->assertNull($contrib->amount);
141 $contrib->amount = 0;
142 $this->assertNull($contrib->amount);
143 $contrib->amount = 42;
144 $this->assertSame(42.0, $contrib->amount);
145 $contrib->amount = '42';
146 $this->assertSame(42.0, $contrib->amount);
147
148 $contrib->type = 156;
149 $this->assertInstanceOf('\Galette\Entity\ContributionsTypes', $contrib->type);
150 $this->assertFalse($contrib->type->id);
151 $contrib->type = 1;
152 $this->assertInstanceOf('\Galette\Entity\ContributionsTypes', $contrib->type);
153 $this->assertEquals(1, $contrib->type->id);
154
155 $contrib->transaction = 'not a transaction id';
156 $this->assertNull($contrib->transaction);
157 $contrib->transaction = 46;
158 $this->assertInstanceOf('\Galette\Entity\Transaction', $contrib->transaction);
159 $this->assertNull($contrib->transaction->id);
160
161 $contrib->member = 'not a member';
162 $this->assertNull($contrib->member);
163 $contrib->member = 118218;
164 $this->assertSame(118218, $contrib->member);
165
166 $contrib->not_a_property = 'abcde';
167 $this->assertFalse(property_exists($contrib, 'not_a_property'));
168
169 $contrib->payment_type = \Galette\Entity\PaymentType::CASH;
170 $this->assertSame('Cash', $contrib->getPaymentType());
171
172 $contrib->payment_type = \Galette\Entity\PaymentType::CHECK;
173 $this->assertSame('Check', $contrib->getPaymentType());
174
175 $contrib->payment_type = \Galette\Entity\PaymentType::OTHER;
176 $this->assertSame('Other', $contrib->getPaymentType());
177
178 $contrib->payment_type = \Galette\Entity\PaymentType::CREDITCARD;
179 $this->assertSame('Credit card', $contrib->getPaymentType());
180
181 $contrib->payment_type = \Galette\Entity\PaymentType::TRANSFER;
182 $this->assertSame('Transfer', $contrib->getPaymentType());
183
184 $contrib->payment_type = \Galette\Entity\PaymentType::PAYPAL;
185 $this->assertSame('Paypal', $contrib->getPaymentType());
186 }
187
188 /**
189 * Test contribution creation
190 *
191 * @return void
192 */
193 public function testCreation()
194 {
195 $this->getMemberOne();
196 //create contribution for member
197 $this->createContribution();
198 }
199
200 /**
201 * Test donation update
202 *
203 * @return void
204 */
205 public function testDonationUpdate()
206 {
207 $this->getMemberOne();
208 //create contribution for member
209 $begin_date = new \DateTime(); // 2020-11-07
210 $begin_date->sub(new \DateInterval('P5M')); // 2020-06-07
211 $begin_date->add(new \DateInterval('P3D')); // 2020-06-10
212
213 $due_date = clone $begin_date;
214 $due_date->add(new \DateInterval('P1Y'));
215 $due_date->sub(new \DateInterval('P1D'));
216
217 $data = [
218 'id_adh' => $this->adh->id,
219 'id_type_cotis' => 4, //donation
220 'montant_cotis' => 12,
221 'type_paiement_cotis' => 3,
222 'info_cotis' => 'FAKER' . $this->seed,
223 'date_enreg' => $begin_date->format('Y-m-d'),
224 'date_debut_cotis' => $begin_date->format('Y-m-d'),
225 'date_fin_cotis' => $due_date->format('Y-m-d'),
226 ];
227 $this->createContrib($data);
228 $this->assertSame(
229 [
230 'id_type_cotis' => 1,
231 'id_adh' => 1,
232 'date_enreg' => 1,
233 'date_debut_cotis' => 1,
234 'date_fin_cotis' => 0,
235 'montant_cotis' => 0
236 ],
237 $this->contrib->getRequired()
238 );
239
240 $this->logSuperAdmin();
241 $data = [
242 'id_adh' => $this->adh->id,
243 'id_type_cotis' => 4, //donation
244 'montant_cotis' => 1280,
245 'type_paiement_cotis' => 4,
246 'info_cotis' => 'FAKER' . $this->seed,
247 'date_enreg' => $begin_date->format('Y-m-d'),
248 'date_debut_cotis' => $begin_date->format('Y-m-d'),
249 'date_fin_cotis' => $due_date->format('Y-m-d'),
250 ];
251 $this->createContrib($data);
252
253 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
254 $this->assertSame(1280.00, $contrib->amount);
255
256 //empty amount
257 $data = [
258 'id_adh' => $this->adh->id,
259 'id_type_cotis' => 4, //donation
260 'montant_cotis' => 0,
261 'type_paiement_cotis' => 4,
262 'info_cotis' => 'FAKER' . $this->seed,
263 'date_enreg' => $begin_date->format('Y-m-d'),
264 'date_debut_cotis' => $begin_date->format('Y-m-d'),
265 'date_fin_cotis' => $due_date->format('Y-m-d'),
266 ];
267 $this->createContrib($data);
268
269 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
270 $this->assertSame(0.00, $contrib->amount);
271 }
272
273 /**
274 * Test contribution update
275 *
276 * @return void
277 */
278 public function testContributionUpdate()
279 {
280 $this->logSuperAdmin();
281
282 $this->getMemberOne();
283 //create contribution for member
284 $begin_date = new \DateTime(); // 2020-11-07
285 $begin_date->sub(new \DateInterval('P5M')); // 2020-06-07
286 $begin_date->add(new \DateInterval('P3D')); // 2020-06-10
287
288 $due_date = clone $begin_date;
289 $due_date->add(new \DateInterval('P1Y'));
290 $due_date->sub(new \DateInterval('P1D'));
291
292 //instanciate contribution as annual fee
293 $this->contrib = new \Galette\Entity\Contribution(
294 $this->zdb,
295 $this->login,
296 [
297 'type' => 1 //annual fee
298 ]
299 );
300 $this->assertSame(
301 [
302 'id_type_cotis' => 1,
303 'id_adh' => 1,
304 'date_enreg' => 1,
305 'date_debut_cotis' => 1,
306 'date_fin_cotis' => 1, //should be 1
307 'montant_cotis' => 1 // should be 1
308 ],
309 $this->contrib->getRequired()
310 );
311
312 $data = [
313 'id_adh' => $this->adh->id,
314 'id_type_cotis' => 1, //annual fee
315 'montant_cotis' => 0,
316 'type_paiement_cotis' => 3,
317 'info_cotis' => 'FAKER' . $this->seed,
318 'date_enreg' => $begin_date->format('Y-m-d'),
319 'date_debut_cotis' => $begin_date->format('Y-m-d'),
320 'date_fin_cotis' => $due_date->format('Y-m-d'),
321 ];
322
323 $this->createContrib($data, $this->contrib);
324
325 $this->assertSame(0.0, $this->contrib->amount);
326 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
327 $this->assertSame(0.0, $contrib->amount);
328
329 //change amount
330 $data['montant_cotis'] = 42;
331 $check = $contrib->check($data, [], []);
332 if (is_array($check)) {
333 var_dump($check);
334 }
335 $this->assertTrue($check);
336
337 $store = $contrib->store();
338 $this->assertTrue($store);
339
340 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
341 $this->assertSame(42.0, $contrib->amount);
342
343 //change amount back to 0
344 $data['montant_cotis'] = 0;
345 $check = $contrib->check($data, [], []);
346 if (is_array($check)) {
347 var_dump($check);
348 }
349 $this->assertTrue($check);
350
351 $store = $contrib->store();
352 $this->assertTrue($store);
353
354 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
355 $this->assertSame(0.0, $contrib->amount);
356 }
357
358 /**
359 * Test end date retrieving
360 * This is based on some Preferences parameters
361 *
362 * @return void
363 */
364 public function testRetrieveEndDate()
365 {
366 global $preferences;
367 $orig_pref_beg_membership = $this->preferences->pref_beg_membership;
368 $orig_pref_membership_ext = $this->preferences->pref_membership_ext;
369 $orig_pref_membership_offermonths = $this->preferences->pref_membership_offermonths;
370
371 $contrib = new \Galette\Entity\Contribution(
372 $this->zdb,
373 $this->login,
374 ['type' => 1] //annual fee
375 );
376
377 // First, check for 12 months renewal
378 $due_date = new \DateTime();
379 $due_date->add(new \DateInterval('P1Y'));
380 $due_date->sub(new \DateInterval('P1D'));
381 $this->assertSame($due_date->format('Y-m-d'), $contrib->end_date);
382
383 //unset pref_beg_membership and pref_membership_ext
384 $preferences->pref_beg_membership = '';
385 $preferences->pref_membership_ext = '';
386
387 $this->expectException('RuntimeException');
388 $this->expectExceptionMessage('Unable to define end date; none of pref_beg_membership nor pref_membership_ext are defined!');
389 $contrib = new \Galette\Entity\Contribution(
390 $this->zdb,
391 $this->login,
392 ['type' => 1] //annual fee
393 );
394
395 // Second, test with beginning of membership date
396 $preferences->pref_beg_membership = '29/05';
397 $due_date = new \DateTime();
398 $due_date->setDate(date('Y'), 5, 28);
399 if ($due_date <= new \DateTime()) {
400 $due_date->add(new \DateInterval('P1Y'));
401 }
402
403 $contrib = new \Galette\Entity\Contribution(
404 $this->zdb,
405 $this->login,
406 ['type' => 1] // annual fee
407 );
408 $this->assertSame($due_date->format('Y-m-d'), $contrib->end_date);
409
410 // Third, test with beginning of membership date and 2 last months offered
411 $begin_date = new \DateTime();
412 $begin_date->add(new \DateInterval('P1M'));
413 $preferences->pref_beg_membership = $begin_date->format('01/m');
414 $preferences->pref_membership_offermonths = 2;
415 $due_date = new \DateTime($begin_date->format('Y-m-01'));
416 $due_date->add(new \DateInterval('P1Y'));
417 $due_date->sub(new \DateInterval('P1D'));
418
419 $contrib = new \Galette\Entity\Contribution(
420 $this->zdb,
421 $this->login,
422 ['type' => 1] // annual fee
423 );
424 $this->assertSame($due_date->format('Y-m-d'), $contrib->end_date);
425
426 //reset
427 $preferences->pref_beg_membership = $orig_pref_beg_membership;
428 $preferences->pref_membership_ext = $orig_pref_membership_ext;
429 $preferences->pref_membership_offermonths = $orig_pref_membership_offermonths;
430 }
431
432 /**
433 * Test checkOverlap method
434 *
435 * @return void
436 */
437 public function testCheckOverlap()
438 {
439 $adh = new \Galette\Entity\Adherent($this->zdb);
440 $adh->setDependencies(
441 $this->preferences,
442 $this->members_fields,
443 $this->history
444 );
445
446 $check = $adh->check(
447 [
448 'nom_adh' => 'Overlapped',
449 'date_crea_adh' => date(_T("Y-m-d")),
450 \Galette\Entity\Status::PK => \Galette\Entity\Status::DEFAULT_STATUS,
451 'fingerprint' => 'FAKER' . $this->seed
452 ],
453 [],
454 []
455 );
456 if (is_array($check)) {
457 var_dump($check);
458 }
459 $this->assertTrue($check);
460
461 $store = $adh->store();
462 $this->assertTrue($store);
463
464 //create first contribution for member
465 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
466
467 $now = new \DateTime();
468 $due_date = clone $now;
469 $due_date->add(new \DateInterval('P1Y'));
470 $due_date->sub(new \DateInterval('P1D'));
471 $data = [
472 \Galette\Entity\Adherent::PK => $adh->id,
473 \Galette\Entity\ContributionsTypes::PK => 1, //annual fee
474 'montant_cotis' => 20,
475 'type_paiement_cotis' => \Galette\Entity\PaymentType::CHECK,
476 'date_enreg' => $now->format(_T("Y-m-d")),
477 'date_debut_cotis' => $now->format(_T("Y-m-d")),
478 'date_fin_cotis' => $due_date->format(_T("Y-m-d")),
479 'info_cotis' => 'FAKER' . $this->seed
480 ];
481
482 $check = $contrib->check($data, [], []);
483 if (is_array($check)) {
484 var_dump($check);
485 }
486 $this->assertTrue($check);
487 $this->assertTrue($contrib->checkOverlap());
488
489 $store = $contrib->store();
490 $this->assertTrue($store);
491
492 //load member from db
493 $adh = new \Galette\Entity\Adherent($this->zdb, $adh->id);
494
495 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
496 $begin_date = clone $due_date;
497 $begin_date->add(new \DateInterval('P1D'));
498 $begin_date->sub(new \DateInterval('P3M'));
499 $due_date = clone $begin_date;
500 $due_date->add(new \DateInterval('P1Y'));
501 $due_date->sub(new \DateInterval('P1D'));
502 $data = [
503 \Galette\Entity\Adherent::PK => $adh->id,
504 \Galette\Entity\ContributionsTypes::PK => 1, //annual fee
505 'montant_cotis' => 20,
506 'type_paiement_cotis' => \Galette\Entity\PaymentType::CHECK,
507 'date_enreg' => $now->format(_T("Y-m-d")),
508 'date_debut_cotis' => $begin_date->format(_T("Y-m-d")),
509 'date_fin_cotis' => $due_date->format(_T("Y-m-d")),
510 'info_cotis' => 'FAKER' . $this->seed
511 ];
512
513 $check = $contrib->check($data, [], []);
514 $this->assertSame(
515 [
516 '- Membership period overlaps period starting at ' . $now->format('Y-m-d')
517 ],
518 $check
519 );
520
521 $this->expectException('RuntimeException');
522 $this->expectExceptionMessage('Existing errors prevents storing contribution');
523 $store = $contrib->store();
524 }
525
526 /**
527 * Test fields labels
528 *
529 * @return void
530 */
531 public function testGetFieldLabel()
532 {
533 $this->assertSame(
534 'Amount',
535 $this->contrib->getFieldLabel('montant_cotis')
536 );
537
538 $this->assertSame(
539 'Date of contribution',
540 $this->contrib->getFieldLabel('date_debut_cotis')
541 );
542
543 $this->contrib->type = 1;
544 $this->assertSame(
545 'Start date of membership',
546 $this->contrib->getFieldLabel('date_debut_cotis')
547 );
548
549 $this->assertSame(
550 'Comments',
551 $this->contrib->getFieldLabel('info_cotis')
552 );
553 }
554
555 /**
556 * Test contribution loading
557 *
558 * @return void
559 */
560 public function testLoad()
561 {
562 $this->login = $this->getMockBuilder(\Galette\Core\Login::class)
563 ->setConstructorArgs(array($this->zdb, $this->i18n))
564 ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin'))
565 ->getMock();
566 $this->login->method('isLogged')->willReturn(true);
567 $this->login->method('isStaff')->willReturn(true);
568 $this->login->method('isAdmin')->willReturn(true);
569
570 $this->getMemberOne();
571
572 //create contribution for member
573 $this->createContribution();
574
575 $id = $this->contrib->id;
576 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
577
578 $this->assertTrue($contrib->load((int)$id));
579 $this->checkContribExpected($contrib);
580
581 $this->assertFalse($contrib->load(1355522012));
582 }
583
584 /**
585 * Test contribution removal
586 *
587 * @return void
588 */
589 public function testRemove()
590 {
591 $this->getMemberOne();
592 $this->createContribution();
593
594 $this->assertTrue($this->contrib->remove());
595 $this->assertFalse($this->contrib->remove());
596 }
597
598 /**
599 * Test can* methods
600 *
601 * @return void
602 */
603 public function testCan()
604 {
605 $this->getMemberOne();
606 //create contribution for member
607 $this->createContribution();
608 $contrib = $this->contrib;
609
610 $this->assertFalse($contrib->canShow($this->login));
611
612 //Superadmin can fully change contributions
613 $this->logSuperAdmin();
614
615 $this->assertTrue($contrib->canShow($this->login));
616
617 //logout
618 $this->login->logOut();
619 $this->assertFalse($this->login->isLogged());
620
621 //Member can fully change its own contributions
622 $mdata = $this->dataAdherentOne();
623 $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh']));
624 $this->assertTrue($this->login->isLogged());
625 $this->assertFalse($this->login->isAdmin());
626 $this->assertFalse($this->login->isStaff());
627
628 $this->assertTrue($contrib->canShow($this->login));
629
630 //logout
631 $this->login->logOut();
632 $this->assertFalse($this->login->isLogged());
633
634 //Another member has no access
635 $this->getMemberTwo();
636 $mdata = $this->dataAdherentTwo();
637 $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh']));
638 $this->assertTrue($this->login->isLogged());
639 $this->assertFalse($this->login->isAdmin());
640 $this->assertFalse($this->login->isStaff());
641
642 $this->assertFalse($contrib->canShow($this->login));
643
644 //parents can chow change children contributions
645 $this->getMemberOne();
646 $member = $this->adh;
647 $mdata = $this->dataAdherentOne();
648 global $login;
649 $login = $this->login;
650 $this->logSuperAdmin();
651
652 $child_data = [
653 'nom_adh' => 'Doe',
654 'prenom_adh' => 'Johny',
655 'parent_id' => $member->id,
656 'attach' => true,
657 'login_adh' => 'child.johny.doe',
658 'fingerprint' => 'FAKER' . $this->seed
659 ];
660 $child = $this->createMember($child_data);
661 $cid = $child->id;
662
663 //contribution for child
664 $begin_date = new \DateTime(); // 2020-11-07
665 $begin_date->sub(new \DateInterval('P5M')); // 2020-06-07
666 $begin_date->add(new \DateInterval('P3D')); // 2020-06-10
667
668 $due_date = clone $begin_date;
669 $due_date->add(new \DateInterval('P1Y'));
670 $due_date->sub(new \DateInterval('P1D'));
671
672 $data = [
673 'id_adh' => $cid,
674 'id_type_cotis' => 1,
675 'montant_cotis' => 25,
676 'type_paiement_cotis' => 3,
677 'info_cotis' => 'FAKER' . $this->seed,
678 'date_enreg' => $begin_date->format('Y-m-d'),
679 'date_debut_cotis' => $begin_date->format('Y-m-d'),
680 'date_fin_cotis' => $due_date->format('Y-m-d'),
681 ];
682 $ccontrib = $this->createContrib($data);
683
684 $this->login->logOut();
685
686 //load child from db
687 $child = new \Galette\Entity\Adherent($this->zdb);
688 $child->enableDep('parent');
689 $this->assertTrue($child->load($cid));
690
691 $this->assertSame($child_data['nom_adh'], $child->name);
692 $this->assertInstanceOf('\Galette\Entity\Adherent', $child->parent);
693 $this->assertSame($member->id, $child->parent->id);
694 $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh']));
695
696 $mdata = $this->dataAdherentOne();
697 $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh']));
698 $this->assertTrue($this->login->isLogged());
699 $this->assertFalse($this->login->isAdmin());
700 $this->assertFalse($this->login->isStaff());
701
702 $this->assertTrue($ccontrib->canShow($this->login));
703
704 //logout
705 $this->login->logOut();
706 $this->assertFalse($this->login->isLogged());
707 }
708
709 /**
710 * Test next year contribution
711 *
712 * @return void
713 */
714 public function testNextYear()
715 {
716 $this->logSuperAdmin();
717 $this->getMemberOne();
718
719 //create contribution for member
720 $begin_date = new \DateTime(); // 2023-12-30
721 $ny_begin_date = clone $begin_date; // 2023-12-30
722 $end_date = clone $begin_date;
723 $begin_date->sub(new \DateInterval('P1Y')); // 2022-12-30
724 $end_date->sub(new \DateInterval('P1D')); // 2023-12-29
725
726 $data = [
727 'id_adh' => $this->adh->id,
728 'id_type_cotis' => 1, //contribution
729 'montant_cotis' => 100,
730 'type_paiement_cotis' => 3,
731 'info_cotis' => 'FAKER' . $this->seed,
732 'date_enreg' => $begin_date->format('Y-m-d'),
733 'date_debut_cotis' => $begin_date->format('Y-m-d'),
734 'duree_mois_cotis' => 12
735 ];
736 $this->createContrib($data);
737
738 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
739 $this->assertSame(100.00, $contrib->amount);
740 $this->assertSame($end_date->format('Y-m-d'), $contrib->end_date);
741
742 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, ['type' => 1, 'adh' => $this->adh->id]);
743 $this->assertSame($ny_begin_date->format('Y-m-d'), $contrib->begin_date);
744 }
745
746 /**
747 * Test next year contribution from a 0.9.x
748 *
749 * @return void
750 */
751 public function testNextYearFrom096()
752 {
753 $this->logSuperAdmin();
754 $this->getMemberOne();
755
756 //create contribution for member
757 $begin_date = new \DateTime(); // 2023-12-30
758 $ny_begin_date = clone $begin_date; // 2023-12-30
759 $end_date = clone $begin_date;
760 $due_date = clone $begin_date;
761
762 $begin_date->sub(new \DateInterval('P1Y')); // 2022-12-30
763
764 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
765 $insert = $this->zdb->insert(\Galette\Entity\Contribution::TABLE);
766 $insert->values(
767 [
768 'id_adh' => $this->adh->id,
769 'id_type_cotis' => 1, //contribution
770 'montant_cotis' => 100,
771 'type_paiement_cotis' => 3,
772 'info_cotis' => 'FAKER' . $this->seed,
773 'date_enreg' => $begin_date->format('Y-m-d'),
774 'date_debut_cotis' => $begin_date->format('Y-m-d'),
775 'date_fin_cotis' => $due_date->format('Y-m-d')
776 ]
777 );
778 $add = $this->zdb->execute($insert);
779 $this->assertSame(1, $add->count());
780 $contribution_id = (int)($add->getGeneratedValue() ?? $this->zdb->getLastGeneratedValue($contrib));
781
782 $update = $this->zdb->update(\Galette\Entity\Adherent::TABLE);
783 $update->set(
784 array('date_echeance' => $due_date->format('Y-m-d'))
785 )->where(
786 [\Galette\Entity\Adherent::PK => $this->adh->id]
787 );
788 $this->zdb->execute($update);
789
790 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $contribution_id);
791 $this->assertSame(100.00, $contrib->amount);
792 $this->assertSame($end_date->format('Y-m-d'), $contrib->end_date);
793
794 $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, ['type' => 1, 'adh' => $this->adh->id, 'payment_type' => 1]);
795 $this->assertSame($ny_begin_date->format('Y-m-d'), $contrib->begin_date);
796
797 $check = $contrib->check(['type_paiement_cotis' => 1, 'montant_cotis' => 1, 'info_cotis' => 'FAKER' . $this->seed], [], []);
798 if (is_array($check)) {
799 var_dump($check);
800 }
801 $this->assertTrue($check);
802
803 $store = $contrib->store();
804 $this->assertTrue($store);
805 }
806 }