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
;
40 use PHPUnit\Framework\TestCase
;
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 TestCase
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',
74 public function setUp(): void
76 $this->zdb
= new \Galette\Core\
Db();
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;
83 $this->lists_config
= new \Galette\Entity\
ListsConfig(
85 \Galette\Entity\Adherent
::TABLE
,
86 $this->members_fields
,
87 $this->members_fields_cats
,
97 public function tearDown(): void
99 if (TYPE_DB
=== 'mysql') {
100 $this->assertSame([], $this->zdb
->getWarnings());
102 $this->resetListsConfig();
106 * Resets lists configuration to defaults
110 private function resetListsConfig()
113 foreach ($this->default_lists
as $key) {
114 $new_list[] = $this->lists_config
->getField($key);
117 $this->assertTrue($this->lists_config
->setListFields($new_list));
125 public function testGetVisibility()
127 $this->lists_config
->load();
129 $visible = $this->lists_config
->getVisibility('nom_adh');
130 $this->assertSame(\Galette\Entity\FieldsConfig
::NOBODY
, $visible);
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);
136 $visible = $this->lists_config
->getVisibility('id_statut');
137 $this->assertSame(\Galette\Entity\FieldsConfig
::STAFF
, $visible);
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);
145 * Test setFields and storage
149 public function testSetFields()
151 $lists_config = $this->lists_config
;
152 $lists_config->installInit();
153 $lists_config->load();
155 $fields = $lists_config->getCategorizedFields();
157 $list = $lists_config->getListedFields();
158 $this->assertCount(6, $list);
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']);
172 'list_adh_contribstatus',
177 foreach ($expecteds as $key) {
178 $new_list[] = $lists_config->getField($key);
180 $this->assertTrue($lists_config->setListFields($new_list));
182 $list = $lists_config->getListedFields();
183 $this->assertCount(7, $list);
185 foreach ($expecteds as $k => $expected) {
186 $this->assertSame($expected, $list[$k]['field_id']);
187 $this->assertSame($k, $list[$k]['list_position']);
190 $field = $lists_config->getField('pseudo_adh');
191 $this->assertSame(-1, $field['list_position']);
192 $this->assertFalse($field['list_visible']);
194 $field = $lists_config->getField('date_modif_adh');
195 $this->assertSame(-1, $field['list_position']);
196 $this->assertFalse($field['list_visible']);
198 // copied from FieldsConfig::testSetFields to ensure it works as excpeted from here.
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']);
205 $town['required'] = false;
206 $town['visible'] = \Galette\Entity\FieldsConfig
::NOBODY
;
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;
215 $this->assertTrue($lists_config->setFields($fields));
217 $lists_config->load();
218 $fields = $lists_config->getCategorizedFields();
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']);
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.
230 * Test get display elements
234 public function testGetDisplayElements()
236 $lists_config = $this->lists_config
;
237 $lists_config->load();
240 $superadmin_login = $this->getMockBuilder(\Galette\Core\Login
::class)
241 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
242 ->onlyMethods(array('isSuperAdmin'))
244 $superadmin_login->method('isSuperAdmin')->willReturn(true);
246 $expecteds = $this->default_lists
;
247 $elements = $lists_config->getDisplayElements($superadmin_login);
248 $this->assertCount(count($this->default_lists
), $elements);
251 $admin_login = $this->getMockBuilder(\Galette\Core\Login
::class)
252 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
253 ->onlyMethods(array('isAdmin'))
255 $admin_login->method('isAdmin')->willReturn(true);
257 $elements = $lists_config->getDisplayElements($admin_login);
258 $this->assertCount(count($this->default_lists
), $elements);
261 $staff_login = $this->getMockBuilder(\Galette\Core\Login
::class)
262 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
263 ->onlyMethods(array('isStaff'))
265 $staff_login->method('isStaff')->willReturn(true);
267 $elements = $lists_config->getDisplayElements($staff_login);
268 $this->assertCount(count($this->default_lists
), $elements);
270 //following tests will have lower ACLS (cannot see status)
278 foreach ($expecteds as $key) {
279 $new_list[] = $lists_config->getField($key);
283 $manager_login = $this->getMockBuilder(\Galette\Core\Login
::class)
284 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
285 ->onlyMethods(array('isGroupManager'))
287 $manager_login->method('isGroupManager')->willReturn(true);
289 $elements = $lists_config->getDisplayElements($manager_login);
290 $this->assertCount(count($new_list), $elements);
292 //to keep last know rank. May switch from 2 to 6 because of field visibility.
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'];
300 $this->assertSame($k, $new_list[$k]['list_position']);
305 $user_login = $this->getMockBuilder(\Galette\Core\Login
::class)
306 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
307 ->onlyMethods(array('isUp2Date'))
309 $user_login->method('isUp2Date')->willReturn(true);
311 $elements = $lists_config->getDisplayElements($user_login);
312 $this->assertCount(count($new_list), $elements);
314 //to keep last know rank. May switch from 2 to 6 because of field visibility.
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'];
322 $this->assertSame($k, $new_list[$k]['list_position']);