You are here

public function EntityDefinitionUpdateTest::testEntityIndexCreateDeleteWithoutData in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest::testEntityIndexCreateDeleteWithoutData()

Tests creating and deleting a multi-field index when there are no existing entities.

File

core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php, line 483
Contains \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest.

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testEntityIndexCreateDeleteWithoutData() {

  // Add an entity index and ensure the update manager reports that as an
  // update to the entity type.
  $this
    ->addEntityIndex();
  $this
    ->assertTrue($this->entityDefinitionUpdateManager
    ->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
  $expected = array(
    'entity_test_update' => array(
      t('Update the %entity_type entity type.', array(
        '%entity_type' => $this->entityManager
          ->getDefinition('entity_test_update')
          ->getLabel(),
      )),
    ),
  );
  $this
    ->assertEqual($this->entityDefinitionUpdateManager
    ->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');

  // Run the update and ensure the new index is created.
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertTrue($this->database
    ->schema()
    ->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created.');

  // Remove the index and ensure the update manager reports that as an
  // update to the entity type.
  $this
    ->removeEntityIndex();
  $this
    ->assertTrue($this->entityDefinitionUpdateManager
    ->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
  $expected = array(
    'entity_test_update' => array(
      t('Update the %entity_type entity type.', array(
        '%entity_type' => $this->entityManager
          ->getDefinition('entity_test_update')
          ->getLabel(),
      )),
    ),
  );
  $this
    ->assertEqual($this->entityDefinitionUpdateManager
    ->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');

  // Run the update and ensure the index is deleted.
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertFalse($this->database
    ->schema()
    ->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index deleted.');

  // Test that composite indexes are handled correctly when dropping and
  // re-creating one of their columns.
  $this
    ->addEntityIndex();
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $storage_definition = $this->entityDefinitionUpdateManager
    ->getFieldStorageDefinition('name', 'entity_test_update');
  $this->entityDefinitionUpdateManager
    ->updateFieldStorageDefinition($storage_definition);
  $this
    ->assertTrue($this->database
    ->schema()
    ->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created.');
  $this->entityDefinitionUpdateManager
    ->uninstallFieldStorageDefinition($storage_definition);
  $this
    ->assertFalse($this->database
    ->schema()
    ->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index deleted.');
  $this->entityDefinitionUpdateManager
    ->installFieldStorageDefinition('name', 'entity_test_update', 'entity_test', $storage_definition);
  $this
    ->assertTrue($this->database
    ->schema()
    ->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created again.');
}