3 namespace Drupal\Tests\block\Kernel\Migrate\d7;
5 use Drupal\block\Entity\Block;
6 use Drupal\block_content\Entity\BlockContent;
7 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
10 * Tests migration of blocks to configuration entities.
14 class MigrateBlockTest extends MigrateDrupal7TestBase {
19 public static $modules = [
34 protected function setUp() {
37 // Install the themes used for this test.
38 $this->container->get('theme_installer')->install(['bartik', 'seven']);
40 $this->installConfig(static::$modules);
41 $this->installEntitySchema('block_content');
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');
49 $this->executeMigrations([
53 'block_content_body_field',
61 * Asserts various aspects of a block.
65 * @param string $plugin_id
66 * The block's plugin ID.
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
73 * @param string $theme
75 * @param string $weight
77 * @param string $label
79 * @param string $label_display
80 * The block label display setting.
82 * Whether the block is expected to be enabled or disabled.
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());
90 $visibility = $block->getVisibility();
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']);
96 $this->assertSame($pages, $visibility['request_path']['pages']);
99 $this->assertSame($region, $block->getRegion());
100 $this->assertSame($theme, $block->getTheme());
101 $this->assertSame($weight, $block->getWeight());
102 $this->assertSame($status, $block->status());
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'));
110 * Tests the block migration.
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, 'User login title', 'visible');
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, 'User login title', 'visible');
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');
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',
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',
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',
151 'bartik_blog_recent',
152 'bartik_book_navigation',
153 'bartik_locale_language',
154 'bartik_forum_active',
157 'seven_book_navigation',
158 'seven_locale_language',
159 'seven_forum_active',
161 'bartik_menu_menu-test-menu',
162 'bartik_statistics_popular',
163 'seven_menu_menu-test-menu',
164 'seven_statistics_popular',
167 $this->assertTrue(empty(Block::loadMultiple($non_existent_blocks)));