You are here

public function EntityDefinitionUpdateTest::testCreateIndexUsingEntityStorageSchemaWithData in Drupal 8

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

Ensures that a new entity level index is created when data exists.

See also

Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::onEntityTypeUpdate

File

core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php, line 1112

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testCreateIndexUsingEntityStorageSchemaWithData() {

  // Save an entity.
  $name = $this
    ->randomString();
  $storage = $this->entityTypeManager
    ->getStorage('entity_test_update');
  $entity = $storage
    ->create([
    'name' => $name,
  ]);
  $entity
    ->save();

  // Create an index.
  $indexes = [
    'entity_test_update__type_index' => [
      'type',
    ],
  ];
  $this->state
    ->set('entity_test_update.additional_entity_indexes', $indexes);
  $entity_type = \Drupal::entityTypeManager()
    ->getDefinition('entity_test_update');
  $original = \Drupal::service('entity.last_installed_schema.repository')
    ->getLastInstalledDefinition('entity_test_update');
  \Drupal::service('entity_type.listener')
    ->onEntityTypeUpdate($entity_type, $original);
  $this
    ->assertTrue($this->database
    ->schema()
    ->indexExists('entity_test_update', 'entity_test_update__type_index'), "New index 'entity_test_update__type_index' has been created on the 'entity_test_update' table.");

  // Check index size in for MySQL.
  if (Database::getConnection()
    ->driver() == 'mysql') {
    $result = Database::getConnection()
      ->query('SHOW INDEX FROM {entity_test_update} WHERE key_name = \'entity_test_update__type_index\' and column_name = \'type\'')
      ->fetchObject();
    $this
      ->assertEqual(191, $result->Sub_part, 'The index length has been restricted to 191 characters for UTF8MB4 compatibility.');
  }
}