]> git.agnieray.net Git - galette.git/commitdiff
Add unit tests for transaction
authorJohan Cwiklinski <johan@x-tnd.be>
Sat, 16 Oct 2021 16:07:28 +0000 (18:07 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Sun, 17 Oct 2021 04:15:40 +0000 (06:15 +0200)
galette/lib/Galette/Entity/Adherent.php
galette/lib/Galette/Entity/Transaction.php
tests/Galette/Core/tests/units/Links.php
tests/Galette/Entity/tests/units/Contribution.php
tests/Galette/Entity/tests/units/PdfModel.php
tests/Galette/Entity/tests/units/Transaction.php [new file with mode: 0644]
tests/GaletteTestCase.php

index a6c67f1438771e516b30846a8ec58c40f5e061d7..bfd5c4dafb15bb89bf0fcb574c49972fdbdf6e51 100644 (file)
@@ -943,8 +943,10 @@ class Adherent
     private function getFieldLabel($field)
     {
         $label = $this->fields[$field]['label'];
-        //remove trailing ':' and then nbsp (for french at least)
-        $label = trim(trim($label ?? '', ':'), '&nbsp;');
+        //replace "&nbsp;"
+        $label = str_replace('&nbsp;', ' ', $label);
+        //remove trailing ':' and then trim
+        $label = trim(trim($label ?? '', ':'));
         return $label;
     }
 
index 5f2eec303f6d533c665141bfa95fa516eadd03aa..bc85904a2d7078b58a31f850d4183d4cb4b76f54 100644 (file)
@@ -285,7 +285,7 @@ class Transaction
 
         $fields = array_keys($this->_fields);
         foreach ($fields as $key) {
-            //first of all, let's sanitize values
+            //first, let's sanitize values
             $key = strtolower($key);
             $prop = '_' . $this->_fields[$key]['propname'];
 
@@ -322,7 +322,7 @@ class Transaction
                                     ),
                                     array(
                                         __("Y-m-d"),
-                                        $this->_fields[$key]['label']
+                                        $this->getFieldLabel($key)
                                     ),
                                     _T("- Wrong date format (%date_format) for %field!")
                                 );
@@ -341,9 +341,7 @@ class Transaction
                         case 'trans_desc':
                             /** TODO: retrieve field length from database and check that */
                             $this->_description = $value;
