From f288f67bb95927859d4120361e553165fb06a6c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 1 Jan 2016 22:24:38 +0100 Subject: [PATCH] fix error with empty values with multiple and non-existant data if a value was array(null) or array(_other => null, _choices => null), the isEmptyValue function failed. --- CustomFields/CustomFieldChoice.php | 10 ++- Tests/CustomFields/CustomFieldsChoiceTest.php | 76 +++++++++++++++---- 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/CustomFields/CustomFieldChoice.php b/CustomFields/CustomFieldChoice.php index 782f5ab17..8787d8dba 100644 --- a/CustomFields/CustomFieldChoice.php +++ b/CustomFields/CustomFieldChoice.php @@ -247,7 +247,7 @@ class CustomFieldChoice extends AbstractCustomField return $value; } else { // we have a field with "allow other" - if (isset($value['_choices'])) { + if (array_key_exists('_choices', $value)) { return $value['_choices']; } else { // we have a field with "multiple" @@ -273,13 +273,17 @@ class CustomFieldChoice extends AbstractCustomField if (is_array($value)) { // if allow other - if (isset($value['_choices'])) { + if (array_key_exists('_choices', $value)) { if ($value['_choices'] === NULL) { return true; } return empty($value['_choices']); } else { // we do not have 'allow other' - return empty($value); + if (count($value) === 1){ + return empty($value[0]); + } else { + return empty($value); + } } } else { return empty($value); diff --git a/Tests/CustomFields/CustomFieldsChoiceTest.php b/Tests/CustomFields/CustomFieldsChoiceTest.php index 6fbc0b67e..28351711c 100644 --- a/Tests/CustomFields/CustomFieldsChoiceTest.php +++ b/Tests/CustomFields/CustomFieldsChoiceTest.php @@ -111,12 +111,22 @@ class CustomFieldsChoiceTest extends KernelTestCase )); $deserialized = $this->cfChoice->deserialize(null, $customField); - $this->assertSame(null, $deserialized); $deserialized = $this->cfChoice->deserialize('', $customField); - $this->assertSame('', $deserialized); + + $deserialized = $this->cfChoice->deserialize(array(null), $customField); + $this->assertSame(null, $deserialized); + + $deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => null), $customField); + $this->assertSame(null, $deserialized); + + $deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => ''), $customField); + $this->assertSame('', $deserialized); + + $deserialized = $this->cfChoice->deserialize(array(null), $customField); + $this->assertSame(null, $deserialized); } /** @@ -158,39 +168,42 @@ class CustomFieldsChoiceTest extends KernelTestCase // from a single to a single $data = array('_other' => 'something', '_choices' => '_other'); $deserialized = $this->cfChoice->deserialize($data, $customField); - $this->assertSame(array('_other' => 'something', '_choices' => '_other'), $deserialized); // from a multiple to a single $data = array('_other' => 'something', '_choices' => array('some', '_other')); $deserialized = $this->cfChoice->deserialize($data, $customField); - $this->assertSame(array('_other' => 'something', '_choices' => '_other'), $deserialized); //test with null data //from a single to a single : $data = array('_other' => 'something', '_choices' => null); $deserialized = $this->cfChoice->deserialize($data, $customField); - $this->assertSame(array('_other' => 'something', '_choices' => null), $deserialized); $data = array('_other' => 'something', '_choices' => ''); $deserialized = $this->cfChoice->deserialize($data, $customField); - $this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized); // from a multiple to a signle $data = array('_other' => 'something', '_choices' => array()); $deserialized = $this->cfChoice->deserialize($data, $customField); - $this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized); $data = array('_other' => 'something', '_choices' => array('')); - $deserialized = $this->cfChoice->deserialize($data, $customField); - + $deserialized = $this->cfChoice->deserialize($data, $customField); $this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized); + $deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => null), $customField); + $this->assertSame(array('_other' => '', '_choices' => null), $deserialized); + + $deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => ''), $customField); + $this->assertSame(array('_other' => '', '_choices' => ''), $deserialized); + + $deserialized = $this->cfChoice->deserialize(array(null), $customField); + $this->assertSame(array('_other' => '', '_choices' => null), $deserialized); + } /** @@ -261,6 +274,15 @@ class CustomFieldsChoiceTest extends KernelTestCase $this->assertSame(array('_other' => '', '_choices' => array()), $deserialized); + + $deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => null), $customField); + $this->assertSame(array('_other' => '', '_choices' => array(null)), $deserialized); + + $deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => ''), $customField); + $this->assertSame(array('_other' => '', '_choices' => array('')), $deserialized); + + $deserialized = $this->cfChoice->deserialize(array(null), $customField); + $this->assertSame(array('_other' => '', '_choices' => array(null)), $deserialized); } /** @@ -303,14 +325,26 @@ class CustomFieldsChoiceTest extends KernelTestCase // from single to multiple $data = 'my-value'; $deserialized = $this->cfChoice->deserialize($data, $customField); - $this->assertSame(array('my-value'), $deserialized); // from multiple to multiple $data = array('my-value'); - $deserialized = $this->cfChoice->deserialize($data, $customField); - + $deserialized = $this->cfChoice->deserialize($data, $customField); $this->assertSame(array('my-value'), $deserialized); + + // from multiple + $data = array(null); + $deserialized = $this->cfChoice->deserialize($data, $customField); + $this->assertSame(array(null), $deserialized); + + $data = array('_other' => null, '_choices' => array(null)); + $deserialized = $this->cfChoice->deserialize($data, $customField); + $this->assertSame(array(null), $deserialized); + + //from single + $data = array('_other' => null, '_choices' => null); + $deserialized = $this->cfChoice->deserialize($data, $customField); + $this->assertSame(array(null), $deserialized); } public function serializedRepresentationDataProvider() @@ -356,7 +390,8 @@ class CustomFieldsChoiceTest extends KernelTestCase CustomFieldChoice::MULTIPLE => true )); - $isEmpty = $this->cfChoice->isEmptyValue($data, $customField); + $deserialized = $this->cfChoice->deserialize($data, $customField); + $isEmpty = $this->cfChoice->isEmptyValue($deserialized, $customField); $this->assertFalse($isEmpty); } @@ -364,7 +399,7 @@ class CustomFieldsChoiceTest extends KernelTestCase /** * * @dataProvider emptyDataProvider - * @param mixed $data + * @param mixed $data deserialized data */ public function testIsEmptyValueEmpty($data) { @@ -379,7 +414,8 @@ class CustomFieldsChoiceTest extends KernelTestCase } /** - * provide empty data in different possible reprsentation + * provide empty data in different possible representations. + * Those data are supposed to be deserialized. * * @return array */ @@ -416,6 +452,16 @@ class CustomFieldsChoiceTest extends KernelTestCase // multiple with allow other array('_other' => '', '_choices' => array()) ), + // 6 + array( + // empty + array('_other' => null, '_choices' => null) + ), + // 7 + array( + // empty + array(null) + ) ); }