]> git.agnieray.net Git - galette.git/commitdiff
Add unit tests on reminders; closes #1455
authorJohan Cwiklinski <johan@x-tnd.be>
Mon, 14 Sep 2020 14:08:27 +0000 (16:08 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Mon, 14 Sep 2020 14:08:27 +0000 (16:08 +0200)
galette/lib/Galette/Entity/Reminder.php
tests/Galette/Repository/tests/units/Reminders.php [new file with mode: 0644]

index 77a4b201d7e2a33907b86d27e4b01bfbe574c8a9..3bbbc56737116dd6953ca59c023246fe3ba24abc 100644 (file)
@@ -65,7 +65,9 @@ class Reminder
     private $type;
     private $dest;
     private $date;
-    private $success;
+    /** @var boolean */
+    private $success = false;
+    /** @var boolean */
     private $nomail;
     private $comment;
     private $replaces;
@@ -227,6 +229,8 @@ class Reminder
     {
         global $preferences;
 
+        $this->success = false;
+
         $type_name = 'late';
         if ($this->type === self::IMPENDING) {
             $type_name = 'impending';
@@ -275,7 +279,6 @@ class Reminder
                 $this->msg = $details;
                 $hist->add($msg, $details);
             } else {
-                $this->success = false;
                 if ($type_name == 'late') {
                     $msg = _T("A problem happened while sending late membership email");
                 } else {
@@ -285,7 +288,6 @@ class Reminder
                 $hist->add($str, $details);
             }
         } else {
-            $this->success = false;
             $this->nomail = true;
             $str = str_replace(
                 '%membership',
@@ -335,7 +337,9 @@ class Reminder
         switch ($name) {
             case 'member_id':
                 return $this->dest->id;
-                break;
+            case 'type':
+            case 'date':
+                return $this->$name;
             default:
                 Analog::log(
                     'Unable to get Reminder property ' . $name,
diff --git a/tests/Galette/Repository/tests/units/Reminders.php b/tests/Galette/Repository/tests/units/Reminders.php
new file mode 100644 (file)
index 0000000..dca543e
--- /dev/null
@@ -0,0 +1,499 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Reminders repository tests
+ *
+ * PHP version 5
+ *
+ * Copyright © 2020 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  Repository
+ * @package   GaletteTests
+ *
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2020 The Galette Team
+ * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
+ * @version   SVN: $Id$
+ * @link      http://galette.tuxfamily.org
+ * @since     2020-09-14
+ */
+
+namespace Galette\Repository\test\units;
+
+use atoum;
+
+/**
+ * Reminders repository tests
+ *
+ * @category  Repository
+ * @name      Remoinders
+ * @package   GaletteTests
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2020 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     2020-09-14
+ */
+class Reminders extends atoum
+{
+    private $zdb;
+    private $seed = 95842355;
+    private $preferences;
+    private $session;
+    private $login;
+    private $history;
+    //private $remove = [];
+    private $i18n;
+    private $contrib;
+    private $members_fields;
+    private $adh;
+    private $ids = [];
+    private $contribs_ids = [];
+
+    /**
+     * Set up tests
+     *
+     * @return void
+     */
+    public function setUp()
+    {
+        $this->zdb = new \Galette\Core\Db();
+        $ct = new \Galette\Entity\ContributionsTypes($this->zdb);
+        if (count($ct->getCompleteList()) === 0) {
+            //status are not yet instanciated.
+            $res = $ct->installInit();
+            $this->boolean($res)->isTrue();
+        }
+    }
+    /**
+     * Set up tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function beforeTestMethod($testMethod)
+    {
+        $this->zdb = new \Galette\Core\Db();
+
+        $this->i18n = new \Galette\Core\I18n(
+            \Galette\Core\I18n::DEFAULT_LANG
+        );
+
+        $this->preferences = new \Galette\Core\Preferences(
+            $this->zdb
+        );
+        $this->session = new \RKA\Session();
+        $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+
+        global $zdb, $login, $hist, $i18n; // globals :(
+        $zdb = $this->zdb;
+        $login = $this->login;
+        $hist = $this->history;
+        $i18n = $this->i18n;
+
+        $status = new \Galette\Entity\Status($this->zdb);
+        if (count($status->getList()) === 0) {
+            $res = $status->installInit();
+            $this->boolean($res)->isTrue();
+        }
+
+        $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
+
+        include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
+        $this->members_fields = $members_fields;
+        $this->adh = new \Galette\Entity\Adherent($this->zdb);
+        $this->adh->setDependencies(
+            $this->preferences,
+            $this->members_fields,
+            $this->history
+        );
+    }
+
+    /**
+     * Tear down tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function afterTestMethod($testMethod)
+    {
+        $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
+        $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
+        $this->zdb->execute($delete);
+
+        $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
+        $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
+        $this->zdb->execute($delete);
+
+        $delete = $this->zdb->delete(\Galette\Entity\Reminder::TABLE);
+        $this->zdb->execute($delete);
+    }
+
+    /**
+     * Test getList
+     *
+     * @return void
+     */
+    public function testGetList()
+    {
+        //impendings
+        $ireminders = new \Galette\Repository\Reminders([\Galette\Entity\Reminder::IMPENDING]);
+        $this->array($ireminders->getList($this->zdb))->isIdenticalTo([]);
+
+        //lates
+        $lreminders = new \Galette\Repository\Reminders([\Galette\Entity\Reminder::LATE]);
+        $this->array($lreminders->getList($this->zdb))->isIdenticalTo([]);
+
+        //all
+        $reminders = new \Galette\Repository\Reminders();
+        $this->array($reminders->getList($this->zdb))->isIdenticalTo([]);
+
+        //create members
+        $this->createAdherent();
+        $id = current($this->ids);
+
+        //create contributions
+        $this->createContribution();
+        $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);
+
+        $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->array($ireminders->getList($this->zdb))->isIdenticalTo([]);
+        $this->array($lreminders->getList($this->zdb))->isEqualTo($rlist);
+
+        //create already sent impending reminder 2 months ago
+        $now = new \DateTime();
+        $send = clone $now;
+        $send->sub(new \DateInterval('P2M'));
+        $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);
+
+        $rlist = $reminders->getList($this->zdb);
+        $this->array($rlist)->hasSize(1);
+        $reminder = current($rlist);
+
+        $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 already sent laste reminder 2 days ago
+        $send = clone $now;
+        $send->sub(new \DateInterval('P2D'));
+        $data = array(
+            'reminder_type'     => \Galette\Entity\Reminder::LATE,
+            '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);
+
+        $rlist = $reminders->getList($this->zdb);
+        $this->array($rlist)->isIdenticalTo([]);
+    }
+
+    /**
+     * Create test user in database
+     *
+     * @return void
+     */
+    private function createAdherent()
+    {
+        $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->fakeMember();
+        $this->createMember($data);
+        $this->checkMemberExpected();
+    }
+
+    /**
+     * Create member from data
+     *
+     * @param array $data Data to use to create member
+     *
+     * @return \Galette\Entity\Adherent
+     */
+    public function createMember(array $data)
+    {
+        $adh = $this->adh;
+        $check = $adh->check($data, [], []);
+        if (is_array($check)) {
+            var_dump($check);
+        }
+        $this->boolean($check)->isTrue();
+
+        $store = $adh->store();
+        $this->boolean($store)->isTrue();
+
+        $this->ids[] = $adh->id;
+    }
+
+    /**
+     * Check members expecteds
+     *
+     * @param Adherent $adh           Member instance, if any
+     * @param array    $new_expecteds Changes on expected values
+     *
+     * @return void
+     */
+    private function checkMemberExpected($adh = null, $new_expecteds = [])
+    {
+        if ($adh === null) {
+            $adh = $this->adh;
+        }
+
+        $expecteds = [
+            'nom_adh' => 'Hoarau',
+            'prenom_adh' => 'Lucas',
+            'ville_adh' => 'Reynaudnec',
+            'cp_adh' => '63077',
+            'adresse_adh' => '2, boulevard Legros',
+            'email_adh' => 'phoarau@tele2.fr',
+            'login_adh' => 'nathalie51',
+            'mdp_adh' => 'T.u!IbKOi|06',
+            'bool_admin_adh' => false,
+            'bool_exempt_adh' => false,
+            'bool_display_info' => false,
+            'sexe_adh' => 1,
+            'prof_adh' => 'Extraction',
+            'titre_adh' => null,
+            'ddn_adh' => '1992-02-22',
+            'lieu_naissance' => 'Fischer',
+            'pseudo_adh' => 'vallet.camille',
+            'pays_adh' => '',
+            'tel_adh' => '05 59 53 59 43',
+            'url_adh' => 'http://bodin.net/omnis-ratione-sint-dolorem-architecto',
+            'activite_adh' => true,
+            'id_statut' => 9,
+            'pref_lang' => 'ca',
+            'fingerprint' => 'FAKER' . $this->seed,
+            'societe_adh' => 'Philippe'
+        ];
+        $expecteds = array_merge($expecteds, $new_expecteds);
+
+        foreach ($expecteds as $key => $value) {
+            $property = $this->members_fields[$key]['propname'];
+            switch ($key) {
+                case 'bool_admin_adh':
+                    $this->boolean($adh->isAdmin())->isIdenticalTo($value);
+                    break;
+                case 'bool_exempt_adh':
+                    $this->boolean($adh->isDueFree())->isIdenticalTo($value);
+                    break;
+                case 'bool_display_info':
+                    $this->boolean($adh->appearsInMembersList())->isIdenticalTo($value);
+                    break;
+                case 'activite_adh':
+                    $this->boolean($adh->isActive())->isIdenticalTo($value);
+                    break;
+                case 'mdp_adh':
+                    $pw_checked = password_verify($value, $adh->password);
+                    $this->boolean($pw_checked)->isTrue();
+                    break;
+                case 'ddn_adh':
+                    //rely on age, not on birthdate
+                    $this->variable($adh->$property)->isNotNull();
+                    $this->string($adh->getAge())->isIdenticalTo(' (28 years old)');
+                    break;
+                default:
+                    $this->variable($adh->$property)->isIdenticalTo(
+                        $value,
+                        "$property expected {$value} got {$adh->$property}"
+                    );
+                    break;
+            }
+        }
+
+        $d = \DateTime::createFromFormat('Y-m-d', $expecteds['ddn_adh']);
+
+        $expected_str = ' (28 years old)';
+        $this->string($adh->getAge())->isIdenticalTo($expected_str);
+        $this->boolean($adh->hasChildren())->isFalse();
+        $this->boolean($adh->hasParent())->isFalse();
+        $this->boolean($adh->hasPicture())->isFalse();
+
+        $this->string($adh->sadmin)->isIdenticalTo('No');
+        $this->string($adh->sdue_free)->isIdenticalTo('No');
+        $this->string($adh->sappears_in_list)->isIdenticalTo('No');
+        $this->string($adh->sstaff)->isIdenticalTo('No');
+        $this->string($adh->sactive)->isIdenticalTo('Active');
+        $this->variable($adh->stitle)->isNull();
+        $this->string($adh->sstatus)->isIdenticalTo('Non-member');
+        $this->string($adh->sfullname)->isIdenticalTo('HOARAU Lucas');
+        $this->string($adh->saddress)->isIdenticalTo('2, boulevard Legros');
+        $this->string($adh->sname)->isIdenticalTo('HOARAU Lucas');
+
+        $this->string($adh->getAddress())->isIdenticalTo($expecteds['adresse_adh']);
+        $this->string($adh->getAddressContinuation())->isEmpty();
+        $this->string($adh->getZipcode())->isIdenticalTo($expecteds['cp_adh']);
+        $this->string($adh->getTown())->isIdenticalTo($expecteds['ville_adh']);
+        $this->string($adh->getCountry())->isIdenticalTo($expecteds['pays_adh']);
+
+        $this->string($adh::getSName($this->zdb, $adh->id))->isIdenticalTo('HOARAU Lucas');
+        $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
+     *
+     * @param array $data Data to use to create contribution
+     *
+     * @return \Galette\Entity\Contribution
+     */
+    public function createContrib(array $data)
+    {
+        $contrib = $this->contrib;
+        $check = $contrib->check($data, [], []);
+        if (is_array($check)) {
+            var_dump($check);
+        }
+        $this->boolean($check)->isTrue();
+
+        $store = $contrib->store();
+        $this->boolean($store)->isTrue();
+
+        $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();
+    }
+}