You are here

public function EntityFieldTest::testFieldOverrideBundleField in Drupal 10

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

Tests creating a field override config for a bundle field.

See also

entity_test_entity_base_field_info_alter()

File

core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php, line 658

Class

EntityFieldTest
Tests the Entity Field API.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldOverrideBundleField() {

  // First make sure the bundle field override in code, which is provided by
  // the test entity works.
  entity_test_create_bundle('some_test_bundle', 'Some test bundle', 'entity_test_field_override');
  $field_definitions = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('entity_test_field_override', 'entity_test_field_override');
  $this
    ->assertEquals('The default description.', $field_definitions['name']
    ->getDescription());
  $this
    ->assertNull($field_definitions['name']
    ->getTargetBundle());
  $field_definitions = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('entity_test_field_override', 'some_test_bundle');
  $this
    ->assertEquals('Custom description.', $field_definitions['name']
    ->getDescription());
  $this
    ->assertEquals('some_test_bundle', $field_definitions['name']
    ->getTargetBundle());

  // Now create a config override of the bundle field.
  $field_config = $field_definitions['name']
    ->getConfig('some_test_bundle');
  $field_config
    ->setTranslatable(FALSE);
  $field_config
    ->save();

  // Make sure both overrides are present.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $field_definitions = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('entity_test_field_override', 'some_test_bundle');
  $this
    ->assertEquals('Custom description.', $field_definitions['name']
    ->getDescription());
  $this
    ->assertEquals('some_test_bundle', $field_definitions['name']
    ->getTargetBundle());
  $this
    ->assertFalse($field_definitions['name']
    ->isTranslatable());
}