Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / block / tests / src / Kernel / BlockRebuildTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Kernel;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\simpletest\BlockCreationTrait;
9
10 /**
11  * Tests block_rebuild().
12  *
13  * @group block
14  */
15 class BlockRebuildTest extends KernelTestBase {
16
17   use BlockCreationTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['block', 'system'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     $this->container->get('theme_installer')->install(['stable', 'classy']);
31     $this->container->get('config.factory')->getEditable('system.theme')->set('default', 'classy')->save();
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function setUpBeforeClass() {
38     parent::setUpBeforeClass();
39
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';
43   }
44
45   /**
46    * @covers ::block_rebuild
47    */
48   public function testRebuildNoBlocks() {
49     block_rebuild();
50     $messages = \Drupal::messenger()->all();
51     \Drupal::messenger()->deleteAll();
52     $this->assertEquals([], $messages);
53   }
54
55   /**
56    * @covers ::block_rebuild
57    */
58   public function testRebuildNoInvalidBlocks() {
59     $this->placeBlock('system_powered_by_block', ['region' => 'content']);
60
61     block_rebuild();
62     $messages = \Drupal::messenger()->all();
63     \Drupal::messenger()->deleteAll();
64     $this->assertEquals([], $messages);
65   }
66
67   /**
68    * @covers ::block_rebuild
69    */
70   public function testRebuildInvalidBlocks() {
71     $this->placeBlock('system_powered_by_block', ['region' => 'content']);
72     $block1 = $this->placeBlock('system_powered_by_block');
73     $block2 = $this->placeBlock('system_powered_by_block');
74     $block2->disable()->save();
75     // Use the config API directly to bypass Block::preSave().
76     \Drupal::configFactory()->getEditable('block.block.' . $block1->id())->set('region', 'INVALID')->save();
77     \Drupal::configFactory()->getEditable('block.block.' . $block2->id())->set('region', 'INVALID')->save();
78
79     // Reload block entities.
80     $block1 = Block::load($block1->id());
81     $block2 = Block::load($block2->id());
82
83     $this->assertSame('INVALID', $block1->getRegion());
84     $this->assertTrue($block1->status());
85     $this->assertSame('INVALID', $block2->getRegion());
86     $this->assertFalse($block2->status());
87
88     block_rebuild();
89
90     // Reload block entities.
91     $block1 = Block::load($block1->id());
92     $block2 = Block::load($block2->id());
93
94     $messages = \Drupal::messenger()->all();
95     \Drupal::messenger()->deleteAll();
96     $expected = ['warning' => [new TranslatableMarkup('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block1->id(), '%region' => 'INVALID'])]];
97     $this->assertEquals($expected, $messages);
98
99     $default_region = system_default_region('classy');
100     $this->assertSame($default_region, $block1->getRegion());
101     $this->assertFalse($block1->status());
102     $this->assertSame($default_region, $block2->getRegion());
103     $this->assertFalse($block2->status());
104   }
105
106 }