You are here

public function EntitySchemaTest::testCustomFieldCreateDelete 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::testCustomFieldCreateDelete()

Tests the custom bundle field creation and deletion.

File

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

Class

EntitySchemaTest
Tests adding a custom bundle field.

Namespace

Drupal\system\Tests\Entity

Code

public function testCustomFieldCreateDelete() {

  // Install the module which adds the field.
  $this
    ->installModule('entity_schema_test');
  $this->entityManager
    ->clearCachedDefinitions();
  $storage_definitions = $this->entityManager
    ->getFieldStorageDefinitions('entity_test');
  $this
    ->assertNotNull($storage_definitions['custom_base_field'], 'Base field definition found.');
  $this
    ->assertNotNull($storage_definitions['custom_bundle_field'], 'Bundle field definition found.');

  // Make sure the field schema can be created.
  $this->entityManager
    ->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']);
  $this->entityManager
    ->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']);

  /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  $table_mapping = $this->entityManager
    ->getStorage('entity_test')
    ->getTableMapping();
  $base_table = current($table_mapping
    ->getTableNames());
  $base_column = current($table_mapping
    ->getColumnNames('custom_base_field'));
  $this
    ->assertTrue($this->database
    ->schema()
    ->fieldExists($base_table, $base_column), 'Table column created');
  $table = $table_mapping
    ->getDedicatedDataTableName($storage_definitions['custom_bundle_field']);
  $this
    ->assertTrue($this->database
    ->schema()
    ->tableExists($table), 'Table created');

  // Make sure the field schema can be deleted.
  $this->entityManager
    ->onFieldStorageDefinitionDelete($storage_definitions['custom_base_field']);
  $this->entityManager
    ->onFieldStorageDefinitionDelete($storage_definitions['custom_bundle_field']);
  $this
    ->assertFalse($this->database
    ->schema()
    ->fieldExists($base_table, $base_column), 'Table column dropped');
  $this
    ->assertFalse($this->database
    ->schema()
    ->tableExists($table), 'Table dropped');
}