You are here

public function EntityDefinitionUpdateTest::testInitialValueFromFieldErrorHandling in Drupal 9

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

Tests the error handling when using initial values from another field.

File

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

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testInitialValueFromFieldErrorHandling() {

  // Check that setting invalid values for 'initial value from field' doesn't
  // work.
  try {
    $this
      ->addBaseField();
    $storage_definition = BaseFieldDefinition::create('string')
      ->setLabel(t('A new base field'))
      ->setInitialValueFromField('field_that_does_not_exist');
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition);
    $this
      ->fail('Using a non-existent field as initial value does not work.');
  } catch (FieldException $e) {
    $this
      ->assertEquals('Illegal initial value definition on new_base_field: The field field_that_does_not_exist does not exist.', $e
      ->getMessage());
  }
  try {
    $this
      ->addBaseField();
    $storage_definition = BaseFieldDefinition::create('integer')
      ->setLabel(t('A new base field'))
      ->setInitialValueFromField('name');
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition);
    $this
      ->fail('Using a field of a different type as initial value does not work.');
  } catch (FieldException $e) {
    $this
      ->assertEquals('Illegal initial value definition on new_base_field: The field types do not match.', $e
      ->getMessage());
  }
  try {

    // Add a base field that will not be stored in the shared tables.
    $initial_field = BaseFieldDefinition::create('string')
      ->setName('initial_field')
      ->setLabel(t('An initial field'))
      ->setCardinality(2);
    $this->state
      ->set('entity_test_update.additional_base_field_definitions', [
      'initial_field' => $initial_field,
    ]);
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition('initial_field', 'entity_test_update', 'entity_test', $initial_field);

    // Now add the base field which will try to use the previously added field
    // as the source of its initial values.
    $new_base_field = BaseFieldDefinition::create('string')
      ->setName('new_base_field')
      ->setLabel(t('A new base field'))
      ->setInitialValueFromField('initial_field');
    $this->state
      ->set('entity_test_update.additional_base_field_definitions', [
      'initial_field' => $initial_field,
      'new_base_field' => $new_base_field,
    ]);
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $new_base_field);
    $this
      ->fail('Using a field that is not stored in the shared tables as initial value does not work.');
  } catch (FieldException $e) {
    $this
      ->assertEquals('Illegal initial value definition on new_base_field: Both fields have to be stored in the shared entity tables.', $e
      ->getMessage());
  }
}