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.
This commit is contained in:
Julien Fastré 2016-01-01 22:24:38 +01:00
parent f145ae7f88
commit f288f67bb9
2 changed files with 68 additions and 18 deletions

View File

@ -247,7 +247,7 @@ class CustomFieldChoice extends AbstractCustomField
return $value; return $value;
} else { } else {
// we have a field with "allow other" // we have a field with "allow other"
if (isset($value['_choices'])) { if (array_key_exists('_choices', $value)) {
return $value['_choices']; return $value['_choices'];
} else { } else {
// we have a field with "multiple" // we have a field with "multiple"
@ -273,14 +273,18 @@ class CustomFieldChoice extends AbstractCustomField
if (is_array($value)) if (is_array($value))
{ {
// if allow other // if allow other
if (isset($value['_choices'])) { if (array_key_exists('_choices', $value)) {
if ($value['_choices'] === NULL) { if ($value['_choices'] === NULL) {
return true; return true;
} }
return empty($value['_choices']); return empty($value['_choices']);
} else { // we do not have 'allow other' } else { // we do not have 'allow other'
if (count($value) === 1){
return empty($value[0]);
} else {
return empty($value); return empty($value);
} }
}
} else { } else {
return empty($value); return empty($value);
} }

View File

@ -111,12 +111,22 @@ class CustomFieldsChoiceTest extends KernelTestCase
)); ));
$deserialized = $this->cfChoice->deserialize(null, $customField); $deserialized = $this->cfChoice->deserialize(null, $customField);
$this->assertSame(null, $deserialized); $this->assertSame(null, $deserialized);
$deserialized = $this->cfChoice->deserialize('', $customField); $deserialized = $this->cfChoice->deserialize('', $customField);
$this->assertSame('', $deserialized); $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 // from a single to a single
$data = array('_other' => 'something', '_choices' => '_other'); $data = array('_other' => 'something', '_choices' => '_other');
$deserialized = $this->cfChoice->deserialize($data, $customField); $deserialized = $this->cfChoice->deserialize($data, $customField);
$this->assertSame(array('_other' => 'something', '_choices' => '_other'), $deserialized); $this->assertSame(array('_other' => 'something', '_choices' => '_other'), $deserialized);
// from a multiple to a single // from a multiple to a single
$data = array('_other' => 'something', '_choices' => array('some', '_other')); $data = array('_other' => 'something', '_choices' => array('some', '_other'));
$deserialized = $this->cfChoice->deserialize($data, $customField); $deserialized = $this->cfChoice->deserialize($data, $customField);
$this->assertSame(array('_other' => 'something', '_choices' => '_other'), $deserialized); $this->assertSame(array('_other' => 'something', '_choices' => '_other'), $deserialized);
//test with null data //test with null data
//from a single to a single : //from a single to a single :
$data = array('_other' => 'something', '_choices' => null); $data = array('_other' => 'something', '_choices' => null);
$deserialized = $this->cfChoice->deserialize($data, $customField); $deserialized = $this->cfChoice->deserialize($data, $customField);
$this->assertSame(array('_other' => 'something', '_choices' => null), $deserialized); $this->assertSame(array('_other' => 'something', '_choices' => null), $deserialized);
$data = array('_other' => 'something', '_choices' => ''); $data = array('_other' => 'something', '_choices' => '');
$deserialized = $this->cfChoice->deserialize($data, $customField); $deserialized = $this->cfChoice->deserialize($data, $customField);
$this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized); $this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized);
// from a multiple to a signle // from a multiple to a signle
$data = array('_other' => 'something', '_choices' => array()); $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); $this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized);
$data = array('_other' => 'something', '_choices' => array('')); $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); $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()), $this->assertSame(array('_other' => '', '_choices' => array()),
$deserialized); $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 // from single to multiple
$data = 'my-value'; $data = 'my-value';
$deserialized = $this->cfChoice->deserialize($data, $customField); $deserialized = $this->cfChoice->deserialize($data, $customField);
$this->assertSame(array('my-value'), $deserialized); $this->assertSame(array('my-value'), $deserialized);
// from multiple to multiple // from multiple to multiple
$data = array('my-value'); $data = array('my-value');
$deserialized = $this->cfChoice->deserialize($data, $customField); $deserialized = $this->cfChoice->deserialize($data, $customField);
$this->assertSame(array('my-value'), $deserialized); $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() public function serializedRepresentationDataProvider()
@ -356,7 +390,8 @@ class CustomFieldsChoiceTest extends KernelTestCase
CustomFieldChoice::MULTIPLE => true CustomFieldChoice::MULTIPLE => true
)); ));
$isEmpty = $this->cfChoice->isEmptyValue($data, $customField); $deserialized = $this->cfChoice->deserialize($data, $customField);
$isEmpty = $this->cfChoice->isEmptyValue($deserialized, $customField);
$this->assertFalse($isEmpty); $this->assertFalse($isEmpty);
} }
@ -364,7 +399,7 @@ class CustomFieldsChoiceTest extends KernelTestCase
/** /**
* *
* @dataProvider emptyDataProvider * @dataProvider emptyDataProvider
* @param mixed $data * @param mixed $data deserialized data
*/ */
public function testIsEmptyValueEmpty($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 * @return array
*/ */
@ -416,6 +452,16 @@ class CustomFieldsChoiceTest extends KernelTestCase
// multiple with allow other // multiple with allow other
array('_other' => '', '_choices' => array()) array('_other' => '', '_choices' => array())
), ),
// 6
array(
// empty
array('_other' => null, '_choices' => null)
),
// 7
array(
// empty
array(null)
)
); );
} }