]> git.agnieray.net Git - galette.git/blob - tests/Galette/Repository/tests/units/Groups.php
Fix query, replace aliases
[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 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 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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2021-11-10
36 */
37
38 namespace Galette\Repository\test\units;
39
40 use Galette\GaletteTestCase;
41
42 /**
43 * Groups repository tests
44 *
45 * @category Repository
46 * @name Groups
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
52 * @since 2021-11-10
53 */
54 class Groups extends GaletteTestCase
55 {
56 private $parents = [];
57 private $children = [];
58 private $subchildren = [];
59 protected $seed = '855224771456';
60
61 /**
62 * Tear down tests
63 *
64 * @return void
65 */
66 public function tearDown()
67 {
68 $this->deleteGroups();
69 }
70
71 /**
72 * Delete groups
73 *
74 * @return void
75 */
76 private function deleteGroups()
77 {
78 $zdb = new \Galette\Core\Db();
79
80 //Clean managers
81 $zdb->db->query(
82 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Entity\Group::GROUPSMANAGERS_TABLE,
83 \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
84 );
85
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);
92 }
93 $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
94 $delete->where->in('group_name', array_keys($group['children']));
95 $zdb->execute($delete);
96 }
97
98 $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
99 $zdb->execute($delete);
100
101 $delete = $zdb->delete(\Galette\Entity\Adherent::TABLE);
102 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
103 $zdb->execute($delete);
104
105 //Clean logs
106 $zdb->db->query(
107 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
108 \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
109 );
110 }
111
112 /**
113 * Groups provider
114 *
115 * @return array[]
116 */
117 protected function groupsProvider(): array
118 {
119 return [
120 [
121 'parent_name' => 'Europe',
122 'children' => [
123 'France' => [
124 'Nord',
125 'Hérault',
126 'Vaucluse',
127 'Gironde'
128 ],
129 'Belgique' => [
130 'Wallonie',
131 'Flandres'
132 ],
133 'Allemagne' => []
134 ]
135 ], [
136 'parent_name' => 'Afrique',
137 'children' => []
138 ], [
139 'parent_name' => 'Amérique',
140 'children' => [
141 'États-unis' => [
142 'Californie',
143 'Ohio',
144 'Massachusetts'
145 ],
146 'Mexique' => []
147 ]
148 ]
149 ];
150 }
151
152 /**
153 * Create groups for tests
154 *
155 * @param string $parent_name Parent name
156 * @param array $children Children
157 *
158 * @dataProvider groupsProvider
159 *
160 * @return void
161 */
162 public function testCreateGroups(string $parent_name, array $children)
163 {
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();
169
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();
177
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();
184 }
185 }
186 }
187
188 /**
189 * Test getSimpleList
190 *
191 * @return void
192 */
193 public function testGetSimpleList()
194 {
195 $list = \Galette\Repository\Groups::getSimpleList();
196 $this->array($list)->hasSize(17);
197
198 foreach ($list as $group_name) {
199 $this->string($group_name)->isNotEmpty();
200 }
201
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);
206 }
207 }
208
209 /**
210 * Test getSimpleList
211 *
212 * @return void
213 */
214 public function testGetList()
215 {
216 $this->logSuperAdmin();
217
218 $groups = new \Galette\Repository\Groups($this->zdb, $this->login);
219
220 $parents_list = $groups->getList(false);
221 $this->array($parents_list)->hasSize(3);
222
223 $parents_list = $groups->getList(true);
224 $this->array($parents_list)->hasSize(17);
225
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};
230
231 $children_list = $groups->getList(true, $europe);
232 $this->array($children_list)->hasSize(4);
233
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();
238
239 $this->login->impersonate($this->adh->id);
240
241 $groups = new \Galette\Repository\Groups($this->zdb, $this->login);
242 $parents_list = $groups->getList();
243 $this->array($parents_list)->hasSize(1);
244 }
245
246 /**
247 * Test group name uniqueness
248 *
249 * @return void
250 */
251 public function testUniqueness()
252 {
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();
259
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};
264
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};
269
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();
276
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();
280 }
281 }