comparison core/modules/block/tests/src/Kernel/BlockRebuildTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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_get_messages();
51 $this->assertEquals([], $messages);
52 }
53
54 /**
55 * @covers ::block_rebuild
56 */
57 public function testRebuildNoInvalidBlocks() {
58 $this->placeBlock('system_powered_by_block', ['region' => 'content']);
59
60 block_rebuild();
61 $messages = drupal_get_messages();
62 $this->assertEquals([], $messages);
63 }
64
65 /**
66 * @covers ::block_rebuild
67 */
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();
76
77 // Reload block entities.
78 $block1 = Block::load($block1->id());
79 $block2 = Block::load($block2->id());
80
81 $this->assertSame('INVALID', $block1->getRegion());
82 $this->assertTrue($block1->status());
83 $this->assertSame('INVALID', $block2->getRegion());
84 $this->assertFalse($block2->status());
85
86 block_rebuild();
87
88 // Reload block entities.
89 $block1 = Block::load($block1->id());
90 $block2 = Block::load($block2->id());
91
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);
95
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());
101 }
102
103 }