]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/ListsConfig.php
Remove 'svn' lines
[galette.git] / tests / Galette / Entity / tests / units / ListsConfig.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * ListsConfig tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020-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 Entity
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020-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 2020-05-16
35 */
36
37 namespace Galette\Entity\test\units;
38
39 use PHPUnit\Framework\TestCase;
40
41 /**
42 * ListsConfig tests class
43 *
44 * @category Entity
45 * @name ListsConfig
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2020-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 2020-05-16
52 */
53 class ListsConfig extends TestCase
54 {
55 private ?\Galette\Entity\ListsConfig $lists_config = null;
56 private \Galette\Core\Db $zdb;
57 private array $members_fields;
58 private array $members_fields_cats;
59 private array $default_lists = [
60 'id_adh',
61 'list_adh_name',
62 'pseudo_adh',
63 'id_statut',
64 'list_adh_contribstatus',
65 'date_modif_adh'
66 ];
67
68 /**
69 * Set up tests
70 *
71 * @return void
72 */
73 public function setUp(): void
74 {
75 $this->zdb = new \Galette\Core\Db();
76
77 include GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
78 $this->members_fields = $members_fields;
79 include GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
80 $this->members_fields_cats = $members_fields_cats;
81
82 $this->lists_config = new \Galette\Entity\ListsConfig(
83 $this->zdb,
84 \Galette\Entity\Adherent::TABLE,
85 $this->members_fields,
86 $this->members_fields_cats,
87 true
88 );
89 }
90
91 /**
92 * Tear down tests
93 *
94 * @return void
95 */
96 public function tearDown(): void
97 {
98 if (TYPE_DB === 'mysql') {
99 $this->assertSame([], $this->zdb->getWarnings());
100 }
101 $this->resetListsConfig();
102 }
103
104 /**
105 * Resets lists configuration to defaults
106 *
107 * @return void
108 */
109 private function resetListsConfig()
110 {
111 $new_list = [];
112 foreach ($this->default_lists as $key) {
113 $new_list[] = $this->lists_config->getField($key);
114 }
115
116 $this->assertTrue($this->lists_config->setListFields($new_list));
117 }
118
119 /**
120 * Test getVisibility
121 *
122 * @return void
123 */
124 public function testGetVisibility()
125 {
126 $this->lists_config->load();
127
128 $visible = $this->lists_config->getVisibility('nom_adh');
129 $this->assertSame(\Galette\Entity\FieldsConfig::NOBODY, $visible);
130
131 //must be the same than nom_adh
132 $visible = $this->lists_config->getVisibility('list_adh_name');
133 $this->assertSame(\Galette\Entity\FieldsConfig::USER_WRITE, $visible);
134
135 $visible = $this->lists_config->getVisibility('id_statut');
136 $this->assertSame(\Galette\Entity\FieldsConfig::STAFF, $visible);
137
138 //must be the same than id_statut
139 $visible = $this->lists_config->getVisibility('list_adh_contribstatus');
140 $this->assertSame(\Galette\Entity\FieldsConfig::STAFF, $visible);
141 }
142
143 /**
144 * Test setFields and storage
145 *
146 * @return void
147 */
148 public function testSetFields()
149 {
150 $lists_config = $this->lists_config;
151 $lists_config->installInit();
152 $lists_config->load();
153
154 $fields = $lists_config->getCategorizedFields();
155
156 $list = $lists_config->getListedFields();
157 $this->assertCount(6, $list);
158
159 $expecteds = $this->default_lists;
160 foreach ($expecteds as $k => $expected) {
161 $this->assertSame($expected, $list[$k]['field_id']);
162 $this->assertSame($k, $list[$k]['list_position']);
163 }
164
165 $expecteds = [
166 'id_adh',
167 'list_adh_name',
168 'email_adh',
169 'tel_adh',
170 'id_statut',
171 'list_adh_contribstatus',
172 'ville_adh'
173 ];
174
175 $new_list = [];
176 foreach ($expecteds as $key) {
177 $new_list[] = $lists_config->getField($key);
178 }
179 $this->assertTrue($lists_config->setListFields($new_list));
180
181 $list = $lists_config->getListedFields();
182 $this->assertCount(7, $list);
183
184 foreach ($expecteds as $k => $expected) {
185 $this->assertSame($expected, $list[$k]['field_id']);
186 $this->assertSame($k, $list[$k]['list_position']);
187 }
188
189 $field = $lists_config->getField('pseudo_adh');
190 $this->assertSame(-1, $field['list_position']);
191 $this->assertFalse($field['list_visible']);
192
193 $field = $lists_config->getField('date_modif_adh');
194 $this->assertSame(-1, $field['list_position']);
195 $this->assertFalse($field['list_visible']);
196
197 // copied from FieldsConfig::testSetFields to ensure it works as excpeted from here.
198 //town
199 $town = &$fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_CONTACT][2]; //3 in FieldsConfig but 2 here.
200 $this->assertSame('ville_adh', $town['field_id']);
201 $this->assertTrue($town['required']);
202 $this->assertSame(\Galette\Entity\FieldsConfig::USER_WRITE, $town['visible']);
203
204 $town['required'] = false;
205 $town['visible'] = \Galette\Entity\FieldsConfig::NOBODY;
206
207 //gsm
208 $gsm = $fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_CONTACT][5]; //6 in FieldsConfig but 5 here.
209 $gsm['position'] = count($fields[1]);
210 unset($fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_CONTACT][5]); //6 in FieldsConfig but 5 here.
211 $gsm['category'] = \Galette\Entity\FieldsCategories::ADH_CATEGORY_IDENTITY;
212 $fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_IDENTITY][] = $gsm;
213
214 $this->assertTrue($lists_config->setFields($fields));
215
216 $lists_config->load();
217 $fields = $lists_config->getCategorizedFields();
218
219 $town = $fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_CONTACT][2]; //3 in FieldsConfig but 2 here.
220 $this->assertFalse($town['required']);
221 $this->assertSame(\Galette\Entity\FieldsConfig::NOBODY, $town['visible']);
222
223 $gsm2 = $fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_IDENTITY][11]; //13 in FieldsConfig but 11 here
224 $this->assertSame($gsm, $gsm2);
225 // /copied from FieldsConfig::testSetFields to ensure it works as expected from here.
226 }
227
228 /**
229 * Test get display elements
230 *
231 * @return void
232 */
233 public function testGetDisplayElements()
234 {
235 $lists_config = $this->lists_config;
236 $lists_config->load();
237
238 //admin
239 $superadmin_login = $this->getMockBuilder(\Galette\Core\Login::class)
240 ->setConstructorArgs(array($this->zdb, new \Galette\Core\I18n()))
241 ->onlyMethods(array('isSuperAdmin'))
242 ->getMock();
243 $superadmin_login->method('isSuperAdmin')->willReturn(true);
244
245 $expecteds = $this->default_lists;
246 $elements = $lists_config->getDisplayElements($superadmin_login);
247 $this->assertCount(count($this->default_lists), $elements);
248
249 //admin
250 $admin_login = $this->getMockBuilder(\Galette\Core\Login::class)
251 ->setConstructorArgs(array($this->zdb, new \Galette\Core\I18n()))
252 ->onlyMethods(array('isAdmin'))
253 ->getMock();
254 $admin_login->method('isAdmin')->willReturn(true);
255
256 $elements = $lists_config->getDisplayElements($admin_login);
257 $this->assertCount(count($this->default_lists), $elements);
258
259 //staff
260 $staff_login = $this->getMockBuilder(\Galette\Core\Login::class)
261 ->setConstructorArgs(array($this->zdb, new \Galette\Core\I18n()))
262 ->onlyMethods(array('isStaff'))
263 ->getMock();
264 $staff_login->method('isStaff')->willReturn(true);
265
266 $elements = $lists_config->getDisplayElements($staff_login);
267 $this->assertCount(count($this->default_lists), $elements);
268
269 //following tests will have lower ACLS (cannot see status)
270 $expecteds = [
271 'id_adh',
272 'list_adh_name',
273 'pseudo_adh',
274 'date_modif_adh'
275 ];
276 $new_list = [];
277 foreach ($expecteds as $key) {
278 $new_list[] = $lists_config->getField($key);
279 }
280
281 //group manager
282 $manager_login = $this->getMockBuilder(\Galette\Core\Login::class)
283 ->setConstructorArgs(array($this->zdb, new \Galette\Core\I18n()))
284 ->onlyMethods(array('isGroupManager'))
285 ->getMock();
286 $manager_login->method('isGroupManager')->willReturn(true);
287
288 $elements = $lists_config->getDisplayElements($manager_login);
289 $this->assertCount(count($new_list), $elements);
290
291 //to keep last know rank. May switch from 2 to 6 because of field visibility.
292 $last_ok = -1;
293 foreach ($expecteds as $k => $expected) {
294 $this->assertSame($expected, $new_list[$k]['field_id']);
295 if ($new_list[$k]['list_position'] != $k - 1) {
296 $this->assertGreaterThan($last_ok, $new_list[$k]['list_position']);
297 $last_ok = $new_list[$k]['list_position'];
298 } else {
299 $this->assertSame($k, $new_list[$k]['list_position']);
300 }
301 }
302
303 //simplemember
304 $user_login = $this->getMockBuilder(\Galette\Core\Login::class)
305 ->setConstructorArgs(array($this->zdb, new \Galette\Core\I18n()))
306 ->onlyMethods(array('isUp2Date'))
307 ->getMock();
308 $user_login->method('isUp2Date')->willReturn(true);
309
310 $elements = $lists_config->getDisplayElements($user_login);
311 $this->assertCount(count($new_list), $elements);
312
313 //to keep last know rank. May switch from 2 to 6 because of field visibility.
314 $last_ok = -1;
315 foreach ($expecteds as $k => $expected) {
316 $this->assertSame($expected, $new_list[$k]['field_id']);
317 if ($new_list[$k]['list_position'] != $k - 1) {
318 $this->assertGreaterThan($last_ok, $new_list[$k]['list_position']);
319 $last_ok = $new_list[$k]['list_position'];
320 } else {
321 $this->assertSame($k, $new_list[$k]['list_position']);
322 }
323 }
324 }
325 }