function 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 32

Class

FieldAttachStorageTest
Tests storage-related Field Attach API functions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testFieldAttachSaveLoad() : void {
  $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;
  }
  /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
  $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->assertCount($cardinality, $entity->{$this->fieldTestData->field_name}, 'Current revision: expected number of values');
  for ($delta = 0; $delta < $cardinality; $delta++) {
    // The field value loaded matches the one inserted or updated.
    $this->assertEquals($values[$current_revision][$delta]['value'], $entity->{$this->fieldTestData->field_name}[$delta]->value, "Current revision: expected value {$delta} was found.");
  }
  // 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->assertCount($cardinality, $entity->{$this->fieldTestData->field_name}, "Revision {$revision_id}: expected number of values.");
    for ($delta = 0; $delta < $cardinality; $delta++) {
      // The field value loaded matches the one inserted or updated.
      $this->assertEquals($values[$revision_id][$delta]['value'], $entity->{$this->fieldTestData->field_name}[$delta]->value, "Revision {$revision_id}: expected value {$delta} was found.");
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.