]> git.agnieray.net Git - galette.git/blob - tests/Galette/Repository/tests/units/Groups.php
Remove 'svn' lines
[galette.git] / tests / Galette / Repository / tests / units / Groups.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Groups repository tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2021-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Repository
28 * @package GaletteTests
29 *
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
34 * @since 2021-11-10
35 */
36
37 namespace Galette\Repository\test\units;
38
39 use Galette\GaletteTestCase;
40
41 /**
42 * Groups repository tests
43 *
44 * @category Repository
45 * @name Groups
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
51 * @since 2021-11-10
52 */
53 class Groups extends GaletteTestCase
54 {
55 private array $parents = [];
56 private array $children = [];
57 private array $subchildren = [];
58 protected int $seed = 855224771456;
59
60 /**
61 * Tear down tests
62 *
63 * @return void
64 */
65 public function tearDown(): void
66 {
67 $this->deleteGroups();
68 }
69
70 /**
71 * Delete groups
72 *
73 * @return void
74 */
75 private function deleteGroups()
76 {
77 $zdb = new \Galette\Core\Db();
78
79 //Clean managers
80 $zdb->db->query(
81 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Entity\Group::GROUPSMANAGERS_TABLE,
82 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
83 );
84
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);
91 }
92 $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
93 $delete->where->in('group_name', array_keys($group['children']));
94 $zdb->execute($delete);
95 }
96
97 $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
98 $zdb->execute($delete);
99
100 $delete = $zdb->delete(\Galette\Entity\Adherent::TABLE);
101 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
102 $zdb->execute($delete);
103
104 //Clean logs
105 $zdb->db->query(
106 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
107 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
108 );
109 }
110
111 /**
112 * Groups provider
113 *
114 * @return array[]
115 */
116 public static function groupsProvider(): array
117 {
118 return [
119 [
120 'parent_name' => 'Europe',
121 'children' => [
122 'France' => [
123 'Nord',
124 'Hérault',
125 'Vaucluse',
126 'Gironde'
127 ],
128 'Belgique' => [
129 'Wallonie',
130 'Flandres'
131 ],
132 'Allemagne' => []
133 ]
134 ], [
135 'parent_name' => 'Afrique',
136 'children' => []
137 ], [
138 'parent_name' => 'Amérique',
139 'children' => [
140 'États-unis' => [
141 'Californie',
142 'Ohio',
143 'Massachusetts'
144 ],
145 'Mexique' => []
146 ]
147 ]
148 ];
149 }
150
151 /**
152 * Create groups for tests
153 *
154 * @param string $parent_name Parent name
155 * @param array $children Children
156 *
157 * @dataProvider groupsProvider
158 *
159 * @return void
160 */
161 public function testCreateGroups(string $parent_name, array $children)
162 {
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();
168
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();
176
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();
183 }
184 }
185 }
186
187 /**
188 * Test getSimpleList
189 *
190 * @return void
191 */
192 public function testGetSimpleList()
193 {
194 $groups = self::groupsProvider();
195 foreach ($groups as $group) {
196 $this->testCreateGroups($group['parent_name'], $group['children']);
197 }
198
199 $list = \Galette\Repository\Groups::getSimpleList();
200 $this->assertCount(17, $list);
201
202 foreach ($list as $group_name) {
203 $this->assertNotEmpty($group_name);
204 }
205
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);
210 }
211 }
212
213 /**
214 * Test getSimpleList
215 *
216 * @return void
217 */
218 public function testGetList()
219 {
220 $this->logSuperAdmin();
221
222 $groups = self::groupsProvider();
223 foreach ($groups as $group) {
224 $this->testCreateGroups($group['parent_name'], $group['children']);
225 }
226
227 $groups = new \Galette\Repository\Groups($this->zdb, $this->login);
228
229 $parents_list = $groups->getList(false);
230 $this->assertCount(3, $parents_list);
231
232 $parents_list = $groups->getList(true);
233 $this->assertCount(17, $parents_list);
234
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};
239
240 $children_list = $groups->getList(true, $europe);
241 $this->assertCount(4, $children_list);
242
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]));
247
248 $this->login->impersonate($this->adh->id);
249
250 $groups = new \Galette\Repository\Groups($this->zdb, $this->login);
251 $parents_list = $groups->getList();
252 $this->assertCount(1, $parents_list);
253 }
254
255 /**
256 * Test group name uniqueness
257 *
258 * @return void
259 */
260 public function testUniqueness()
261 {
262 $groups = self::groupsProvider();
263 foreach ($groups as $group) {
264 $this->testCreateGroups($group['parent_name'], $group['children']);
265 }
266
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();
273
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};
278
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};
283
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));
290
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));
294 }
295 }