3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Entity\test\units
;
43 * ListsConfig tests class
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
54 class ListsConfig
extends atoum
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 = [
65 'list_adh_contribstatus',
72 * @param string $method Calling method
76 public function beforeTestMethod($method)
78 $this->zdb
= new \Galette\Core\
Db();
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;
85 $this->lists_config
= new \Galette\Entity\
ListsConfig(
87 \Galette\Entity\Adherent
::TABLE
,
88 $this->members_fields
,
89 $this->members_fields_cats
,
97 * @param string $method Calling method
101 public function afterTestMethod($method)
103 if (TYPE_DB
=== 'mysql') {
104 $this->array($this->zdb
->getWarnings())->isIdenticalTo([]);
106 $this->resetListsConfig();
110 * Resets lists configuration to defaults
114 private function resetListsConfig()
117 foreach ($this->default_lists
as $key) {
118 $new_list[] = $this->lists_config
->getField($key);
121 $this->boolean($this->lists_config
->setListFields($new_list))->isTrue();
129 public function testGetVisibility()
131 $this->lists_config
->load();
133 $visible = $this->lists_config
->getVisibility('nom_adh');
134 $this->integer($visible)->isIdenticalTo(\Galette\Entity\FieldsConfig
::NOBODY
);
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
);
140 $visible = $this->lists_config
->getVisibility('id_statut');
141 $this->integer($visible)->isIdenticalTo(\Galette\Entity\FieldsConfig
::STAFF
);
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
);
149 * Test setFields and storage
153 public function testSetFields()
155 $lists_config = $this->lists_config
;
156 $lists_config->installInit();
157 $lists_config->load();
159 $fields = $lists_config->getCategorizedFields();
161 $list = $lists_config->getListedFields();
162 $this->array($list)->hasSize(6);
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);
176 'list_adh_contribstatus',
181 foreach ($expecteds as $key) {
182 $new_list[] = $lists_config->getField($key);
184 $this->boolean($lists_config->setListFields($new_list))->isTrue();
186 $list = $lists_config->getListedFields();
187 $this->array($list)->hasSize(7);
189 foreach ($expecteds as $k => $expected) {
190 $this->string($list[$k]['field_id'])->isIdenticalTo($expected);
191 $this->integer($list[$k]['list_position'])->isIdenticalTo($k);
194 $field = $lists_config->getField('pseudo_adh');
195 $this->integer($field['list_position'])->isIdenticalTo(-1);
196 $this->boolean($field['list_visible'])->isFalse();
198 $field = $lists_config->getField('date_modif_adh');
199 $this->integer($field['list_position'])->isIdenticalTo(-1);
200 $this->boolean($field['list_visible'])->isFalse();
202 // copied from FieldsConfig::testSetFields to ensure it works as excpeted from here.
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
);
209 $town['required'] = false;
210 $town['visible'] = \Galette\Entity\FieldsConfig
::NOBODY
;
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;
219 $this->boolean($lists_config->setFields($fields))->isTrue();
221 $lists_config->load();
222 $fields = $lists_config->getCategorizedFields();
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
);
228 $gsm2 = $fields[\Galette\Entity\FieldsCategories
::ADH_CATEGORY_IDENTITY
][11]; //13 in FieldsConfig but 11 here
229 $this->array($gsm2)->isIdenticalTo($gsm);
230 // /copied from FieldsConfig::testSetFields to ensure it works as expected from here.
234 * Test get display elements
238 public function testGetDisplayElements()
240 $lists_config = $this->lists_config
;
241 $lists_config->load();
244 $superadmin_login = new \mock\Galette\Core\
Login(
246 new \Galette\Core\
I18n()
248 $this->calling($superadmin_login)->isSuperAdmin
= true;
250 $expecteds = $this->default_lists
;
251 $elements = $lists_config->getDisplayElements($superadmin_login);
252 $this->array($elements)
253 ->hasSize(count($this->default_lists
));
256 $admin_login = new \mock\Galette\Core\
Login(
258 new \Galette\Core\
I18n()
260 $this->calling($admin_login)->isAdmin
= true;
262 $expecteds = $this->default_lists
;
263 $elements = $lists_config->getDisplayElements($admin_login);
264 $this->array($elements)
265 ->hasSize(count($this->default_lists
));
268 $staff_login = new \mock\Galette\Core\
Login(
270 new \Galette\Core\
I18n()
272 $this->calling($staff_login)->isStaff
= true;
274 $expecteds = $this->default_lists
;
275 $elements = $lists_config->getDisplayElements($staff_login);
276 $this->array($elements)
277 ->hasSize(count($this->default_lists
));
279 //following tests will have lower ACLS (cannot see status)
287 foreach ($expecteds as $key) {
288 $new_list[] = $lists_config->getField($key);
293 $manager_login = new \mock\Galette\Core\
Login(
295 new \Galette\Core\
I18n()
297 $this->calling($manager_login)->isGroupManager
= true;
299 $elements = $lists_config->getDisplayElements($manager_login);
300 $this->array($elements)
301 ->hasSize(count($new_list));
303 //to keep last know rank. May switch from 2 to 6 because of bield visibility.
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'];
311 $this->integer($new_list[$k]['list_position'])->isIdenticalTo($k);
316 $user_login = new \mock\Galette\Core\
Login(
318 new \Galette\Core\
I18n()
320 $this->calling($user_login)->isUp2Date
= true;
322 $elements = $lists_config->getDisplayElements($user_login);
323 $this->array($elements)
324 ->hasSize(count($new_list));
326 //to keep last know rank. May switch from 2 to 6 because of bield visibility.
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'];
334 $this->integer($new_list[$k]['list_position'])->isIdenticalTo($k);