]> git.agnieray.net Git - galette.git/commitdiff
Handle sequence on PostgreSQL for titles; closes #1374 refs #1158
authorJohan Cwiklinski <jcwiklinski@teclib.com>
Sun, 15 Dec 2019 07:52:54 +0000 (08:52 +0100)
committerJohan Cwiklinski <jcwiklinski@teclib.com>
Tue, 17 Dec 2019 23:40:17 +0000 (00:40 +0100)
Add nit tests
Set ID after add new title

galette/lib/Galette/Core/Db.php
galette/lib/Galette/Entity/Entitled.php
galette/lib/Galette/Entity/Title.php
galette/lib/Galette/Repository/Titles.php
galette/lib/Galette/Util/FakeData.php
tests/Galette/Entity/tests/units/Title.php [new file with mode: 0644]

index 0e279248f7ae18a76bbe6704791331d5328fc6d1..678716a970932dc8a441e40a88fa2f9d9b0054a1 100644 (file)
@@ -868,4 +868,40 @@ class Db
 
         return $infos;
     }
+
+    /**
+     * Handle sequence on PostgreSQL
+     *
+     * When inserting a value on a field with a sequence,
+     * this one is not incremented.
+     * This happens when installing system values (for status, titles, ...)
+     *
+     * @see https://bugs.galette.eu/issues/1158
+     * @see https://bugs.galette.eu/issues/1374
+     *
+     * @param sting  $table    Table name
+     * @param intger $expected Expected value
+     *
+     * @return void
+     */
+    public function handleSequence($table, $expected)
+    {
+        if ($this->isPostgres()) {
+            //check for Postgres sequence
+            //see https://bugs.galette.eu/issues/1158
+            //see https://bugs.galette.eu/issues/1374
+            $seq = $table . '_id_seq';
+
+            $select = $this->select($seq);
+            $select->columns(['last_value']);
+            $results = $this->execute($select);
+            $result = $results->current();
+            if ($result->last_value < $expected) {
+                $this->db->query(
+                    'SELECT setval(\'' . PREFIX_DB . $seq . '\', ' . $expected . ')',
+                    Adapter::QUERY_MODE_EXECUTE
+                );
+            }
+        }
+    }
 }
index b1dd83d3d441b6dfec3400ffb03f15e97ea5ee2d..37d5821362d2772741099195de329e03a5d9ca9b 100644 (file)
@@ -182,23 +182,10 @@ abstract class Entitled
             $insert->values($values);
             $stmt = $this->zdb->sql->prepareStatementForSqlObject($insert);
 
-            if ($this->zdb->isPostgres()) {
-                //check for Postgres sequence
-                //see https://bugs.galette.eu/issues/1158
-                $expected = count(static::$defaults);
-                $seq = $this->table . '_id_seq';
-
-                $select = $this->zdb->select($seq);
-                $select->columns(['last_value']);
-                $results = $this->zdb->execute($select);
-                $result = $results->current();
-                if ($result->last_value < $expected) {
-                    $this->zdb->db->query(
-                        'SELECT setval(\'' . PREFIX_DB . $seq . '\', ' . $expected . ')',
-                        Adapter::QUERY_MODE_EXECUTE
-                    );
-                }
-            }
+            $this->zdb->handleSequence(
+                $this->table,
+                count(static::$defaults)
+            );
 
             $fnames = array_keys($values);
             foreach ($class::$defaults as $d) {
index e5d36e86706647e8e0c7e97cebe4fabca02eb22e..cd72ecaf77c8606b0a691c7ac6994b4a46b5404a 100644 (file)
@@ -156,6 +156,10 @@ class Title
                     Analog::log('Not stored!', Analog::ERROR);
                     return false;
                 }
+
+                $this->id = (int)$zdb->driver->getLastGeneratedValue(
+                    PREFIX_DB . self::TABLE . '_id_seq'
+                );
             }
             return true;
         } catch (\Exception $e) {
index 25015f49ebd24f431e41600e7d698f8f01f81f9d..e2b65c4b56b6d0e7bc209d743610b22ca5ae0144 100644 (file)
@@ -139,6 +139,11 @@ class Titles
             );
             $stmt = $zdb->sql->prepareStatementForSqlObject($insert);
 
