function AddToAllBundlesConfigActionTest::testInstantiateNewFieldOnAllBundles

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

File

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

Class

AddToAllBundlesConfigActionTest
Tests Add To All Bundles Config Action.

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('entity_test');
  $this->assertArrayNotHasKey('body', $storage_definitions);
  $this->applyAction('field.storage.entity_test.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' => 'entity_test',
    'field_name' => 'body',
  ]);
  ksort($body_fields);
  $this->assertSame([
    'entity_test.entity_test.body',
    'entity_test.one.body',
    'entity_test.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.