You are here

public function EntityDefinitionUpdateTest::testBundleFieldUpdateWithExistingData in Drupal 8

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

Tests updating a bundle field when it has existing data.

File

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

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testBundleFieldUpdateWithExistingData() {

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

  // Save an entity with the bundle field populated.
  entity_test_create_bundle('custom');
  $this->entityTypeManager
    ->getStorage('entity_test_update')
    ->create([
    'type' => 'test_bundle',
    'new_bundle_field' => 'foo',
  ])
    ->save();

  // Change the field's field type and apply updates. It's expected to
  // throw an exception.
  $this
    ->modifyBundleField();
  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.
  }
}