You are here

function FieldSqlNoRevisionsTestCase::testFieldAttachInsertAndUpdate in Field SQL norevisions 7.2

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

Reads mysql to verify correct data is written when using insert and update.

File

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

Class

FieldSqlNoRevisionsTestCase
Tests field storage.

Code

function testFieldAttachInsertAndUpdate() {
  $entity_type = 'test_entity';
  $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
  $langcode = LANGUAGE_NONE;

  // Test insert.
  $values = array();

  // Note: we try to insert one extra value ('<=' instead of '<').
  // TODO : test empty values filtering and "compression" (store consecutive deltas).
  for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
    $values[$delta]['value'] = mt_rand(1, 127);
  }
  $entity->{$this->field_name}[$langcode] = $rev_values[0] = $values;
  field_attach_insert($entity_type, $entity);
  $rows = db_select($this->table, 't')
    ->fields('t')
    ->execute()
    ->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
  foreach ($values as $delta => $value) {
    if ($delta < $this->field['cardinality']) {
      $this
        ->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Value {$delta} is inserted correctly"));
    }
    else {
      $this
        ->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets inserted.");
    }
  }

  // Test update.
  $entity = field_test_create_stub_entity(0, 1, $this->instance['bundle']);
  $values = array();

  // Note: we try to update one extra value ('<=' instead of '<').
  for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
    $values[$delta]['value'] = mt_rand(1, 127);
  }
  $entity->{$this->field_name}[$langcode] = $rev_values[1] = $values;
  field_attach_update($entity_type, $entity);
  $rows = db_select($this->table, 't')
    ->fields('t')
    ->execute()
    ->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
  foreach ($values as $delta => $value) {
    if ($delta < $this->field['cardinality']) {
      $this
        ->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Value {$delta} is updated correctly"));
    }
    else {
      $this
        ->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets updated.");
    }
  }

  // Check that update leaves the field data untouched if
  // $entity->{$field_name} is absent.
  unset($entity->{$this->field_name});
  field_attach_update($entity_type, $entity);
  $rows = db_select($this->table, 't')
    ->fields('t')
    ->execute()
    ->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
  foreach ($values as $delta => $value) {
    if ($delta < $this->field['cardinality']) {
      $this
        ->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Update with no field_name entry leaves value {$delta} untouched"));
    }
  }

  // Check that update with an empty $entity->$field_name empties the field.
  $entity->{$this->field_name} = NULL;
  field_attach_update($entity_type, $entity);
  $rows = db_select($this->table, 't')
    ->fields('t')
    ->execute()
    ->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
  $this
    ->assertEqual(count($rows), 0, t("Update with an empty field_name entry empties the field."));
}