function EntityDefinitionUpdateTest::testEntityIndexCreateDeleteWithoutData

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testEntityIndexCreateDeleteWithoutData()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testEntityIndexCreateDeleteWithoutData()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testEntityIndexCreateDeleteWithoutData()

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

File

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

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\KernelTests\Core\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 = [
        'entity_test_update' => [
            t('The %entity_type entity type needs to be updated.', [
                '%entity_type' => $this->entityTypeManager
                    ->getDefinition('entity_test_update')
                    ->getLabel(),
            ]),
        ],
    ];
    $this->assertEquals($expected, $this->entityDefinitionUpdateManager
        ->getChangeSummary(), 'EntityDefinitionUpdateManager reports the expected change summary.');
    // Run the update and ensure the new index is created.
    $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__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 = [
        'entity_test_update' => [
            t('The %entity_type entity type needs to be updated.', [
                '%entity_type' => $this->entityTypeManager
                    ->getDefinition('entity_test_update')
                    ->getLabel(),
            ]),
        ],
    ];
    $this->assertEquals($expected, $this->entityDefinitionUpdateManager
        ->getChangeSummary(), 'EntityDefinitionUpdateManager reports the expected change summary.');
    // Run the update and ensure the index is deleted.
    $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->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();
    $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);
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.