4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
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.
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.
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/>.
22 namespace Galette\Entity\test\units
;
24 use PHPUnit\Framework\TestCase
;
25 use Galette\GaletteTestCase
;
26 use Laminas\Db\Adapter\Adapter
;
31 * @author Johan Cwiklinski <johan@x-tnd.be>
33 class Group
extends GaletteTestCase
35 protected array $excluded_after_methods = ['testUnicity'];
42 public function tearDown(): void
44 $this->deleteGroups();
53 private function deleteGroups()
55 $delete = $this->zdb
->delete(\Galette\Entity\Group
::TABLE
);
56 $delete->where('parent_group IS NOT NULL');
57 $this->zdb
->execute($delete);
59 $delete = $this->zdb
->delete(\Galette\Entity\Group
::TABLE
);
60 $this->zdb
->execute($delete);
63 $this->zdb
->db
->query(
64 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Core\History
::TABLE
,
65 \Laminas\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
74 public function testGroup()
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());
96 public function testSingleGroup()
101 $group = new \Galette\Entity\
Group();
102 $group->setLogin($this->login
);
104 $group->setName('A group');
105 $this->assertTrue($group->store());
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());
121 $group = new \Galette\Entity\
Group();
122 $this->assertTrue($group->load($group_id));
123 $this->assertSame('A group', $group->getName());
125 $group->setName('A group - edited');
126 $this->assertTrue($group->store());
128 $group = new \Galette\Entity\
Group($group_id);
129 $this->assertSame('A group - edited', $group->getName());
133 * Test group name unicity
137 public function testUnicity()
142 $group = new \Galette\Entity\
Group();
143 $group->setLogin($this->login
);
145 $group->setName('A group');
146 $this->assertTrue($group->store());
147 $group_id = $group->getId();
149 //update without changes should be ok
150 $group = new \Galette\Entity\
Group($group_id);
151 $this->assertTrue($group->store());
153 //Adding another group with same name throws an exception
154 $group = new \Galette\Entity\
Group();
155 $group->setLogin($this->login
);
157 $this->expectExceptionMessage('The group name you have requested already exists in the database.');
158 $group->setName('A group');
159 $this->assertFalse($group->store());
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());
166 $group = new \Galette\Entity\
Group();
167 $group->setName('Unique one');
168 $this->assertTrue($group->store());
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());
181 public function testSubGroup()
186 $group = new \Galette\Entity\
Group();
187 $group->setName('A parent group');
188 $this->assertTrue($group->store());
189 $parent_id = $group->getId();
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());
198 $group = new \Galette\Entity\
Group();
199 $group->setName('Another child group');
200 $this->assertTrue($group->store());
201 $child_id_2 = $group->getId();
203 $group->setParentGroup($parent_id);
204 $this->assertTrue($group->store());
205 $this->assertSame($parent_id, $group->getParentGroup()->getId());
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);
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);
220 $group = new \Galette\Entity\
Group($child_id_1);
221 $this->assertTrue($group->detach());
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());
230 $group = new \Galette\Entity\
Group($child_id_2);
231 $this->assertSame(['A parent group'], $group->getParents());
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());
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());
250 public function testRemove()
255 $group = new \Galette\Entity\
Group();
256 $group->setName('A parent group');
257 $this->assertTrue($group->store());
258 $parent_id = $group->getId();
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());
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));