]> git.agnieray.net Git - galette.git/blob - tests/Galette/Repository/tests/units/Groups.php
84cc4159e242ab2a2e7f6357b6aa43f99b22c0f0
[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
60 /**
61 * Tear down tests
62 *
63 * @return void
64 */
65 public function tearDown()
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 $groups = $this->groupsProvider();
80 foreach ($groups as $group) {
81 foreach ($group['children'] as $child) {
82 $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
83 $delete->where->in('group_name', $child);
84 $zdb->execute($delete);
85 }
86 $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
87 $delete->where->in('group_name', array_keys($group['children']));
88 $zdb->execute($delete);
89 }
90
91 $delete = $zdb->delete(\Galette\Entity\Group::TABLE);
92 $zdb->execute($delete);
93
94 //Clean logs
95 $zdb->db->query(
96 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
97 \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
98 );
99 }
100
101 /**
102 * Groups provider
103 *
104 * @return array[]
105 */
106 protected function groupsProvider(): array
107 {
108 return [
109 [
110 'parent_name' => 'Europe',
111 'children' => [
112 'France' => [
113 'Nord',
114 'Hérault',
115 'Vaucluse',
116 'Gironde'
117 ],
118 'Belgique' => [
119 'Wallonie',
120 'Flandres'
121 ],
122 'Allemagne' => []
123 ]
124 ], [
125 'parent_name' => 'Afrique',
126 'children' => []
127 ], [
128 'parent_name' => 'Amérique',
129 'children' => [
130 'États-unis' => [
131 'Californie',
132 'Ohio',
133 'Massachusetts'
134 ],
135 'Mexique' => []
136 ]
137 ]
138 ];
139 }
140
141 /**
142 * Create groups for tests
143 *
144 * @param string $parent_name Parent name
145 * @param array $children Children
146 *
147 * @dataProvider groupsProvider
148 *
149 * @return void
150 */
151 public function testCreateGroups(string $parent_name, array $children)
152 {
153 $group = new \Galette\Entity\Group();
154 $group->setName($parent_name);
155 $this->boolean($group->store())->isTrue();
156 $parent_id = $group->getId();
157 $this->parents[] = $group->getId();
158
159 foreach ($children as $child => $subchildren) {
160 $group = new \Galette\Entity\Group();
161 $group->setName($child);
162 $group->setParentGroup($parent_id);
163 $this->boolean($group->store())->isTrue();
164 $sub_id = $group->getId();
165 $this->children[] = $group->getId();
166
167 foreach ($subchildren as $subchild) {
168 $group = new \Galette\Entity\Group();
169 $group->setName($subchild);
170 $group->setParentGroup($sub_id);
171 $this->boolean($group->store())->isTrue();
172 $this->subchildren[] = $group->getId();
173 }
174 }
175 }
176
177 /**
178 * Test getSimpleList
179 *
180 * @return void
181 */
182 public function testGetSimpleList()
183 {
184 $list = \Galette\Repository\Groups::getSimpleList();
185 $this->array($list)->hasSize(17);
186
187 foreach ($list as $group_name) {
188 $this->string($group_name)->isNotEmpty();
189 }
190
191 $list = \Galette\Repository\Groups::getSimpleList(true);
192 $this->array($list)->hasSize(17);
193 foreach ($list as $group) {
194 $this->object($group)->isInstanceOf(\Galette\Entity\Group::class);
195 }
196 }
197
198 /**
199 * Test getSimpleList
200 *
201 * @return void
202 */
203 public function testGetList()
204 {
205 $this->logSuperAdmin();
206 $groups = new \Galette\Repository\Groups($this->zdb, $this->login);
207
208 $parents_list = $groups->getList(false);
209 $this->array($parents_list)->hasSize(3);
210
211 $parents_list = $groups->getList(true);
212 $this->array($parents_list)->hasSize(17);
213
214 $select = $this->zdb->select(\Galette\Entity\Group::TABLE);
215 $select->where(['group_name' => 'Europe']);
216 $result = $this->zdb->execute($select)->current();
217 $europe = $result->{\Galette\Entity\Group::PK};
218
219 $children_list = $groups->getList(true, $europe);
220 $this->array($children_list)->hasSize(4);
221 }
222
223 /**
224 * Test group name unicity
225 *
226 * @return void
227 */
228 public function testUnicity()
229 {
230 $group = new \Galette\Entity\Group();
231 $group->setLogin($this->login);
232 $unique_name = 'One group to rule them all';
233 $group->setName($unique_name);
234 $this->boolean($group->store())->isTrue();
235 $group_id = $group->getId();
236
237 $select = $this->zdb->select(\Galette\Entity\Group::TABLE);
238 $select->where(['group_name' => 'Europe']);
239 $result = $this->zdb->execute($select)->current();
240 $europe = $result->{\Galette\Entity\Group::PK};
241
242 $select = $this->zdb->select(\Galette\Entity\Group::TABLE);
243 $select->where(['group_name' => 'France']);
244 $result = $this->zdb->execute($select)->current();
245 $france = $result->{\Galette\Entity\Group::PK};
246
247 //name already exists - not unique
248 $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, $unique_name))->isFalse();
249 //name does not exist on another level - unique
250 $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, $unique_name, $europe))->isTrue();
251 //name is the current one - unique
252 $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, $unique_name, null, $group_id))->isTrue();
253
254 //tests on another level
255 $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, 'Nord', $france))->isFalse();
256 $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, 'Creuse', $france))->isTrue();
257 }
258 }