Skip to content

Commit ef362dc

Browse files
authored
Merge pull request #6196 from rldhont/fix-checkbox-config
[Bugfix] Checkbox config does not well used default values
2 parents 882e66e + 7f9a5dc commit ef362dc

File tree

4 files changed

+120
-16
lines changed

4 files changed

+120
-16
lines changed

lizmap/modules/lizmap/lib/Project/Qgis/BaseQgisObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected function set(array $data): void
174174
continue;
175175
}
176176
if ($value === null
177-
&& in_array($property, $this->defaultValues)) {
177+
&& array_key_exists($property, $this->defaultValues)) {
178178
// if the value is null and the property has a default value, use the default value
179179
$this->data[$property] = $this->defaultValues[$property];
180180

lizmap/modules/lizmap/lib/Project/Qgis/Layer/EditWidget/CheckBoxConfig.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @property string $CheckedState
3030
* @property string $UncheckedState
3131
* @property int $TextDisplayMethod
32+
* @property bool $AllowNullState
3233
*/
3334
class CheckBoxConfig extends Qgis\BaseQgisObject
3435
{
@@ -37,12 +38,25 @@ class CheckBoxConfig extends Qgis\BaseQgisObject
3738
'CheckedState',
3839
'UncheckedState',
3940
'TextDisplayMethod',
41+
'AllowNullState',
4042
);
4143

4244
/** @var array The default values */
4345
protected $defaultValues = array(
4446
'CheckedState' => '',
4547
'UncheckedState' => '',
4648
'TextDisplayMethod' => 0,
49+
'AllowNullState' => false,
4750
);
51+
52+
protected function set(array $data): void
53+
{
54+
if (array_key_exists('TextDisplayMethod', $data)) {
55+
$data['TextDisplayMethod'] = (int) $data['TextDisplayMethod'];
56+
}
57+
if (array_key_exists('AllowNullState', $data)) {
58+
$data['AllowNullState'] = filter_var($data['AllowNullState'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
59+
}
60+
parent::set($data);
61+
}
4862
}

tests/units/classes/Project/Qgis/BaseQgisObjectTest.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ class Person extends Qgis\BaseQgisObject
1818
);
1919
}
2020

