]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/ListsConfig.php
1bde22bfdcd3a44d397f4deca3ecb1417123553f
[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 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 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 atoum;
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 2016 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 atoum
55 {
56 private $lists_config = null;
57 private $zdb;
58 private $members_fields;
59 private $members_fields_cats;
60 private $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 * @param string $method Calling method
73 *
74 * @return void
75 */
76 public function beforeTestMethod($method)
77 {
78 $this->zdb = new \Galette\Core\Db();
79
80 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
81 $this->members_fields = $members_fields;
82 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
83 $this->members_fields_cats = $members_fields_cats;
84
85 $this->lists_config = new \Galette\Entity\ListsConfig(
86 $this->zdb,
87 \Galette\Entity\Adherent::TABLE,
88 $this->members_fields,
89 $this->members_fields_cats,
90 true
91 );
92 }
93
94 /**
95 * Tear down tests
96 *
97 * @param string $method Calling method
98 *
99 * @return void
100 */
101 public function afterTestMethod($method)
102 {
103 if (TYPE_DB === 'mysql') {
104 $this->array($this->zdb->getWarnings())->isIdenticalTo([]);
105 }
106 $this->resetListsConfig();
107 }
108
109 /**
110 * Resets lists configuration to defaults
111 *
112 * @return void
113 */
114 private function resetListsConfig()
115 {
116 $new_list = [];
117 foreach ($this->default_lists as $key) {
118 $new_list[] = $this->lists_config->getField($key);
119 }
120
121 $this->boolean($this->lists_config->setListFields($new_list))->isTrue();
122 }
123
124 /**
125 * Test getVisibility
126 *
127 * @return void
128 */
129 public function testGetVisibility()
130 {
131 $this->lists_config->load();
132
133 $visible = $this->lists_config->getVisibility('nom_adh');
134 $this->integer($visible)->isIdenticalTo(\Galette\Entity\FieldsConfig::NOBODY);
135
136 //must be the same than nom_adh
137 $visible = $this->lists_config->getVisibility('list_adh_name');
138 $this->integer($visible)->isIdenticalTo(\Galette\Entity\FieldsConfig::USER_WRITE);
139
140 $visible = $this->lists_config->getVisibility('id_statut');
141 $this->integer($visible)->isIdenticalTo(\Galette\Entity\FieldsConfig::STAFF);
142
143 //must be the same than id_statut
144 $visible = $this->lists_config->getVisibility('list_adh_contribstatus');
145 $this->integer($visible)->isIdenticalTo(\Galette\Entity\FieldsConfig::STAFF);
146 }
147
148 /**
149 * Test setFields and storage
150 *
151 * @return void
152 */
153 public function testSetFields()
154 {
155 $lists_config = $this->lists_config;
156 $lists_config->installInit();
157 $lists_config->load();
158
159 $fields = $lists_config->getCategorizedFields();
160
161 $list = $lists_config->getListedFields();
162 $this->array($list)->hasSize(6);
163
164 $expecteds = $this->default_lists;
165 foreach ($expecteds as $k => $expected) {
166 $this->string($list[$k]['field_id'])->isIdenticalTo($expected);
167 $this->integer($list[$k]['list_position'])->isIdenticalTo($k);
168 }
169
170 $expecteds = [
171 'id_adh',
172 'list_adh_name',
173 'email_adh',
174 'tel_adh',
175 'id_statut',
176 'list_adh_contribstatus',
177 'ville_adh'
178 ];
179
180 $new_list = [];
181 foreach ($expecteds as $key) {
182 $new_list[] = $lists_config->getField($key);
183 }
184 $this->boolean($lists_config->setListFields($new_list))->isTrue();
185
186 $list = $lists_config->getListedFields();
187 $this->array($list)->hasSize(7);
188
189 foreach ($expecteds as $k => $expected) {
190 $this->string($list[$k]['field_id'])->isIdenticalTo($expected);
191 $this->integer($list[$k]['list_position'])->isIdenticalTo($k);
192 }
193
194 $field = $lists_config->getField('pseudo_adh');
195 $this->integer($field['list_position'])->isIdenticalTo(-1);
196 $this->boolean($field['list_visible'])->isFalse();
197
198 $field = $lists_config->getField('date_modif_adh');
199 $this->integer($field['list_position'])->isIdenticalTo(-1);
200 $this->boolean($field['list_visible'])->isFalse();
201
202 // copied from FieldsConfig::testSetFields to ensure it works as excpeted from here.
203 //town
204 $town = &$fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_CONTACT][2]; //3 in FieldsConfig but 2 here.
205 $this->string($town['field_id'])->isIdenticalTo('ville_adh');
206 $this->boolean($town['required'])->isTrue();
207 $this->integer($town['visible'])->isIdenticalTo(\Galette\Entity\FieldsConfig::USER_WRITE);
208
209 $town['required'] = false;
210 $town['visible'] = \Galette\Entity\FieldsConfig::NOBODY;
211
212 //gsm
213 $gsm = $fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_CONTACT][5]; //6 in FieldsConfig but 5 here.
214 $gsm['position'] = count($fields[1]);
215 unset($fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_CONTACT][5]); //6 in FieldsConfig but 5 here.
216 $gsm['category'] = \Galette\Entity\FieldsCategories::ADH_CATEGORY_IDENTITY;
217 $fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_IDENTITY][] = $gsm;
218
219 $this->boolean($lists_config->setFields($fields))->isTrue();
220
221 $lists_config->load();
222 $fields = $lists_config->getCategorizedFields();
223
224 $town = $fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_CONTACT][2]; //3 in FieldsConfig but 2 here.
225 $this->boolean($town['required'])->isFalse();
226 $this->integer($town['visible'])->isIdenticalTo(\Galette\Entity\FieldsConfig::NOBODY);
227
228 $gsm2 = $fields[\Galette\Entity\FieldsCategories::ADH_CATEGORY_IDENTITY][10]; //12 in FieldsConfig but 10 here
229 $this->array($gsm2)->isIdenticalTo($gsm);
230 // /copied from FieldsConfig::testSetFields to ensure it works as expected from here.
231 }
232
233 /**
234 * Test get display elements
235 *
236 * @return void
237 */
238 public function testGetDisplayElements()
239 {
240 $lists_config = $this->lists_config;
241 $lists_config->load();
242
243 //admin
244 $superadmin_login = new \mock\Galette\Core\Login(
245 $this->zdb,
246 new \Galette\Core\I18n()
247 );
248 $this->calling($superadmin_login)->isSuperAdmin = true;
249
250 $expecteds = $this->default_lists;
251 $elements = $lists_config->getDisplayElements($superadmin_login);
252 $this->array($elements)
253 ->hasSize(count($this->default_lists));
254
255 //admin
256 $admin_login = new \mock\Galette\Core\Login(
257 $this->zdb,
258 new \Galette\Core\I18n()
259 );
260 $this->calling($admin_login)->isAdmin = true;
261
262 $expecteds = $this->default_lists;
263 $elements = $lists_config->getDisplayElements($admin_login);
264 $this->array($elements)
265 ->hasSize(count($this->default_lists));
266
267 //staff
268 $staff_login = new \mock\Galette\Core\Login(
269 $this->zdb,
270 new \Galette\Core\I18n()
271 );
272 $this->calling($staff_login)->isStaff = true;
273
274 $expecteds = $this->default_lists;
275 $elements = $lists_config->getDisplayElements($staff_login);
276 $this->array($elements)
277 ->hasSize(count($this->default_lists));
278
279 //following tests will have lower ACLS (cannot see status)
280 $expecteds = [
281 'id_adh',
282 'list_adh_name',
283 'pseudo_adh',
284 'date_modif_adh'
285 ];
286 $new_list = [];
287 foreach ($expecteds as $key) {
288 $new_list[] = $lists_config->getField($key);
289 }
290
291
292 //group manager
293 $manager_login = new \mock\Galette\Core\Login(
294 $this->zdb,
295 new \Galette\Core\I18n()
296 );
297 $this->calling($manager_login)->isGroupManager = true;
298
299 $elements = $lists_config->getDisplayElements($manager_login);
300 $this->array($elements)
301 ->hasSize(count($new_list));
302
303 //to keep last know rank. May switch from 2 to 6 because of bield visibility.
304 $last_ok = -1;
305 foreach ($expecteds as $k => $expected) {
306 $this->string($new_list[$k]['field_id'])->isIdenticalTo($expected);
307 if ($new_list[$k]['list_position'] != $k - 1) {
308 $this->integer($new_list[$k]['list_position'])->isGreaterThan($last_ok);
309 $last_ok = $new_list[$k]['list_position'];
310 } else {
311 $this->integer($new_list[$k]['list_position'])->isIdenticalTo($k);
312 }
313 }
314
315 //simplemember
316 $user_login = new \mock\Galette\Core\Login(
317 $this->zdb,
318 new \Galette\Core\I18n()
319 );
320 $this->calling($user_login)->isUp2Date = true;
321
322 $elements = $lists_config->getDisplayElements($user_login);
323 $this->array($elements)
324 ->hasSize(count($new_list));
325
326 //to keep last know rank. May switch from 2 to 6 because of bield visibility.
327 $last_ok = -1;
328 foreach ($expecteds as $k => $expected) {
329 $this->string($new_list[$k]['field_id'])->isIdenticalTo($expected);
330 if ($new_list[$k]['list_position'] != $k - 1) {
331 $this->integer($new_list[$k]['list_position'])->isGreaterThan($last_ok);
332 $last_ok = $new_list[$k]['list_position'];
333 } else {
334 $this->integer($new_list[$k]['list_position'])->isIdenticalTo($k);
335 }
336 }
337 }
338 }