function EntitySchemaTest::testEntitySchemaUpdate

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php \Drupal\KernelTests\Core\Entity\EntitySchemaTest::testEntitySchemaUpdate()
  2. 8.9.x 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 103

Class

EntitySchemaTest
Tests the default entity storage schema handler.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntitySchemaUpdate() : void {
  $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), "Entity schema correct for the {$table} table.");
  }
  $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), "Field schema correct for the {$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), "Entity schema correct for the {$table} table.");
  }
  foreach ($dedicated_tables as $table) {
    $this->assertTrue($schema_handler->tableExists($table), "Field schema correct for the {$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), "Entity schema correct for the {$table} table.");
  }
  $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), "Field schema correct for the {$table} table.");
}

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