+            $zdb->handleSequence(
+                self::TABLE,
+                count(self::$defaults)
+            );
+
             foreach (self::$defaults as $d) {
                 $short = _T($d['short_label']);
                 $long = null;
index 7637ff8fe66b96c11d29f735cdeb96fac95e08bf..e87c9c3af77d99fbcfa612346ac7786cecd3e85d 100644 (file)
@@ -89,7 +89,7 @@ class FakeData
     protected $groups       = [];
     protected $mids         = [];
     protected $transactions = [];
-    protected $titles;
+    protected $titles       = [];
     protected $status;
     protected $contrib_types;
 
@@ -406,10 +406,6 @@ class FakeData
         $creation_date = $faker->dateTimeBetween($startDate = '-3 years', $endDate = 'now');
         $mdp_adh = $faker->password();
 
-        if ($this->titles === null) {
-            $this->titles = Titles::getArrayList($this->zdb);
-        }
-
         if ($this->status === null) {
             $status = new Status($this->zdb);
             $this->status = array_keys($status->getList());
diff --git a/tests/Galette/Entity/tests/units/Title.php b/tests/Galette/Entity/tests/units/Title.php
new file mode 100644 (file)
index 0000000..1603b8c
--- /dev/null
@@ -0,0 +1,150 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Titles tests
+ *
+ * PHP version 5
+ *
+ * Copyright © 2019 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 2019 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     2019-12-14
+ */
+
+namespace Galette\Entity\test\units;
+
+use \atoum;
+use Zend\Db\Adapter\Adapter;
+
+/**
+ * Status tests
+ *
+ * @category  Entity
+ * @name      Title
+ * @package   GaletteTests
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2019 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     2019-12-14
+ */
+class Title extends atoum
+{
+    private $zdb;
+    private $remove = [];
+
+    /**
+     * Set up tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function beforeTestMethod($testMethod)
+    {
+        $this->zdb = new \Galette\Core\Db();
+    }
+
+    /**
+     * Tear down tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function afterTestMethod($testMethod)
+    {
+        $this->deleteTitle();
+    }
+
+    /**
+     * Delete status
+     *
+     * @return void
+     */
+    private function deleteTitle()
+    {
+        if (is_array($this->remove) && count($this->remove) > 0) {
+            $delete = $this->zdb->delete(\Galette\Entity\Title::TABLE);
+            $delete->where->in(\Galette\Entity\Title::PK, $this->remove);
+            $this->zdb->execute($delete);
+        }
+
+        //Clean logs
+        $this->zdb->db->query(
+            'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
+            \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
+        );
+    }
+
+    /**
+     * Test title
+     *
+     * @return void
+     */
+    public function testTitle()
+    {
+        global $zdb;
+        $zdb = $this->zdb;
+
+        $titles = new \Galette\Repository\Titles($this->zdb);
+        if (count($titles->getList($this->zdb)) === 0) {
+            $res = $titles->installInit($this->zdb);
+            $this->boolean($res)->isTrue();
+        }
+
+        $title = new \Galette\Entity\Title();
+
+        $title->short = 'Te.';
+        $title->long = 'Test';
+        $this->boolean($title->store($this->zdb))->isTrue();
+
+        $id = $title->id;
+        $this->remove[] = $id;
+        $title = new \Galette\Entity\Title($id); //reload
+
+        $title->long = 'Test title';
+        $this->boolean($title->store($this->zdb))->isTrue();
+        $title = new \Galette\Entity\Title($id); //reload
+
+        $this->string($title->long)->isIdenticalTo('Test title');
+
+        $title = new \Galette\Entity\Title(\Galette\Entity\Title::MR);
+        $this->exception(
+            function () use ($title) {
+                $title->remove($this->zdb);
+            }
+        )
+            ->hasMessage('You cannot delete Mr. or Mrs. titles!')
+            ->isInstanceOf('\RuntimeException');
+
+        $title = new \Galette\Entity\Title($id); //reload
+        $this->boolean(
+            $title->remove($this->zdb)
+        )->isTrue();
+    }
+}