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
33 * @link http://galette.tuxfamily.org
37 namespace Galette\Entity\test\units
;
39 use PHPUnit\Framework\TestCase
;
42 * ListsConfig tests class
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
53 class ListsConfig
extends TestCase
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 = [
64 'list_adh_contribstatus',
73 public function setUp(): void
75 $this->zdb
= new \Galette\Core\
Db();
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;
82 $this->lists_config
= new \Galette\Entity\
ListsConfig(
84 \Galette\Entity\Adherent
::TABLE
,
85 $this->members_fields
,
86 $this->members_fields_cats
,
96 public function tearDown(): void
98 if (TYPE_DB
=== 'mysql') {
99 $this->assertSame([], $this->zdb
->getWarnings());
101 $this->resetListsConfig();
105 * Resets lists configuration to defaults
109 private function resetListsConfig()
112 foreach ($this->default_lists
as $key) {
113 $new_list[] = $this->lists_config
->getField($key);
116 $this->assertTrue($this->lists_config
->setListFields($new_list));
124 public function testGetVisibility()
126 $this->lists_config
->load();
128 $visible = $this->lists_config
->getVisibility('nom_adh');
129 $this->assertSame(\Galette\Entity\FieldsConfig
::NOBODY
, $visible);
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);
135 $visible = $this->lists_config
->getVisibility('id_statut');
136 $this->assertSame(\Galette\Entity\FieldsConfig
::STAFF
, $visible);
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);
144 * Test setFields and storage
148 public function testSetFields()
150 $lists_config = $this->lists_config
;
151 $lists_config->installInit();
152 $lists_config->load();
154 $fields = $lists_config->getCategorizedFields();
156 $list = $lists_config->getListedFields();
157 $this->assertCount(6, $list);
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']);
171 'list_adh_contribstatus',
176 foreach ($expecteds as $key) {
177 $new_list[] = $lists_config->getField($key);
179 $this->assertTrue($lists_config->setListFields($new_list));
181 $list = $lists_config->getListedFields();
182 $this->assertCount(7, $list);
184 foreach ($expecteds as $k => $expected) {
185 $this->assertSame($expected, $list[$k]['field_id']);
186 $this->assertSame($k, $list[$k]['list_position']);
189 $field = $lists_config->getField('pseudo_adh');
190 $this->assertSame(-1, $field['list_position']);
191 $this->assertFalse($field['list_visible']);
193 $field = $lists_config->getField('date_modif_adh');
194 $this->assertSame(-1, $field['list_position']);
195 $this->assertFalse($field['list_visible']);
197 // copied from FieldsConfig::testSetFields to ensure it works as excpeted from here.
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']);
204 $town['required'] = false;
205 $town['visible'] = \Galette\Entity\FieldsConfig
::NOBODY
;
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;
214 $this->assertTrue($lists_config->setFields($fields));
216 $lists_config->load();
217 $fields = $lists_config->getCategorizedFields();
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']);
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.
229 * Test get display elements
233 public function testGetDisplayElements()
235 $lists_config = $this->lists_config
;
236 $lists_config->load();
239 $superadmin_login = $this->getMockBuilder(\Galette\Core\Login
::class)
240 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
241 ->onlyMethods(array('isSuperAdmin'))
243 $superadmin_login->method('isSuperAdmin')->willReturn(true);
245 $expecteds = $this->default_lists
;
246 $elements = $lists_config->getDisplayElements($superadmin_login);
247 $this->assertCount(count($this->default_lists
), $elements);
250 $admin_login = $this->getMockBuilder(\Galette\Core\Login
::class)
251 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
252 ->onlyMethods(array('isAdmin'))
254 $admin_login->method('isAdmin')->willReturn(true);
256 $elements = $lists_config->getDisplayElements($admin_login);
257 $this->assertCount(count($this->default_lists
), $elements);
260 $staff_login = $this->getMockBuilder(\Galette\Core\Login
::class)
261 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
262 ->onlyMethods(array('isStaff'))
264 $staff_login->method('isStaff')->willReturn(true);
266 $elements = $lists_config->getDisplayElements($staff_login);
267 $this->assertCount(count($this->default_lists
), $elements);
269 //following tests will have lower ACLS (cannot see status)
277 foreach ($expecteds as $key) {
278 $new_list[] = $lists_config->getField($key);
282 $manager_login = $this->getMockBuilder(\Galette\Core\Login
::class)
283 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
284 ->onlyMethods(array('isGroupManager'))
286 $manager_login->method('isGroupManager')->willReturn(true);
288 $elements = $lists_config->getDisplayElements($manager_login);
289 $this->assertCount(count($new_list), $elements);
291 //to keep last know rank. May switch from 2 to 6 because of field visibility.
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'];
299 $this->assertSame($k, $new_list[$k]['list_position']);
304 $user_login = $this->getMockBuilder(\Galette\Core\Login
::class)
305 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
306 ->onlyMethods(array('isUp2Date'))
308 $user_login->method('isUp2Date')->willReturn(true);
310 $elements = $lists_config->getDisplayElements($user_login);
311 $this->assertCount(count($new_list), $elements);
313 //to keep last know rank. May switch from 2 to 6 because of field visibility.
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'];
321 $this->assertSame($k, $new_list[$k]['list_position']);