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