3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Groups repository tests
10 * Copyright © 2021-2023 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-2023 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
33 * @link http://galette.tuxfamily.org
37 namespace Galette\Repository\test\units
;
39 use Galette\GaletteTestCase
;
42 * Groups repository tests
44 * @category Repository
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2021-2023 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
53 class Groups
extends GaletteTestCase
55 private array $parents = [];
56 private array $children = [];
57 private array $subchildren = [];
58 protected int $seed = 855224771456;
65 public function tearDown(): void
67 $this->deleteGroups();
75 private function deleteGroups()
77 $zdb = new \Galette\Core\
Db();
81 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Entity\Group
::GROUPSMANAGERS_TABLE
,
82 \Laminas\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
85 $groups = self
::groupsProvider();
86 foreach ($groups as $group) {
87 foreach ($group['children'] as $child) {
88 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
89 $delete->where
->in('group_name', $child);
90 $zdb->execute($delete);
92 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
93 $delete->where
->in('group_name', array_keys($group['children']));
94 $zdb->execute($delete);
97 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
98 $zdb->execute($delete);
100 $delete = $zdb->delete(\Galette\Entity\Adherent
::TABLE
);
101 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
102 $zdb->execute($delete);
106 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Core\History
::TABLE
,
107 \Laminas\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
116 public static function groupsProvider(): array
120 'parent_name' => 'Europe',
135 'parent_name' => 'Afrique',
138 'parent_name' => 'Amérique',
152 * Create groups for tests
154 * @param string $parent_name Parent name
155 * @param array $children Children
157 * @dataProvider groupsProvider
161 public function testCreateGroups(string $parent_name, array $children)
163 $group = new \Galette\Entity\
Group();
164 $group->setName($parent_name);
165 $this->assertTrue($group->store());
166 $parent_id = $group->getId();
167 $this->parents
[] = $group->getId();
169 foreach ($children as $child => $subchildren) {
170 $group = new \Galette\Entity\
Group();
171 $group->setName($child);
172 $group->setParentGroup($parent_id);
173 $this->assertTrue($group->store());
174 $sub_id = $group->getId();
175 $this->children
[] = $group->getId();
177 foreach ($subchildren as $subchild) {
178 $group = new \Galette\Entity\
Group();
179 $group->setName($subchild);
180 $group->setParentGroup($sub_id);
181 $this->assertTrue($group->store());
182 $this->subchildren
[] = $group->getId();
192 public function testGetSimpleList()
194 $groups = self
::groupsProvider();
195 foreach ($groups as $group) {
196 $this->testCreateGroups($group['parent_name'], $group['children']);
199 $list = \Galette\Repository\Groups
::getSimpleList();
200 $this->assertCount(17, $list);
202 foreach ($list as $group_name) {
203 $this->assertNotEmpty($group_name);
206 $list = \Galette\Repository\Groups
::getSimpleList(true);
207 $this->assertCount(17, $list);
208 foreach ($list as $group) {
209 $this->assertInstanceOf(\Galette\Entity\Group
::class, $group);
218 public function testGetList()
220 $this->logSuperAdmin();
222 $groups = self
::groupsProvider();
223 foreach ($groups as $group) {
224 $this->testCreateGroups($group['parent_name'], $group['children']);
227 $groups = new \Galette\Repository\
Groups($this->zdb
, $this->login
);
229 $parents_list = $groups->getList(false);
230 $this->assertCount(3, $parents_list);
232 $parents_list = $groups->getList(true);
233 $this->assertCount(17, $parents_list);
235 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
236 $select->where(['group_name' => 'Europe']);
237 $result = $this->zdb
->execute($select)->current();
238 $europe = $result->{\Galette\Entity\Group
::PK
};
240 $children_list = $groups->getList(true, $europe);
241 $this->assertCount(4, $children_list);
243 //set manager on one group, impersonate him, and check it gets only one group
244 $this->getMemberOne();
245 $group = new \Galette\Entity\
Group((int)$europe);
246 $this->assertTrue($group->setManagers([$this->adh
]));
248 $this->login
->impersonate($this->adh
->id
);
250 $groups = new \Galette\Repository\
Groups($this->zdb
, $this->login
);
251 $parents_list = $groups->getList();
252 $this->assertCount(1, $parents_list);
256 * Test group name uniqueness
260 public function testUniqueness()
262 $groups = self
::groupsProvider();
263 foreach ($groups as $group) {
264 $this->testCreateGroups($group['parent_name'], $group['children']);
267 $group = new \Galette\Entity\
Group();
268 $group->setLogin($this->login
);
269 $unique_name = 'One group to rule them all';
270 $group->setName($unique_name);
271 $this->assertTrue($group->store());
272 $group_id = $group->getId();
274 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
275 $select->where(['group_name' => 'Europe']);
276 $result = $this->zdb
->execute($select)->current();
277 $europe = $result->{\Galette\Entity\Group
::PK
};
279 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
280 $select->where(['group_name' => 'France']);
281 $result = $this->zdb
->execute($select)->current();
282 $france = $result->{\Galette\Entity\Group
::PK
};
284 //name already exists - not unique
285 $this->assertFalse(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name));
286 //name does not exist on another level - unique
287 $this->assertTrue(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name, $europe));
288 //name is the current one - unique
289 $this->assertTrue(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name, null, $group_id));
291 //tests on another level
292 $this->assertFalse(\Galette\Repository\Groups
::isUnique($this->zdb
, 'Nord', $france));
293 $this->assertTrue(\Galette\Repository\Groups
::isUnique($this->zdb
, 'Creuse', $france));