]> git.agnieray.net Git - galette.git/commitdiff
Improve tests
authorJohan Cwiklinski <johan@x-tnd.be>
Sun, 17 Oct 2021 07:13:19 +0000 (09:13 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Sun, 17 Oct 2021 07:13:19 +0000 (09:13 +0200)
galette/lib/Galette/Entity/Contribution.php
galette/lib/Galette/Entity/Transaction.php
tests/Galette/Entity/tests/units/Contribution.php
tests/Galette/Entity/tests/units/Transaction.php
tests/GaletteTestCase.php

index 834842b986028498769a9dc0dbb9bea8775eac5e..5e8f830e87acfc819aa08f2816c63f76eca85315 100644 (file)
@@ -351,9 +351,9 @@ class Contribution
         $pk = self::PK;
         $this->_id = (int)$r->$pk;
         $this->_date = $r->date_enreg;
-        $this->_amount = $r->montant_cotis;
+        $this->_amount = (int)$r->montant_cotis;
         //save original amount, we need it for transactions parts calculations
-        $this->_orig_amount = $r->montant_cotis;
+        $this->_orig_amount = (int)$r->montant_cotis;
         $this->_payment_type = $r->type_paiement_cotis;
         $this->_info = $r->info_cotis;
         $this->_begin_date = $r->date_debut_cotis;
@@ -1428,7 +1428,7 @@ class Contribution
             'id_adh'            => 1,
             'date_enreg'        => 1,
             'date_debut_cotis'  => 1,
-            'date_fin_cotis'    => $this->isFee(),
+            'date_fin_cotis'    => $this->isFee() ? 1 : 0,
             'montant_cotis'     => $this->isFee() ? 1 : 0
         ];
     }
index bc85904a2d7078b58a31f850d4183d4cb4b76f54..1a43651c8abedd874632ab0d4d88063438afc8ec 100644 (file)
@@ -601,32 +601,30 @@ class Transaction
                     }
                     break;
                 case 'id':
-                    if ($this->_id !== null) {
+                    if ($this->$rname !== null) {
                         return (int)$this->$rname;
                     }
                     return null;
+                case 'amount':
+                    if ($this->$rname !== null) {
+                        return (double)$this->$rname;
+                    }
+                    return null;
                 default:
                     return $this->$rname;
             }
         } else {
+            Analog::log(
+                sprintf(
+                    'Property %1$s does not exists for transaction',
+                    $name
+                ),
+                Analog::WARNING
+            );
             return false;
         }
     }
 
