BlockInvalidRegionTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\Tests\block\Functional;
  3. use Drupal\Tests\BrowserTestBase;
  4. use Drupal\block\Entity\Block;
  5. /**
  6. * Tests that an active block assigned to a non-existing region triggers the
  7. * warning message and is disabled.
  8. *
  9. * @group block
  10. */
  11. class BlockInvalidRegionTest extends BrowserTestBase {
  12. /**
  13. * Modules to install.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['block', 'block_test'];
  18. protected function setUp() {
  19. parent::setUp();
  20. // Create an admin user.
  21. $admin_user = $this->drupalCreateUser([
  22. 'administer site configuration',
  23. 'access administration pages',
  24. 'administer blocks',
  25. ]);
  26. $this->drupalLogin($admin_user);
  27. }
  28. /**
  29. * Tests that blocks assigned to invalid regions work correctly.
  30. */
  31. public function testBlockInInvalidRegion() {
  32. // Enable a test block and place it in an invalid region.
  33. $block = $this->drupalPlaceBlock('test_html');
  34. \Drupal::configFactory()->getEditable('block.block.' . $block->id())->set('region', 'invalid_region')->save();
  35. $block = Block::load($block->id());
  36. $warning_message = t('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block->id(), '%region' => 'invalid_region']);
  37. // Clearing the cache should disable the test block placed in the invalid region.
  38. $this->drupalPostForm('admin/config/development/performance', [], 'Clear all caches');
  39. $this->assertRaw($warning_message, 'Enabled block was in the invalid region and has been disabled.');
  40. // Clear the cache to check if the warning message is not triggered.
  41. $this->drupalPostForm('admin/config/development/performance', [], 'Clear all caches');
  42. $this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.');
  43. // Place disabled test block in the invalid region of the default theme.
  44. \Drupal::configFactory()->getEditable('block.block.' . $block->id())->set('region', 'invalid_region')->save();
  45. $block = Block::load($block->id());
  46. // Clear the cache to check if the warning message is not triggered.
  47. $this->drupalPostForm('admin/config/development/performance', [], 'Clear all caches');
  48. $this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.');
  49. }
  50. }