FieldSqlStorageTestCase::testFieldUpdateIndexesWithData

7 field_sql_storage.test FieldSqlStorageTestCase::testFieldUpdateIndexesWithData()
8 field_sql_storage.test FieldSqlStorageTestCase::testFieldUpdateIndexesWithData()

Test adding and removing indexes while data is present.

File

modules/field/modules/field_sql_storage/field_sql_storage.test, line 336
Tests for field_sql_storage.module.

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);
  $tables = array(_field_sql_storage_tablename($field), _field_sql_storage_revision_tablename($field));

  // Verify the indexes we will create do not exist yet.
  foreach ($tables as $table) {
    $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);
  foreach ($tables as $table) {
    $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);
  foreach ($tables as $table) {
    $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"));
}
Login or register to post comments