-    /**
-     * Global setter method
-     *
-     * @param string $name  name of the property we want to assign a value to
-     * @param object $value a relevant value for the property
-     *
-     * @return void
-     */
-    public function __set($name, $value)
-    {
-        /*$forbidden = array('fields');*/
-        /** TODO: What to do ? :-) */
-    }
-
     /**
      * Get field label
      *
index 2fcf0ebbdc43fd7b3678c79ab82e88f72e9a5ba3..f3f2ae60dfeb6c43b256e4462411e159f56bcb25 100644 (file)
@@ -239,6 +239,67 @@ class Contribution extends GaletteTestCase
         $this->createContribution();
     }
 
+    /**
+     * Test contribution update
+     *
+     * @return void
+     */
+    public function testUpdate()
+    {
+        $this->getMemberOne();
+        //create contribution for member
+        $bdate = new \DateTime(); // 2020-11-07
+        $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
+        $bdate->add(new \DateInterval('P3D')); // 2020-06-10
+
+        $edate = clone $bdate;
+        $edate->add(new \DateInterval('P1Y'));
+
+        $data = [
+            'id_adh' => $this->adh->id,
+            'id_type_cotis' => 4, //donation
+            'montant_cotis' => 12,
+            'type_paiement_cotis' => 3,
+            'info_cotis' => 'FAKER' . $this->seed,
+            'date_enreg' => $bdate->format('Y-m-d'),
+            'date_debut_cotis' => $bdate->format('Y-m-d'),
+            'date_fin_cotis' => $edate->format('Y-m-d'),
+        ];
+        $this->createContrib($data);
+        $this->array($this->contrib->getRequired())->isIdenticalTo([
+            'id_type_cotis'     => 1,
+            'id_adh'            => 1,
+            'date_enreg'        => 1,
+            'date_debut_cotis'  => 1,
+            'date_fin_cotis'    => 0,
+            'montant_cotis'     => 0
+        ]);
+
+        $this->logSuperAdmin();
+        $data = [
+            'id_adh' => $this->adh->id,
+            'id_type_cotis' => 4, //donation
+            'montant_cotis' => 1280,
+            'type_paiement_cotis' => 4,
+            'info_cotis' => 'FAKER' . $this->seed,
+            'date_enreg' => $bdate->format('Y-m-d'),
+            'date_debut_cotis' => $bdate->format('Y-m-d'),
+            'date_fin_cotis' => $edate->format('Y-m-d'),
+
+        ];
+        $check = $this->contrib->check($data, [], []);
+        if (is_array($check)) {
+            var_dump($check);
+        }
+        $this->boolean($check)->isTrue();
+
+        $store = $this->contrib->store();
+        $this->boolean($store)->isTrue();
+
+        $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
+        $this->variable($contrib->amount)->isIdenticalTo(1280);
+    }
+
     /**
      * Test end date retrieving
      * This is based on some Preferences parameters
@@ -255,7 +316,7 @@ class Contribution extends GaletteTestCase
         $contrib = new \Galette\Entity\Contribution(
             $this->zdb,
             $this->login,
-            ['type' => 1] //anual fee
+            ['type' => 1] //annual fee
         );
 
         // First, check for 12 months renewal
@@ -272,7 +333,7 @@ class Contribution extends GaletteTestCase
                 $contrib = new \Galette\Entity\Contribution(
                     $this->zdb,
                     $this->login,
-                    ['type' => 1] //anual fee
+                    ['type' => 1] //annual fee
                 );
             }
         )
@@ -290,7 +351,7 @@ class Contribution extends GaletteTestCase
         $contrib = new \Galette\Entity\Contribution(
             $this->zdb,
             $this->login,
-            ['type' => 1] // anual fee
+            ['type' => 1] // annual fee
         );
         $this->string($contrib->end_date)->isIdenticalTo($expected->format('Y-m-d'));
 
@@ -305,7 +366,7 @@ class Contribution extends GaletteTestCase
         $contrib = new \Galette\Entity\Contribution(
             $this->zdb,
             $this->login,
-            ['type' => 1] // anual fee
+            ['type' => 1] // annual fee
         );
         $this->string($contrib->end_date)->isIdenticalTo($expected->format('Y-m-t'));
 
@@ -355,7 +416,7 @@ class Contribution extends GaletteTestCase
         $end_date->add(new \DateInterval('P1Y'));
         $data = [
             \Galette\Entity\Adherent::PK            => $adh->id,
-            \Galette\Entity\ContributionsTypes::PK  => 1, //anual fee
+            \Galette\Entity\ContributionsTypes::PK  => 1, //annual fee
             'montant_cotis'                         => 20,
             'type_paiement_cotis'                   => \Galette\Entity\PaymentType::CHECK,
             'date_enreg'                            => $now->format(_T("Y-m-d")),
@@ -384,7 +445,7 @@ class Contribution extends GaletteTestCase
         $end_date->add(new \DateInterval('P1Y'));
         $data = [
             \Galette\Entity\Adherent::PK            => $adh->id,
-            \Galette\Entity\ContributionsTypes::PK  => 1, //anual fee
+            \Galette\Entity\ContributionsTypes::PK  => 1, //anunal fee
             'montant_cotis'                         => 20,
             'type_paiement_cotis'                   => \Galette\Entity\PaymentType::CHECK,
             'date_enreg'                            => $now->format(_T("Y-m-d")),
index 91c04728f6293a8c9a7ffc6e8fdecb55ab265948..e4cfa742562f7593ac933652b8a26decad1b0e50 100644 (file)
@@ -201,7 +201,7 @@ class Transaction extends GaletteTestCase
         $data = ['trans_amount' => 1256];
         $check = $transaction->check($data, ['trans_amount' => 1], []);
         $this->boolean($check)->isTrue();
-        $this->string($transaction->amount)->isIdenticalTo('1256');
+        $this->variable($transaction->amount)->isIdenticalTo(1256.00);
 
         //set a bad description
         $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.'];
@@ -222,6 +222,34 @@ class Transaction extends GaletteTestCase
         $this->createTransaction();
     }
 
+    /**
+     * Test transaction update
+     *
+     * @return void
+     */
+    public function testUpdate()
+    {
+        $this->getMemberOne();
+        //create transaction for member
+        $this->createTransaction();
+
+        $this->logSuperAdmin();
+        $data = [
+            'trans_amount' => 42
+        ];
+        $check = $this->transaction->check($data, [], []);
+        if (is_array($check)) {
+            var_dump($check);
+        }
+        $this->boolean($check)->isTrue();
+
+        $store = $this->transaction->store($this->history);
+        $this->boolean($store)->isTrue();
+
+        $transaction = new \Galette\Entity\Transaction($this->zdb, $this->login, $this->transaction->id);
+        $this->variable($transaction->amount)->isIdenticalTo(42.00);
+    }
+
     /**
      * Test fields labels
      *
@@ -301,7 +329,7 @@ class Transaction extends GaletteTestCase
 
         $this->boolean($transaction->canShow($this->login))->isFalse();
 
-        //Superadmin can fully change contributions
+        //Superadmin can fully change transactions
         $this->logSuperAdmin();
 
         $this->boolean($transaction->canShow($this->login))->isTrue();
@@ -310,7 +338,7 @@ class Transaction extends GaletteTestCase
         $this->login->logOut();
         $this->boolean($this->login->isLogged())->isFalse();
 
-        //Member can fully change its own contributions
+        //Member can fully change its own transactions
         $mdata = $this->dataAdherentOne();
         $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
         $this->boolean($this->login->isLogged())->isTrue();
@@ -333,7 +361,7 @@ class Transaction extends GaletteTestCase
 
         $this->boolean($transaction->canShow($this->login))->isFalse();
 
-        //parents can chow change children contributions
+        //parents can chow change children transactions
         $this->getMemberOne();
         $member = $this->adh;
         $mdata = $this->dataAdherentOne();
@@ -352,7 +380,7 @@ class Transaction extends GaletteTestCase
         $child = $this->createMember($child_data);
         $cid = $child->id;
 
-        //contribution for child
+        //transaction for child
         $date = new \DateTime(); // 2020-11-07
 
         $data = [
@@ -396,4 +424,112 @@ class Transaction extends GaletteTestCase
         $this->login->logOut();
         $this->boolean($this->login->isLogged())->isFalse();
     }
+
+    /**
+     * Test a transaction
+     *
+     * @return void
+     */
+    public function testTransaction(): void
+    {
+        $this->logSuperAdmin();
+        $this->getMemberOne();
+        //create transaction for member
+        $this->createTransaction();
+
+        $contribs_ids = [];
+        $tid = $this->transaction->id;
+
+        //create a contribution attached to transaction
+        $bdate = new \DateTime(); // 2020-11-07
+        $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
+        $bdate->add(new \DateInterval('P3D')); // 2020-06-10
+
+        $edate = clone $bdate;
+        $edate->add(new \DateInterval('P1Y'));
+
+        $data = [
+            'id_adh' => $this->adh->id,
+            'id_type_cotis' => 1,
+            'montant_cotis' => 25,
+            'type_paiement_cotis' => 3,
+            'info_cotis' => 'FAKER' . $this->seed,
+            'date_enreg' => $bdate->format('Y-m-d'),
+            'date_debut_cotis' => $bdate->format('Y-m-d'),
+            'date_fin_cotis' => $edate->format('Y-m-d'),
+            \Galette\Entity\Transaction::PK => $tid
+        ];
+        $contrib = $this->createContrib($data);
+        $contribs_ids[] = $contrib->id;
+
+        $this->boolean($contrib->isTransactionPart())->isTrue();
+        $this->boolean($contrib->isTransactionPartOf($this->transaction->id))->isTrue();
+
+        $this->float($this->transaction->getDispatchedAmount())->isIdenticalTo((double)25);
+        $this->float($this->transaction->getMissingAmount())->isIdenticalTo((double)67);
+        $this->string($this->transaction->getRowClass())->isIdenticalTo('transaction-uncomplete');
+
+        //complete the transaction
+        $data = [
+            'id_adh' => $this->adh->id,
+            'id_type_cotis' => 4, //donation
+            'montant_cotis' => 67,
+            'type_paiement_cotis' => 3,
+            'info_cotis' => 'FAKER' . $this->seed,
+            'date_enreg' => $bdate->format('Y-m-d'),
+            'date_debut_cotis' => $bdate->format('Y-m-d'),
+            'date_fin_cotis' => $edate->format('Y-m-d'),
+            \Galette\Entity\Transaction::PK => $tid
+        ];
+        $contrib = $this->createContrib($data);
+        $contribs_ids[] = $contrib->id;
+
+        $this->boolean($contrib->isTransactionPart())->isTrue();
+        $this->boolean($contrib->isTransactionPartOf($this->transaction->id))->isTrue();
+        $this->boolean($contrib->isFee())->isFalse();
+        $this->string($contrib->getTypeLabel())->isIdenticalTo('Donation');
+        $this->string($contrib->getRawType())->isIdenticalTo('donation');
+
+
+        $this->float($this->transaction->getDispatchedAmount())->isIdenticalTo((double)92);
+        $this->float($this->transaction->getMissingAmount())->isIdenticalTo((double)0);
+        $this->string($this->transaction->getRowClass())->isIdenticalTo('transaction-normal');
+
+        //cannot add more
+        $data = [
+            'id_adh' => $this->adh->id,
+            'id_type_cotis' => 4, //donation
+            'montant_cotis' => 36,
+            'type_paiement_cotis' => 3,
+            'info_cotis' => 'FAKER' . $this->seed,
+            'date_enreg' => $bdate->format('Y-m-d'),
+            'date_debut_cotis' => $bdate->format('Y-m-d'),
+            'date_fin_cotis' => $edate->format('Y-m-d'),
+            \Galette\Entity\Transaction::PK => $tid
+        ];
+        $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
+        $check = $contrib->check($data, [], []);
+        $this->array($check)->isIdenticalTo(['- Sum of all contributions exceed corresponding transaction amount.']);
+
+        $contrib_id = $contribs_ids[0];
+        $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $contrib_id);
+        $this->boolean($contrib->unsetTransactionPart($this->zdb, $this->login, $tid, $contrib_id))->isTrue();
+
+        $this->float($this->transaction->getDispatchedAmount())->isIdenticalTo((double)67);
+        $this->float($this->transaction->getMissingAmount())->isIdenticalTo((double)25);
+        $this->string($this->transaction->getRowClass())->isIdenticalTo('transaction-uncomplete');
+
+        $this->boolean($contrib->setTransactionPart($this->zdb, $tid, $contrib_id))->isTrue();
+
+        $this->float($this->transaction->getDispatchedAmount())->isIdenticalTo((double)92);
+        $this->float($this->transaction->getMissingAmount())->isIdenticalTo((double)0);
+        $this->string($this->transaction->getRowClass())->isIdenticalTo('transaction-normal');
+
+        //delete transaction, and ensures all contributions has been removed as well
+        $this->boolean($this->transaction->remove($this->history))->isTrue();
+        $this->boolean($this->transaction->load($tid))->isFalse();
+        foreach ($contribs_ids as $contrib_id) {
+            $this->boolean($this->contrib->load($contrib_id))->isFalse();
+        }
+    }
 }
