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 = [];
59 protected $seed = '855224771456';
66 public function tearDown()
68 $this->deleteGroups();
76 private function deleteGroups()
78 $zdb = new \Galette\Core\
Db();
82 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Entity\Group
::GROUPSMANAGERS_TABLE
,
83 \Zend\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
86 $groups = $this->groupsProvider();
87 foreach ($groups as $group) {
88 foreach ($group['children'] as $child) {
89 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
90 $delete->where
->in('group_name', $child);
91 $zdb->execute($delete);
93 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
94 $delete->where
->in('group_name', array_keys($group['children']));
95 $zdb->execute($delete);
98 $delete = $zdb->delete(\Galette\Entity\Group
::TABLE
);
99 $zdb->execute($delete);
101 $delete = $zdb->delete(\Galette\Entity\Adherent
::TABLE
);
102 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
103 $zdb->execute($delete);
107 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Core\History
::TABLE
,
108 \Zend\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
117 protected function groupsProvider(): array
121 'parent_name' => 'Europe',
136 'parent_name' => 'Afrique',
139 'parent_name' => 'Amérique',
153 * Create groups for tests
155 * @param string $parent_name Parent name
156 * @param array $children Children
158 * @dataProvider groupsProvider
162 public function testCreateGroups(string $parent_name, array $children)
164 $group = new \Galette\Entity\
Group();
165 $group->setName($parent_name);
166 $this->boolean($group->store())->isTrue();
167 $parent_id = $group->getId();
168 $this->parents
[] = $group->getId();
170 foreach ($children as $child => $subchildren) {
171 $group = new \Galette\Entity\
Group();
172 $group->setName($child);
173 $group->setParentGroup($parent_id);
174 $this->boolean($group->store())->isTrue();
175 $sub_id = $group->getId();
176 $this->children
[] = $group->getId();
178 foreach ($subchildren as $subchild) {
179 $group = new \Galette\Entity\
Group();
180 $group->setName($subchild);
181 $group->setParentGroup($sub_id);
182 $this->boolean($group->store())->isTrue();
183 $this->subchildren
[] = $group->getId();
193 public function testGetSimpleList()
195 $list = \Galette\Repository\Groups
::getSimpleList();
196 $this->array($list)->hasSize(17);
198 foreach ($list as $group_name) {
199 $this->string($group_name)->isNotEmpty();
202 $list = \Galette\Repository\Groups
::getSimpleList(true);
203 $this->array($list)->hasSize(17);
204 foreach ($list as $group) {
205 $this->object($group)->isInstanceOf(\Galette\Entity\Group
::class);
214 public function testGetList()
216 $this->logSuperAdmin();
218 $groups = new \Galette\Repository\
Groups($this->zdb
, $this->login
);
220 $parents_list = $groups->getList(false);
221 $this->array($parents_list)->hasSize(3);
223 $parents_list = $groups->getList(true);
224 $this->array($parents_list)->hasSize(17);
226 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
227 $select->where(['group_name' => 'Europe']);
228 $result = $this->zdb
->execute($select)->current();
229 $europe = $result->{\Galette\Entity\Group
::PK
};
231 $children_list = $groups->getList(true, $europe);
232 $this->array($children_list)->hasSize(4);
234 //set manager on one group, impersonate him, and check it gets only one group
235 $this->getMemberOne();
236 $group = new \Galette\Entity\
Group((int)$europe);
237 $this->boolean($group->setManagers([$this->adh
]))->isTrue();
239 $this->login
->impersonate($this->adh
->id
);
241 $groups = new \Galette\Repository\
Groups($this->zdb
, $this->login
);
242 $parents_list = $groups->getList();
243 $this->array($parents_list)->hasSize(1);
247 * Test group name uniqueness
251 public function testUniqueness()
253 $group = new \Galette\Entity\
Group();
254 $group->setLogin($this->login
);
255 $unique_name = 'One group to rule them all';
256 $group->setName($unique_name);
257 $this->boolean($group->store())->isTrue();
258 $group_id = $group->getId();
260 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
261 $select->where(['group_name' => 'Europe']);
262 $result = $this->zdb
->execute($select)->current();
263 $europe = $result->{\Galette\Entity\Group
::PK
};
265 $select = $this->zdb
->select(\Galette\Entity\Group
::TABLE
);
266 $select->where(['group_name' => 'France']);
267 $result = $this->zdb
->execute($select)->current();
268 $france = $result->{\Galette\Entity\Group
::PK
};
270 //name already exists - not unique
271 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name))->isFalse();
272 //name does not exist on another level - unique
273 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name, $europe))->isTrue();
274 //name is the current one - unique
275 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, $unique_name, null, $group_id))->isTrue();
277 //tests on another level
278 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, 'Nord', $france))->isFalse();
279 $this->boolean(\Galette\Repository\Groups
::isUnique($this->zdb
, 'Creuse', $france))->isTrue();