21+
class Car extends Qgis\BaseQgisObject
22+
{
23+
/** @var array<string> The instance properties */
24+
protected $properties = array(
25+
'color',
26+
);
27+
28+
/** @var array The default values */
29+
protected $defaultValues = array(
30+
'color' => 'white',
31+
);
32+
}
33+
2134
/**
2235
* @internal
2336
*
@@ -37,16 +50,49 @@ public function testGetData(): void
3750
$p1Data = array('name' => 'Foo');
3851
$p1 = new Person($p1Data);
3952

40-
$this->assertEquals($p1Data, $p1->getData());
53+
$this->assertSame($p1Data, $p1->getData());
4154

4255
$p2 = new Person(array('name' => 'Bar', 'parent' => $p1));
4356
$p2Data = array('name' => 'Bar', 'parent' => $p1Data);
4457

45-
$this->assertEquals($p2Data, $p2->getData());
58+
$this->assertSame($p2Data, $p2->getData());
4659

4760
$p3 = new Person(array('name' => 'FooBar', 'children' => array($p1)));
4861
$p3Data = array('name' => 'FooBar', 'children' => array($p1Data));
4962

50-
$this->assertEquals($p3Data, $p3->getData());
63+
$this->assertSame($p3Data, $p3->getData());
64+
}
65+
66+
public function testDefaultValues(): void
67+
{
68+
$defaultData = array(
69+
'color' => 'white',
70+
);
71+
$blueData = array(
72+
'color' => 'blue',
73+
);
74+
$nullData = array(
75+
'color' => null,
76+
);
77+
78+
// empty data
79+
$c1 = new Car(array());
80+
$this->assertSame($defaultData, $c1->getData());
81+
$this->assertNotSame($blueData, $c1->getData());
82+
$this->assertNotSame($nullData, $c1->getData());
83+
84+
// null data
85+
$c2 = new Car($nullData);
86+
$this->assertSame($defaultData, $c2->getData());
87+
$this->assertNotSame($blueData, $c2->getData());
88+
$this->assertNotSame($nullData, $c2->getData());
89+
$this->assertSame($c1->getData(), $c2->getData());
90+
91+
// blue data
92+
$c3 = new Car($blueData);
93+
$this->assertSame($blueData, $c3->getData());
94+
$this->assertNotSame($defaultData, $c3->getData());
95+
$this->assertNotSame($nullData, $c3->getData());
96+
$this->assertNotSame($c1->getData(), $c3->getData());
5197
}
5298
}

tests/units/classes/Project/Qgis/VectorLayerEditWidgetTest.php

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public function testFromXmlReader(): void
3737
'UseHtml' => false,
3838
);
3939
foreach ($config as $prop => $value) {
40-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
40+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
4141
}
42-
$this->assertEquals($config, $editWidget->config->getData());
42+
$this->assertSame($config, $editWidget->config->getData());
4343

4444
// Simple default old
4545
$xmlStr = '
@@ -64,8 +64,9 @@ public function testFromXmlReader(): void
6464
'UseHtml' => false,
6565
);
6666
foreach ($config as $prop => $value) {
67-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
67+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
6868
}
69+
$this->assertSame($config, $editWidget->config->getData());
6970

7071
// Default config
7172
$xmlStr = '
@@ -87,8 +88,9 @@ public function testFromXmlReader(): void
8788
'UseHtml' => false,
8889
);
8990
foreach ($config as $prop => $value) {
90-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
91+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
9192
}
93+
$this->assertSame($config, $editWidget->config->getData());
9294
}
9395

9496
public function testCheckBoxFromXmlReader(): void
@@ -116,10 +118,12 @@ public function testCheckBoxFromXmlReader(): void
116118
'CheckedState' => '1',
117119
'UncheckedState' => '0',
118120
'TextDisplayMethod' => 1,
121+
'AllowNullState' => false,
119122
);
120123
foreach ($config as $prop => $value) {
121-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
124+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
122125
}
126+
$this->assertSame($config, $editWidget->config->getData());
123127

124128
$xmlStr = '
125129
<editWidget type="CheckBox">
@@ -142,10 +146,12 @@ public function testCheckBoxFromXmlReader(): void
142146
'CheckedState' => '1',
143147
'UncheckedState' => '0',
144148
'TextDisplayMethod' => 0,
149+
'AllowNullState' => false,
145150
);
146151
foreach ($config as $prop => $value) {
147-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
152+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
148153
}
154+
$this->assertSame($config, $editWidget->config->getData());
149155

150156
// Default config
151157
$xmlStr = '
@@ -166,10 +172,44 @@ public function testCheckBoxFromXmlReader(): void
166172
'CheckedState' => '',
167173
'UncheckedState' => '',
168174
'TextDisplayMethod' => 0,
175+
'AllowNullState' => false,
176+
);
177+
foreach ($config as $prop => $value) {
178+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
179+
}
180+
$this->assertSame($config, $editWidget->config->getData());
181+
182+
// invalid CheckedState and UncheckedState
183+
// allow NULL
184+
$xmlStr = '
185+
<editWidget type="CheckBox">
186+
<config>
187+
<Option type="Map">
188+
<Option name="AllowNullState" type="bool" value="true"/>
189+
<Option name="CheckedState" type="invalid"/>
190+
<Option name="TextDisplayMethod" type="int" value="0"/>
191+
<Option name="UncheckedState" type="invalid"/>
192+
</Option>
193+
</config>
194+
</editWidget>
195+
';
196+
$oXml = App\XmlTools::xmlReaderFromString($xmlStr);
197+
$editWidget = VectorLayerEditWidget::fromXmlReader($oXml);
198+
199+
$this->assertEquals('CheckBox', $editWidget->type);
200+
$this->assertNotNull($editWidget->config);
201+
$this->assertInstanceOf(EditWidget\CheckBoxConfig::class, $editWidget->config);
202+
203+
$config = array(
204+
'CheckedState' => '',
205+
'UncheckedState' => '',
206+
'TextDisplayMethod' => 0,
207+
'AllowNullState' => true,
169208
);
170209
foreach ($config as $prop => $value) {
171-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
210+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
172211
}
212+
$this->assertSame($config, $editWidget->config->getData());
173213
}
174214

175215
public function testDateTimeFromXmlReader(): void
@@ -203,8 +243,9 @@ public function testDateTimeFromXmlReader(): void
203243
'field_iso_format' => false,
204244
);
205245
foreach ($config as $prop => $value) {
206-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
246+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
207247
}
248+
$this->assertSame($config, $editWidget->config->getData());
208249

209250
// Default config
210251
$xmlStr = '
@@ -229,8 +270,9 @@ public function testDateTimeFromXmlReader(): void
229270
'field_iso_format' => false,
230271
);
231272
foreach ($config as $prop => $value) {
232-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
273+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
233274
}
275+
$this->assertSame($config, $editWidget->config->getData());
234276
}
235277

236278
public function testRangeFromXmlReader(): void
@@ -262,12 +304,13 @@ public function testRangeFromXmlReader(): void
262304
'Max' => 2147483647,
263305
'Min' => -2147483648,
264306
'Precision' => 0,
265-
'Step' => 1,
307+
'Step' => 1.0,
266308
'Style' => 'SpinBox',
267309
);
268310
foreach ($config as $prop => $value) {
269-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
311+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
270312
}
313+
$this->assertSame($config, $editWidget->config->getData());
271314

272315
// Default config
273316
$xmlStr = '
@@ -289,8 +332,9 @@ public function testRangeFromXmlReader(): void
289332
'Style' => 'SpinBox',
290333
);
291334
foreach ($config as $prop => $value) {
292-
$this->assertEquals($value, $editWidget->config->{$prop}, $prop);
335+
$this->assertSame($value, $editWidget->config->{$prop}, $prop);
293336
}
337+
$this->assertSame($config, $editWidget->config->getData());
294338
$notSet = array(
295339
'Max',
296340
'Min',

0 commit comments

Comments
 (0)