Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testSingleActionCalls()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testSingleActionCalls()

Tests applying single updates.

File

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

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testSingleActionCalls() {
  $db_schema = $this->database
    ->schema();

  // Ensure that a non-existing entity type cannot be installed.
  $message = 'A non-existing entity type cannot be installed';
  try {
    $this->entityDefinitionUpdateManager
      ->installEntityType(new ContentEntityType([
      'id' => 'foo',
    ]));
    $this
      ->fail($message);
  } catch (PluginNotFoundException $e) {

    // Expected exception; just continue testing.
  }

  // Ensure that a field cannot be installed on non-existing entity type.
  $message = 'A field cannot be installed on a non-existing entity type';
  try {
    $storage_definition = BaseFieldDefinition::create('string')
      ->setLabel(t('A new revisionable base field'))
      ->setRevisionable(TRUE);
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition('bar', 'foo', 'entity_test', $storage_definition);
    $this
      ->fail($message);
  } catch (PluginNotFoundException $e) {

    // Expected exception; just continue testing.
  }

  // Ensure that installing an existing entity type is a no-op.
  $entity_type = $this->entityDefinitionUpdateManager
    ->getEntityType('entity_test_update');
  $this->entityDefinitionUpdateManager
    ->installEntityType($entity_type);
  $this
    ->assertTrue($db_schema
    ->tableExists('entity_test_update'), 'Installing an existing entity type is a no-op');

  // Create a new base field.
  $this
    ->addRevisionableBaseField();
  $storage_definition = BaseFieldDefinition::create('string')
    ->setLabel(t('A new revisionable base field'))
    ->setRevisionable(TRUE);
  $this
    ->assertFalse($db_schema
    ->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' does not exist before applying the update.");
  $this->entityDefinitionUpdateManager
    ->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition);
  $this
    ->assertTrue($db_schema
    ->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table.");

  // Ensure that installing an existing field is a no-op.
  $this->entityDefinitionUpdateManager
    ->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition);
  $this
    ->assertTrue($db_schema
    ->fieldExists('entity_test_update', 'new_base_field'), 'Installing an existing field is a no-op');

  // Update an existing field schema.
  $this
    ->modifyBaseField();
  $storage_definition = BaseFieldDefinition::create('text')
    ->setName('new_base_field')
    ->setTargetEntityTypeId('entity_test_update')
    ->setLabel(t('A new revisionable base field'))
    ->setRevisionable(TRUE);
  $this->entityDefinitionUpdateManager
    ->updateFieldStorageDefinition($storage_definition);
  $this
    ->assertFalse($db_schema
    ->fieldExists('entity_test_update', 'new_base_field'), "Previous schema for 'new_base_field' no longer exists.");
  $this
    ->assertTrue($db_schema
    ->fieldExists('entity_test_update', 'new_base_field__value') && $db_schema
    ->fieldExists('entity_test_update', 'new_base_field__format'), "New schema for 'new_base_field' has been created.");

  // Drop an existing field schema.
  $this->entityDefinitionUpdateManager
    ->uninstallFieldStorageDefinition($storage_definition);
  $this
    ->assertFalse($db_schema
    ->fieldExists('entity_test_update', 'new_base_field__value') || $db_schema
    ->fieldExists('entity_test_update', 'new_base_field__format'), "The schema for 'new_base_field' has been dropped.");

  // Make the entity type revisionable.
  $this
    ->updateEntityTypeToRevisionable();
  $this
    ->assertFalse($db_schema
    ->tableExists('entity_test_update_revision'), "The 'entity_test_update_revision' does not exist before applying the update.");
  $this
    ->updateEntityTypeToRevisionable(TRUE);
  $this
    ->assertTrue($db_schema
    ->tableExists('entity_test_update_revision'), "The 'entity_test_update_revision' table has been created.");
}