public function FieldAttachStorageTest::testFieldAttachSaveEmptyData in Drupal 8
Same name and namespace in other branches
- 9 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testFieldAttachSaveEmptyData()
Tests insert and update with empty or NULL fields.
File
- core/
modules/ field/ tests/ src/ Kernel/ FieldAttachStorageTest.php, line 148
Class
- FieldAttachStorageTest
- Tests storage-related Field Attach API functions.
Namespace
Drupal\Tests\field\KernelCode
public function testFieldAttachSaveEmptyData() {
$entity_type = 'entity_test';
$this
->createFieldWithStorage('', $entity_type);
$entity_init = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'id' => 1,
]);
// Insert: Field is NULL.
$entity = clone $entity_init;
$entity->{$this->fieldTestData->field_name} = NULL;
$entity
->enforceIsNew();
$entity = $this
->entitySaveReload($entity);
$this
->assertTrue($entity->{$this->fieldTestData->field_name}
->isEmpty(), 'Insert: NULL field results in no value saved');
// All saves after this point should be updates, not inserts.
$entity_init
->enforceIsNew(FALSE);
// Add some real data.
$entity = clone $entity_init;
$values = $this
->_generateTestFieldValues(1);
$entity->{$this->fieldTestData->field_name} = $values;
$entity = $this
->entitySaveReload($entity);
$this
->assertEqual($entity->{$this->fieldTestData->field_name}
->getValue(), $values, 'Field data saved');
// Update: Field is NULL. Data should be wiped.
$entity = clone $entity_init;
$entity->{$this->fieldTestData->field_name} = NULL;
$entity = $this
->entitySaveReload($entity);
$this
->assertTrue($entity->{$this->fieldTestData->field_name}
->isEmpty(), 'Update: NULL field removes existing values');
// Re-add some data.
$entity = clone $entity_init;
$values = $this
->_generateTestFieldValues(1);
$entity->{$this->fieldTestData->field_name} = $values;
$entity = $this
->entitySaveReload($entity);
$this
->assertEqual($entity->{$this->fieldTestData->field_name}
->getValue(), $values, 'Field data saved');
// Update: Field is empty array. Data should be wiped.
$entity = clone $entity_init;
$entity->{$this->fieldTestData->field_name} = [];
$entity = $this
->entitySaveReload($entity);
$this
->assertTrue($entity->{$this->fieldTestData->field_name}
->isEmpty(), 'Update: empty array removes existing values');
}