function FieldAttachStorageTest::testFieldAttachSaveEmptyData
Same name in other branches
- 8.9.x core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testFieldAttachSaveEmptyData()
- 10 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testFieldAttachSaveEmptyData()
- 11.x 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 150
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->assertEquals($values, $entity->{$this->fieldTestData->field_name}
->getValue(), '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->assertEquals($values, $entity->{$this->fieldTestData->field_name}
->getValue(), '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');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.