3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2021 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 2021 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.tuxfamily.org
37 namespace Galette\Entity\test\units
;
39 use Galette\GaletteTestCase
;
42 * Transaction tests class
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2021 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
53 class Transaction
extends GaletteTestCase
55 protected $seed = 95842354;
56 /** @var \Galette\Entity\Transaction */
60 * Cleanup after each test method
62 * @param string $method Calling method
66 public function afterTestMethod($method)
68 $this->zdb
= new \Galette\Core\
Db();
70 //first, remove contributions
71 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
72 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
73 $this->zdb
->execute($delete);
75 //then, remove transactions
76 $delete = $this->zdb
->delete(\Galette\Entity\Transaction
::TABLE
);
77 $delete->where(['trans_desc' => 'FAKER' . $this->seed
]);
78 $this->zdb
->execute($delete);
80 //remove members with parents
81 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
82 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
83 $delete->where('parent_id IS NOT NULL');
84 $this->zdb
->execute($delete);
86 //remove all others members
87 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
88 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
89 $this->zdb
->execute($delete);
91 $this->cleanHistory();
97 * @param string $testMethod Calling method
101 public function beforeTestMethod($testMethod)
103 parent
::beforeTestMethod($testMethod);
104 $this->initContributionsTypes();
106 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
107 $this->transaction
= new \Galette\Entity\
Transaction($this->zdb
, $this->login
);
109 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
110 $this->adh
->setDependencies(
112 $this->members_fields
,
118 * Create test transaction in database
122 private function createTransaction()
124 $date = new \
DateTime(); // 2020-11-07
126 'id_adh' => $this->adh
->id
,
127 'trans_date' => $date->format('Y-m-d'),
128 'trans_amount' => 92,
129 'trans_desc' => 'FAKER' . $this->seed
132 $this->transaction
= new \Galette\Entity\
Transaction($this->zdb
, $this->login
);
133 $check = $this->transaction
->check($data, [], []);
134 if (is_array($check)) {
137 $this->boolean($check)->isTrue();
139 $store = $this->transaction
->store($this->history
);
140 $this->boolean($store)->isTrue();
142 return $this->transaction
;
146 * Test empty transaction
150 public function testEmpty()
152 $this->variable($this->transaction
->id
)->isNull();
153 $this->variable($this->transaction
->date
)->isEqualTo(date('Y-m-d'));
154 $this->variable($this->transaction
->amount
)->isNull();
155 $this->variable($this->transaction
->description
)->isNull();
157 $this->float($this->transaction
->getDispatchedAmount())->isIdenticalTo((double)0);
158 $this->float($this->transaction
->getMissingAmount())->isIdenticalTo((double)0);
159 $this->string($this->transaction
->getRowClass())->isIdenticalTo('transaction-normal');
160 $this->array($this->transaction
->fields
)
163 \Galette\Entity\Transaction
::PK
,
164 \Galette\Entity\Adherent
::PK
,
170 $this->variable($this->transaction
->unknown_property
)->isEqualTo(false);
174 * Test getter and setter special cases
178 public function testGetterSetter()
180 $transaction = $this->transaction
;
183 $data = ['trans_date' => 'mypassword'];
184 $expected = ['- Wrong date format (Y-m-d) for Date!'];
185 $check = $transaction->check($data, [], []);
186 $this->array($check)->isIdenticalTo($expected);
189 $data = ['trans_date' => '1999-01-01'];
190 $check = $transaction->check($data, [], []);
191 $this->boolean($check)->isTrue();
192 $this->string($transaction->date
)->isIdenticalTo('1999-01-01');
195 $data = ['trans_amount' => 'mypassword'];
196 $expected = ['- The amount must be an integer!'];
197 $check = $transaction->check($data, [], []);
198 $this->array($check)->isIdenticalTo($expected);
200 //set a correct amount
201 $data = ['trans_amount' => 1256];
202 $check = $transaction->check($data, ['trans_amount' => 1], []);
203 $this->boolean($check)->isTrue();
204 $this->variable($transaction->amount
)->isIdenticalTo(1256.00);
206 //set a bad description
207 $data = ['trans_desc' => 'this is a very long description that should give an error; because the length of transaction description is limited to 150 characters long, even if this is quite hard to find something to write.'];
208 $expected = ['- Transaction description must be 150 characters long maximum.'];
209 $check = $transaction->check($data, [], []);
210 $this->array($check)->isIdenticalTo($expected);
214 * Test transaction creation
218 public function testCreation()
220 $this->getMemberOne();
221 //create transaction for member
222 $this->createTransaction();
226 * Test transaction update
230 public function testUpdate()
232 $this->getMemberOne();
233 //create transaction for member
234 $this->createTransaction();
236 $this->logSuperAdmin();
240 $check = $this->transaction
->check($data, [], []);
241 if (is_array($check)) {
244 $this->boolean($check)->isTrue();
246 $store = $this->transaction
->store($this->history
);
247 $this->boolean($store)->isTrue();
249 $transaction = new \Galette\Entity\
Transaction($this->zdb
, $this->login
, $this->transaction
->id
);
250 $this->variable($transaction->amount
)->isIdenticalTo(42.00);
258 public function testGetFieldLabel()
260 $this->string($this->transaction
->getFieldLabel('trans_amount'))
261 ->isIdenticalTo('Amount');
263 $this->string($this->transaction
->getFieldLabel('trans_date'))
264 ->isIdenticalTo('Date');
266 $this->string($this->transaction
->getFieldLabel('trans_desc'))
267 ->isIdenticalTo('Description');
269 $this->string($this->transaction
->getFieldLabel(\Galette\Entity\Adherent
::PK
))
270 ->isIdenticalTo('Originator');
274 * Test transaction loading
278 public function testLoad()
280 $this->login
= new \mock\Galette\Core\
Login($this->zdb
, $this->i18n
);
281 $this->calling($this->login
)->isLogged
= true;
282 $this->calling($this->login
)->isStaff
= true;
283 $this->calling($this->login
)->isAdmin
= true;
285 $this->getMemberOne();
287 //create transaction for member
288 $this->createTransaction();
290 $id = $this->transaction
->id
;
291 $transaction = new \Galette\Entity\
Transaction($this->zdb
, $this->login
);
293 $this->boolean($transaction->load((int)$id))->isTrue();
294 $this->boolean($transaction->load(1355522012))->isFalse();
298 * Test transaction removal
302 public function testRemove()
304 $this->logSuperAdmin();
306 $this->getMemberOne();
307 $this->createTransaction();
309 $tid = $this->transaction
->id
;
310 $this->boolean($this->transaction
->load($tid))->isTrue();
311 $this->boolean($this->transaction
->remove($this->history
))->isTrue();
312 $this->boolean($this->transaction
->load($tid))->isFalse();
314 $transaction = new \Galette\Entity\
Transaction($this->zdb
, $this->login
);
315 $this->boolean($transaction->remove($this->history
))->isFalse();
323 public function testCan()
325 $this->getMemberOne();
326 //create transaction for member
327 $this->createTransaction();
328 $transaction = $this->transaction
;
330 $this->boolean($transaction->canShow($this->login
))->isFalse();
332 //Superadmin can fully change transactions
333 $this->logSuperAdmin();
335 $this->boolean($transaction->canShow($this->login
))->isTrue();
338 $this->login
->logOut();
339 $this->boolean($this->login
->isLogged())->isFalse();
341 //Member can fully change its own transactions
342 $mdata = $this->dataAdherentOne();
343 $this->boolean($this->login
->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
344 $this->boolean($this->login
->isLogged())->isTrue();
345 $this->boolean($this->login
->isAdmin())->isFalse();
346 $this->boolean($this->login
->isStaff())->isFalse();
348 $this->boolean($transaction->canShow($this->login
))->isTrue();
351 $this->login
->logOut();
352 $this->boolean($this->login
->isLogged())->isFalse();
354 //Another member has no access
355 $this->getMemberTwo();
356 $mdata = $this->dataAdherentTwo();
357 $this->boolean($this->login
->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
358 $this->boolean($this->login
->isLogged())->isTrue();
359 $this->boolean($this->login
->isAdmin())->isFalse();
360 $this->boolean($this->login
->isStaff())->isFalse();
362 $this->boolean($transaction->canShow($this->login
))->isFalse();
364 //parents can chow change children transactions
365 $this->getMemberOne();
366 $member = $this->adh
;
367 $mdata = $this->dataAdherentOne();
369 $login = $this->login
;
370 $this->logSuperAdmin();
374 'prenom_adh' => 'Johny',
375 'parent_id' => $member->id
,
377 'login_adh' => 'child.johny.doe',
378 'fingerprint' => 'FAKER' . $this->seed
380 $child = $this->createMember($child_data);
383 //transaction for child
384 $date = new \
DateTime(); // 2020-11-07
388 'trans_date' => $date->format('Y-m-d'),
389 'trans_amount' => 92,
390 'trans_desc' => 'FAKER' . $this->seed
393 $ctransaction = new \Galette\Entity\
Transaction($this->zdb
, $this->login
);
394 $check = $ctransaction->check($data, [], []);
395 if (is_array($check)) {
398 $this->boolean($check)->isTrue();
400 $store = $ctransaction->store($this->history
);
401 $this->boolean($store)->isTrue();
403 $this->login
->logOut();
406 $child = new \Galette\Entity\
Adherent($this->zdb
);
407 $child->enableDep('parent');
408 $this->boolean($child->load($cid))->isTrue();
410 $this->string($child->name
)->isIdenticalTo($child_data['nom_adh']);
411 $this->object($child->parent
)->isInstanceOf('\Galette\Entity\Adherent');
412 $this->integer($child->parent
->id
)->isIdenticalTo($member->id
);
413 $this->boolean($this->login
->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
415 $mdata = $this->dataAdherentOne();
416 $this->boolean($this->login
->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
417 $this->boolean($this->login
->isLogged())->isTrue();
418 $this->boolean($this->login
->isAdmin())->isFalse();
419 $this->boolean($this->login
->isStaff())->isFalse();
421 $this->boolean($ctransaction->canShow($this->login
))->isTrue();
424 $this->login
->logOut();
425 $this->boolean($this->login
->isLogged())->isFalse();
433 public function testTransaction(): void
435 $this->logSuperAdmin();
436 $this->getMemberOne();
437 //create transaction for member
438 $this->createTransaction();
441 $tid = $this->transaction
->id
;
443 //create a contribution attached to transaction
444 $bdate = new \
DateTime(); // 2020-11-07
445 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
446 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
448 $edate = clone $bdate;
449 $edate->add(new \
DateInterval('P1Y'));
452 'id_adh' => $this->adh
->id
,
453 'id_type_cotis' => 1,
454 'montant_cotis' => 25,
455 'type_paiement_cotis' => 3,
456 'info_cotis' => 'FAKER' . $this->seed
,
457 'date_enreg' => $bdate->format('Y-m-d'),
458 'date_debut_cotis' => $bdate->format('Y-m-d'),
459 'date_fin_cotis' => $edate->format('Y-m-d'),
460 \Galette\Entity\Transaction
::PK
=> $tid
462 $contrib = $this->createContrib($data);
463 $contribs_ids[] = $contrib->id
;
465 $this->boolean($contrib->isTransactionPart())->isTrue();
466 $this->boolean($contrib->isTransactionPartOf($this->transaction
->id
))->isTrue();
468 $this->float($this->transaction
->getDispatchedAmount())->isIdenticalTo((double)25);
469 $this->float($this->transaction
->getMissingAmount())->isIdenticalTo((double)67);
470 $this->string($this->transaction
->getRowClass())->isIdenticalTo('transaction-uncomplete');
472 //complete the transaction
474 'id_adh' => $this->adh
->id
,
475 'id_type_cotis' => 4, //donation
476 'montant_cotis' => 67,
477 'type_paiement_cotis' => 3,
478 'info_cotis' => 'FAKER' . $this->seed
,
479 'date_enreg' => $bdate->format('Y-m-d'),
480 'date_debut_cotis' => $bdate->format('Y-m-d'),
481 'date_fin_cotis' => $edate->format('Y-m-d'),
482 \Galette\Entity\Transaction
::PK
=> $tid
484 $contrib = $this->createContrib($data);
485 $contribs_ids[] = $contrib->id
;
487 $this->boolean($contrib->isTransactionPart())->isTrue();
488 $this->boolean($contrib->isTransactionPartOf($this->transaction
->id
))->isTrue();
489 $this->boolean($contrib->isFee())->isFalse();
490 $this->string($contrib->getTypeLabel())->isIdenticalTo('Donation');
491 $this->string($contrib->getRawType())->isIdenticalTo('donation');
494 $this->float($this->transaction
->getDispatchedAmount())->isIdenticalTo((double)92);
495 $this->float($this->transaction
->getMissingAmount())->isIdenticalTo((double)0);
496 $this->string($this->transaction
->getRowClass())->isIdenticalTo('transaction-normal');
500 'id_adh' => $this->adh
->id
,
501 'id_type_cotis' => 4, //donation
502 'montant_cotis' => 36,
503 'type_paiement_cotis' => 3,
504 'info_cotis' => 'FAKER' . $this->seed
,
505 'date_enreg' => $bdate->format('Y-m-d'),
506 'date_debut_cotis' => $bdate->format('Y-m-d'),
507 'date_fin_cotis' => $edate->format('Y-m-d'),
508 \Galette\Entity\Transaction
::PK
=> $tid
510 $contrib = new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
511 $check = $contrib->check($data, [], []);
512 $this->array($check)->isIdenticalTo(['- Sum of all contributions exceed corresponding transaction amount.']);
514 $contrib_id = $contribs_ids[0];
515 $contrib = new \Galette\Entity\
Contribution($this->zdb
, $this->login
, $contrib_id);
516 $this->boolean($contrib->unsetTransactionPart($this->zdb
, $this->login
, $tid, $contrib_id))->isTrue();
518 $this->float($this->transaction
->getDispatchedAmount())->isIdenticalTo((double)67);
519 $this->float($this->transaction
->getMissingAmount())->isIdenticalTo((double)25);
520 $this->string($this->transaction
->getRowClass())->isIdenticalTo('transaction-uncomplete');
522 $this->boolean($contrib->setTransactionPart($this->zdb
, $tid, $contrib_id))->isTrue();
524 $this->float($this->transaction
->getDispatchedAmount())->isIdenticalTo((double)92);
525 $this->float($this->transaction
->getMissingAmount())->isIdenticalTo((double)0);
526 $this->string($this->transaction
->getRowClass())->isIdenticalTo('transaction-normal');
528 //delete transaction, and ensures all contributions has been removed as well
529 $this->boolean($this->transaction
->remove($this->history
))->isTrue();
530 $this->boolean($this->transaction
->load($tid))->isFalse();
531 foreach ($contribs_ids as $contrib_id) {
532 $this->boolean($this->contrib
->load($contrib_id))->isFalse();