function AddToAllBundlesConfigActionTest::testInstantiateNewFieldOnAllBundles

Same name and namespace in other branches
  1. 11.x core/modules/field/tests/src/Kernel/AddToAllBundlesConfigActionTest.php \Drupal\Tests\field\Kernel\AddToAllBundlesConfigActionTest::testInstantiateNewFieldOnAllBundles()

Tests instantiating a field on all bundles of an entity type.

File

core/modules/field/tests/src/Kernel/AddToAllBundlesConfigActionTest.php, line 51

Class

AddToAllBundlesConfigActionTest
@covers \Drupal\field\Plugin\ConfigAction\AddToAllBundles[[api-linebreak]]

Namespace

Drupal\Tests\field\Kernel

Code

public function testInstantiateNewFieldOnAllBundles() : void {
  // Ensure the body field doesn't actually exist yet.
  $storage_definitions = $this->container
    ->get(EntityFieldManagerInterface::class)
    ->getFieldStorageDefinitions('node');
  $this->assertArrayNotHasKey('body', $storage_definitions);
  $this->applyAction('field.storage.node.body');
  // Fields and expected data exist.
  /** @var \Drupal\field\FieldConfigInterface[] $body_fields */
  $body_fields = $this->container
    ->get(EntityTypeManagerInterface::class)
    ->getStorage('field_config')
    ->loadByProperties([
    'entity_type' => 'node',
    'field_name' => 'body',
  ]);
  ksort($body_fields);
  $this->assertSame([
    'node.one.body',
    'node.two.body',
  ], array_keys($body_fields));
  foreach ($body_fields as $field) {
    $this->assertSame('Body field label', $field->label());
    $this->assertSame('Set by config actions.', $field->getDescription());
  }
  // Expect an error when the 'addToAllBundles' action is invoked on anything
  // other than a field storage config entity.
  $this->expectException(PluginNotFoundException::class);
  $this->expectExceptionMessage('The "user_role" entity does not support the "addToAllBundles" config action.');
  $this->applyAction('user.role.anonymous');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.