]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Group.php
6721231f289214b3fb329cbd97cc2484e709afe9
[galette.git] / tests / Galette / Entity / tests / units / Group.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Galette\Entity\test\units;
23
24 use PHPUnit\Framework\TestCase;
25 use Galette\GaletteTestCase;
26 use Laminas\Db\Adapter\Adapter;
27
28 /**
29 * Group tests
30 *
31 * @author Johan Cwiklinski <johan@x-tnd.be>
32 */
33 class Group extends GaletteTestCase
34 {
35 protected array $excluded_after_methods = ['testUnicity'];
36
37 /**
38 * Tear down tests
39 *
40 * @return void
41 */
42 public function tearDown(): void
43 {
44 $this->deleteGroups();
45 parent::tearDown();
46 }
47
48 /**
49 * Delete groups
50 *
51 * @return void
52 */
53 private function deleteGroups()
54 {
55 $delete = $this->zdb->delete(\Galette\Entity\Group::TABLE);
56 $delete->where('parent_group IS NOT NULL');
57 $this->zdb->execute($delete);
58
59 $delete = $this->zdb->delete(\Galette\Entity\Group::TABLE);
60 $this->zdb->execute($delete);
61
62 //Clean logs
63 $this->zdb->db->query(
64 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
65 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
66 );
67 }
68
69 /**
70 * Test empty group
71 *
72 * @return void
73 */
74 public function testGroup()
75 {
76 global $zdb;
77 $zdb = $this->zdb;
78
79 $group = new \Galette\Entity\Group();
80 $this->logSuperAdmin();
81 $group->setLogin($this->login);
82 //$this->assertFalse($group->isManager($this->login));
83 $this->assertNull($group->getId());
84 $this->assertSame(0, $group->getLevel());
85 $this->assertNull($group->getName());
86 $this->assertNull($group->getFullName());
87 $this->assertNull($group->getIndentName());
88 $this->assertNull($group->getParentGroup());
89 }
90
91 /**
92 * Test single group
93 *
94 * @return void
95 */
96 public function testSingleGroup()
97 {
98 global $zdb;
99 $zdb = $this->zdb;
100
101 $group = new \Galette\Entity\Group();
102 $group->setLogin($this->login);
103
104 $group->setName('A group');
105 $this->assertTrue($group->store());
106
107 $this->assertFalse($group->isManager($this->login));
108 $group_id = $group->getId();
109 $this->assertGreaterThan(0, $group_id);
110 $this->assertSame(0, $group->getLevel());
111 $this->assertSame('A group', $group->getName());
112 $this->assertSame('A group', $group->getFullName());
113 $this->assertSame('A group', $group->getIndentName());
114 $this->assertEmpty($group->getMembers());
115 $this->assertSame(0, $group->getMemberCount());
116 $this->assertEmpty($group->getManagers());
117 $this->assertEmpty($group->getGroups());
118 $this->assertNull($group->getParentGroup());
119
120 //edit group
121 $group = new \Galette\Entity\Group();
122 $this->assertTrue($group->load($group_id));
123 $this->assertSame('A group', $group->getName());
124
125 $group->setName('A group - edited');
126 $this->assertTrue($group->store());
127
128 $group = new \Galette\Entity\Group($group_id);
129 $this->assertSame('A group - edited', $group->getName());
130 }
131
132 /**
133 * Test group name unicity
134 *
135 * @return void
136 */
137 public function testUnicity()
138 {
139 global $zdb;
140 $zdb = $this->zdb;
141
142 $group = new \Galette\Entity\Group();
143 $group->setLogin($this->login);
144
145 $group->setName('A group');
146 $this->assertTrue($group->store());
147 $group_id = $group->getId();
148
149 //update without changes should be ok
150 $group = new \Galette\Entity\Group($group_id);
151 $this->assertTrue($group->store());
152
153 //Adding another group with same name throws an exception
154 $group = new \Galette\Entity\Group();
155 $group->setLogin($this->login);
156
157 $this->expectExceptionMessage('The group name you have requested already exists in the database.');
158 $group->setName('A group');
159 $this->assertFalse($group->store());
160
161 //update with changes should be ok
162 $group = new \Galette\Entity\Group($group_id);
163 $group->setName('A group - edited');
164 $this->assertTrue($group->store());
165
166 $group = new \Galette\Entity\Group();
167 $group->setName('Unique one');
168 $this->assertTrue($group->store());
169
170 //editing using an existing name is not ok
171 $this->expectExceptionMessage('The group name you have requested already exists in the database.');
172 $group->setName('A group - edited');
173 $this->assertFalse($group->store());
174 }
175
176 /**
177 * Test sub groups
178 *
179 * @return void
180 */
181 public function testSubGroup()
182 {
183 global $zdb;
184 $zdb = $this->zdb;
185
186 $group = new \Galette\Entity\Group();
187 $group->setName('A parent group');
188 $this->assertTrue($group->store());
189 $parent_id = $group->getId();
190
191 $group = new \Galette\Entity\Group();
192 $group->setName('A child group');
193 $group->setParentGroup($parent_id);
194 $this->assertTrue($group->store());
195 $child_id_1 = $group->getId();
196 $this->assertSame($parent_id, $group->getParentGroup()->getId());
197
198 $group = new \Galette\Entity\Group();
199 $group->setName('Another child group');
200 $this->assertTrue($group->store());
201 $child_id_2 = $group->getId();
202
203 $group->setParentGroup($parent_id);
204 $this->assertTrue($group->store());
205 $this->assertSame($parent_id, $group->getParentGroup()->getId());
206
207 //non-logged-in will not see children groups
208 $group = new \Galette\Entity\Group($parent_id);
209 $group->setLogin($this->login);
210 $children = $group->getGroups();
211 $this->assertCount(0, $children);
212
213 //admin will not see children groups
214 $group = new \Galette\Entity\Group($parent_id);
215 $this->logSuperAdmin();
216 $group->setLogin($this->login);
217 $children = $group->getGroups();
218 $this->assertCount(2, $children);
219
220 $group = new \Galette\Entity\Group($child_id_1);
221 $this->assertTrue($group->detach());
222
223 $group = new \Galette\Entity\Group($parent_id);
224 $this->logSuperAdmin();
225 $group->setLogin($this->login);
226 $children = $group->getGroups();
227 $this->assertCount(1, $children);
228 $this->assertSame('Another child group', $children[0]->getName());
229
230 $group = new \Galette\Entity\Group($child_id_2);
231 $this->assertSame(['A parent group'], $group->getParents());
232
233 $group = new \Galette\Entity\Group();
234 $group->setName('A second level child group');
235 $group->setParentGroup($child_id_2);
236 $this->assertTrue($group->store());
237 $child_id_3 = $group->getId();
238 $this->assertSame($child_id_2, $group->getParentGroup()->getId());
239
240 $group = new \Galette\Entity\Group($child_id_3);
241 $this->assertSame(['A parent group', 'Another child group'], $group->getParents());
242 $this->assertTrue($group->detach());
243 }
244
245 /**
246 * Test removal
247 *
248 * @return void
249 */
250 public function testRemove()
251 {
252 global $zdb;
253 $zdb = $this->zdb;
254
255 $group = new \Galette\Entity\Group();
256 $group->setName('A parent group');
257 $this->assertTrue($group->store());
258 $parent_id = $group->getId();
259
260 $group = new \Galette\Entity\Group();
261 $group->setName('A child group');
262 $group->setParentGroup($parent_id);
263 $this->assertTrue($group->store());
264 $child_id_1 = $group->getId();
265 $this->assertSame($parent_id, $group->getParentGroup()->getId());
266
267 $group = new \Galette\Entity\Group($parent_id);
268 $this->logSuperAdmin();
269 $group->setLogin($this->login);
270 $this->assertFalse($group->remove()); //still have children, not removed
271 $this->assertTrue($group->load($parent_id));
272 $this->assertTrue($group->remove(true)); //cascade removal, all will be removed
273 $this->assertFalse($group->load($parent_id));
274 }
275 }