function FieldSqlStorageTest::testFieldUpdateIndexesWithData

Test adding and removing indexes while data is present.

File

drupal/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php, line 353
Definition of Drupal\field_sql_storage\FieldSqlStorageTest.

Class

FieldSqlStorageTest
Tests field storage.

Namespace

Drupal\field_sql_storage\Tests

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_entity(1, 1, $instance['bundle']);
  $entity->{$field_name}[LANGUAGE_NOT_SPECIFIED][0]['value'] = 'field data';
  $entity
    ->save();

  // Add an index
  $field = array(
    'field_name' => $field_name,
    'indexes' => array(
      'value' => array(
        array(
          'value',
          255,
        ),
      ),
    ),
  );
  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(
        array(
          'value',
          127,
        ),
        array(
          'format',
          127,
        ),
      ),
    ),
  );
  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_entity(1, 1, $instance['bundle']);
  field_attach_load('test_entity', array(
    1 => $entity,
  ));
  $this
    ->assertEqual($entity->{$field_name}[LANGUAGE_NOT_SPECIFIED][0]['value'], 'field data', t("Index changes performed without dropping the tables"));
}