NewDefaultThemeBlocksTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Drupal\Tests\block\Functional;
  3. use Drupal\Tests\BrowserTestBase;
  4. /**
  5. * Tests that the new default theme gets blocks.
  6. *
  7. * @group block
  8. */
  9. class NewDefaultThemeBlocksTest extends BrowserTestBase {
  10. /**
  11. * Modules to install.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['block'];
  16. /**
  17. * Check the enabled Bartik blocks are correctly copied over.
  18. */
  19. public function testNewDefaultThemeBlocks() {
  20. $default_theme = $this->config('system.theme')->get('default');
  21. // Add two instances of the user login block.
  22. $this->drupalPlaceBlock('user_login_block', [
  23. 'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
  24. ]);
  25. $this->drupalPlaceBlock('user_login_block', [
  26. 'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
  27. ]);
  28. // Add an instance of a different block.
  29. $this->drupalPlaceBlock('system_powered_by_block', [
  30. 'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
  31. ]);
  32. // Install a different theme.
  33. $new_theme = 'bartik';
  34. $this->assertFalse($new_theme == $default_theme, 'The new theme is different from the previous default theme.');
  35. \Drupal::service('theme_handler')->install([$new_theme]);
  36. $this->config('system.theme')
  37. ->set('default', $new_theme)
  38. ->save();
  39. /** @var \Drupal\Core\Entity\EntityStorageInterface $block_storage */
  40. $block_storage = $this->container->get('entity_type.manager')->getStorage('block');
  41. // Ensure that the new theme has all the blocks as the previous default.
  42. $default_block_names = $block_storage->getQuery()
  43. ->condition('theme', $default_theme)
  44. ->execute();
  45. $new_blocks = $block_storage->getQuery()
  46. ->condition('theme', $new_theme)
  47. ->execute();
  48. $this->assertTrue(count($default_block_names) == count($new_blocks), 'The new default theme has the same number of blocks as the previous theme.');
  49. foreach ($default_block_names as $default_block_name) {
  50. // Remove the matching block from the list of blocks in the new theme.
  51. // E.g., if the old theme has block.block.stark_admin,
  52. // unset block.block.bartik_admin.
  53. unset($new_blocks[str_replace($default_theme . '_', $new_theme . '_', $default_block_name)]);
  54. }
  55. $this->assertTrue(empty($new_blocks), 'The new theme has exactly the same blocks as the previous default theme.');
  56. // Install a hidden base theme and ensure blocks are not copied.
  57. $base_theme = 'test_basetheme';
  58. \Drupal::service('theme_handler')->install([$base_theme]);
  59. $new_blocks = $block_storage->getQuery()
  60. ->condition('theme', $base_theme)
  61. ->execute();
  62. $this->assertTrue(empty($new_blocks), 'Installing a hidden base theme does not copy blocks from the default theme.');
  63. }
  64. }