3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Groups repository tests
10 * Copyright © 2021 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
27 * @category Repository
28 * @package GaletteTests
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2021 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Repository\test\units
;
40 use Galette\GaletteTestCase
;
43 * Groups repository tests
45 * @category Repository
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2021 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
54 class Groups
extends GaletteTestCase
56 private $parents = [];
57 private $children = [];
58 private $subchildren = [];
65 public function tearDown()
67 $this->deleteGroups();
75 private function deleteGroups()
77 $zdb = new \Galette\Core\
Db();
79 $groups = $this->groupsProvider();
80 foreach ($groups as $group) {
81 foreach ($group['children'] as $child) {
82 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
83 $delete->where
->in('group_name', $child);
84 $zdb->execute($delete);
86 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
87 $delete->where
->in('group_name', array_keys($group['children']));
88 $zdb->execute($delete);
91 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
92 $zdb->execute($delete);
96 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Core\History
::TABLE
,
97 \Zend\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
106 protected function groupsProvider(): array
110 'parent_name' => 'Europe',
125 'parent_name' => 'Afrique',
128 'parent_name' => 'Amérique',
142 * Create groups for tests
144 * @param string $parent_name Parent name
145 * @param array $children Children
147 * @dataProvider groupsProvider
151 public function testCreateGroups(string $parent_name, array $children)
153 $group = new \Galette\Entity\
Group();
154 $group->setName($parent_name);
155 $this->boolean($group->store())->isTrue();
156 $parent_id = $group->getId();
157 $this->parents
[] = $group->getId();
159 foreach ($children as $child => $subchildren) {
160 $group = new \Galette\Entity\
Group();
161 $group->setName($child);
162 $group->setParentGroup($parent_id);
163 $this->boolean($group->store())->isTrue();
164 $sub_id = $group->getId();
165 $this->children
[] = $group->getId();
167 foreach ($subchildren as $subchild) {
168 $group = new \Galette\Entity\
Group();
169 $group->setName($subchild);
170 $group->setParentGroup($sub_id);
171 $this->boolean($group->store())->isTrue();
172 $this->subchildren
[] = $group->getId();
182 public function testGetSimpleList()
184 $list = \Galette\Repository\Groups
::getSimpleList();
185 $this->array($list)->hasSize(17);
187 foreach ($list as $group_name) {
188 $this->string($group_name)->isNotEmpty();
191 $list = \Galette\Repository\Groups
::getSimpleList(true);
192 $this->array($list)->hasSize(17);
193 foreach ($list as $group) {
194 $this->object($group)->isInstanceOf(\Galette\Entity\Group
::class);
203 public function testGetList()
205 $this->logSuperAdmin();
206 $groups = new \Galette\Repository\
Groups($this->zdb
, $this->login
);
208 $parents_list = $groups->getList(false);
209 $this->array($parents_list)->hasSize(3);
211 $parents_list = $groups->getList(true);
212 $this->array($parents_list)->hasSize(17);
214 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
215 $select->where(['group_name' => 'Europe']);
216 $result = $this->zdb
->execute($select)->current();
217 $europe = $result->{\Galette\Entity\Group
::PK
};
219 $children_list = $groups->getList(true, $europe);
220 $this->array($children_list)->hasSize(4);
224 * Test group name unicity
228 public function testUnicity()
230 $group = new \Galette\Entity\
Group();
231 $group->setLogin($this->login
);
232 $unique_name = 'One group to rule them all';
233 $group->setName($unique_name);
234 $this->boolean($group->store())->isTrue();
235 $group_id = $group->getId();
237 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
238 $select->where(['group_name' => 'Europe']);
239 $result = $this->zdb
->execute($select)->current();
240 $europe = $result->{\Galette\Entity\Group
::PK
};
242 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
243 $select->where(['group_name' => 'France']);
244 $result = $this->zdb
->execute($select)->current();
245 $france = $result->{\Galette\Entity\Group
::PK
};
247 //name already exists - not unique
248 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name))->isFalse();
249 //name does not exist on another level - unique
250 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name, $europe))->isTrue();
251 //name is the current one - unique
252 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name, null, $group_id))->isTrue();
254 //tests on another level
255 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, 'Nord', $france))->isFalse();
256 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, 'Creuse', $france))->isTrue();