function AddNavigationBlockConfigActionTest::testAddBlockToNavigation

Tests add item logic.

Attributes

#[TestWith([ 0, 0, ])] #[TestWith([ 1, 1, ])] #[TestWith([ 3, 3, ])] #[TestWith([ 7, 3, ])]

File

core/modules/navigation/tests/src/Kernel/ConfigAction/AddNavigationBlockConfigActionTest.php, line 50

Class

AddNavigationBlockConfigActionTest
Tests Add Navigation Block Config Action.

Namespace

Drupal\Tests\navigation\Kernel\ConfigAction

Code

public function testAddBlockToNavigation($delta, $computed_delta) : void {
  // Load the navigation section storage.
  $navigation_storage = \Drupal::service('plugin.manager.layout_builder.section_storage')->load('navigation', [
    'navigation' => new Context(new ContextDefinition('string'), 'navigation'),
  ]);
  $section = $navigation_storage->getSection(0);
  $components = $section->getComponentsByRegion('content');
  $this->assertCount(3, $components);
  $data = [
    'delta' => $delta,
    'configuration' => [
      'id' => 'navigation_menu:content',
      'label' => 'Content From Recipe',
      'label_display' => 'visible',
      'provider' => 'navigation',
      'level' => 1,
      'depth' => 2,
    ],
  ];
  // Use the action to add a new block to Navigation.
  /** @var \Drupal\Core\Config\Action\ConfigActionManager $manager */
  $manager = $this->container
    ->get('plugin.manager.config_action');
  $manager->applyAction('addNavigationBlock', 'navigation.block_layout', $data);
  // Load the config after the execution.
  $navigation_storage = \Drupal::service('plugin.manager.layout_builder.section_storage')->load('navigation', [
    'navigation' => new Context(new ContextDefinition('string'), 'navigation'),
  ]);
  $section = $navigation_storage->getSection(0);
  $components = $section->getComponentsByRegion('content');
  $this->assertCount(4, $components);
  $component = array_values($components)[$computed_delta];
  $this->assertSame('content', $component->getRegion());
  $this->assertEquals($data['configuration'], $component->get('configuration'));
}

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