function 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 <a href="/api/drupal/core%21modules%21field%21src%21Plugin%21ConfigAction%21AddToAllBundles.php/class/AddToAllBundles/11.x" title="Adds a field to all bundles of its target entity type." class="local">\Drupal\field\Plugin\ConfigAction\AddToAllBundles</a>

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 "addToAllBundles" plugin does not exist.');
    $this->applyAction('user.role.anonymous');
}

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