You are here

public function EntityDefinitionUpdateTest::testCreateIndexUsingEntityStorageSchemaWithData in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php \Drupal\system\Tests\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/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php, line 731
Contains \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest.

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testCreateIndexUsingEntityStorageSchemaWithData() {

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

  // Create an index.
  $indexes = array(
    'entity_test_update__type_index' => array(
      'type',
    ),
  );
  $this->state
    ->set('entity_test_update.additional_entity_indexes', $indexes);
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $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.');
  }
}