Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / block / tests / src / Functional / NewDefaultThemeBlocksTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests that the new default theme gets blocks.
9  *
10  * @group block
11  */
12 class NewDefaultThemeBlocksTest extends BrowserTestBase {
13
14   /**
15    * Modules to install.
16    *
17    * @var array
18    */
19   public static $modules = ['block'];
20
21   /**
22    * Check the enabled Bartik blocks are correctly copied over.
23    */
24   public function testNewDefaultThemeBlocks() {
25     $default_theme = $this->config('system.theme')->get('default');
26
27     // Add two instances of the user login block.
28     $this->drupalPlaceBlock('user_login_block', [
29       'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
30     ]);
31     $this->drupalPlaceBlock('user_login_block', [
32       'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
33     ]);
34
35     // Add an instance of a different block.
36     $this->drupalPlaceBlock('system_powered_by_block', [
37       'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
38     ]);
39
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)
46       ->save();
47
48     /** @var \Drupal\Core\Entity\EntityStorageInterface $block_storage */
49     $block_storage = $this->container->get('entity_type.manager')->getStorage('block');
50
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)
54       ->execute();
55     $new_blocks = $block_storage->getQuery()
56       ->condition('theme', $new_theme)
57       ->execute();
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)]);
64     }
65     $this->assertTrue(empty($new_blocks), 'The new theme has exactly the same blocks as the previous default theme.');
66
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)
72       ->execute();
73     $this->assertTrue(empty($new_blocks), 'Installing a hidden base theme does not copy blocks from the default theme.');
74   }
75
76 }