-                            if (trim($value) == '') {
-                                $this->errors[] = _T("- Empty transaction description!");
-                            } elseif (mb_strlen($value) > 150) {
+                            if (mb_strlen($value) > 150) {
                                 $this->errors[] = _T("- Transaction description must be 150 characters long maximum.");
                             }
                             break;
@@ -485,8 +483,12 @@ class Transaction
      *
      * @return double
      */
-    public function getDispatchedAmount()
+    public function getDispatchedAmount(): float
     {
+        if (empty($this->_id)) {
+            return (double)0;
+        }
+
         try {
             $select = $this->zdb->select(Contribution::TABLE);
             $select->columns(
@@ -515,6 +517,10 @@ class Transaction
      */
     public function getMissingAmount()
     {
+        if (empty($this->_id)) {
+            return (double)$this->amount;
+        }
+
         try {
             $select = $this->zdb->select(Contribution::TABLE);
             $select->columns(
@@ -576,7 +582,7 @@ class Transaction
         $forbidden = array();
 
         $rname = '_' . $name;
-        if (!in_array($name, $forbidden) && isset($this->$rname)) {
+        if (!in_array($name, $forbidden) && property_exists($this, $rname)) {
             switch ($name) {
                 case 'date':
                     if ($this->$rname != '') {
@@ -594,9 +600,13 @@ class Transaction
                         }
                     }
                     break;
+                case 'id':
+                    if ($this->_id !== null) {
+                        return (int)$this->$rname;
+                    }
+                    return null;
                 default:
                     return $this->$rname;
-                    break;
             }
         } else {
             return false;
@@ -624,11 +634,13 @@ class Transaction
      *
      * @return string
      */
-    private function getFieldLabel($field)
+    public function getFieldLabel($field)
     {
         $label = $this->_fields[$field]['label'];
-        //remove trailing ':' and then nbsp (for french at least)
-        $label = trim(trim($label, ':'), '&nbsp;');
+        //replace "&nbsp;"
+        $label = str_replace('&nbsp;', ' ', $label);
+        //remove trailing ':' and then trim
+        $label = trim(trim($label, ':'));
         return $label;
     }
 
index d3558b1628fbd146699125b6cb32d03fca310d70..b75e23dfdf1c11899c3786ad4e15b2a857168015 100644 (file)
@@ -275,7 +275,7 @@ class Links extends GaletteTestCase
      *
      * @return void
      */
-    private function createContribution()
+    protected function createContribution()
     {
         $bdate = new \DateTime(); // 2020-11-07
         $bdate->sub(new \DateInterval('P1Y')); // 2019-11-07
@@ -307,7 +307,7 @@ class Links extends GaletteTestCase
      *
      * @return void
      */
-    private function checkContribExpected($contrib = null, $new_expecteds = [])
+    protected function checkContribExpected($contrib = null, $new_expecteds = [])
     {
         if ($contrib === null) {
             $contrib = $this->contrib;
index 63862634feadbf464526dd932bf965473df0b285..b2b80fd8515255c8c45b5360345d54d0ebc292c9 100644 (file)
@@ -56,7 +56,7 @@ class Contribution extends GaletteTestCase
     protected $seed = 95842354;
 
     /**
-     * Cleanup after testeach test method
+     * Cleanup after each test method
      *
      * @param string $method Calling method
      *
@@ -101,46 +101,6 @@ class Contribution extends GaletteTestCase
         );
     }
 
-    /**
-     * Create test contribution in database
-     *
-     * @return void
-     */
-    private function createContribution()
-    {
-        $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' => 92,
-            '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->checkContribExpected();
-    }
-
-    /**
-     * Loads contribution from a resultset
-     *
-     * @param ResultSet $rs ResultSet
-     *
-     * @return void
-     */
-    private function loadContribution($rs)
-    {
-        $this->adh = new \Galette\Entity\Contribution($this->zdb, $this->login, $rs);
-    }
-
     /**
      * Test empty contribution
      *
@@ -230,7 +190,7 @@ class Contribution extends GaletteTestCase
         $this->variable($contrib->transaction)->isNull();
         $contrib->transaction = 46;
         $this->object($contrib->transaction)->isInstanceOf('\Galette\Entity\Transaction');
-        $this->boolean($contrib->transaction->id)->isFalse();
+        $this->variable($contrib->transaction->id)->isNull();
 
         $contrib->member = 'not a member';
         $this->variable($contrib->member)->isNull();
@@ -265,65 +225,6 @@ class Contribution extends GaletteTestCase
         $this->string($contrib->spayment_type)->isIdenticalTo('Paypal');
     }
 
-    /**
-     * Check contributions expecteds
-     *
-     * @param Contribution $contrib       Contribution instance, if any
-     * @param array        $new_expecteds Changes on expected values
-     *
-     * @return void
-     */
-    private function checkContribExpected($contrib = null, $new_expecteds = [])
-    {
-        if ($contrib === null) {
-            $contrib = $this->contrib;
-        }
-
-        $date_begin = $contrib->raw_begin_date;
-        $date_end = clone $date_begin;
-        $date_end->add(new \DateInterval('P1Y'));
-
-        $this->object($contrib->raw_date)->isInstanceOf('DateTime');
-        $this->object($contrib->raw_begin_date)->isInstanceOf('DateTime');
-        $this->object($contrib->raw_end_date)->isInstanceOf('DateTime');
-
-        $expecteds = [
-            'id_adh' => "{$this->adh->id}",
-            'id_type_cotis' => 1,
-            'montant_cotis' => '92',
-            'type_paiement_cotis' => '3',
-            'info_cotis' => 'FAKER' . $this->seed,
-            'date_fin_cotis' => $date_end->format('Y-m-d'),
-        ];
-        $expecteds = array_merge($expecteds, $new_expecteds);
-
-        $this->string($contrib->raw_end_date->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
-
-        foreach ($expecteds as $key => $value) {
-            $property = $this->contrib->fields[$key]['propname'];
-            switch ($key) {
-                case \Galette\Entity\ContributionsTypes::PK:
-                    $ct = $this->contrib->type;
-                    if ($ct instanceof \Galette\Entity\ContributionsTypes) {
-                        $this->integer((int)$ct->id)->isIdenticalTo($value);
-                    } else {
-                        $this->integer($ct)->isIdenticalTo($value);
-                    }
-                    break;
-                default:
-                    $this->variable($contrib->$property)->isEqualTo($value, $property);
-                    break;
-            }
-        }
-
-        //load member from db
-        $this->adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
-        //member is now up-to-date
-        $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();
-    }
-
     /**
      * Test contribution creation
      *
@@ -539,6 +440,9 @@ class Contribution extends GaletteTestCase
         $this->contrib->type = 1;
         $this->string($this->contrib->getFieldLabel('date_debut_cotis'))
             ->isIdenticalTo('Start date of membership');
+
+        $this->string($this->contrib->getFieldLabel('info_cotis'))
+            ->isIdenticalTo('Comments');
     }
 
     /**
index 93333ccca9277ac93e842043caf3271f381566c2..17fe39cecc75fd557caf96e8895b13e1a2337deb 100644 (file)
@@ -350,7 +350,7 @@ class PdfModel extends GaletteTestCase
         $this->createMember($data);
         $model->setMember($this->adh);
 
-        $this->createContribution($cdf);
+        $this->createPdfContribution($cdf);
         $model->setContribution($this->contrib);
 
         $this->string($model->hheader)->isIdenticalTo("<table>
@@ -417,7 +417,7 @@ Au milieu
      *
      * @return void
      */
-    private function createContribution($cdf)
+    protected function createPdfContribution($cdf)
     {
         $bdate = new \DateTime(); // 2020-11-07
         $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
diff --git a/tests/Galette/Entity/tests/units/Transaction.php b/tests/Galette/Entity/tests/units/Transaction.php
new file mode 100644 (file)
index 0000000..4aa84e9
--- /dev/null
@@ -0,0 +1,403 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Transaction tests
+ *
+ * PHP version 5
+ *
+ * Copyright © 2021 The Galette Team
+ *
+ * This file is part of Galette (http://galette.tuxfamily.org).
+ *
+ * Galette is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Galette is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Galette. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Entity
+ * @package   GaletteTests
+ *
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2021 The Galette Team
+ * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
+ * @link      http://galette.tuxfamily.org
+ * @since     2021-10-16
+ */
+
+namespace Galette\Entity\test\units;
+
+use Galette\GaletteTestCase;
+
+/**
+ * Transaction tests class
+ *
+ * @category  Entity
+ * @name      Transaction
+ * @package   GaletteTests
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2021 The Galette Team
+ * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
+ * @link      http://galette.tuxfamily.org
+ * @since     2021-10-16
+ */
+class Transaction extends GaletteTestCase
+{
+    protected $seed = 95842354;
+    /** @var \Galette\Entity\Transaction */
+    private $transaction;
+
+    /**
+     * Cleanup after each test method
+     *
+     * @param string $method Calling method
+     *
+     * @return void
+     */
+    public function afterTestMethod($method)
+    {
+        $this->zdb = new \Galette\Core\Db();
+
+        //first, remove contributions
+        $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
+        $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
+        $this->zdb->execute($delete);
+
+        //then, remove transactions
+        $delete = $this->zdb->delete(\Galette\Entity\Transaction::TABLE);
+        $delete->where(['trans_desc' => 'FAKER' . $this->seed]);
+        $this->zdb->execute($delete);
+
+        //remove members with parents
+        $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
+        $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
+        $delete->where('parent_id IS NOT NULL');
+        $this->zdb->execute($delete);
+
+        //remove all others members
+        $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
+        $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
+        $this->zdb->execute($delete);
+    }
+
+    /**
+     * Set up tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function beforeTestMethod($testMethod)
+    {
+        parent::beforeTestMethod($testMethod);
+        $this->initContributionsTypes();
+
+        $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
+        $this->transaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
+
+        $this->adh = new \Galette\Entity\Adherent($this->zdb);
+        $this->adh->setDependencies(
+            $this->preferences,
+            $this->members_fields,
+            $this->history
+        );
+    }
+
+    /**
+     * Create test transaction in database
+     *
+     * @return void
+     */
+    private function createTransaction()
+    {
+        $date = new \DateTime(); // 2020-11-07
+        $data = [
+            'id_adh' => $this->adh->id,
+            'trans_date' => $date->format('Y-m-d'),
+            'trans_amount' => 92,
+            'trans_desc' => 'FAKER' . $this->seed
+        ];
+
+        $this->transaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
+        $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();
+
+        return $this->transaction;
+    }
+
+    /**
+     * Test empty transaction
+     *
+     * @return void
+     */
+    public function testEmpty()
+    {
+        $this->variable($this->transaction->id)->isNull();
+        $this->variable($this->transaction->date)->isEqualTo(date('Y-m-d'));
+        $this->variable($this->transaction->amount)->isNull();
+        $this->variable($this->transaction->description)->isNull();
+
+        $this->float($this->transaction->getDispatchedAmount())->isIdenticalTo((double)0);
+        $this->float($this->transaction->getMissingAmount())->isIdenticalTo((double)0);
+        $this->string($this->transaction->getRowClass())->isIdenticalTo('transaction-normal');
+        $this->array($this->transaction->fields)
+            ->hasSize(5)
+            ->hasKeys([
+                \Galette\Entity\Transaction::PK,
+                \Galette\Entity\Adherent::PK,
+                'trans_date',
+                'trans_amount',
+                'trans_desc'
+            ]);
+
+        $this->variable($this->transaction->unknown_property)->isEqualTo(false);
+    }
+
+    /**
+     * Test getter and setter special cases
+     *
+     * @return void
+     */
+    public function testGetterSetter()
+    {
+        $transaction = $this->transaction;
+
+        //set a bad date
+        $data = ['trans_date' => 'mypassword'];
+        $expected = ['- Wrong date format (Y-m-d) for Date!'];
+        $check = $transaction->check($data, [], []);
+        $this->array($check)->isIdenticalTo($expected);
+
+        //set a correct date
+        $data = ['trans_date' => '1999-01-01'];
+        $check = $transaction->check($data, [], []);
+        $this->boolean($check)->isTrue();
+        $this->string($transaction->date)->isIdenticalTo('1999-01-01');
+
+        //set a bad amount
+        $data = ['trans_amount' => 'mypassword'];
+        $expected = ['- The amount must be an integer!'];
+        $check = $transaction->check($data, [], []);
+        $this->array($check)->isIdenticalTo($expected);
+
+        //set a correct amount
+        $data = ['trans_amount' => 1256];
+        $check = $transaction->check($data, ['trans_amount' => 1], []);
+        $this->boolean($check)->isTrue();
+        $this->string($transaction->amount)->isIdenticalTo('1256');
+
+        //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.'];
+        $expected = ['- Transaction description must be 150 characters long maximum.'];
+        $check = $transaction->check($data, [], []);
+        $this->array($check)->isIdenticalTo($expected);
+    }
+
+    /**
+     * Test transaction creation
+     *
+     * @return void
+     */
+    public function testCreation()
+    {
+        $this->getMemberOne();
+        //create transaction for member
+        $this->createTransaction();
+    }
+
+    /**
+     * Test fields labels
+     *
+     * @return void
+     */
+    public function testGetFieldLabel()
+    {
+        $this->string($this->transaction->getFieldLabel('trans_amount'))
+            ->isIdenticalTo('Amount');
+
+        $this->string($this->transaction->getFieldLabel('trans_date'))
+            ->isIdenticalTo('Date');
+
+        $this->string($this->transaction->getFieldLabel('trans_desc'))
+            ->isIdenticalTo('Description');
+
+        $this->string($this->transaction->getFieldLabel(\Galette\Entity\Adherent::PK))
+            ->isIdenticalTo('Originator');
+    }
+
+    /**
+     * Test transaction loading
+     *
+     * @return void
+     */
+    public function testLoad()
+    {
+        $this->login = new \mock\Galette\Core\Login($this->zdb, $this->i18n);
+        $this->calling($this->login)->isLogged = true;
+        $this->calling($this->login)->isStaff = true;
+        $this->calling($this->login)->isAdmin = true;
+
+        $this->getMemberOne();
+
+        //create transaction for member
+        $this->createTransaction();
+
+        $id = $this->transaction->id;
+        $transaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
+
+        $this->boolean($transaction->load((int)$id))->isTrue();
+        $this->boolean($transaction->load(1355522012))->isFalse();
+    }
+
+    /**
+     * Test transaction removal
+     *
+     * @return void
+     */
+    public function testRemove()
+    {
+        $this->login->logAdmin('superadmin', $this->preferences);
+        $this->boolean($this->login->isLogged())->isTrue();
+        $this->boolean($this->login->isSuperAdmin())->isTrue();
+
+        $this->getMemberOne();
+        $this->createTransaction();
+
+        $tid = $this->transaction->id;
+        $this->boolean($this->transaction->load($tid))->isTrue();
+        $this->boolean($this->transaction->remove($this->history))->isTrue();
+        $this->boolean($this->transaction->load($tid))->isFalse();
+
+        $transaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
+        $this->boolean($transaction->remove($this->history))->isFalse();
+    }
+
+    /**
+     * Test can* methods
+     *
+     * @return void
+     */
+    public function testCan()
+    {
+        $this->getMemberOne();
+        //create contribution for member
+        $this->createTransaction();
+        $transaction = $this->transaction;
+
+        $this->boolean($transaction->canShow($this->login))->isFalse();
+
+        //Superadmin can fully change contributions
+        $this->login->logAdmin('superadmin', $this->preferences);
+        $this->boolean($this->login->isLogged())->isTrue();
+        $this->boolean($this->login->isSuperAdmin())->isTrue();
+
+        $this->boolean($transaction->canShow($this->login))->isTrue();
+
+        //logout
+        $this->login->logOut();
+        $this->boolean($this->login->isLogged())->isFalse();
+
+        //Member can fully change its own contributions
+        $mdata = $this->dataAdherentOne();
+        $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
+        $this->boolean($this->login->isLogged())->isTrue();
+        $this->boolean($this->login->isAdmin())->isFalse();
+        $this->boolean($this->login->isStaff())->isFalse();
+
+        $this->boolean($transaction->canShow($this->login))->isTrue();
+
+        //logout
+        $this->login->logOut();
+        $this->boolean($this->login->isLogged())->isFalse();
+
+        //Another member has no access
+        $this->getMemberTwo();
+        $mdata = $this->dataAdherentTwo();
+        $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
+        $this->boolean($this->login->isLogged())->isTrue();
+        $this->boolean($this->login->isAdmin())->isFalse();
+        $this->boolean($this->login->isStaff())->isFalse();
+
+        $this->boolean($transaction->canShow($this->login))->isFalse();
+
+        //parents can chow change children contributions
+        $this->getMemberOne();
+        $member = $this->adh;
+        $mdata = $this->dataAdherentOne();
+        global $login;
+        $login = $this->login;
+        $this->login->logAdmin('superadmin', $this->preferences);
+        $this->boolean($this->login->isLogged())->isTrue();
+        $this->boolean($this->login->isSuperAdmin())->isTrue();
+
+        $child_data = [
+            'nom_adh'       => 'Doe',
+            'prenom_adh'    => 'Johny',
+            'parent_id'     => $member->id,
+            'attach'        => true,
+            'login_adh'     => 'child.johny.doe',
+            'fingerprint' => 'FAKER' . $this->seed
+        ];
+        $child = $this->createMember($child_data);
+        $cid = $child->id;
+
+        //contribution for child
+        $date = new \DateTime(); // 2020-11-07
+
+        $data = [
+            'id_adh' => $cid,
+            'trans_date' => $date->format('Y-m-d'),
+            'trans_amount' => 92,
+            'trans_desc' => 'FAKER' . $this->seed
+        ];
+
+        $ctransaction = new \Galette\Entity\Transaction($this->zdb, $this->login);
+        $check = $ctransaction->check($data, [], []);
+        if (is_array($check)) {
+            var_dump($check);
+        }
+        $this->boolean($check)->isTrue();
+
+        $store = $ctransaction->store($this->history);
+        $this->boolean($store)->isTrue();
+
+        $this->login->logOut();
+
+        //load child from db
+        $child = new \Galette\Entity\Adherent($this->zdb);
+        $child->enableDep('parent');
+        $this->boolean($child->load($cid))->isTrue();
+
+        $this->string($child->name)->isIdenticalTo($child_data['nom_adh']);
+        $this->object($child->parent)->isInstanceOf('\Galette\Entity\Adherent');
+        $this->integer($child->parent->id)->isIdenticalTo($member->id);
+        $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
+
+        $mdata = $this->dataAdherentOne();
+        $this->boolean($this->login->login($mdata['login_adh'], $mdata['mdp_adh']))->isTrue();
+        $this->boolean($this->login->isLogged())->isTrue();
+        $this->boolean($this->login->isAdmin())->isFalse();
+        $this->boolean($this->login->isStaff())->isFalse();
+
+        $this->boolean($ctransaction->canShow($this->login))->isTrue();
+
+        //logout
+        $this->login->logOut();
+        $this->boolean($this->login->isLogged())->isFalse();
+    }
+}
index 60c05a3ab549c8233ba987acf0057daccf6e4dc4..4378493019ea8be04b3bfa77eadd029f954601c2 100644 (file)
@@ -579,6 +579,93 @@ abstract class GaletteTestCase extends atoum
         return $contrib;
     }
 
+    /**
+     * Create test contribution in database
+     *
+     * @return void
+     */
+    protected function createContribution()
+    {
+        $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' => 92,
+            '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->checkContribExpected();
+    }
+
+    /**
+     * Check contributions expected
+     *
+     * @param Contribution $contrib       Contribution instance, if any
+     * @param array        $new_expecteds Changes on expected values
+     *
+     * @return void
+     */
+    protected function checkContribExpected($contrib = null, $new_expecteds = [])
+    {
+        if ($contrib === null) {
+            $contrib = $this->contrib;
+        }
+
+        $date_begin = $contrib->raw_begin_date;
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
+
+        $this->object($contrib->raw_date)->isInstanceOf('DateTime');
+        $this->object($contrib->raw_begin_date)->isInstanceOf('DateTime');
+        $this->object($contrib->raw_end_date)->isInstanceOf('DateTime');
+
+        $expecteds = [
+            'id_adh' => "{$this->adh->id}",
+            'id_type_cotis' => 1,
+            'montant_cotis' => '92',
+            'type_paiement_cotis' => '3',
+            'info_cotis' => 'FAKER' . $this->seed,
+            'date_fin_cotis' => $date_end->format('Y-m-d'),
+        ];
+        $expecteds = array_merge($expecteds, $new_expecteds);
+
+        $this->string($contrib->raw_end_date->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
+
+        foreach ($expecteds as $key => $value) {
+            $property = $this->contrib->fields[$key]['propname'];
+            switch ($key) {
+                case \Galette\Entity\ContributionsTypes::PK:
+                    $ct = $this->contrib->type;
+                    if ($ct instanceof \Galette\Entity\ContributionsTypes) {
+                        $this->integer((int)$ct->id)->isIdenticalTo($value);
+                    } else {
+                        $this->integer($ct)->isIdenticalTo($value);
+                    }
+                    break;
+                default:
+                    $this->variable($contrib->$property)->isEqualTo($value, $property);
+                    break;
+            }
+        }
+
+        //load member from db
+        $this->adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id);
+        //member is now up-to-date
+        $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();
+    }
+
     /**
      * Initialize default status in database
      *