index 141d56cad4534bcc4f3ba1a45816b19fb87e6043..f7aec32434826a40b1f2988af6535223c52f36a8 100644 (file)
@@ -595,7 +595,7 @@ abstract class GaletteTestCase extends atoum
 
         $data = [
             'id_adh' => $this->adh->id,
-            'id_type_cotis' => 1,
+            'id_type_cotis' => 1, //annual fee
             'montant_cotis' => 92,
             'type_paiement_cotis' => 3,
             'info_cotis' => 'FAKER' . $this->seed,
@@ -631,7 +631,7 @@ abstract class GaletteTestCase extends atoum
 
         $expecteds = [
             'id_adh' => "{$this->adh->id}",
-            'id_type_cotis' => 1,
+            'id_type_cotis' => 1, //annual fee
             'montant_cotis' => '92',
             'type_paiement_cotis' => '3',
             'info_cotis' => 'FAKER' . $this->seed,
@@ -664,6 +664,17 @@ abstract class GaletteTestCase extends atoum
         $this->string($this->adh->getRowClass())->isIdenticalTo('active cotis-ok');
         $this->string($this->adh->due_date)->isIdenticalTo($this->contrib->end_date);
         $this->boolean($this->adh->isUp2Date())->isTrue();
+        $this->boolean($contrib->isFee())->isTrue();
+        $this->string($contrib->getTypeLabel())->isIdenticalTo('Membership');
+        $this->string($contrib->getRawType())->isIdenticalTo('membership');
+        $this->array($this->contrib->getRequired())->isIdenticalTo([
+            'id_type_cotis'     => 1,
+            'id_adh'            => 1,
+            'date_enreg'        => 1,
+            'date_debut_cotis'  => 1,
+            'date_fin_cotis'    => 1,
+            'montant_cotis'     => 1
+        ]);
     }
 
     /**