You are here

protected function EntityDefinitionTestTrait::applyEntityUpdates in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php \Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait::applyEntityUpdates()

Applies all the detected valid changes.

Use this with care, as it will apply updates for any module, which will lead to unpredictable results.

Parameters

string $entity_type_id: (optional) Applies changes only for the specified entity type ID. Defaults to NULL.

21 calls to EntityDefinitionTestTrait::applyEntityUpdates()
ContentEntityNonRevisionableFieldTest::testMultiColumnNonRevisionableBaseField in core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php
Tests multi column non revisionable base field for revisionable entity.
DefaultTableMappingIntegrationTest::setUp in core/tests/Drupal/KernelTests/Core/Entity/DefaultTableMappingIntegrationTest.php
EntityDefinitionTestTrait::enableNewEntityType in core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php
Enables a new entity type definition.
EntityDefinitionUpdateTest::testBaseFieldCreateDeleteWithExistingEntities in core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php
Tests creating and deleting a base field if entities exist.
EntityDefinitionUpdateTest::testBaseFieldCreateUpdateDeleteWithoutData in core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php
Tests creating, updating, and deleting a base field if no entities exist.

... See full list

File

core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php, line 25

Class

EntityDefinitionTestTrait
Provides some test methods used to update existing entity definitions.

Namespace

Drupal\Tests\system\Functional\Entity\Traits

Code

protected function applyEntityUpdates($entity_type_id = NULL) {
  $complete_change_list = \Drupal::entityDefinitionUpdateManager()
    ->getChangeList();
  if ($complete_change_list) {

    // In case there are changes, explicitly invalidate caches.
    \Drupal::entityTypeManager()
      ->clearCachedDefinitions();
    \Drupal::service('entity_field.manager')
      ->clearCachedFieldDefinitions();
  }
  if ($entity_type_id) {
    $complete_change_list = array_intersect_key($complete_change_list, [
      $entity_type_id => TRUE,
    ]);
  }
  foreach ($complete_change_list as $entity_type_id => $change_list) {

    // Process entity type definition changes before storage definitions ones
    // this is necessary when you change an entity type from non-revisionable
    // to revisionable and at the same time add revisionable fields to the
    // entity type.
    if (!empty($change_list['entity_type'])) {
      $this
        ->doEntityUpdate($change_list['entity_type'], $entity_type_id);
    }

    // Process field storage definition changes.
    if (!empty($change_list['field_storage_definitions'])) {
      $storage_definitions = \Drupal::service('entity_field.manager')
        ->getFieldStorageDefinitions($entity_type_id);
      $original_storage_definitions = \Drupal::service('entity.last_installed_schema.repository')
        ->getLastInstalledFieldStorageDefinitions($entity_type_id);
      foreach ($change_list['field_storage_definitions'] as $field_name => $change) {
        $storage_definition = isset($storage_definitions[$field_name]) ? $storage_definitions[$field_name] : NULL;
        $original_storage_definition = isset($original_storage_definitions[$field_name]) ? $original_storage_definitions[$field_name] : NULL;
        $this
          ->doFieldUpdate($change, $storage_definition, $original_storage_definition);
      }
    }
  }
}