function EntitySchemaTest::testEntitySchemaUpdate

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

Tests that entity schema responds to changes in the entity type definition.

File

core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php, line 101

Class

EntitySchemaTest
Tests the default entity storage schema handler.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntitySchemaUpdate() {
    $this->installModule('entity_schema_test');
    $storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_update');
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']);
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']);
    $schema_handler = $this->database
        ->schema();
    $tables = [
        'entity_test_update',
        'entity_test_update_revision',
        'entity_test_update_data',
        'entity_test_update_revision_data',
    ];
    $dedicated_tables = [
        'entity_test_update__custom_bundle_field',
        'entity_test_update_revision__custom_bundle_field',
    ];
    // Initially only the base table and the dedicated field data table should
    // exist.
    foreach ($tables as $index => $table) {
        $this->assertEquals(!$index, $schema_handler->tableExists($table), new FormattableMarkup('Entity schema correct for the @table table.', [
            '@table' => $table,
        ]));
    }
    $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), new FormattableMarkup('Field schema correct for the @table table.', [
        '@table' => $table,
    ]));
    // Update the entity type definition and check that the entity schema now
    // supports translations and revisions.
    $this->updateEntityType(TRUE);
    foreach ($tables as $table) {
        $this->assertTrue($schema_handler->tableExists($table), new FormattableMarkup('Entity schema correct for the @table table.', [
            '@table' => $table,
        ]));
    }
    foreach ($dedicated_tables as $table) {
        $this->assertTrue($schema_handler->tableExists($table), new FormattableMarkup('Field schema correct for the @table table.', [
            '@table' => $table,
        ]));
    }
    // Revert changes and check that the entity schema now does not support
    // neither translations nor revisions.
    $this->updateEntityType(FALSE);
    foreach ($tables as $index => $table) {
        $this->assertEquals(!$index, $schema_handler->tableExists($table), new FormattableMarkup('Entity schema correct for the @table table.', [
            '@table' => $table,
        ]));
    }
    $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), new FormattableMarkup('Field schema correct for the @table table.', [
        '@table' => $table,
    ]));
}

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