Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / block / tests / src / Kernel / Migrate / d7 / MigrateBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Kernel\Migrate\d7;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\block_content\Entity\BlockContent;
7 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
8
9 /**
10  * Tests migration of blocks to configuration entities.
11  *
12  * @group block
13  */
14 class MigrateBlockTest extends MigrateDrupal7TestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'block',
21     'views',
22     'comment',
23     'menu_ui',
24     'block_content',
25     'node',
26     'text',
27     'filter',
28     'user',
29   ];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36
37     // Install the themes used for this test.
38     $this->container->get('theme_installer')->install(['bartik', 'seven']);
39
40     $this->installConfig(static::$modules);
41     $this->installEntitySchema('block_content');
42
43     // Set Bartik and Seven as the default public and admin theme.
44     $config = $this->config('system.theme');
45     $config->set('default', 'bartik');
46     $config->set('admin', 'seven');
47     $config->save();
48
49     $this->executeMigrations([
50       'd7_filter_format',
51       'd7_user_role',
52       'block_content_type',
53       'block_content_body_field',
54       'd7_custom_block',
55       'd7_block',
56     ]);
57     block_rebuild();
58   }
59
60   /**
61    * Asserts various aspects of a block.
62    *
63    * @param string $id
64    *   The block ID.
65    * @param string $plugin_id
66    *   The block's plugin ID.
67    * @param array $roles
68    *   Role IDs the block is expected to have.
69    * @param string $pages
70    *   The list of pages on which the block should appear.
71    * @param string $region
72    *   The display region.
73    * @param string $theme
74    *   The theme.
75    * @param string $weight
76    *   The block weight.
77    * @param string $label
78    *   The block label.
79    * @param string $label_display
80    *   The block label display setting.
81    * @param bool $status
82    *   Whether the block is expected to be enabled or disabled.
83    */
84   public function assertEntity($id, $plugin_id, array $roles, $pages, $region, $theme, $weight, $label, $label_display, $status = TRUE) {
85     $block = Block::load($id);
86     $this->assertTrue($block instanceof Block);
87     /** @var \Drupal\block\BlockInterface $block */
88     $this->assertSame($plugin_id, $block->getPluginId());
89
90     $visibility = $block->getVisibility();
91     if ($roles) {
92       $this->assertSame($roles, array_values($visibility['user_role']['roles']));
93       $this->assertSame('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
94     }
95     if ($pages) {
96       $this->assertSame($pages, $visibility['request_path']['pages']);
97     }
98
99     $this->assertSame($region, $block->getRegion());
100     $this->assertSame($theme, $block->getTheme());
101     $this->assertSame($weight, $block->getWeight());
102     $this->assertSame($status, $block->status());
103
104     $config = $this->config('block.block.' . $id);
105     $this->assertSame($label, $config->get('settings.label'));
106     $this->assertSame($label_display, $config->get('settings.label_display'));
107   }
108
109   /**
110    * Tests the block migration.
111    */
112   public function testBlockMigration() {
113     $this->assertEntity('bartik_system_main', 'system_main_block', [], '', 'content', 'bartik', 0, '', '0');
114     $this->assertEntity('bartik_search_form', 'search_form_block', [], '', 'sidebar_first', 'bartik', -1, '', '0');
115     $this->assertEntity('bartik_user_login', 'user_login_block', [], '', 'sidebar_first', 'bartik', 0, '', '0');
116     $this->assertEntity('bartik_system_powered_by', 'system_powered_by_block', [], '', 'footer_fifth', 'bartik', 10, '', '0');
117     $this->assertEntity('seven_system_main', 'system_main_block', [], '', 'content', 'seven', 0, '', '0');
118     $this->assertEntity('seven_user_login', 'user_login_block', [], '', 'content', 'seven', 10, '', '0');
119
120     // The d7_custom_block migration should have migrated a block containing a
121     // mildly amusing limerick. We'll need its UUID to determine
122     // bartik_block_1's plugin ID.
123     $uuid = BlockContent::load(1)->uuid();
124     $this->assertEntity('bartik_block_1', 'block_content:' . $uuid, ['authenticated'], '', 'highlighted', 'bartik', 0, 'Mildly amusing limerick of the day', 'visible');
125
126     // Assert that disabled blocks (or enabled blocks whose plugin IDs could
127     // be resolved) did not migrate.
128     $non_existent_blocks = [
129       'bartik_system_navigation',
130       'bartik_system_help',
131       'seven_user_new',
132       'seven_search_form',
133       'bartik_comment_recent',
134       'bartik_node_syndicate',
135       'bartik_node_recent',
136       'bartik_shortcut_shortcuts',
137       'bartik_system_management',
138       'bartik_system_user-menu',
139       'bartik_system_main-menu',
140       'bartik_user_new',
141       'bartik_user_online',
142       'seven_comment_recent',
143       'seven_node_syndicate',
144       'seven_shortcut_shortcuts',
145       'seven_system_powered-by',
146       'seven_system_navigation',
147       'seven_system_management',
148       'seven_system_user-menu',
149       'seven_system_main-menu',
150       'seven_user_online',
151       'bartik_blog_recent',
152       'bartik_book_navigation',
153       'bartik_locale_language',
154       'bartik_forum_active',
155       'bartik_forum_new',
156       'seven_blog_recent',
157       'seven_book_navigation',
158       'seven_locale_language',
159       'seven_forum_active',
160       'seven_forum_new',
161       'bartik_menu_menu-test-menu',
162       'bartik_statistics_popular',
163       'seven_menu_menu-test-menu',
164       'seven_statistics_popular',
165       'seven_block_1',
166     ];
167     $this->assertTrue(empty(Block::loadMultiple($non_existent_blocks)));
168   }
169
170 }