From b5547ee63b23d1a7479922edc9e5b42498e22489 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Wed, 10 Nov 2021 23:05:55 +0100 Subject: [PATCH] Add tests on Group entity and repository --- galette/lib/Galette/Entity/Group.php | 20 +- galette/lib/Galette/Repository/Groups.php | 2 +- tests/Galette/Entity/tests/units/Group.php | 275 ++++++++++++++++++ .../Galette/Repository/tests/units/Groups.php | 222 ++++++++++++++ 4 files changed, 503 insertions(+), 16 deletions(-) create mode 100644 tests/Galette/Entity/tests/units/Group.php create mode 100644 tests/Galette/Repository/tests/units/Groups.php diff --git a/galette/lib/Galette/Entity/Group.php b/galette/lib/Galette/Entity/Group.php index 85988a8c2c..cfedcc315d 100644 --- a/galette/lib/Galette/Entity/Group.php +++ b/galette/lib/Galette/Entity/Group.php @@ -133,7 +133,7 @@ public function load($id) */ 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 @@ public function isManager(Login $login) 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 @@ public function setName($name) 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 * diff --git a/galette/lib/Galette/Repository/Groups.php b/galette/lib/Galette/Repository/Groups.php index 9839f6db86..1abb0d2107 100644 --- a/galette/lib/Galette/Repository/Groups.php +++ b/galette/lib/Galette/Repository/Groups.php @@ -420,7 +420,7 @@ public static function isUnique(Db $zdb, $name) 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 index 0000000000..1fe10346ed --- /dev/null +++ b/tests/Galette/Entity/tests/units/Group.php @@ -0,0 +1,275 @@ +. + * + * @category Repository + * @package GaletteTests + * + * @author Johan Cwiklinski + * @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 + * @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 index 0000000000..a2bb6312c3 --- /dev/null +++ b/tests/Galette/Repository/tests/units/Groups.php @@ -0,0 +1,222 @@ +. + * + * @category Repository + * @package GaletteTests + * + * @author Johan Cwiklinski + * @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 + * @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); + } +}