You are here

function FieldSqlStorageTest::testFieldWrite in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php \Drupal\system\Tests\Entity\FieldSqlStorageTest::testFieldWrite()

Tests field saving works correctly by reading directly from the tables.

File

core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php, line 182
Contains \Drupal\system\Tests\Entity\FieldSqlStorageTest.

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\system\Tests\Entity

Code

function testFieldWrite() {
  $entity_type = $bundle = 'entity_test_rev';
  $entity = entity_create($entity_type);
  $revision_values = array();

  // Check insert. Add one value too many.
  $values = array();
  for ($delta = 0; $delta <= $this->fieldCardinality; $delta++) {
    $values[$delta]['value'] = mt_rand(1, 127);
  }
  $entity->{$this->fieldName} = $values;
  $entity
    ->save();

  // Read the tables and check the correct values have been stored.
  $rows = db_select($this->table, 't')
    ->fields('t')
    ->execute()
    ->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
  $this
    ->assertEqual(count($rows), $this->fieldCardinality);
  foreach ($rows as $delta => $row) {
    $expected = array(
      'bundle' => $bundle,
      'deleted' => 0,
      'entity_id' => $entity
        ->id(),
      'revision_id' => $entity
        ->getRevisionId(),
      'langcode' => $entity
        ->language()
        ->getId(),
      'delta' => $delta,
      $this->fieldName . '_value' => $values[$delta]['value'],
    );
    $this
      ->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
  }

  // Test update. Add less values and check that the previous values did not
  // persist.
  $values = array();
  for ($delta = 0; $delta <= $this->fieldCardinality - 2; $delta++) {
    $values[$delta]['value'] = mt_rand(1, 127);
  }
  $entity->{$this->fieldName} = $values;
  $entity
    ->save();
  $rows = db_select($this->table, 't')
    ->fields('t')
    ->execute()
    ->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
  $this
    ->assertEqual(count($rows), count($values));
  foreach ($rows as $delta => $row) {
    $expected = array(
      'bundle' => $bundle,
      'deleted' => 0,
      'entity_id' => $entity
        ->id(),
      'revision_id' => $entity
        ->getRevisionId(),
      'langcode' => $entity
        ->language()
        ->getId(),
      'delta' => $delta,
      $this->fieldName . '_value' => $values[$delta]['value'],
    );
    $this
      ->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
  }

  // Create a new revision.
  $revision_values[$entity
    ->getRevisionId()] = $values;
  $values = array();
  for ($delta = 0; $delta < $this->fieldCardinality; $delta++) {
    $values[$delta]['value'] = mt_rand(1, 127);
  }
  $entity->{$this->fieldName} = $values;
  $entity
    ->setNewRevision();
  $entity
    ->save();
  $revision_values[$entity
    ->getRevisionId()] = $values;

  // Check that data for both revisions are in the revision table.
  foreach ($revision_values as $revision_id => $values) {
    $rows = db_select($this->revisionTable, 't')
      ->fields('t')
      ->condition('revision_id', $revision_id)
      ->execute()
      ->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
    $this
      ->assertEqual(count($rows), min(count($values), $this->fieldCardinality));
    foreach ($rows as $delta => $row) {
      $expected = array(
        'bundle' => $bundle,
        'deleted' => 0,
        'entity_id' => $entity
          ->id(),
        'revision_id' => $revision_id,
        'langcode' => $entity
          ->language()
          ->getId(),
        'delta' => $delta,
        $this->fieldName . '_value' => $values[$delta]['value'],
      );
      $this
        ->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
    }
  }

  // Test emptying the field.
  $entity->{$this->fieldName} = NULL;
  $entity
    ->save();
  $rows = db_select($this->table, 't')
    ->fields('t')
    ->execute()
    ->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
  $this
    ->assertEqual(count($rows), 0);
}