Verified Commit abea4c7b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2919158 by danflanagan8, quietone, mikelutz, maxocub, Spokje: Add the...

Issue #2919158 by danflanagan8, quietone, mikelutz, maxocub, Spokje: Add the MigrationPluginManager to Drupal\migrate\Plugin\migrate\id_map\Sql

(cherry picked from commit 275a3281)
parent 53fdb351
Loading
Loading
Loading
Loading
+28 −8
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateMessageInterface;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\Row;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigrateMapSaveEvent;
@@ -143,6 +144,13 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
   */
  protected $currentKey = [];

  /**
   * The migration plugin manager.
   *
   * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
   */
  protected $migrationPluginManager;

  /**
   * Constructs an SQL object.
   *
@@ -158,8 +166,10 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
   *   The migration to do.
   * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
   *   The migration plugin manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher) {
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher, MigrationPluginManagerInterface $migration_plugin_manager = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->migration = $migration;
    $this->eventDispatcher = $event_dispatcher;
@@ -176,6 +186,12 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
    $this->mapTableName = mb_substr($this->mapTableName, 0, 63 - $prefix_length);
    $this->messageTableName = 'migrate_message_' . mb_strtolower($machine_name);
    $this->messageTableName = mb_substr($this->messageTableName, 0, 63 - $prefix_length);

    if (!$migration_plugin_manager) {
      @trigger_error('Calling Sql::__construct() without the $migration_manager argument is deprecated in drupal:9.5.0 and the $migration_manager argument will be required in drupal:11.0.0. See https://www.drupal.org/node/3277306', E_USER_DEPRECATED);
      $migration_plugin_manager = \Drupal::service('plugin.manager.migration');
    }
    $this->migrationPluginManager = $migration_plugin_manager;
  }

  /**
@@ -187,7 +203,8 @@ public static function create(ContainerInterface $container, array $configuratio
      $plugin_id,
      $plugin_definition,
      $migration,
      $container->get('event_dispatcher')
      $container->get('event_dispatcher'),
      $container->get('plugin.manager.migration')
    );
  }

@@ -996,13 +1013,17 @@ public function valid() {
  /**
   * Returns the migration plugin manager.
   *
   * @todo Inject as a dependency in https://www.drupal.org/node/2919158.
   *
   * @return \Drupal\migrate\Plugin\MigrationPluginManagerInterface
   *   The migration plugin manager.
   *
   * @deprecated in drupal:9.5.0 and is removed from drupal:11.0.0. Use
   *   $this->migrationPluginManager instead.
   *
   * @see https://www.drupal.org/node/3277306
   */
  protected function getMigrationPluginManager() {
    return \Drupal::service('plugin.manager.migration');
    @trigger_error('deprecated in drupal:9.5.0 and is removed from drupal:11.0.0. Use $this->migrationPluginManager instead. See https://www.drupal.org/node/3277306', E_USER_DEPRECATED);
    return $this->migrationPluginManager;
  }

  /**
@@ -1023,13 +1044,12 @@ public function getHighestId() {
    // If there's a bundle, it means we have a derived migration and we need to
    // find all the mapping tables from the related derived migrations.
    if ($base_id = substr($this->migration->id(), 0, strpos($this->migration->id(), $this::DERIVATIVE_SEPARATOR))) {
      $migration_manager = $this->getMigrationPluginManager();
      $migrations = $migration_manager->getDefinitions();
      $migrations = $this->migrationPluginManager->getDefinitions();
      foreach ($migrations as $migration_id => $migration) {
        if ($migration['id'] === $base_id) {
          // Get this derived migration's mapping table and add it to the list
          // of mapping tables to look in for the highest ID.
          $stub = $migration_manager->createInstance($migration_id);
          $stub = $this->migrationPluginManager->createInstance($migration_id);
          $map_tables[$migration_id] = $stub->getIdMap()->mapTableName();
        }
      }
+36 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\migrate\Kernel\Plugin\id_map;

use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\Plugin\migrate\id_map\Sql;

/**
 * Tests deprecation notice in Sql constructor.
 *
 * @group migrate
 * @group legacy
 */
class SqlDeprecationTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['migrate'];

  /**
   * @covers ::__construct
   */
  public function testOptionalParametersDeprecation(): void {
    $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal();
    $this->expectDeprecation('Calling Sql::__construct() without the $migration_manager argument is deprecated in drupal:9.5.0 and the $migration_manager argument will be required in drupal:11.0.0. See https://www.drupal.org/node/3277306');
    new Sql(
      [],
      'sql',
      [],
      $migration,
      $this->container->get('event_dispatcher')
    );
  }

}
+2 −2
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ public function testEnsureTables($ids) {
    $this->migrationDefinition['source']['ids'] = $ids;
    $migration = $this->migrationPluginManager->createStubMigration($this->migrationDefinition);

    $map = new TestSqlIdMap($this->database, [], 'test', [], $migration, $this->eventDispatcher);
    $map = new TestSqlIdMap($this->database, [], 'test', [], $migration, $this->eventDispatcher, $this->migrationPluginManager);
    $map->ensureTables();

    // Checks that the map table was created.
@@ -149,7 +149,7 @@ public function testFailEnsureTables($ids) {
      ->createStubMigration($this->migrationDefinition);

    // Use local id map plugin to force an error.
    $map = new SqlIdMapTest($this->database, [], 'test', [], $migration, $this->eventDispatcher);
    $map = new SqlIdMapTest($this->database, [], 'test', [], $migration, $this->eventDispatcher, $this->migrationPluginManager);

    $this->expectException(DatabaseExceptionWrapper::class);
    $this->expectExceptionMessage("Syntax error or access violation: 1074 Column length too big for column 'sourceid1' (max = 16383); use BLOB or TEXT instead:");
+2 −1
Original line number Diff line number Diff line
@@ -231,7 +231,8 @@ protected function runEnsureTablesTest($schema) {
      ->willReturn($plugin);
    /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher */
    $event_dispatcher = $this->createMock('Symfony\Contracts\EventDispatcher\EventDispatcherInterface');
    $map = new TestSqlIdMap($database, [], 'sql', [], $migration, $event_dispatcher);
    $migration_manager = $this->createMock('Drupal\migrate\Plugin\MigrationPluginManagerInterface');
    $map = new TestSqlIdMap($database, [], 'sql', [], $migration, $event_dispatcher, $migration_manager);
    $map->getDatabase();
  }

+20 −1
Original line number Diff line number Diff line
@@ -2,7 +2,9 @@

namespace Drupal\Tests\migrate\Unit;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\sqlite\Driver\Database\sqlite\Connection;
use Drupal\migrate\Plugin\MigrationPluginManager;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
@@ -111,8 +113,9 @@ protected function getIdMap() {
      ->method('getDestinationPlugin')
      ->willReturn($plugin);
    $event_dispatcher = $this->createMock('Symfony\Contracts\EventDispatcher\EventDispatcherInterface');
    $migration_manager = $this->createMock('Drupal\migrate\Plugin\MigrationPluginManagerInterface');

    $id_map = new TestSqlIdMap($this->database, [], 'sql', [], $migration, $event_dispatcher);
    $id_map = new TestSqlIdMap($this->database, [], 'sql', [], $migration, $event_dispatcher, $migration_manager);
    $migration
      ->method('getIdMap')
      ->willReturn($id_map);
@@ -1176,4 +1179,20 @@ public function getHighestIdInvalidDataProvider() {
    ];
  }

  /**
   * Tests deprecation message from Sql::getMigrationPluginManager().
   *
   * @group legacy
   */
  public function testGetMigrationPluginManagerDeprecation() {
    $container = new ContainerBuilder();
    $migration_plugin_manager = $this->createMock(MigrationPluginManager::class);
    $container->set('plugin.manager.migration', $migration_plugin_manager);
    \Drupal::setContainer($container);

    $this->expectDeprecation('deprecated in drupal:9.5.0 and is removed from drupal:11.0.0. Use $this->migrationPluginManager instead. See https://www.drupal.org/node/3277306');
    $id_map = $this->getIdMap();
    $id_map->getMigrationPluginManager();
  }

}
Loading