]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Transaction.php
Improve tests
[galette.git] / tests / Galette / Entity / tests / units / Transaction.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Transaction tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2021 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 Entity
28 * @package GaletteTests
29 *
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
34 * @since 2021-10-16
35 */
36
37 namespace Galette\Entity\test\units;
38
39 use Galette\GaletteTestCase;
40
41 /**
42 * Transaction tests class
43 *
44 * @category Entity
45 * @name Transaction
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
51 * @since 2021-10-16
52 */
53 class Transaction extends GaletteTestCase
54 {
55 protected $seed = 95842354;
56 /** @var \Galette\Entity\Transaction */
57 private $transaction;
58
59 /**
60 * Cleanup after each test method
61 *
62 * @param string $method Calling method
63 *
64 * @return void
65 */
66 public function afterTestMethod($method)
67 {
68 $this->zdb = new \Galette\Core\Db();
69
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);
74
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);
79
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);
85
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);
90
91 $this->cleanHistory();
92 }
93
94 /**
95 * Set up tests
96 *
97 * @param string $testMethod Calling method
98 *
99 * @return void
100 */
101 public function beforeTestMethod($testMethod)
102 {
103 parent::beforeTestMethod($testMethod);
104 $this->initContributionsTypes();
105
106 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
107 $this->transaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
108
109 $this->adh = new \Galette\Entity\Adherent($this->zdb);
110 $this->adh->setDependencies(
111 $this->preferences,
112 $this->members_fields,
113 $this->history
114 );
115 }
116
117 /**
118 * Create test transaction in database
119 *
120 * @return void
121 */
122 private function createTransaction()
123 {
124 $date = new \DateTime(); // 2020-11-07
125 $data = [
126 'id_adh' => $this->adh->id,
127 'trans_date' => $date->format('Y-m-d'),
128 'trans_amount' => 92,
129 'trans_desc' => 'FAKER' . $this->seed
130 ];
131
132 $this->transaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
133 $check = $this->transaction->check($data, [], []);
134 if (is_array($check)) {
135 var_dump($check);
136 }
137 $this->boolean($check)->isTrue();
138
139 $store = $this->transaction->store($this->history);
140 $this->boolean($store)->isTrue();
141
142 return $this->transaction;
143 }
144
145 /**
146 * Test empty transaction
147 *
148 * @return void
149 */
150 public function testEmpty()
151 {
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();
156
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)
161 ->hasSize(5)
162 ->hasKeys([
163 \Galette\Entity\Transaction::PK,
164 \Galette\Entity\Adherent::PK,
165 'trans_date',
166 'trans_amount',
167 'trans_desc'
168 ]);
169
170 $this->variable($this->transaction->unknown_property)->isEqualTo(false);
171 }
172
173 /**
174 * Test getter and setter special cases
175 *
176 * @return void
177 */
178 public function testGetterSetter()
179 {
180 $transaction = $this->transaction;
181
182 //set a bad date
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);
187
188 //set a correct date
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');
193
194 //set a bad amount
195 $data = ['trans_amount' => 'mypassword'];
196 $expected = ['- The amount must be an integer!'];
197 $check = $transaction->check($data, [], []);
198 $this->array($check)->isIdenticalTo($expected);
199
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);
205
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);
211 }
212
213 /**
214 * Test transaction creation
215 *
216 * @return void
217 */
218 public function testCreation()
219 {
220 $this->getMemberOne();
221 //create transaction for member
222 $this->createTransaction();
223 }
224
225 /**
226 * Test transaction update
227 *
228 * @return void
229 */
230 public function testUpdate()
231 {
232 $this->getMemberOne();
233 //create transaction for member
234 $this->createTransaction();
235
236 $this->logSuperAdmin();
237 $data = [
238 'trans_amount' => 42
239 ];
240 $check = $this->transaction->check($data, [], []);
241 if (is_array($check)) {
242 var_dump($check);
243 }
244 $this->boolean($check)->isTrue();
245
246 $store = $this->transaction->store($this->history);
247 $this->boolean($store)->isTrue();
248
249 $transaction = new \Galette\Entity\Transaction($this->zdb, $this->login, $this->transaction->id);
250 $this->variable($transaction->amount)->isIdenticalTo(42.00);
251 }
252
253 /**
254 * Test fields labels
255 *
256 * @return void
257 */
258 public function testGetFieldLabel()
259 {
260 $this->string($this->transaction->getFieldLabel('trans_amount'))
261 ->isIdenticalTo('Amount');
262
263 $this->string($this->transaction->getFieldLabel('trans_date'))
264 ->isIdenticalTo('Date');
265
266 $this->string($this->transaction->getFieldLabel('trans_desc'))
267 ->isIdenticalTo('Description');
268
269 $this->string($this->transaction->getFieldLabel(\Galette\Entity\Adherent::PK))
270 ->isIdenticalTo('Originator');
271 }
272
273 /**
274 * Test transaction loading
275 *
276 * @return void
277 */
278 public function testLoad()
279 {
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;
284
285 $this->getMemberOne();
286
287 //create transaction for member
288 $this->createTransaction();
289
290 $id = $this->transaction->id;
291 $transaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
292
293 $this->boolean($transaction->load((int)$id))->isTrue();
294 $this->boolean($transaction->load(1355522012))->isFalse();
295 }
296
297 /**
298 * Test transaction removal
299 *
300 * @return void
301 */
302 public function testRemove()
303 {
304 $this->logSuperAdmin();
305
306 $this->getMemberOne();
307 $this->createTransaction();
308
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();
313
314 $transaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
315 $this->boolean($transaction->remove($this->history))->isFalse();
316 }
317
318 /**
319 * Test can* methods
320 *
321 * @return void
322 */
323 public function testCan()
324 {
325 $this->getMemberOne();
326 //create transaction for member
327 $this->createTransaction();
328 $transaction = $this->transaction;
329
330 $this->boolean($transaction->canShow($this->login))->isFalse();
331
332 //Superadmin can fully change transactions
333 $this->logSuperAdmin();
334
335 $this->boolean($transaction->canShow($this->login))->isTrue();
336
337 //logout
338 $this->login->logOut();
339 $this->boolean($this->login->isLogged())->isFalse();
340
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();
347
348 $this->boolean($transaction->canShow($this->login))->isTrue();
349
350 //logout
351 $this->login->logOut();
352 $this->boolean($this->login->isLogged())->isFalse();
353
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();
361
362 $this->boolean($transaction->canShow($this->login))->isFalse();
363
364 //parents can chow change children transactions
365 $this->getMemberOne();
366 $member = $this->adh;
367 $mdata = $this->dataAdherentOne();
368 global $login;
369 $login = $this->login;
370 $this->logSuperAdmin();
371
372 $child_data = [
373 'nom_adh' => 'Doe',
374 'prenom_adh' => 'Johny',
375 'parent_id' => $member->id,
376 'attach' => true,
377 'login_adh' => 'child.johny.doe',
378 'fingerprint' => 'FAKER' . $this->seed
379 ];
380 $child = $this->createMember($child_data);
381 $cid = $child->id;
382
383 //transaction for child
384 $date = new \DateTime(); // 2020-11-07
385
386 $data = [
387 'id_adh' => $cid,
388 'trans_date' => $date->format('Y-m-d'),
389 'trans_amount' => 92,
390 'trans_desc' => 'FAKER' . $this->seed
391 ];
392
393 $ctransaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
394 $check = $ctransaction->check($data, [], []);
395 if (is_array($check)) {
396 var_dump($check);
397 }
398 $this->boolean($check)->isTrue();
399
400 $store = $ctransaction->store($this->history);
401 $this->boolean($store)->isTrue();
402
403 $this->login->logOut();
404
405 //load child from db
406 $child = new \Galette\Entity\Adherent($this->zdb);
407 $child->enableDep('parent');
408 $this->boolean($child->load($cid))->isTrue();
409
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();
414
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();
420
421 $this->boolean($ctransaction->canShow($this->login))->isTrue();
422
423 //logout
424 $this->login->logOut();
425 $this->boolean($this->login->isLogged())->isFalse();
426 }
427
428 /**
429 * Test a transaction
430 *
431 * @return void
432 */
433 public function testTransaction(): void
434 {
435 $this->logSuperAdmin();
436 $this->getMemberOne();
437 //create transaction for member
438 $this->createTransaction();
439
440 $contribs_ids = [];
441 $tid = $this->transaction->id;
442
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
447
448 $edate = clone $bdate;
449 $edate->add(new \DateInterval('P1Y'));
450
451 $data = [
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
461 ];
462 $contrib = $this->createContrib($data);
463 $contribs_ids[] = $contrib->id;
464
465 $this->boolean($contrib->isTransactionPart())->isTrue();
466 $this->boolean($contrib->isTransactionPartOf($this->transaction->id))->isTrue();
467
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');
471
472 //complete the transaction
473 $data = [
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
483 ];
484 $contrib = $this->createContrib($data);
485 $contribs_ids[] = $contrib->id;
486
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');
492
493
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');
497
498 //cannot add more
499 $data = [
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
509 ];
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.']);
513
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();
517
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');
521
522 $this->boolean($contrib->setTransactionPart($this->zdb, $tid, $contrib_id))->isTrue();
523
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');
527
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();
533 }
534 }
535 }