function ConfigActionsTest::testPlaceBlockAtPosition

Tests placing a block at the first and last position in a region.

File

core/modules/block/tests/src/Kernel/ConfigActionsTest.php, line 159

Class

ConfigActionsTest
@covers \Drupal\block\Plugin\ConfigAction\PlaceBlock[[api-linebreak]] @covers \Drupal\block\Plugin\ConfigAction\PlaceBlockDeriver[[api-linebreak]] @group block

Namespace

Drupal\Tests\block\Kernel

Code

public function testPlaceBlockAtPosition() : void {
  // Ensure there's at least one block already in the region.
  $block = Block::create([
    'id' => 'block_1',
    'theme' => 'olivero',
    'region' => 'content_above',
    'weight' => 0,
    'plugin' => 'system_powered_by_block',
  ]);
  $block->save();
  $this->configActionManager
    ->applyAction('placeBlockInDefaultTheme', 'block.block.first', [
    'plugin' => $block->getPluginId(),
    'region' => [
      $block->getTheme() => $block->getRegion(),
    ],
    'position' => 'first',
  ]);
  $this->configActionManager
    ->applyAction('placeBlockInDefaultTheme', 'block.block.last', [
    'plugin' => $block->getPluginId(),
    'region' => [
      $block->getTheme() => $block->getRegion(),
    ],
    'position' => 'last',
  ]);
  // Query for blocks in the region, ordered by weight.
  $blocks = $this->container
    ->get(EntityTypeManagerInterface::class)
    ->getStorage('block')
    ->getQuery()
    ->condition('theme', $block->getTheme())
    ->condition('region', $block->getRegion())
    ->sort('weight', 'ASC')
    ->execute();
  $this->assertGreaterThanOrEqual(3, $blocks);
  $this->assertSame('first', key($blocks));
  $this->assertSame('last', end($blocks));
}

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