function BlockRebuildTest::testRebuildInvalidBlocks

Same name in other branches
  1. 9 core/modules/block/tests/src/Kernel/BlockRebuildTest.php \Drupal\Tests\block\Kernel\BlockRebuildTest::testRebuildInvalidBlocks()
  2. 8.9.x core/modules/block/tests/src/Kernel/BlockRebuildTest.php \Drupal\Tests\block\Kernel\BlockRebuildTest::testRebuildInvalidBlocks()
  3. 10 core/modules/block/tests/src/Kernel/BlockRebuildTest.php \Drupal\Tests\block\Kernel\BlockRebuildTest::testRebuildInvalidBlocks()

@covers ::block_rebuild

File

core/modules/block/tests/src/Kernel/BlockRebuildTest.php, line 75

Class

BlockRebuildTest
Tests block_rebuild().

Namespace

Drupal\Tests\block\Kernel

Code

public function testRebuildInvalidBlocks() : void {
    $this->placeBlock('system_powered_by_block', [
        'region' => 'content',
    ]);
    $block1 = $this->placeBlock('system_powered_by_block', [
        'id' => 'invalid_block1',
    ]);
    $block2 = $this->placeBlock('system_powered_by_block', [
        'id' => 'invalid_block2',
    ]);
    $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());
    $blockRebuild = new BlockHooks();
    $blockRebuild->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('stark');
    $this->assertSame($default_region, $block1->getRegion());
    $this->assertFalse($block1->status());
    $this->assertSame($default_region, $block2->getRegion());
    $this->assertFalse($block2->status());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.