You are here

public function FieldStorageCrudTest::testUpdateForbid in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php \Drupal\Tests\field\Kernel\FieldStorageCrudTest::testUpdateForbid()

Tests field type modules forbidding an update.

File

core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php, line 473

Class

FieldStorageCrudTest
Tests field storage create, read, update, and delete.

Namespace

Drupal\Tests\field\Kernel

Code

public function testUpdateForbid() {
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'forbidden',
    'entity_type' => 'entity_test',
    'type' => 'test_field',
    'settings' => [
      'changeable' => 0,
      'unchangeable' => 0,
    ],
  ]);
  $field_storage
    ->save();
  $field_storage
    ->setSetting('changeable', $field_storage
    ->getSetting('changeable') + 1);
  try {
    $field_storage
      ->save();
  } catch (FieldStorageDefinitionUpdateForbiddenException $e) {
    $this
      ->fail('An unchangeable setting cannot be updated.');
  }
  $field_storage
    ->setSetting('unchangeable', $field_storage
    ->getSetting('unchangeable') + 1);
  try {
    $field_storage
      ->save();
    $this
      ->fail('An unchangeable setting can be updated.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(FieldStorageDefinitionUpdateForbiddenException::class, $e);
  }
}