BlockRebuildTest.php in Drupal 10
File
core/modules/block/tests/src/Kernel/BlockRebuildTest.php
View source
<?php
namespace Drupal\Tests\block\Kernel;
use Drupal\block\Entity\Block;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\block\Traits\BlockCreationTrait;
class BlockRebuildTest extends KernelTestBase {
use BlockCreationTrait;
protected static $modules = [
'block',
'system',
];
protected function setUp() : void {
parent::setUp();
$this->container
->get('theme_installer')
->install([
'stark',
]);
$this->container
->get('config.factory')
->getEditable('system.theme')
->set('default', 'stark')
->save();
}
public static function setUpBeforeClass() : void {
parent::setUpBeforeClass();
require_once static::getDrupalRoot() . '/core/modules/block/block.module';
}
public function testRebuildNoBlocks() {
block_rebuild();
$messages = \Drupal::messenger()
->all();
\Drupal::messenger()
->deleteAll();
$this
->assertEquals([], $messages);
}
public function testRebuildNoInvalidBlocks() {
$this
->placeBlock('system_powered_by_block', [
'region' => 'content',
]);
block_rebuild();
$messages = \Drupal::messenger()
->all();
\Drupal::messenger()
->deleteAll();
$this
->assertEquals([], $messages);
}
public function testRebuildInvalidBlocks() {
$this
->placeBlock('system_powered_by_block', [
'region' => 'content',
]);
$block1 = $this
->placeBlock('system_powered_by_block');
$block2 = $this
->placeBlock('system_powered_by_block');
$block2
->disable()
->save();
\Drupal::configFactory()
->getEditable('block.block.' . $block1
->id())
->set('region', 'INVALID')
->save();
\Drupal::configFactory()
->getEditable('block.block.' . $block2
->id())
->set('region', 'INVALID')
->save();
$block1 = Block::load($block1
->id());
$block2 = Block::load($block2
->id());
$this
->assertSame('INVALID', $block1
->getRegion());
$this
->assertTrue($block1
->status());
$this
->assertSame('INVALID', $block2
->getRegion());
$this
->assertFalse($block2
->status());
block_rebuild();
$block1 = Block::load($block1
->id());
$block2 = Block::load($block2
->id());
$messages = \Drupal::messenger()
->all();
\Drupal::messenger()
->deleteAll();
$expected = [
'warning' => [
new TranslatableMarkup('The block %info was assigned to the invalid region %region and has been disabled.', [
'%info' => $block1
->id(),
'%region' => 'INVALID',
]),
],
];
$this
->assertEquals($expected, $messages);
$default_region = system_default_region('stark');
$this
->assertSame($default_region, $block1
->getRegion());
$this
->assertFalse($block1
->status());
$this
->assertSame($default_region, $block2
->getRegion());
$this
->assertFalse($block2
->status());
}
}