3 namespace Drupal\Tests\block\Kernel;
5 use Drupal\block\Entity\Block;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\simpletest\BlockCreationTrait;
11 * Tests block_rebuild().
15 class BlockRebuildTest extends KernelTestBase {
17 use BlockCreationTrait;
22 public static $modules = ['block', 'system'];
27 protected function setUp() {
30 $this->container->get('theme_installer')->install(['stable', 'classy']);
31 $this->container->get('config.factory')->getEditable('system.theme')->set('default', 'classy')->save();
37 public static function setUpBeforeClass() {
38 parent::setUpBeforeClass();
40 // @todo Once block_rebuild() is refactored to auto-loadable code, remove
41 // this require statement.
42 require_once static::getDrupalRoot() . '/core/modules/block/block.module';
46 * @covers ::block_rebuild
48 public function testRebuildNoBlocks() {
50 $messages = drupal_get_messages();
51 $this->assertEquals([], $messages);
55 * @covers ::block_rebuild
57 public function testRebuildNoInvalidBlocks() {
58 $this->placeBlock('system_powered_by_block', ['region' => 'content']);
61 $messages = drupal_get_messages();
62 $this->assertEquals([], $messages);
66 * @covers ::block_rebuild
68 public function testRebuildInvalidBlocks() {
69 $this->placeBlock('system_powered_by_block', ['region' => 'content']);
70 $block1 = $this->placeBlock('system_powered_by_block');
71 $block2 = $this->placeBlock('system_powered_by_block');
72 $block2->disable()->save();
73 // Use the config API directly to bypass Block::preSave().
74 \Drupal::configFactory()->getEditable('block.block.' . $block1->id())->set('region', 'INVALID')->save();
75 \Drupal::configFactory()->getEditable('block.block.' . $block2->id())->set('region', 'INVALID')->save();
77 // Reload block entities.
78 $block1 = Block::load($block1->id());
79 $block2 = Block::load($block2->id());
81 $this->assertSame('INVALID', $block1->getRegion());
82 $this->assertTrue($block1->status());
83 $this->assertSame('INVALID', $block2->getRegion());
84 $this->assertFalse($block2->status());
88 // Reload block entities.
89 $block1 = Block::load($block1->id());
90 $block2 = Block::load($block2->id());
92 $messages = drupal_get_messages();
93 $expected = ['warning' => [new TranslatableMarkup('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block1->id(), '%region' => 'INVALID'])]];
94 $this->assertEquals($expected, $messages);
96 $default_region = system_default_region('classy');
97 $this->assertSame($default_region, $block1->getRegion());
98 $this->assertFalse($block1->status());
99 $this->assertSame($default_region, $block2->getRegion());
100 $this->assertFalse($block2->status());