Unverified Commit e8b62503 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3004929 by mikelutz, heddn: Fix 'The...

Issue #3004929 by mikelutz, heddn: Fix 'The Drupal\migrate\Plugin\migrate\process\Migration is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use Drupal\migrate\Plugin\migrate\process\MigrationLookup'

(cherry picked from commit efc4dd97)
parent ee89209b
Loading
Loading
Loading
Loading
+62 −15
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrateProcessInterface;
@@ -19,13 +20,27 @@
class BlockPluginId extends ProcessPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The migration process plugin, configured for lookups in d6_custom_block
   * and d7_custom_block.
   * The migration process plugin.
   *
   * The plugin is configured for lookups in d6_custom_block and
   * d7_custom_block.
   *
   * @var \Drupal\migrate\Plugin\MigrateProcessInterface
   *
   * @deprecated in drupal:8.8.x and is removed from drupal:9.0.0. Use
   *   the migrate.lookup service instead.
   *
   * @see https://www.drupal.org/node/3047268
   */
  protected $migrationPlugin;

  /**
   * The migrate lookup service.
   *
   * @var \Drupal\migrate\MigrateLookupInterface
   */
  protected $migrateLookup;

  /**
   * The block_content entity storage handler.
   *
@@ -34,12 +49,32 @@ class BlockPluginId extends ProcessPluginBase implements ContainerFactoryPluginI
  protected $blockContentStorage;

  /**
   * {@inheritdoc}
   * Constructs a BlockPluginId object.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The block content storage object.
   * @param \Drupal\migrate\MigrateLookupInterface $migrate_lookup
   *   The migrate lookup service.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityStorageInterface $storage, MigrateProcessInterface $migration_plugin) {
  // @codingStandardsIgnoreLine
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityStorageInterface $storage, $migrate_lookup) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    if ($migrate_lookup instanceof MigrateProcessInterface) {
      @trigger_error('Passing a migration process plugin as the fifth argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268', E_USER_DEPRECATED);
      $this->migrationPlugin = $migrate_lookup;
      $migrate_lookup = \Drupal::service('migrate.lookup');
    }
    elseif (!$migrate_lookup instanceof MigrateLookupInterface) {
      throw new \InvalidArgumentException("The fifth argument to " . __METHOD__ . " must be an instance of MigrateLookupInterface.");
    }
    $this->blockContentStorage = $storage;
    $this->migrationPlugin = $migration_plugin;
    $this->migrateLookup = $migrate_lookup;
  }

  /**
@@ -47,18 +82,12 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    $entity_type_manager = $container->get('entity_type.manager');
    $migration_configuration = [
      'migration' => [
        'd6_custom_block',
        'd7_custom_block',
      ],
    ];
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $entity_type_manager->getDefinition('block_content') ? $entity_type_manager->getStorage('block_content') : NULL,
      $container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration)
      $container->get('migrate.lookup')
    );
  }

@@ -77,17 +106,35 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
            return 'aggregator_feed_block';
          }
          break;

        case 'menu':
          return "system_menu_block:$delta";

        case 'block':
          if ($this->blockContentStorage) {
            // This BC layer is included because if the plugin constructor was
            // called in the legacy way with a migration_lookup process plugin,
            // it  may have been preconfigured with a different migration to
            // look up against. While this is unlikely, for maximum BC we will
            // continue to use the plugin to do the lookup if it is provided,
            // and support for this will be removed in Drupal 9.
            if ($this->migrationPlugin) {
              $block_id = $this->migrationPlugin
                ->transform($delta, $migrate_executable, $row, $destination_property);
            if ($block_id) {
            }
            else {
              $lookup_result = $this->migrateLookup->lookup(['d6_custom_block', 'd7_custom_block'], [$delta]);
              if ($lookup_result) {
                $block_id = $lookup_result[0]['id'];
              }
            }

            if (!empty($block_id)) {
              return 'block_content:' . $this->blockContentStorage->load($block_id)->uuid();
            }
          }
          break;

        default:
          break;
      }
+57 −13
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
@@ -27,13 +28,27 @@ class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPlugi
  protected $moduleHandler;

  /**
   * The migration process plugin, configured for lookups in the d6_user_role
   * and d7_user_role migrations.
   * The migration process plugin.
   *
   * The plugin is configured for lookups in the d6_user_role and d7_user_role
   * migrations.
   *
   * @var \Drupal\migrate\Plugin\MigrateProcessInterface
   *
   * @deprecated in drupal:8.8.x and is removed from drupal:9.0.0. Use
   *   the migrate.lookup service instead.
   *
   * @see https://www.drupal.org/node/3047268
   */
  protected $migrationPlugin;

  /**
   * The migrate lookup service.
   *
   * @var \Drupal\migrate\MigrateLookupInterface
   */
  protected $migrateLookup;

  /**
   * Whether or not to skip blocks that use PHP for visibility. Only applies
   * if the PHP module is not enabled.
@@ -43,12 +58,32 @@ class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPlugi
  protected $skipPHP = FALSE;

  /**
   * {@inheritdoc}
   * Constructs a BlockVisibility object.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\migrate\MigrateLookupInterface $migrate_lookup
   *   The migrate lookup service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, MigrateProcessInterface $migration_plugin) {
  // @codingStandardsIgnoreLine
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, $migrate_lookup) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    if ($migrate_lookup instanceof MigrateProcessInterface) {
      @trigger_error('Passing a migration process plugin as the fifth argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268', E_USER_DEPRECATED);
      $this->migrationPlugin = $migrate_lookup;
      $migrate_lookup = \Drupal::service('migrate.lookup');
    }
    elseif (!$migrate_lookup instanceof MigrateLookupInterface) {
      throw new \InvalidArgumentException("The fifth argument to " . __METHOD__ . " must be an instance of MigrateLookupInterface.");
    }
    $this->moduleHandler = $module_handler;
    $this->migrationPlugin = $migration_plugin;
    $this->migrateLookup = $migrate_lookup;

    if (isset($configuration['skip_php'])) {
      $this->skipPHP = $configuration['skip_php'];
@@ -59,18 +94,12 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    $migration_configuration = [
      'migration' => [
        'd6_user_role',
        'd7_user_role',
      ],
    ];
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('module_handler'),
      $container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration)
      $container->get('migrate.lookup')
    );
  }

@@ -94,8 +123,23 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
      ];

      foreach ($roles as $key => $role_id) {
        // This BC layer is included because if the plugin constructor was
        // called in the legacy way with a migration_lookup process plugin, it
        // may have been preconfigured with a different migration to look up
        // against. While this is unlikely, for maximum BC we will continue to
        // use the plugin to do the lookup if it is provided, and support for
        // this will be removed in Drupal 9.
        if ($this->migrationPlugin) {
          $roles[$key] = $this->migrationPlugin->transform($role_id, $migrate_executable, $row, $destination_property);
        }
        else {
          $lookup_result = $this->migrateLookup->lookup(['d6_user_role', 'd7_user_role'], [$role_id]);
          if ($lookup_result) {
            $roles[$key] = $lookup_result[0]['id'];
          }
        }

      }
      $visibility['user_role']['roles'] = array_combine($roles, $roles);
    }

+5 −5
Original line number Diff line number Diff line
@@ -4,8 +4,8 @@

use Drupal\block\Plugin\migrate\process\BlockVisibility;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;

/**
@@ -29,8 +29,8 @@ class BlockVisibilityTest extends MigrateProcessTestCase {
  protected function setUp() {
    parent::setUp();
    $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
    $migration_plugin = $this->prophesize(MigrateProcessInterface::class);
    $this->plugin = new BlockVisibility([], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migration_plugin->reveal());
    $migrate_lookup = $this->prophesize(MigrateLookupInterface::class);
    $this->plugin = new BlockVisibility([], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migrate_lookup->reveal());
  }

  /**
@@ -86,7 +86,7 @@ public function testTransformPhpDisabled() {
   */
  public function testTransformException() {
    $this->moduleHandler->moduleExists('php')->willReturn(FALSE);
    $migration_plugin = $this->prophesize(MigrateProcessInterface::class);
    $migrate_lookup = $this->prophesize(MigrateLookupInterface::class);
    $this->row = $this->getMockBuilder('Drupal\migrate\Row')
      ->disableOriginalConstructor()
      ->setMethods(['getSourceProperty'])
@@ -94,7 +94,7 @@ public function testTransformException() {
    $this->row->expects($this->exactly(2))
      ->method('getSourceProperty')
      ->willReturnMap([['bid', 99], ['module', 'foobar']]);
    $this->plugin = new BlockVisibility(['skip_php' => TRUE], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migration_plugin->reveal());
    $this->plugin = new BlockVisibility(['skip_php' => TRUE], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migrate_lookup->reveal());
    $this->expectException(MigrateSkipRowException::class);
    $this->expectExceptionMessage("The block with bid '99' from module 'foobar' will have no PHP or request_path visibility configuration.");
    $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destinationproperty');
+50 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\block\Unit\Plugin\migrate\process;

use Drupal\block\Plugin\migrate\process\BlockPluginId;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;

/**
 * Tests legacy usage of BlockPluginId.
 *
 * @group block
 * @group legacy
 *
 * @coversDefaultClass \Drupal\block\Plugin\migrate\process\BlockPluginId
 */
class LegacyBlockPluginIdTest extends MigrateProcessTestCase {

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $migrate_lookup = $this->prophesize(MigrateLookupInterface::class);
    $container = new ContainerBuilder();
    $container->set('migrate.lookup', $migrate_lookup->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * Tests legacy construction.
   *
   * @expectedDeprecation Passing a migration process plugin as the fifth argument to Drupal\block\Plugin\migrate\process\BlockPluginId::__construct is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268
   */
  public function testConstruct() {
    $process_plugin = $this->prophesize(MigrateProcessInterface::class);
    $process_plugin->transform(1, $this->migrateExecutable, $this->row, 'destination_property')->willReturn(3);
    $block = $this->prophesize(BlockContent::class);
    $block->uuid()->willReturn('123456789');
    $storage = $this->prophesize(EntityStorageInterface::class);
    $storage->load(3)->willReturn($block->reveal());
    $plugin = new BlockPluginId([], '', [], $storage->reveal(), $process_plugin->reveal());
    $this->assertSame('block_content:123456789', $plugin->transform(['block', 1], $this->migrateExecutable, $this->row, 'destination_property'));
  }

}
+135 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\block\Unit\Plugin\migrate\process;

use Drupal\block\Plugin\migrate\process\BlockVisibility;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;

/**
 * Tests the block_visibility process plugin.
 *
 * @coversDefaultClass \Drupal\block\Plugin\migrate\process\BlockVisibility
 *
 * @group block
 * @group legacy
 */
class LegacyBlockVisibilityTest extends MigrateProcessTestCase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $migrate_lookup = $this->prophesize(MigrateLookupInterface::class);
    $container = new ContainerBuilder();
    $container->set('migrate.lookup', $migrate_lookup->reveal());
    \Drupal::setContainer($container);
    $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
    $migration_plugin = $this->prophesize(MigrateProcessInterface::class);
    $this->plugin = new BlockVisibility([], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migration_plugin->reveal());
  }

  /**
   * Tests Transform.
   *
   * @covers ::transform
   *
   * @expectedDeprecation Passing a migration process plugin as the fifth argument to Drupal\block\Plugin\migrate\process\BlockVisibility::__construct is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268
   */
  public function testTransformNoData() {
    $transformed_value = $this->plugin->transform([0, '', []], $this->migrateExecutable, $this->row, 'destinationproperty');
    $this->assertEmpty($transformed_value);
  }

  /**
   * Tests Transform.
   *
   * @covers ::transform
   *
   * @expectedDeprecation Passing a migration process plugin as the fifth argument to Drupal\block\Plugin\migrate\process\BlockVisibility::__construct is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268
   */
  public function testTransformSinglePageWithFront() {
    $visibility = $this->plugin->transform([0, '<front>', []], $this->migrateExecutable, $this->row, 'destinationproperty');
    $this->assertSame('request_path', $visibility['request_path']['id']);
    $this->assertTrue($visibility['request_path']['negate']);
    $this->assertSame('<front>', $visibility['request_path']['pages']);
  }

  /**
   * Tests Transform.
   *
   * @covers ::transform
   *
   * @expectedDeprecation Passing a migration process plugin as the fifth argument to Drupal\block\Plugin\migrate\process\BlockVisibility::__construct is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268
   */
  public function testTransformMultiplePagesWithFront() {
    $visibility = $this->plugin->transform([1, "foo\n/bar\rbaz\r\n<front>", []], $this->migrateExecutable, $this->row, 'destinationproperty');
    $this->assertSame('request_path', $visibility['request_path']['id']);
    $this->assertFalse($visibility['request_path']['negate']);
    $this->assertSame("/foo\n/bar\n/baz\n<front>", $visibility['request_path']['pages']);
  }

  /**
   * Tests Transform.
   *
   * @covers ::transform
   *
   * @expectedDeprecation Passing a migration process plugin as the fifth argument to Drupal\block\Plugin\migrate\process\BlockVisibility::__construct is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268
   */
  public function testTransformPhpEnabled() {
    $this->moduleHandler->moduleExists('php')->willReturn(TRUE);
    $visibility = $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destinationproperty');
    $this->assertSame('php', $visibility['php']['id']);
    $this->assertFalse($visibility['php']['negate']);
    $this->assertSame('<?php', $visibility['php']['php']);
  }

  /**
   * Tests Transform.
   *
   * @covers ::transform
   *
   * @expectedDeprecation Passing a migration process plugin as the fifth argument to Drupal\block\Plugin\migrate\process\BlockVisibility::__construct is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268
   */
  public function testTransformPhpDisabled() {
    $this->moduleHandler->moduleExists('php')->willReturn(FALSE);
    $transformed_value = $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destinationproperty');
    $this->assertEmpty($transformed_value);
  }

  /**
   * Tests Transform.
   *
   * @covers ::transform
   *
   * @expectedDeprecation Passing a migration process plugin as the fifth argument to Drupal\block\Plugin\migrate\process\BlockVisibility::__construct is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268
   */
  public function testTransformException() {
    $this->moduleHandler->moduleExists('php')->willReturn(FALSE);
    $migration_plugin = $this->prophesize(MigrateProcessInterface::class);
    $this->row = $this->getMockBuilder('Drupal\migrate\Row')
      ->disableOriginalConstructor()
      ->setMethods(['getSourceProperty'])
      ->getMock();
    $this->row->expects($this->exactly(2))
      ->method('getSourceProperty')
      ->willReturnMap([['bid', 99], ['module', 'foobar']]);
    $this->plugin = new BlockVisibility(['skip_php' => TRUE], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migration_plugin->reveal());
    $this->expectException(MigrateSkipRowException::class);
    $this->expectExceptionMessage("The block with bid '99' from module 'foobar' will have no PHP or request_path visibility configuration.");
    $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destinationproperty');
  }

}
Loading