You are here

function FieldSqlNoRevisionsTestCase::testFieldUpdateIndexesWithData in Field SQL norevisions 7.2

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

Test adding and removing indexes while data is present.

File

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

Class

FieldSqlNoRevisionsTestCase
Tests field storage.

Code

function testFieldUpdateIndexesWithData() {

  // Create a decimal field.
  $field_name = 'testfield';
  $field = array(
    'field_name' => $field_name,
    'type' => 'text',
  );
  $field = field_create_field($field);
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
  );
  $instance = field_create_instance($instance);
  $table = _field_sql_norevisions_tablename($field);

  // Verify the indexes we will create do not exist yet.
  $this
    ->assertFalse(Database::getConnection()
    ->schema()
    ->indexExists($table, 'value'), t("No index named value exists in {$table}"));
  $this
    ->assertFalse(Database::getConnection()
    ->schema()
    ->indexExists($table, 'value_format'), t("No index named value_format exists in {$table}"));

  // Add data so the table cannot be dropped.
  $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
  $entity->{$field_name}[LANGUAGE_NONE][0]['value'] = 'field data';
  field_attach_insert('test_entity', $entity);

  // Add an index
  $field = array(
    'field_name' => $field_name,
    'indexes' => array(
      'value' => array(
        'value',
      ),
    ),
  );
  field_update_field($field);
  $this
    ->assertTrue(Database::getConnection()
    ->schema()
    ->indexExists($table, "{$field_name}_value"), t("Index on value created in {$table}"));

  // Add a different index, removing the existing custom one.
  $field = array(
    'field_name' => $field_name,
    'indexes' => array(
      'value_format' => array(
        'value',
        'format',
      ),
    ),
  );
  field_update_field($field);
  $this
    ->assertTrue(Database::getConnection()
    ->schema()
    ->indexExists($table, "{$field_name}_value_format"), t("Index on value_format created in {$table}"));
  $this
    ->assertFalse(Database::getConnection()
    ->schema()
    ->indexExists($table, "{$field_name}_value"), t("Index on value removed in {$table}"));

  // Verify that the tables were not dropped.
  $entity = field_test_create_stub_entity(0, 0, $instance['bundle']);
  field_attach_load('test_entity', array(
    0 => $entity,
  ));
  $this
    ->assertEqual($entity->{$field_name}[LANGUAGE_NONE][0]['value'], 'field data', t("Index changes performed without dropping the tables"));
}