Commit 1a1b6b51 authored by alexpott's avatar alexpott

Issue #2569703 by phenaproxima, benjy, webchick: When migrating to the same...

Issue #2569703 by phenaproxima, benjy, webchick: When migrating to the same theme with different region names, block will not migrate properly
parent 3dbe7ed6
...@@ -7,30 +7,68 @@ ...@@ -7,30 +7,68 @@
namespace Drupal\block\Plugin\migrate\process; namespace Drupal\block\Plugin\migrate\process;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row; use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* @MigrateProcessPlugin( * @MigrateProcessPlugin(
* id = "block_region" * id = "block_region"
* ) * )
*/ */
class BlockRegion extends ProcessPluginBase { class BlockRegion extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/** /**
* {@inheritdoc} * List of regions, keyed by theme.
*
* @var array[]
*/
protected $regions;
/**
* Constructs a BlockRegion plugin instance.
* *
* Set the destination block region, based on the source region and theme as * @param array $configuration
* well as the current destination default theme. * The plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param array $regions
* Array of region maps, keyed by theme.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $regions) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->regions = $regions;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$regions = array();
foreach ($container->get('theme_handler')->listInfo() as $key => $theme) {
$regions[$key] = $theme->info['regions'];
}
return new static($configuration, $plugin_id, $plugin_definition, $regions);
}
/**
* {@inheritdoc}
*/ */
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
// Set the destination region, based on the source region and theme as well
// as the current destination default theme.
list($region, $source_theme, $destination_theme) = $value; list($region, $source_theme, $destination_theme) = $value;
// Theme is the same on both source and destination, we will assume they // Theme is the same on both source and destination, so ensure that the
// have the same regions. // region exists in the destination theme.
if (strtolower($source_theme) == strtolower($destination_theme)) { if (strtolower($source_theme) == strtolower($destination_theme)) {
return $region; if (isset($this->regions[$destination_theme][$region])) {
return $region;
}
} }
// If the source and destination theme are different, try to use the // If the source and destination theme are different, try to use the
......
...@@ -154,7 +154,7 @@ public function testBlockMigration() { ...@@ -154,7 +154,7 @@ public function testBlockMigration() {
$visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['id'] = 'request_path';
$visibility['request_path']['negate'] = FALSE; $visibility['request_path']['negate'] = FALSE;
$visibility['request_path']['pages'] = '/node'; $visibility['request_path']['pages'] = '/node';
$this->assertEntity('block_1', $visibility, 'right', 'bluemarine', -4); $this->assertEntity('block_1', $visibility, 'sidebar_second', 'bluemarine', -4);
$visibility = []; $visibility = [];
$this->assertEntity('block_2', $visibility, 'right', 'test_theme', -7); $this->assertEntity('block_2', $visibility, 'right', 'test_theme', -7);
......
...@@ -23,7 +23,7 @@ class MigrateBlockTest extends MigrateDrupal7TestBase { ...@@ -23,7 +23,7 @@ class MigrateBlockTest extends MigrateDrupal7TestBase {
* *
* @var array * @var array
*/ */
static $modules = array( static $modules = [
'block', 'block',
'views', 'views',
'comment', 'comment',
...@@ -33,7 +33,7 @@ class MigrateBlockTest extends MigrateDrupal7TestBase { ...@@ -33,7 +33,7 @@ class MigrateBlockTest extends MigrateDrupal7TestBase {
'text', 'text',
'filter', 'filter',
'user', 'user',
); ];
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -50,7 +50,7 @@ protected function setUp() { ...@@ -50,7 +50,7 @@ protected function setUp() {
$config->save(); $config->save();
// Install one of D8's test themes. // Install one of D8's test themes.
\Drupal::service('theme_handler')->install(array('test_theme')); \Drupal::service('theme_handler')->install(['bartik']);
$this->executeMigration('d7_filter_format'); $this->executeMigration('d7_filter_format');
$this->executeMigration('d7_user_role'); $this->executeMigration('d7_user_role');
...@@ -117,7 +117,7 @@ public function testBlockMigration() { ...@@ -117,7 +117,7 @@ public function testBlockMigration() {
// Assert that disabled blocks (or enabled blocks whose plugin IDs could // Assert that disabled blocks (or enabled blocks whose plugin IDs could
// be resolved) did not migrate. // be resolved) did not migrate.
$non_existent_blocks = array( $non_existent_blocks = [
'bartik_system_navigation', 'bartik_system_navigation',
'bartik_system_help', 'bartik_system_help',
'seven_user_new', 'seven_user_new',
...@@ -155,7 +155,7 @@ public function testBlockMigration() { ...@@ -155,7 +155,7 @@ public function testBlockMigration() {
'seven_menu_menu-test-menu', 'seven_menu_menu-test-menu',
'seven_statistics_popular', 'seven_statistics_popular',
'seven_block_1', 'seven_block_1',
); ];
$this->assertTrue(empty(Block::loadMultiple($non_existent_blocks))); $this->assertTrue(empty(Block::loadMultiple($non_existent_blocks)));
} }
......
<?php
/**
* @file
* Contains \Drupal\Tests\block\Unit\Plugin\migrate\process\BlockRegionTest.
*/
namespace Drupal\Tests\block\Unit\Plugin\migrate\process;
use Drupal\block\Plugin\migrate\process\BlockRegion;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\block\Plugin\migrate\process\BlockRegion
* @group block
*/
class BlockRegionTest extends UnitTestCase {
/**
* Transforms a value through the block_region plugin.
*
* @param array $value
* The value to transform.
* @param \Drupal\migrate\Row|NULL $row
* (optional) The mocked row.
*
* @return array|string
* The transformed value.
*/
protected function transform(array $value, Row $row = NULL) {
$executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();
if (empty($row)) {
$row = $this->prophesize(Row::class)->reveal();
}
$regions = array(
'bartik' => array(
'triptych_first' => 'Triptych first',
'triptych_second' => 'Triptych second',
'triptych_third' => 'Triptych third',
),
);
$plugin = new BlockRegion(['region_map' => []], 'block_region', [], $regions);
return $plugin->transform($value, $executable, $row, 'foo');
}
/**
* If the source and destination themes are identical, the region should only
* be passed through if it actually exists in the destination theme.
*
* @covers ::transform
*/
public function testTransformSameThemeRegionExists() {
$this->assertSame('triptych_second', $this->transform(['triptych_second', 'bartik', 'bartik']));
}
/**
* If the source and destination themes are identical, the region should be
* changed to 'content' if it doesn't exist in the destination theme.
*
* @covers ::transform
*/
public function testTransformSameThemeRegionNotExists() {
$this->assertSame('content', $this->transform(['footer', 'bartik', 'bartik']));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment