You are here

public function FieldableEntityDefinitionUpdateTest::testFieldableEntityTypeUpdates in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\FieldableEntityDefinitionUpdateTest::testFieldableEntityTypeUpdates()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\FieldableEntityDefinitionUpdateTest::testFieldableEntityTypeUpdates()

@covers ::updateFieldableEntityType @dataProvider providerTestFieldableEntityTypeUpdates

File

core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php, line 132

Class

FieldableEntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager's fieldable entity update functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldableEntityTypeUpdates($initial_rev, $initial_mul, $new_rev, $new_mul, $data_migration_supported) {

  // The 'entity_test_update' entity type is neither revisionable nor
  // translatable by default, so we need to get it into the initial testing
  // state. This also covers the "no existing data" scenario for fieldable
  // entity type updates.
  if ($initial_rev || $initial_mul) {
    $entity_type = $this
      ->getUpdatedEntityTypeDefinition($initial_rev, $initial_mul);
    $field_storage_definitions = $this
      ->getUpdatedFieldStorageDefinitions($initial_rev, $initial_mul);
    $this->entityDefinitionUpdateManager
      ->updateFieldableEntityType($entity_type, $field_storage_definitions);
    $this
      ->assertEntityTypeSchema($initial_rev, $initial_mul);
  }

  // Add a few entities so we can test the data copying step.
  $this
    ->insertData($initial_rev, $initial_mul);
  $updated_entity_type = $this
    ->getUpdatedEntityTypeDefinition($new_rev, $new_mul);
  $updated_field_storage_definitions = $this
    ->getUpdatedFieldStorageDefinitions($new_rev, $new_mul);
  if (!$data_migration_supported) {
    $this
      ->expectException(EntityStorageException::class);
    $this
      ->expectExceptionMessage('Converting an entity type from revisionable to non-revisionable or from translatable to non-translatable is not supported.');
  }

  // Check that existing data can be retrieved from the storage before the
  // entity schema is updated.
  if ($data_migration_supported) {
    $this
      ->assertEntityData($initial_rev, $initial_mul);
  }

  // Enable the creation of a new base field during a fieldable entity type
  // update.
  $this->state
    ->set('entity_test_update.install_new_base_field_during_update', TRUE);

  // Simulate a batch run since we are converting the entities one by one.
  $sandbox = [];
  do {
    $this->entityDefinitionUpdateManager
      ->updateFieldableEntityType($updated_entity_type, $updated_field_storage_definitions, $sandbox);
  } while ($sandbox['#finished'] != 1);
  $this
    ->assertEntityTypeSchema($new_rev, $new_mul, TRUE);
  $this
    ->assertEntityData($initial_rev, $initial_mul);
  $change_list = $this->entityDefinitionUpdateManager
    ->getChangeList();
  $this
    ->assertArrayNotHasKey('entity_test_update', $change_list, "There are no remaining updates for the 'entity_test_update' entity type.");

  // Check that we can still save new entities after the schema has been
  // updated.
  $this
    ->insertData($new_rev, $new_mul);

  // Check that the backup tables have been kept in place.
  $this
    ->assertBackupTables();
}