You are here

public function EntitySchemaTest::testEntitySchemaUpdate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntitySchemaTest.php \Drupal\system\Tests\Entity\EntitySchemaTest::testEntitySchemaUpdate()

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

File

core/modules/system/src/Tests/Entity/EntitySchemaTest.php, line 83
Contains \Drupal\system\Tests\Entity\EntitySchemaTest.

Class

EntitySchemaTest
Tests adding a custom bundle field.

Namespace

Drupal\system\Tests\Entity

Code

public function testEntitySchemaUpdate() {
  $this
    ->installModule('entity_schema_test');
  $storage_definitions = $this->entityManager
    ->getFieldStorageDefinitions('entity_test');
  $this->entityManager
    ->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']);
  $this->entityManager
    ->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']);
  $schema_handler = $this->database
    ->schema();
  $tables = array(
    'entity_test',
    'entity_test_revision',
    'entity_test_field_data',
    'entity_test_field_revision',
  );
  $dedicated_tables = array(
    'entity_test__custom_bundle_field',
    'entity_test_revision__custom_bundle_field',
  );

  // Initially only the base table and the dedicated field data table should
  // exist.
  foreach ($tables as $index => $table) {
    $this
      ->assertEqual($schema_handler
      ->tableExists($table), !$index, SafeMarkup::format('Entity schema correct for the @table table.', array(
      '@table' => $table,
    )));
  }
  $this
    ->assertTrue($schema_handler
    ->tableExists($dedicated_tables[0]), SafeMarkup::format('Field schema correct for the @table table.', array(
    '@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), SafeMarkup::format('Entity schema correct for the @table table.', array(
      '@table' => $table,
    )));
  }
  foreach ($dedicated_tables as $table) {
    $this
      ->assertTrue($schema_handler
      ->tableExists($table), SafeMarkup::format('Field schema correct for the @table table.', array(
      '@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
      ->assertEqual($schema_handler
      ->tableExists($table), !$index, SafeMarkup::format('Entity schema correct for the @table table.', array(
      '@table' => $table,
    )));
  }
  $this
    ->assertTrue($schema_handler
    ->tableExists($dedicated_tables[0]), SafeMarkup::format('Field schema correct for the @table table.', array(
    '@table' => $table,
  )));
}