summaryrefslogtreecommitdiffstats
path: root/core/modules/block/tests/src/Kernel/BlockRebuildTest.php
blob: 2443c21052ff1d7bddf480ef6dba7ea8081833c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace Drupal\Tests\block\Kernel;

use Drupal\block\Entity\Block;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\simpletest\BlockCreationTrait;

/**
 * Tests block_rebuild().
 *
 * @group block
 */
class BlockRebuildTest extends KernelTestBase {

  use BlockCreationTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = ['block', 'system'];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->container->get('theme_installer')->install(['stable', 'classy']);
    $this->container->get('config.factory')->getEditable('system.theme')->set('default', 'classy')->save();
  }

  /**
   * {@inheritdoc}
   */
  public static function setUpBeforeClass() {
    parent::setUpBeforeClass();

    // @todo Once block_rebuild() is refactored to auto-loadable code, remove
    //   this require statement.
    require_once static::getDrupalRoot() . '/core/modules/block/block.module';
  }

  /**
   * @covers ::block_rebuild
   */
  public function testRebuildNoBlocks() {
    block_rebuild();
    $messages = \Drupal::messenger()->all();
    \Drupal::messenger()->deleteAll();
    $this->assertEquals([], $messages);
  }

  /**
   * @covers ::block_rebuild
   */
  public function testRebuildNoInvalidBlocks() {
    $this->placeBlock('system_powered_by_block', ['region' => 'content']);

    block_rebuild();
    $messages = \Drupal::messenger()->all();
    \Drupal::messenger()->deleteAll();
    $this->assertEquals([], $messages);
  }

  /**
   * @covers ::block_rebuild
   */
  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();
    // Use the config API directly to bypass Block::preSave().
    \Drupal::configFactory()->getEditable('block.block.' . $block1->id())->set('region', 'INVALID')->save();
    \Drupal::configFactory()->getEditable('block.block.' . $block2->id())->set('region', 'INVALID')->save();

    // Reload block entities.
    $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();

    // Reload block entities.
    $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('classy');
    $this->assertSame($default_region, $block1->getRegion());
    $this->assertFalse($block1->status());
    $this->assertSame($default_region, $block2->getRegion());
    $this->assertFalse($block2->status());
  }

}