MigrateBlockTest.php 5.49 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\block\Kernel\Migrate\d7;
4 5 6

use Drupal\block\Entity\Block;
use Drupal\block_content\Entity\BlockContent;
7
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
8 9 10 11 12 13 14 15

/**
 * Tests migration of blocks to configuration entities.
 *
 * @group block
 */
class MigrateBlockTest extends MigrateDrupal7TestBase {

16
  /**
17
   * {@inheritdoc}
18
   */
19
  public static $modules = [
20 21 22 23 24 25 26 27
    'block',
    'views',
    'comment',
    'menu_ui',
    'block_content',
    'node',
    'text',
    'filter',
28
    'path_alias',
29
    'user',
30
  ];
31 32 33 34 35 36

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
37 38 39 40

    // Install the themes used for this test.
    $this->container->get('theme_installer')->install(['bartik', 'seven']);

41
    $this->installEntitySchema('block_content');
42
    $this->installConfig(static::$modules);
43 44 45 46 47 48 49

    // Set Bartik and Seven as the default public and admin theme.
    $config = $this->config('system.theme');
    $config->set('default', 'bartik');
    $config->set('admin', 'seven');
    $config->save();

50 51 52 53 54 55 56 57
    $this->executeMigrations([
      'd7_filter_format',
      'd7_user_role',
      'block_content_type',
      'block_content_body_field',
      'd7_custom_block',
      'd7_block',
    ]);
58
    block_rebuild();
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  }

  /**
   * Asserts various aspects of a block.
   *
   * @param string $id
   *   The block ID.
   * @param string $plugin_id
   *   The block's plugin ID.
   * @param array $roles
   *   Role IDs the block is expected to have.
   * @param string $pages
   *   The list of pages on which the block should appear.
   * @param string $region
   *   The display region.
   * @param string $theme
   *   The theme.
   * @param string $weight
   *   The block weight.
78 79 80 81
   * @param string $label
   *   The block label.
   * @param string $label_display
   *   The block label display setting.
82 83
   * @param bool $status
   *   Whether the block is expected to be enabled or disabled.
84
   */
85
  public function assertEntity($id, $plugin_id, array $roles, $pages, $region, $theme, $weight, $label, $label_display, $status = TRUE) {
86
    $block = Block::load($id);
87
    $this->assertInstanceOf(Block::class, $block);
88
    /** @var \Drupal\block\BlockInterface $block */
89
    $this->assertSame($plugin_id, $block->getPluginId());
90 91 92

    $visibility = $block->getVisibility();
    if ($roles) {
93 94
      $this->assertSame($roles, array_values($visibility['user_role']['roles']));
      $this->assertSame('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
95 96
    }
    if ($pages) {
97
      $this->assertSame($pages, $visibility['request_path']['pages']);
98 99
    }

100 101 102 103
    $this->assertSame($region, $block->getRegion());
    $this->assertSame($theme, $block->getTheme());
    $this->assertSame($weight, $block->getWeight());
    $this->assertSame($status, $block->status());
104 105

    $config = $this->config('block.block.' . $id);
106 107
    $this->assertSame($label, $config->get('settings.label'));
    $this->assertSame($label_display, $config->get('settings.label_display'));
108 109 110 111 112 113
  }

  /**
   * Tests the block migration.
   */
  public function testBlockMigration() {
114 115
    $this->assertEntity('bartik_system_main', 'system_main_block', [], '', 'content', 'bartik', 0, '', '0');
    $this->assertEntity('bartik_search_form', 'search_form_block', [], '', 'sidebar_first', 'bartik', -1, '', '0');
116
    $this->assertEntity('bartik_user_login', 'user_login_block', [], '', 'sidebar_first', 'bartik', 0, 'User login title', 'visible');
117
    $this->assertEntity('bartik_system_powered_by', 'system_powered_by_block', [], '', 'footer_fifth', 'bartik', 10, '', '0');
118
    $this->assertEntity('seven_system_main', 'system_main_block', [], '', 'content', 'seven', 0, '', '0');
119
    $this->assertEntity('seven_user_login', 'user_login_block', [], '', 'content', 'seven', 10, 'User login title', 'visible');
120 121 122 123 124

    // The d7_custom_block migration should have migrated a block containing a
    // mildly amusing limerick. We'll need its UUID to determine
    // bartik_block_1's plugin ID.
    $uuid = BlockContent::load(1)->uuid();
125
    $this->assertEntity('bartik_block_1', 'block_content:' . $uuid, ['authenticated'], '', 'highlighted', 'bartik', 0, 'Mildly amusing limerick of the day', 'visible');
126 127 128

    // Assert that disabled blocks (or enabled blocks whose plugin IDs could
    // be resolved) did not migrate.
129
    $non_existent_blocks = [
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
      'bartik_system_navigation',
      'bartik_system_help',
      'seven_user_new',
      'seven_search_form',
      'bartik_comment_recent',
      'bartik_node_syndicate',
      'bartik_node_recent',
      'bartik_shortcut_shortcuts',
      'bartik_system_management',
      'bartik_system_user-menu',
      'bartik_system_main-menu',
      'bartik_user_new',
      'bartik_user_online',
      'seven_comment_recent',
      'seven_node_syndicate',
      'seven_shortcut_shortcuts',
      'seven_system_powered-by',
      'seven_system_navigation',
      'seven_system_management',
      'seven_system_user-menu',
      'seven_system_main-menu',
      'seven_user_online',
      'bartik_blog_recent',
      'bartik_book_navigation',
      'bartik_locale_language',
      'bartik_forum_active',
      'bartik_forum_new',
      'seven_blog_recent',
      'seven_book_navigation',
      'seven_locale_language',
      'seven_forum_active',
      'seven_forum_new',
      'bartik_menu_menu-test-menu',
      'bartik_statistics_popular',
      'seven_menu_menu-test-menu',
      'seven_statistics_popular',
      'seven_block_1',
167
    ];
168 169 170 171
    $this->assertTrue(empty(Block::loadMultiple($non_existent_blocks)));
  }

}