You are here

public function FieldSqlStorageTest::testFieldUpdateIndexesWithData in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldUpdateIndexesWithData()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldUpdateIndexesWithData()

Test adding and removing indexes while data is present.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php, line 388

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldUpdateIndexesWithData() {

  // Create a decimal field.
  $field_name = 'testfield';
  $entity_type = 'entity_test_rev';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'type' => 'text',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $entity_type,
  ]);
  $field
    ->save();
  $tables = [
    $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", [
      '@table' => $table,
    ]));
    $this
      ->assertFalse(Database::getConnection()
      ->schema()
      ->indexExists($table, 'value_format'), t("No index named value_format exists in @table", [
      '@table' => $table,
    ]));
  }

  // Add data so the table cannot be dropped.
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->create([
    '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", [
      '@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", [
      '@table' => $table,
    ]));
    $this
      ->assertFalse(Database::getConnection()
      ->schema()
      ->indexExists($table, "{$field_name}_value"), t("Index on value removed in @table", [
      '@table' => $table,
    ]));
  }

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