]> git.agnieray.net Git - galette.git/commitdiff
Add tests on Group entity and repository
authorJohan Cwiklinski <johan@x-tnd.be>
Wed, 10 Nov 2021 22:05:55 +0000 (23:05 +0100)
committerJohan Cwiklinski <johan@x-tnd.be>
Thu, 11 Nov 2021 09:05:07 +0000 (10:05 +0100)
galette/lib/Galette/Entity/Group.php
galette/lib/Galette/Repository/Groups.php
tests/Galette/Entity/tests/units/Group.php [new file with mode: 0644]
tests/Galette/Repository/tests/units/Groups.php [new file with mode: 0644]

index 85988a8c2c34eee63ad00c1661c9fbad0a979858..cfedcc315d0d8a06ddb1c1a2641b2e26cc744e34 100644 (file)
@@ -133,7 +133,7 @@ class Group
      */
     private function loadFromRS($r)
     {
-        $this->id = $r->id_group;
+        $this->id = (int)$r->id_group;
         $this->group_name = $r->group_name;
         $this->creation_date = $r->creation_date;
         if ($r->parent_group) {
@@ -485,10 +485,13 @@ class Group
             return true;
         } else {
             //let's check if current logged-in user is part of group managers
+            if (!is_array($this->managers)) {
+                $this->loadPersons(self::MANAGER_TYPE);
+            }
+
             foreach ($this->managers as $manager) {
                 if ($login->login == $manager->login) {
                     return true;
-                    break;
                 }
             }
             return false;
@@ -655,19 +658,6 @@ class Group
         return $this;
     }
 
-    /**
-     * Set all subgroups
-     *
-     * @param array $groups Groups id
-     *
-     * @return Group
-     */
-    public function setSubgroups($groups)
-    {
-        $this->groups = $groups;
-        return $this;
-    }
-
     /**
      * check if can Set parent group
      *
index 9839f6db86a0499889f09d36b29b1d1dbee5ae00..1abb0d210701f0409b3377dd94bf72fbd825b027 100644 (file)
@@ -420,7 +420,7 @@ class Groups
             return !($results->count() > 0);
         } catch (Throwable $e) {
             Analog::log(
-                'Cannot list groups (simple) | ' . $e->getMessage(),
+                'Cannot check group name unicity | ' . $e->getMessage(),
                 Analog::WARNING
             );
             throw $e;
diff --git a/tests/Galette/Entity/tests/units/Group.php b/tests/Galette/Entity/tests/units/Group.php
new file mode 100644 (file)
index 0000000..1fe1034
--- /dev/null
@@ -0,0 +1,275 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Group 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  Repository
+ * @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
+ * @version   SVN: $Id$
+ * @link      http://galette.tuxfamily.org
+ * @since     2021-11-10
+ */
+
+namespace Galette\Entity\test\units;
+
+use atoum;
+use Galette\GaletteTestCase;
+use Zend\Db\Adapter\Adapter;
+
+/**
+ * Group tests
+ *
+ * @category  Entity
+ * @name      Title
+ * @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-11-10
+ */
+class Group extends GaletteTestCase
+{
+    protected $excluded_after_methods = ['testUnicity'];
+
+    /**
+     * Tear down tests
+     *
+     * @param string $method Calling method
+     *
+     * @return void
+     */
+    public function afterTestMethod($method)
+    {
+        $this->deleteGroups();
+        parent::afterTestMethod($method);
+    }
+
+    /**
+     * Delete groups
+     *
+     * @return void
+     */
+    private function deleteGroups()
+    {
+        $delete = $this->zdb->delete(\Galette\Entity\Group::TABLE);
+        $delete->where('parent_group IS NOT NULL');
+        $this->zdb->execute($delete);
+
+        $delete = $this->zdb->delete(\Galette\Entity\Group::TABLE);
+        $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 empty group
+     *
+     * @return void
+     */
+    public function testGroup()
+    {
+        global $zdb;
+        $zdb = $this->zdb;
+
+        $group = new \Galette\Entity\Group();
+        $this->logSuperAdmin();
+        $group->setLogin($this->login);
+        //$this->boolean($group->isManager($this->login))->isFalse();
+        $this->variable($group->getId())->isNull();
+        $this->integer($group->getLevel())->isIdenticalTo(0);
+        $this->variable($group->getName())->isNull();
+        $this->variable($group->getFullName())->isNull();
+        $this->variable($group->getIndentName())->isNull();
+        $this->variable($group->getMembers())->isNull();
+        $this->variable($group->getMembers())->isNull();
+        $this->array($group->getGroups())->isEmpty();
+        $this->variable($group->getParentGroup())->isNull();
+    }
+
+    /**
+     * Test single group
+     *
+     * @return void
+     */
+    public function testSingleGroup()
+    {
+        global $zdb;
+        $zdb = $this->zdb;
+
+        $group = new \Galette\Entity\Group();
+        $group->setLogin($this->login);
+
+        $group->setName('A group');
+        $this->boolean($group->store())->isTrue();
+
+        $this->boolean($group->isManager($this->login))->isFalse();
+        $group_id = $group->getId();
+        $this->integer($group_id)->isGreaterThan(0);
+        $this->integer($group->getLevel())->isIdenticalTo(0);
+        $this->string($group->getName())->isIdenticalTo('A group');
+        $this->string($group->getFullName())->isIdenticalTo('A group');
+        $this->string($group->getIndentName())->isIdenticalTo('A group');
+        $this->array($group->getMembers())->isEmpty();
+        $this->integer($group->getMemberCount())->isIdenticalTo(0);
+        $this->array($group->getManagers())->isEmpty();
+        $this->array($group->getGroups())->isEmpty();
+        $this->variable($group->getParentGroup())->isNull();
+
+        //edit group
+        $group = new \Galette\Entity\Group();
+        $this->boolean($group->load($group_id))->isTrue();
+        $this->string($group->getName())->isIdenticalTo('A group');
+
+        $group->setName('A group - edited');
+        $this->boolean($group->store())->isTrue();
+
+        $group = new \Galette\Entity\Group($group_id);
+        $this->string($group->getName())->isIdenticalTo('A group - edited');
+    }
+
+    /**
+     * Test group name unicity
+     *
+     * @return void
+     */
+    public function testUnicity()
+    {
+        global $zdb;
+        $zdb = $this->zdb;
+
+        $group = new \Galette\Entity\Group();
+        $group->setLogin($this->login);
+
+        $group->setName('A group');
+        $this->boolean($group->store())->isTrue();
+        $group_id = $group->getId();
+
+        //Adding another group with same name throws an exception
+        $group = new \Galette\Entity\Group();
+        $group->setLogin($this->login);
+
+        $this
+            ->exception(
+                function () use ($group) {
+                    $group->setName('A group');
+                    $this->boolean($group->store())->isFalse();
+                }
+            )->hasMessage('Duplicate entry');
+    }
+
+    /**
+     * Test sub groups
+     *
+     * @return void
+     */
+    public function testSubGroup()
+    {
+        global $zdb;
+        $zdb = $this->zdb;
+
+        $group = new \Galette\Entity\Group();
+        $group->setName('A parent group');
+        $this->boolean($group->store())->isTrue();
+        $parent_id = $group->getId();
+
+        $group = new \Galette\Entity\Group();
+        $group->setName('A child group');
+        $group->setParentGroup($parent_id);
+        $this->boolean($group->store())->isTrue();
+        $child_id_1 = $group->getId();
+        $this->integer($group->getParentGroup()->getId())->isIdenticalTo($parent_id);
+
+        $group = new \Galette\Entity\Group();
+        $group->setName('Another child group');
+        $this->boolean($group->store())->isTrue();
+        $child_id_2 = $group->getId();
+
+        $group->setParentGroup($parent_id);
+        $this->boolean($group->store())->isTrue();
+        $this->integer($group->getParentGroup()->getId())->isIdenticalTo($parent_id);
+
+        //non-logged-in will not see children groups
+        $group = new \Galette\Entity\Group($parent_id);
+        $group->setLogin($this->login);
+        $children = $group->getGroups();
+        $this->array($children)->hasSize(0);
+
+        //admin will not see children groups
+        $group = new \Galette\Entity\Group($parent_id);
+        $this->logSuperAdmin();
+        $group->setLogin($this->login);
+        $children = $group->getGroups();
+        $this->array($children)->hasSize(2);
+
+        $group = new \Galette\Entity\Group($child_id_1);
+        $this->boolean($group->detach())->isTrue();
+
+        $group = new \Galette\Entity\Group($parent_id);
+        $this->logSuperAdmin();
+        $group->setLogin($this->login);
+        $children = $group->getGroups();
+        $this->array($children)->hasSize(1);
+        $this->string($children[0]->getName())->isIdenticalTo('Another child group');
+    }
+
+    /**
+     * Test removal
+     *
+     * @return void
+     */
+    public function testRemove()
+    {
+        global $zdb;
+        $zdb = $this->zdb;
+
+        $group = new \Galette\Entity\Group();
+        $group->setName('A parent group');
+        $this->boolean($group->store())->isTrue();
+        $parent_id = $group->getId();
+
+        $group = new \Galette\Entity\Group();
+        $group->setName('A child group');
+        $group->setParentGroup($parent_id);
+        $this->boolean($group->store())->isTrue();
+        $child_id_1 = $group->getId();
+        $this->integer($group->getParentGroup()->getId())->isIdenticalTo($parent_id);
+
+        $group = new \Galette\Entity\Group($parent_id);
+        $this->logSuperAdmin();
+        $group->setLogin($this->login);
+        $this->boolean($group->remove())->isFalse(); //still have children, not removed
+        $this->boolean($group->load($parent_id))->isTrue();
+        $this->boolean($group->remove(true))->isTrue(); //cascade removal, all will be removed
+        $this->boolean($group->load($parent_id))->isFalse();
+    }
+}
diff --git a/tests/Galette/Repository/tests/units/Groups.php b/tests/Galette/Repository/tests/units/Groups.php
new file mode 100644 (file)
index 0000000..a2bb631
--- /dev/null
@@ -0,0 +1,222 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Groups repository 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  Repository
+ * @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
+ * @version   SVN: $Id$
+ * @link      http://galette.tuxfamily.org
+ * @since     2021-11-10
+ */
+
+namespace Galette\Repository\test\units;
+
+use Galette\GaletteTestCase;
+
+/**
+ * Groups repository tests
+ *
+ * @category  Repository
+ * @name      Groups
+ * @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-11-10
+ */
+class Groups extends GaletteTestCase
+{
+    private $parents = [];
+    private $children = [];
+    private $subchildren = [];
+
+    /**
+     * Tear down tests
+     *
+     * @return void
+     */
+    public function tearDown()
+    {
+        $this->deleteGroups();
+    }
+
+    /**
+     * Delete groups
+     *
+     * @return void
+     */
+    private function deleteGroups()
+    {
+        $zdb = new \Galette\Core\Db();
+
+        $groups = $this->groupsProvider();
+        foreach ($groups as $group) {
+            foreach ($group['children'] as $child) {
+                $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
+                $delete->where->in('group_name', $child);
+                $zdb->execute($delete);
+            }
+            $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
+            $delete->where->in('group_name', array_keys($group['children']));
+            $zdb->execute($delete);
+        }
+
+        $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
+        $zdb->execute($delete);
+
+        //Clean logs
+        $zdb->db->query(
+            'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
+            \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
+        );
+    }
+
+    /**
+     * Groups provider
+     *
+     * @return array[]
+     */
+    protected function groupsProvider(): array
+    {
+        return [
+            [
+                'parent_name' => 'Europe',
+                'children' => [
+                    'France' => [
+                        'Nord',
+                        'Hérault',
+                        'Vaucluse',
+                        'Gironde'
+                    ],
+                    'Belgique' => [
+                        'Wallonie',
+                        'Flandres'
+                    ],
+                    'Allemagne' => []
+                ]
+            ], [
+                'parent_name' => 'Afrique',
+                'children' => []
+            ], [
+                'parent_name' => 'Amérique',
+                'children' => [
+                    'États-unis' => [
+                        'Californie',
+                        'Ohio',
+                        'Massachusetts'
+                    ],
+                    'Mexique' => []
+                ]
+            ]
+        ];
+    }
+
+    /**
+     * Create groups for tests
+     *
+     * @param string $parent_name Parent name
+     * @param array  $children    Children
+     *
+     * @dataProvider groupsProvider
+     *
+     * @return void
+     */
+    public function testCreateGroups(string $parent_name, array $children)
+    {
+        $group = new \Galette\Entity\Group();
+        $group->setName($parent_name);
+        $this->boolean($group->store())->isTrue();
+        $parent_id = $group->getId();
+        $this->parents[] = $group->getId();
+
+        foreach ($children as $child => $subchildren) {
+            $group = new \Galette\Entity\Group();
+            $group->setName($child);
+            $group->setParentGroup($parent_id);
+            $this->boolean($group->store())->isTrue();
+            $sub_id = $group->getId();
+            $this->children[] = $group->getId();
+
+            foreach ($subchildren as $subchild) {
+                $group = new \Galette\Entity\Group();
+                $group->setName($subchild);
+                $group->setParentGroup($sub_id);
+                $this->boolean($group->store())->isTrue();
+                $this->subchildren[] = $group->getId();
+            }
+        }
+    }
+
+    /**
+     * Test getSimpleList
+     *
+     * @return void
+     */
+    public function testGetSimpleList()
+    {
+        $list = \Galette\Repository\Groups::getSimpleList();
+        $this->array($list)->hasSize(17);
+
+        foreach ($list as $group_name) {
+            $this->string($group_name)->isNotEmpty();
+        }
+
+        $list = \Galette\Repository\Groups::getSimpleList(true);
+        $this->array($list)->hasSize(17);
+        foreach ($list as $group) {
+            $this->object($group)->isInstanceOf(\Galette\Entity\Group::class);
+        }
+    }
+
+    /**
+     * Test getSimpleList
+     *
+     * @return void
+     */
+    public function testGetList()
+    {
+        $this->logSuperAdmin();
+        $groups = new \Galette\Repository\Groups($this->zdb, $this->login);
+
+        $parents_list = $groups->getList(false);
+        $this->array($parents_list)->hasSize(3);
+
+        $parents_list = $groups->getList(true);
+        $this->array($parents_list)->hasSize(17);
+
+        $select = $this->zdb->select(\Galette\Entity\Group::TABLE);
+        $select->where(['group_name' => 'Europe']);
+        $result = $this->zdb->execute($select)->current();
+        $europe = $result->{\Galette\Entity\Group::PK};
+
+        $children_list = $groups->getList(true, $europe);
+        $this->array($children_list)->hasSize(4);
+    }
+}