You are here

function FieldSqlStorageTest::testFieldUpdateIndexesWithData in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php \Drupal\system\Tests\Entity\FieldSqlStorageTest::testFieldUpdateIndexesWithData()

Test adding and removing indexes while data is present.

File

core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php, line 386
Contains \Drupal\system\Tests\Entity\FieldSqlStorageTest.

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\system\Tests\Entity

Code

function testFieldUpdateIndexesWithData() {

  // Create a decimal field.
  $field_name = 'testfield';
  $entity_type = 'entity_test_rev';
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'type' => 'text',
  ));
  $field_storage
    ->save();
  $field = entity_create('field_config', array(
    'field_storage' => $field_storage,
    'bundle' => $entity_type,
  ));
  $field
    ->save();
  $tables = array(
    $this->tableMapping
      ->getDedicatedDataTableName($field_storage),
    $this->tableMapping
      ->getDedicatedRevisionTableName($field_storage),
  );

  // 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", array(
      '@table' => $table,
    )));
    $this
      ->assertFalse(Database::getConnection()
      ->schema()
      ->indexExists($table, 'value_format'), t("No index named value_format exists in @table", array(
      '@table' => $table,
    )));
  }

  // Add data so the table cannot be dropped.
  $entity = entity_create($entity_type, array(
    'id' => 1,
    'revision_id' => 1,
  ));
  $entity->{$field_name}->value = 'field data';
  $entity
    ->enforceIsNew();
  $entity
    ->save();

  // Add an index.
  $field_storage
    ->setIndexes([
    'value' => [
      [
        'value',
        255,
      ],
    ],
  ]);
  $field_storage
    ->save();
  foreach ($tables as $table) {
    $this
      ->assertTrue(Database::getConnection()
      ->schema()
      ->indexExists($table, "{$field_name}_value"), t("Index on value created in @table", array(
      '@table' => $table,
    )));
  }

  // Add a different index, removing the existing custom one.
  $field_storage
    ->setIndexes([
    'value_format' => [
      [
        'value',
        127,
      ],
      [
        'format',
        127,
      ],
    ],
  ]);
  $field_storage
    ->save();
  foreach ($tables as $table) {
    $this
      ->assertTrue(Database::getConnection()
      ->schema()
      ->indexExists($table, "{$field_name}_value_format"), t("Index on value_format created in @table", array(
      '@table' => $table,
    )));
    $this
      ->assertFalse(Database::getConnection()
      ->schema()
      ->indexExists($table, "{$field_name}_value"), t("Index on value removed in @table", array(
      '@table' => $table,
    )));
  }

  // Verify that the tables were not dropped in the process.
  $entity = $this->container
    ->get('entity.manager')
    ->getStorage($entity_type)
    ->load(1);
  $this
    ->assertEqual($entity->{$field_name}->value, 'field data', t("Index changes performed without dropping the tables"));
}