function AddComponentTest::testAddComponent

Tests adding a component to a view display using a config action.

@dataProvider provider

File

core/modules/layout_builder/tests/src/Kernel/Plugin/ConfigAction/AddComponentTest.php, line 76

Class

AddComponentTest
@coversDefaultClass \Drupal\layout_builder\Plugin\ConfigAction\AddComponent

Namespace

Drupal\Tests\layout_builder\Kernel\Plugin\ConfigAction

Code

public function testAddComponent(array $config_action_value, string $expected_region, int $added_component_expected_weight, int $existing_component_expected_weight, ?array $expected_error = NULL) : void {
    if ($expected_error !== NULL) {
        $this->expectException($expected_error[0]);
        $this->expectExceptionMessage($expected_error[1]);
    }
    $this->configActionManager
        ->applyAction('addComponentToLayout', 'core.entity_view_display.entity_test.bundle_with_extra_fields.default', $config_action_value);
    $view_display = $this->container
        ->get(EntityTypeManagerInterface::class)
        ->getStorage('entity_view_display')
        ->load('entity_test.bundle_with_extra_fields.default');
    $this->plugin
        ->setContextValue('display', $view_display);
    $components = $this->plugin
        ->getSection(0)
        ->getComponents();
    $uuid = end($components)->getUuid();
    // If we pass the same existing UUID, we replace it.
    $is_replacing = $added_component_expected_weight === $existing_component_expected_weight;
    $expected_existing_plugin = $is_replacing ? 'my_plugin_id' : 'extra_field_block:entity_test:bundle_with_extra_fields:display_extra_field';
    $this->assertCount($is_replacing ? 1 : 2, $components);
    $this->assertSame($expected_existing_plugin, $components['1445597a-c674-431d-ac0a-277d99347a7f']->getPluginId());
    $this->assertSame('my_plugin_id', $components[$uuid]->getPluginId());
    $this->assertSame($expected_region, $components[$uuid]->getRegion());
    $this->assertSame($added_component_expected_weight, $components[$uuid]->getWeight());
    // Assert weight of the existing component in the layout_twocol_section
    // first region.
    $this->assertSame($existing_component_expected_weight, $components['1445597a-c674-431d-ac0a-277d99347a7f']->getWeight());
    // Assert the component configuration (defined with its config schema), and the
    // additional configuration (ignored in config schema)
    $this->assertSame($config_action_value['component']['configuration'], $components[$uuid]->get('configuration'));
    $this->assertSame($config_action_value['component']['additional'] ?? [], $components[$uuid]->get('additional'));
}

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