]> git.agnieray.net Git - galette.git/commitdiff
Officially allows "0" as contribution amount; closes #1767
authorJohan Cwiklinski <johan@x-tnd.be>
Thu, 4 Jan 2024 04:52:12 +0000 (05:52 +0100)
committerJohan Cwiklinski <johan@x-tnd.be>
Fri, 5 Jan 2024 08:33:50 +0000 (09:33 +0100)
Add related tests

galette/lib/Galette/Entity/Contribution.php
tests/Galette/Entity/tests/units/Contribution.php
tests/GaletteTestCase.php

index 49d9e948a795c0e6f97d4e4d447593be66b7cd95..bf933ffb7a3d490f5c6e617f8b854ea6a7849e4a 100644 (file)
@@ -474,8 +474,8 @@ class Contribution
                         break;
                     case 'montant_cotis':
                         $value = strtr($value, ',', '.');
-                        if (!empty($value)) {
-                            $this->_amount = $value;
+                        if (!empty($value) || $value === '0') {
+                            $this->_amount = (double)$value;
                         }
                         if (!is_numeric($value) && $value !== '') {
                             $this->errors[] = _T("- The amount must be an integer!");
index 9fbf60cbebb593750b47ec2a7435c9e591c7cd86..0e5a1a85c72681d2e08d0f84ca5602a94edb9469 100644 (file)
@@ -232,11 +232,11 @@ class Contribution extends GaletteTestCase
     }
 
     /**
-     * Test contribution update
+     * Test donation update
      *
      * @return void
      */
-    public function testUpdate()
+    public function testDonationUpdate()
     {
         $this->getMemberOne();
         //create contribution for member
@@ -304,6 +304,91 @@ class Contribution extends GaletteTestCase
         $this->assertSame(0.00, $contrib->amount);
     }
 
+    /**
+     * Test contribution update
+     *
+     * @return void
+     */
+    public function testContributionUpdate()
+    {
+        $this->logSuperAdmin();
+
+        $this->getMemberOne();
+        //create contribution for member
+        $begin_date = new \DateTime(); // 2020-11-07
+        $begin_date->sub(new \DateInterval('P5M')); // 2020-06-07
+        $begin_date->add(new \DateInterval('P3D')); // 2020-06-10
+
+        $due_date = clone $begin_date;
+        $due_date->add(new \DateInterval('P1Y'));
+        $due_date->sub(new \DateInterval('P1D'));
+
+        //instanciate contribution as annual fee
+        $this->contrib = new \Galette\Entity\Contribution(
+            $this->zdb,
+            $this->login,
+            [
+                'type' => 1 //annual fee
+            ]
+        );
+        $this->assertSame(
+            [
+                'id_type_cotis'     => 1,
+                'id_adh'            => 1,
+                'date_enreg'        => 1,
+                'date_debut_cotis'  => 1,
+                'date_fin_cotis'    => 1, //should be 1
+                'montant_cotis'     => 1 // should be 1
+            ],
+            $this->contrib->getRequired()
+        );
+
+        $data = [
+            'id_adh' => $this->adh->id,
+            'id_type_cotis' => 1, //annual fee
+            'montant_cotis' => 0,
+            'type_paiement_cotis' => 3,
+            'info_cotis' => 'FAKER' . $this->seed,
+            'date_enreg' => $begin_date->format('Y-m-d'),
+            'date_debut_cotis' => $begin_date->format('Y-m-d'),
+            'date_fin_cotis' => $due_date->format('Y-m-d'),
+        ];
+
+        $this->createContrib($data, $this->contrib);
+
+        $this->assertSame(0.0, $this->contrib->amount);
+        $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
+        $this->assertSame(0.0, $contrib->amount);
+
+        //change amount
+        $data['montant_cotis'] = 42;
+        $check = $contrib->check($data, [], []);
+        if (is_array($check)) {
+            var_dump($check);
+        }
+        $this->assertTrue($check);
+
+        $store = $contrib->store();
+        $this->assertTrue($store);
+
+        $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
+        $this->assertSame(42.0, $contrib->amount);
+
+        //change amount back to 0
+        $data['montant_cotis'] = 0;
+        $check = $contrib->check($data, [], []);
+        if (is_array($check)) {
+            var_dump($check);
+        }
+        $this->assertTrue($check);
+
+        $store = $contrib->store();
+        $this->assertTrue($store);
+
+        $contrib = new \Galette\Entity\Contribution($this->zdb, $this->login, $this->contrib->id);
+        $this->assertSame(0.0, $contrib->amount);
+    }
+
     /**
      * Test end date retrieving
      * This is based on some Preferences parameters
index ce2b062f5b93b2a23ee1f80b78406a0f7dbefd1b..4f9dcbc32e89325052232fde3a3a44dfdf30d364 100644 (file)
@@ -579,14 +579,18 @@ abstract class GaletteTestCase extends TestCase
     /**
      * Create contribution from data
      *
-     * @param array $data Data to use to create contribution
+     * @param array                         $data Data to use to create contribution
+     * @param ?\Galette\Entity\Contribution $contrib Contribution instance, if any
      *
      * @return \Galette\Entity\Contribution
      */
-    public function createContrib(array $data)
+    public function createContrib(array $data, \Galette\Entity\Contribution $contrib = null)
     {
-        $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
-        $contrib = $this->contrib;
+        if ($contrib === null) {
+            $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
+            $contrib = $this->contrib;
+        }
+
         $check = $contrib->check($data, [], []);
         if (is_array($check)) {
             var_dump($check);