]> git.agnieray.net Git - galette.git/commitdiff
Fix late reminders sent before the 1st month
authorJohan Cwiklinski <johan@x-tnd.be>
Mon, 28 Sep 2020 05:32:04 +0000 (07:32 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Mon, 28 Sep 2020 05:41:24 +0000 (07:41 +0200)
Add unit tests on worflow
closes #1487

galette/lib/Galette/Repository/Reminders.php
tests/Galette/Repository/tests/units/Reminders.php

index e1b7f24aa331f00817f422d06f803de81580b46a..9c4317073b7c3bd55aa595b3cd20e482bc7a2ba3 100644 (file)
@@ -167,8 +167,8 @@ class Reminders
                         $second = clone $due_date;
                         $first->modify('1 month');
                         $second->modify('2 month');
-                        if ($r->last_reminder == null || $now >= $second || $now >= $first) {
-                            if ($r->last_reminder == '') {
+                        if ($now >= $second || $now >= $first) {
+                            if ($r->last_reminder === null || $r->last_reminder == '') {
                                 $date_checked = true;
                             } else {
                                 $last_reminder = new \DateTime($r->last_reminder);
index dca543e431e4be38edc1b1f3b8800df7ec0ef9e2..b2a8bd941f363854dcc5fc4a4d16d97375bd742d 100644 (file)
@@ -168,37 +168,120 @@ class Reminders extends atoum
         $reminders = new \Galette\Repository\Reminders();
         $this->array($reminders->getList($this->zdb))->isIdenticalTo([]);
 
-        //create members
+        //create member
         $this->createAdherent();
         $id = current($this->ids);
 
-        //create contributions
-        $this->createContribution();
+        //create contribution, just about to be impending
+        $now = new \DateTime();
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_begin->add(new \DateInterval('P1M'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
+
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
         $cid = current($this->contribs_ids);
 
         $adh = $this->adh;
         $this->boolean($adh->load($id))->isTrue();
-        //member is late
-        $this->boolean($adh->isUp2Date())->isFalse();
 
-        $rlist = $reminders->getList($this->zdb);
-        $this->array($rlist)->hasSize(1);
-        $reminder = current($rlist);
+        //member is up to date, no reminder to send
+        $this->boolean($this->adh->isUp2Date())->isTrue();
+        $this->array($reminders->getList($this->zdb))->isIdenticalTo([]);
 
-        $this->integer($reminder->member_id)->isIdenticalTo($id);
-        $this->integer($reminder->type)->isIdenticalTo(\Galette\Entity\Reminder::LATE);
-        $this->variable($reminder->date)->isNull();
-        $this->variable($reminder->getMessage())->isNull();
-        $this->boolean($reminder->isSuccess())->isFalse();
-        $this->boolean($reminder->hasMail())->isTrue();
+        //create contribution, just impending
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_begin->add(new \DateInterval('P1M'));
+        $date_begin->sub(new \DateInterval('P1D'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
 
-        $this->array($ireminders->getList($this->zdb))->isIdenticalTo([]);
-        $this->array($lreminders->getList($this->zdb))->isEqualTo($rlist);
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
+        $cid = current($this->contribs_ids);
 
-        //create already sent impending reminder 2 months ago
-        $now = new \DateTime();
+        $adh = $this->adh;
+        $this->boolean($adh->load($id))->isTrue();
+
+        //member is up to date, there is one impending reminder to send
+        $this->boolean($this->adh->isUp2Date())->isTrue();
+        $this->array($reminders->getList($this->zdb))->hasSize(1);
+        $this->array($lreminders->getList($this->zdb))->hasSize(0);
+        $this->array($ireminders->getList($this->zdb))->hasSize(1);
+
+        //create contribution, just impending less than 7 days
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_begin->add(new \DateInterval('P4D'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
+
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
+        $cid = current($this->contribs_ids);
+
+        $adh = $this->adh;
+        $this->boolean($adh->load($id))->isTrue();
+
+        //member is up to date, there is one impending reminder to send
+        $this->boolean($this->adh->isUp2Date())->isTrue();
+        $this->array($reminders->getList($this->zdb))->hasSize(1);
+        $this->array($lreminders->getList($this->zdb))->hasSize(0);
+        $this->array($ireminders->getList($this->zdb))->hasSize(1);
+
+        //add a sent first impending reminder
+        $send = clone $now;
+        $send->sub(new \DateInterval('P1M'));
+        $data = array(
+            'reminder_type'     => \Galette\Entity\Reminder::IMPENDING,
+            'reminder_dest'     => $id,
+            'reminder_date'     => $send->format('Y-m-d'),
+            'reminder_success'  => true,
+            'reminder_nomail'   => ($this->zdb->isPostgres() ? 'false' : 0)
+        );
+
+        $insert = $this->zdb->insert(\Galette\Entity\Reminder::TABLE);
+        $insert->values($data);
+
+        $add = $this->zdb->execute($insert);
+        $this->integer($add->count())->isGreaterThan(0);
+
+        //there is still one reminder to send
+        $this->boolean($this->adh->isUp2Date())->isTrue();
+        $this->array($reminders->getList($this->zdb))->hasSize(1);
+        $this->array($lreminders->getList($this->zdb))->hasSize(0);
+        $this->array($ireminders->getList($this->zdb))->hasSize(1);
+
+        //add a sent second impending reminder, yesterday
         $send = clone $now;
-        $send->sub(new \DateInterval('P2M'));
+        $send->sub(new \DateInterval('P1D'));
         $data = array(
             'reminder_type'     => \Galette\Entity\Reminder::IMPENDING,
             'reminder_dest'     => $id,
@@ -213,20 +296,157 @@ class Reminders extends atoum
         $add = $this->zdb->execute($insert);
         $this->integer($add->count())->isGreaterThan(0);
 
-        $rlist = $reminders->getList($this->zdb);
-        $this->array($rlist)->hasSize(1);
-        $reminder = current($rlist);
+        //nothing to send!
+        $this->boolean($this->adh->isUp2Date())->isTrue();
+        $this->array($reminders->getList($this->zdb))->hasSize(0);
+        $this->array($lreminders->getList($this->zdb))->hasSize(0);
+        $this->array($ireminders->getList($this->zdb))->hasSize(0);
+
+        //create contribution, expiration day
+        $now = new \DateTime();
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
+
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
+        $cid = current($this->contribs_ids);
+
+        $adh = $this->adh;
+        $this->boolean($adh->load($id))->isTrue();
+
+        //member is up to date, no reminder to send
+        $this->boolean($this->adh->isUp2Date())->isTrue();
+        $this->array($reminders->getList($this->zdb))->isIdenticalTo([]);
+
+        //create contribution, just late
+        $now = new \DateTime();
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_begin->sub(new \DateInterval('P1D'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
+
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
+        $cid = current($this->contribs_ids);
+
+        $adh = $this->adh;
+        $this->boolean($adh->load($id))->isTrue();
+
+        //member is not up to date, but less than one month, no reminder to send
+        $this->boolean($this->adh->isUp2Date())->isFalse();
+        $this->array($reminders->getList($this->zdb))->hasSize(0);
+        $this->array($lreminders->getList($this->zdb))->hasSize(0);
+        $this->array($ireminders->getList($this->zdb))->hasSize(0);
+
+        //create contribution, late by 30 days
+        $now = new \DateTime();
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_begin->sub(new \DateInterval('P30D'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
 
-        $this->integer($reminder->member_id)->isIdenticalTo($id);
-        $this->integer($reminder->type)->isIdenticalTo(\Galette\Entity\Reminder::LATE);
-        $this->variable($reminder->date)->isNull();
-        $this->variable($reminder->getMessage())->isNull();
-        $this->boolean($reminder->isSuccess())->isFalse();
-        $this->boolean($reminder->hasMail())->isTrue();
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
+        $cid = current($this->contribs_ids);
 
-        //create already sent laste reminder 2 days ago
+        $adh = $this->adh;
+        $this->boolean($adh->load($id))->isTrue();
+
+        //member is not up to date, but less than one month, no reminder to send
+        $this->boolean($this->adh->isUp2Date())->isFalse();
+        $this->array($reminders->getList($this->zdb))->hasSize(0);
+        $this->array($lreminders->getList($this->zdb))->hasSize(0);
+        $this->array($ireminders->getList($this->zdb))->hasSize(0);
+
+        //create contribution, late by 31 days
+        $now = new \DateTime();
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_begin->sub(new \DateInterval('P31D'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
+
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
+        $cid = current($this->contribs_ids);
+
+        $adh = $this->adh;
+        $this->boolean($adh->load($id))->isTrue();
+
+        //member is not up to date for one month, one late reminder to send
+        $this->boolean($this->adh->isUp2Date())->isFalse();
+        $this->array($reminders->getList($this->zdb))->hasSize(1);
+        $this->array($lreminders->getList($this->zdb))->hasSize(1);
+        $this->array($ireminders->getList($this->zdb))->hasSize(0);
+
+        //create contribution, late by 40 days
+        $now = new \DateTime();
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_begin->sub(new \DateInterval('P40D'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
+
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
+        $cid = current($this->contribs_ids);
+
+        $adh = $this->adh;
+        $this->boolean($adh->load($id))->isTrue();
+
+        //member is not up to date for one month, one late reminder to send
+        $this->boolean($this->adh->isUp2Date())->isFalse();
+        $this->array($reminders->getList($this->zdb))->hasSize(1);
+        $this->array($lreminders->getList($this->zdb))->hasSize(1);
+        $this->array($ireminders->getList($this->zdb))->hasSize(0);
+
+        //add a sent late reminder, at it should have been
         $send = clone $now;
-        $send->sub(new \DateInterval('P2D'));
+        $send->sub(new \DateInterval('P5D'));
         $data = array(
             'reminder_type'     => \Galette\Entity\Reminder::LATE,
             'reminder_dest'     => $id,
@@ -241,8 +461,40 @@ class Reminders extends atoum
         $add = $this->zdb->execute($insert);
         $this->integer($add->count())->isGreaterThan(0);
 
-        $rlist = $reminders->getList($this->zdb);
-        $this->array($rlist)->isIdenticalTo([]);
+        //nothing to send!
+        $this->boolean($this->adh->isUp2Date())->isFalse();
+        $this->array($reminders->getList($this->zdb))->hasSize(0);
+        $this->array($lreminders->getList($this->zdb))->hasSize(0);
+        $this->array($ireminders->getList($this->zdb))->hasSize(0);
+
+        //create contribution, late by 2 months
+        $now = new \DateTime();
+        $date_begin = clone $now;
+        $date_begin->sub(new \DateInterval('P1Y'));
+        $date_begin->sub(new \DateInterval('P2M'));
+        $date_end = clone $date_begin;
+        $date_end->add(new \DateInterval('P1Y'));
+
+        $this->createContrib([
+            'id_adh'                => $id,
+            'id_type_cotis'         => 3,
+            'montant_cotis'         => '111',
+            'type_paiement_cotis'   => '6',
+            'info_cotis'            => 'FAKER' . $this->seed,
+            'date_fin_cotis'        => $date_end->format('Y-m-d'),
+            'date_enreg'            => $now->format('Y-m-d'),
+            'date_debut_cotis'      => $now->format('Y-m-d')
+        ]);
+        $cid = current($this->contribs_ids);
+
+        $adh = $this->adh;
+        $this->boolean($adh->load($id))->isTrue();
+
+        //member is not up to date for one month, one late reminder to send
+        $this->boolean($this->adh->isUp2Date())->isFalse();
+        $this->array($reminders->getList($this->zdb))->hasSize(1);
+        $this->array($lreminders->getList($this->zdb))->hasSize(1);
+        $this->array($ireminders->getList($this->zdb))->hasSize(0);
     }
 
     /**
@@ -394,28 +646,6 @@ class Reminders extends atoum
         $this->string($adh->getRowClass())->isIdenticalTo('active cotis-never');
     }
 
-    /**
-     * Create test contribution in database
-     *
-     * @return void
-     */
-    private function createContribution()
-    {
-        $fakedata = new \Galette\Util\FakeData($this->zdb, $this->i18n);
-        $fakedata
-            ->setSeed($this->seed)
-            ->setDependencies(
-                $this->preferences,
-                $this->members_fields,
-                $this->history,
-                $this->login
-            );
-
-        $data = $fakedata->fakeContrib($this->adh->id);
-        $this->createContrib($data);
-        $this->checkContribExpected();
-    }
-
     /**
      * Create contribution from data
      *
@@ -433,67 +663,8 @@ class Reminders extends atoum
         $this->boolean($check)->isTrue();
 
         $store = $contrib->store();
-        $this->boolean($store)->isTrue();
+        $this->boolean($store)->isTrue($store);
 
         $this->contribs_ids[] = (int)$contrib->id;
     }
-
-    /**
-     * 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' => 3,
-            'montant_cotis' => '111',
-            'type_paiement_cotis' => '6',
-            '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-late');
-        $this->string($this->adh->due_date)->isIdenticalTo($this->contrib->end_date);
-        $this->boolean($this->adh->isUp2Date())->isFalse();
-    }
 }