e0876fd1e487f9781998e64482d595b7abea527d
[yaffs-website] / web / core / modules / block / tests / src / Kernel / Migrate / d6 / MigrateBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Kernel\Migrate\d6;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
7
8 /**
9  * Tests migration of blocks to configuration entities.
10  *
11  * @group migrate_drupal_6
12  */
13 class MigrateBlockTest extends MigrateDrupal6TestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = [
19     'block',
20     'views',
21     'comment',
22     'menu_ui',
23     'block_content',
24     'taxonomy',
25     'node',
26     'aggregator',
27     'book',
28     'forum',
29     'statistics',
30   ];
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37
38     // Install the themes used for this test.
39     $this->container->get('theme_installer')->install(['bartik', 'test_theme']);
40
41     $this->installConfig(['block_content']);
42     $this->installEntitySchema('block_content');
43
44     // Set Bartik as the default public theme.
45     $config = $this->config('system.theme');
46     $config->set('default', 'bartik');
47     $config->save();
48
49     $this->executeMigrations([
50       'd6_filter_format',
51       'block_content_type',
52       'block_content_body_field',
53       'd6_custom_block',
54       'd6_user_role',
55       'd6_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 array $visibility
66    *   The block visibility settings.
67    * @param string $region
68    *   The display region.
69    * @param string $theme
70    *   The theme.
71    * @param string $weight
72    *   The block weight.
73    * @param array $settings
74    *   (optional) The block settings.
75    * @param bool $status
76    *   Whether the block is expected to be enabled or disabled.
77    */
78   public function assertEntity($id, $visibility, $region, $theme, $weight, array $settings = NULL, $status = TRUE) {
79     $block = Block::load($id);
80     $this->assertTrue($block instanceof Block);
81     $this->assertSame($visibility, $block->getVisibility());
82     $this->assertSame($region, $block->getRegion());
83     $this->assertSame($theme, $block->getTheme());
84     $this->assertSame($weight, $block->getWeight());
85     $this->assertSame($status, $block->status());
86     if ($settings) {
87       $block_settings = $block->get('settings');
88       $block_settings['id'] = current(explode(':', $block_settings['id']));
89       $this->assertEquals($settings, $block_settings);
90     }
91   }
92
93   /**
94    * Tests the block migration.
95    */
96   public function testBlockMigration() {
97     $blocks = Block::loadMultiple();
98     $this->assertCount(14, $blocks);
99
100     // Check user blocks.
101     $visibility = [
102       'request_path' => [
103         'id' => 'request_path',
104         'negate' => TRUE,
105         'pages' => "<front>\n/node/1\n/blog/*",
106       ],
107     ];
108     $settings = [
109       'id' => 'user_login_block',
110       'label' => '',
111       'provider' => 'user',
112       'label_display' => '0',
113     ];
114     $this->assertEntity('user', $visibility, 'sidebar_first', 'bartik', -10, $settings);
115
116     $visibility = [];
117     $settings = [
118       'id' => 'system_menu_block',
119       'label' => 'zu - Navigation',
120       'provider' => 'system',
121       'label_display' => 'visible',
122       'level' => 1,
123       'depth' => 0,
124     ];
125     $this->assertEntity('user_1', $visibility, 'sidebar_first', 'bartik', -11, $settings);
126
127     $visibility = [
128       'user_role' => [
129         'id' => 'user_role',
130         'roles' => [
131           'authenticated' => 'authenticated',
132         ],
133         'context_mapping' => [
134           'user' => '@user.current_user_context:current_user',
135         ],
136         'negate' => FALSE,
137       ],
138     ];
139     $settings = [
140       'id' => 'broken',
141       'label' => '',
142       'provider' => 'core',
143       'label_display' => '0',
144       'items_per_page' => '5',
145     ];
146     $this->assertEntity('user_2', $visibility, 'sidebar_second', 'bartik', -11, $settings);
147
148     $visibility = [
149       'user_role' => [
150         'id' => 'user_role',
151         'roles' => [
152           'migrate_test_role_1' => 'migrate_test_role_1',
153         ],
154         'context_mapping' => [
155           'user' => '@user.current_user_context:current_user',
156         ],
157         'negate' => FALSE,
158       ],
159     ];
160     $settings = [
161       'id' => 'broken',
162       'label' => '',
163       'provider' => 'core',
164       'label_display' => '0',
165       'items_per_page' => '10',
166     ];
167     $this->assertEntity('user_3', $visibility, 'sidebar_second', 'bartik', -10, $settings);
168
169     // Check system block.
170     $visibility = [
171       'request_path' => [
172         'id' => 'request_path',
173         'negate' => TRUE,
174         'pages' => '/node/1',
175       ],
176     ];
177     $settings = [
178       'id' => 'system_powered_by_block',
179       'label' => '',
180       'provider' => 'system',
181       'label_display' => '0',
182     ];
183     $this->assertEntity('system', $visibility, 'footer_fifth', 'bartik', -5, $settings);
184
185     // Check menu blocks.
186     $settings = [
187       'id' => 'broken',
188       'label' => '',
189       'provider' => 'core',
190       'label_display' => '0',
191     ];
192     $this->assertEntity('menu', [], 'header', 'bartik', -5, $settings);
193
194     // Check aggregator block.
195     $settings = [
196       'id' => 'aggregator_feed_block',
197       'label' => '',
198       'provider' => 'aggregator',
199       'label_display' => '0',
200       'block_count' => 7,
201       'feed' => '5',
202     ];
203     $this->assertEntity('aggregator', [], 'sidebar_second', 'bartik', -2, $settings);
204
205     // Check book block.
206     $settings = [
207       'id' => 'book_navigation',
208       'label' => '',
209       'provider' => 'book',
210       'label_display' => '0',
211       'block_mode' => 'book pages',
212     ];
213     $this->assertEntity('book', [], 'sidebar_second', 'bartik', -4, $settings);
214
215     // Check forum block settings.
216     $settings = [
217       'id' => 'forum_active_block',
218       'label' => '',
219       'provider' => 'forum',
220       'label_display' => '0',
221       'block_count' => 3,
222       'properties' => [
223         'administrative' => '1',
224       ],
225     ];
226     $this->assertEntity('forum', [], 'sidebar_first', 'bartik', -8, $settings);
227
228     $settings = [
229       'id' => 'forum_new_block',
230       'label' => '',
231       'provider' => 'forum',
232       'label_display' => '0',
233       'block_count' => 4,
234       'properties' => [
235         'administrative' => '1',
236       ],
237     ];
238     $this->assertEntity('forum_1', [], 'sidebar_first', 'bartik', -9, $settings);
239
240     // Check statistic block settings.
241     $settings = [
242       'id' => 'broken',
243       'label' => '',
244       'provider' => 'core',
245       'label_display' => '0',
246       'top_day_num' => 7,
247       'top_all_num' => 8,
248       'top_last_num' => 9,
249     ];
250     $this->assertEntity('statistics', [], 'sidebar_second', 'bartik', 0, $settings);
251
252     // Check custom blocks.
253     $visibility = [
254       'request_path' => [
255         'id' => 'request_path',
256         'negate' => FALSE,
257         'pages' => '<front>',
258       ],
259     ];
260     $settings = [
261       'id' => 'block_content',
262       'label' => 'Static Block',
263       'provider' => 'block_content',
264       'label_display' => 'visible',
265       'status' => TRUE,
266       'info' => '',
267       'view_mode' => 'full',
268     ];
269     $this->assertEntity('block', $visibility, 'content', 'bartik', 0, $settings);
270
271     $visibility = [
272       'request_path' => [
273         'id' => 'request_path',
274         'negate' => FALSE,
275         'pages' => '/node',
276       ],
277     ];
278     $settings = [
279       'id' => 'block_content',
280       'label' => 'Another Static Block',
281       'provider' => 'block_content',
282       'label_display' => 'visible',
283       'status' => TRUE,
284       'info' => '',
285       'view_mode' => 'full',
286     ];
287     // We expect this block to be disabled because '' is not a valid region,
288     // and block_rebuild() will disable any block in an invalid region.
289     $this->assertEntity('block_1', $visibility, '', 'bluemarine', -4, $settings, FALSE);
290
291     $settings = [
292       'id' => 'block_content',
293       'label' => '',
294       'provider' => 'block_content',
295       'label_display' => '0',
296       'status' => TRUE,
297       'info' => '',
298       'view_mode' => 'full',
299     ];
300     $this->assertEntity('block_2', [], 'right', 'test_theme', -7, $settings);
301
302     // Custom block with php code is not migrated.
303     $block = Block::load('block_3');
304     $this->assertFalse($block instanceof Block);
305   }
306
307 }