BlockRebuildTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Drupal\Tests\block\Kernel;
  3. use Drupal\block\Entity\Block;
  4. use Drupal\Core\StringTranslation\TranslatableMarkup;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Drupal\simpletest\BlockCreationTrait;
  7. /**
  8. * Tests block_rebuild().
  9. *
  10. * @group block
  11. */
  12. class BlockRebuildTest extends KernelTestBase {
  13. use BlockCreationTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static $modules = ['block', 'system'];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp() {
  22. parent::setUp();
  23. $this->container->get('theme_installer')->install(['stable', 'classy']);
  24. $this->container->get('config.factory')->getEditable('system.theme')->set('default', 'classy')->save();
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function setUpBeforeClass() {
  30. parent::setUpBeforeClass();
  31. // @todo Once block_rebuild() is refactored to auto-loadable code, remove
  32. // this require statement.
  33. require_once static::getDrupalRoot() . '/core/modules/block/block.module';
  34. }
  35. /**
  36. * @covers ::block_rebuild
  37. */
  38. public function testRebuildNoBlocks() {
  39. block_rebuild();
  40. $messages = \Drupal::messenger()->all();
  41. \Drupal::messenger()->deleteAll();
  42. $this->assertEquals([], $messages);
  43. }
  44. /**
  45. * @covers ::block_rebuild
  46. */
  47. public function testRebuildNoInvalidBlocks() {
  48. $this->placeBlock('system_powered_by_block', ['region' => 'content']);
  49. block_rebuild();
  50. $messages = \Drupal::messenger()->all();
  51. \Drupal::messenger()->deleteAll();
  52. $this->assertEquals([], $messages);
  53. }
  54. /**
  55. * @covers ::block_rebuild
  56. */
  57. public function testRebuildInvalidBlocks() {
  58. $this->placeBlock('system_powered_by_block', ['region' => 'content']);
  59. $block1 = $this->placeBlock('system_powered_by_block');
  60. $block2 = $this->placeBlock('system_powered_by_block');
  61. $block2->disable()->save();
  62. // Use the config API directly to bypass Block::preSave().
  63. \Drupal::configFactory()->getEditable('block.block.' . $block1->id())->set('region', 'INVALID')->save();
  64. \Drupal::configFactory()->getEditable('block.block.' . $block2->id())->set('region', 'INVALID')->save();
  65. // Reload block entities.
  66. $block1 = Block::load($block1->id());
  67. $block2 = Block::load($block2->id());
  68. $this->assertSame('INVALID', $block1->getRegion());
  69. $this->assertTrue($block1->status());
  70. $this->assertSame('INVALID', $block2->getRegion());
  71. $this->assertFalse($block2->status());
  72. block_rebuild();
  73. // Reload block entities.
  74. $block1 = Block::load($block1->id());
  75. $block2 = Block::load($block2->id());
  76. $messages = \Drupal::messenger()->all();
  77. \Drupal::messenger()->deleteAll();
  78. $expected = ['warning' => [new TranslatableMarkup('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block1->id(), '%region' => 'INVALID'])]];
  79. $this->assertEquals($expected, $messages);
  80. $default_region = system_default_region('classy');
  81. $this->assertSame($default_region, $block1->getRegion());
  82. $this->assertFalse($block1->status());
  83. $this->assertSame($default_region, $block2->getRegion());
  84. $this->assertFalse($block2->status());
  85. }
  86. }