You are here

function FieldSqlNoRevisionsTestCase::testFieldAttachLoad in Field SQL norevisions 7.2

Same name and namespace in other branches
  1. 7 field_sql_norevisions.test \FieldSqlNoRevisionsTestCase::testFieldAttachLoad()

Uses the mysql tables and records to verify field_load_revision works correctly.

File

./field_sql_norevisions.test, line 41
Tests for field_sql_norevisions.module.

Class

FieldSqlNoRevisionsTestCase
Tests field storage.

Code

function testFieldAttachLoad() {
  $entity_type = 'test_entity';
  $eid = 0;
  $langcode = LANGUAGE_NONE;
  $columns = array(
    'entity_type',
    'entity_id',
    'revision_id',
    'delta',
    'language',
    $this->field_name . '_value',
  );
  for ($evid = 0; $evid < 4; ++$evid) {
    $values[$evid] = array();

    // Note: we insert one extra value ('<=' instead of '<').
    for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
      $value = mt_rand(1, 127);
      $values[$evid][] = $value;
    }
  }

  // Insert data for the "most current revision" into the field table
  $query = db_insert($this->table)
    ->fields($columns);
  foreach ($values[0] as $delta => $value) {
    $query
      ->values(array(
      $entity_type,
      $eid,
      0,
      $delta,
      $langcode,
      $value,
    ));
  }
  $query
    ->execute();

  // Load the "most current revision"
  $entity = field_test_create_stub_entity($eid, 0, $this->instance['bundle']);
  field_attach_load($entity_type, array(
    $eid => $entity,
  ));
  foreach ($values[0] as $delta => $value) {
    if ($delta < $this->field['cardinality']) {
      $this
        ->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $value, "Value {$delta} is loaded correctly for current revision");
    }
    else {
      $this
        ->assertFalse(array_key_exists($delta, $entity->{$this->field_name}[$langcode]), "No extraneous value gets loaded for current revision.");
    }
  }

  // Test that loading every revision just returns the current value
  for ($evid = 0; $evid < 4; ++$evid) {
    $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
    field_attach_load_revision($entity_type, array(
      $eid => $entity,
    ));
    foreach ($values[$evid] as $delta => $value) {
      if ($delta < $this->field['cardinality']) {
        $this
          ->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $values[0][$delta], "Value {$delta} for revision {$evid} is loaded correctly");
      }
      else {
        $this
          ->assertFalse(array_key_exists($delta, $entity->{$this->field_name}[$langcode]), "No extraneous value gets loaded for revision {$evid}.");
      }
    }
  }

  // Add a translation in an unavailable language and verify it is not loaded.
  $eid = $evid = 1;
  $unavailable_language = 'xx';
  $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
  $values = array(
    $entity_type,
    $eid,
    $evid,
    0,
    $unavailable_language,
    mt_rand(1, 127),
  );
  db_insert($this->table)
    ->fields($columns)
    ->values($values)
    ->execute();
  field_attach_load($entity_type, array(
    $eid => $entity,
  ));
  $this
    ->assertFalse(array_key_exists($unavailable_language, $entity->{$this->field_name}), 'Field translation in an unavailable language ignored');
}