3 namespace Drupal\Tests\block\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests that the new default theme gets blocks.
12 class NewDefaultThemeBlocksTest extends BrowserTestBase {
19 public static $modules = ['block'];
22 * Check the enabled Bartik blocks are correctly copied over.
24 public function testNewDefaultThemeBlocks() {
25 $default_theme = $this->config('system.theme')->get('default');
27 // Add two instances of the user login block.
28 $this->drupalPlaceBlock('user_login_block', [
29 'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
31 $this->drupalPlaceBlock('user_login_block', [
32 'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
35 // Add an instance of a different block.
36 $this->drupalPlaceBlock('system_powered_by_block', [
37 'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
40 // Install a different theme.
41 $new_theme = 'bartik';
42 $this->assertFalse($new_theme == $default_theme, 'The new theme is different from the previous default theme.');
43 \Drupal::service('theme_handler')->install([$new_theme]);
44 $this->config('system.theme')
45 ->set('default', $new_theme)
48 /** @var \Drupal\Core\Entity\EntityStorageInterface $block_storage */
49 $block_storage = $this->container->get('entity_type.manager')->getStorage('block');
51 // Ensure that the new theme has all the blocks as the previous default.
52 $default_block_names = $block_storage->getQuery()
53 ->condition('theme', $default_theme)
55 $new_blocks = $block_storage->getQuery()
56 ->condition('theme', $new_theme)
58 $this->assertTrue(count($default_block_names) == count($new_blocks), 'The new default theme has the same number of blocks as the previous theme.');
59 foreach ($default_block_names as $default_block_name) {
60 // Remove the matching block from the list of blocks in the new theme.
61 // E.g., if the old theme has block.block.stark_admin,
62 // unset block.block.bartik_admin.
63 unset($new_blocks[str_replace($default_theme . '_', $new_theme . '_', $default_block_name)]);
65 $this->assertTrue(empty($new_blocks), 'The new theme has exactly the same blocks as the previous default theme.');
67 // Install a hidden base theme and ensure blocks are not copied.
68 $base_theme = 'test_basetheme';
69 \Drupal::service('theme_handler')->install([$base_theme]);
70 $new_blocks = $block_storage->getQuery()
71 ->condition('theme', $base_theme)
73 $this->assertTrue(empty($new_blocks), 'Installing a hidden base theme does not copy blocks from the default theme.');