You are here

function FieldAttachStorageTest::testFieldAttachSaveLoad in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldAttachStorageTest.php \Drupal\field\Tests\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/src/Tests/FieldAttachStorageTest.php, line 31
Contains \Drupal\field\Tests\FieldAttachStorageTest.

Class

FieldAttachStorageTest
Tests storage-related Field Attach API functions.

Namespace

Drupal\field\Tests

Code

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 = array();
  $entity = entity_create($entity_type);
  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.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'], format_string('Current revision: expected value %delta was found.', array(
      '%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, format_string('Revision %revision_id: expected number of values.', array(
      '%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'], format_string('Revision %revision_id: expected value %delta was found.', array(
        '%revision_id' => $revision_id,
        '%delta' => $delta,
      )));
    }
  }
}