You are here

public function EntityDefinitionUpdateTest::testBaseFieldUpdateWithExistingData in Drupal 10

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

Tests updating a base field when it has existing data.

File

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

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testBaseFieldUpdateWithExistingData() {

  // Add the base field and run the update.
  $this
    ->addBaseField();
  $this
    ->applyEntityUpdates();

  // Save an entity with the base field populated.
  $this->entityTypeManager
    ->getStorage('entity_test_update')
    ->create([
    'new_base_field' => 'foo',
  ])
    ->save();

  // Change the field's field type and apply updates. It's expected to
  // throw an exception.
  $this
    ->modifyBaseField();
  try {
    $this
      ->applyEntityUpdates();
    $this
      ->fail('FieldStorageDefinitionUpdateForbiddenException thrown when trying to update a field schema that has data.');
  } catch (FieldStorageDefinitionUpdateForbiddenException $e) {

    // Expected exception; just continue testing.
  }
}