function FieldAttachStorageTest::testFieldAttachSaveLoad
Same name in other branches
- 9 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testFieldAttachSaveLoad()
- 10 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testFieldAttachSaveLoad()
- 11.x core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testFieldAttachSaveLoad()
Check field values insert, update and load.
Works independently of the underlying field storage backend. Inserts or updates random field data and then loads and verifies the data.
File
-
core/
modules/ field/ tests/ src/ Kernel/ FieldAttachStorageTest.php, line 28
Class
- FieldAttachStorageTest
- Tests storage-related Field Attach API functions.
Namespace
Drupal\Tests\field\KernelCode
public function testFieldAttachSaveLoad() {
$entity_type = 'entity_test_rev';
$this->createFieldWithStorage('', $entity_type);
$cardinality = $this->fieldTestData->field_storage
->getCardinality();
// TODO : test empty values filtering and "compression" (store consecutive deltas).
// Preparation: create three revisions and store them in $revision array.
$values = [];
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create();
for ($revision_id = 0; $revision_id < 3; $revision_id++) {
// Note: we try to insert one extra value.
$current_values = $this->_generateTestFieldValues($cardinality + 1);
$entity->{$this->fieldTestData->field_name}
->setValue($current_values);
$entity->setNewRevision();
$entity->save();
$entity_id = $entity->id();
$current_revision = $entity->getRevisionId();
$values[$current_revision] = $current_values;
}
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$storage->resetCache();
$entity = $storage->load($entity_id);
// Confirm current revision loads the correct data.
// Number of values per field loaded equals the field cardinality.
$this->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, 'Current revision: expected number of values');
for ($delta = 0; $delta < $cardinality; $delta++) {
// The field value loaded matches the one inserted or updated.
$this->assertEqual($entity->{$this->fieldTestData->field_name}[$delta]->value, $values[$current_revision][$delta]['value'], new FormattableMarkup('Current revision: expected value %delta was found.', [
'%delta' => $delta,
]));
}
// Confirm each revision loads the correct data.
foreach (array_keys($values) as $revision_id) {
$entity = $storage->loadRevision($revision_id);
// Number of values per field loaded equals the field cardinality.
$this->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, new FormattableMarkup('Revision %revision_id: expected number of values.', [
'%revision_id' => $revision_id,
]));
for ($delta = 0; $delta < $cardinality; $delta++) {
// The field value loaded matches the one inserted or updated.
$this->assertEqual($entity->{$this->fieldTestData->field_name}[$delta]->value, $values[$revision_id][$delta]['value'], new FormattableMarkup('Revision %revision_id: expected value %delta was found.', [
'%revision_id' => $revision_id,
'%delta' => $delta,
]));
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.