From 275a32813b336624686db218fcb32f6378369b8f Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 4 Oct 2022 11:08:11 +0100 Subject: [PATCH] Issue #2919158 by danflanagan8, quietone, mikelutz, maxocub, Spokje: Add the MigrationPluginManager to Drupal\migrate\Plugin\migrate\id_map\Sql --- .../migrate/src/Plugin/migrate/id_map/Sql.php | 36 ++++++++++++++----- .../Plugin/id_map/SqlDeprecationTest.php | 36 +++++++++++++++++++ .../src/Kernel/Plugin/id_map/SqlTest.php | 4 +-- .../Unit/MigrateSqlIdMapEnsureTablesTest.php | 3 +- .../tests/src/Unit/MigrateSqlIdMapTest.php | 21 ++++++++++- .../migrate/tests/src/Unit/TestSqlIdMap.php | 14 ++++++-- 6 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 core/modules/migrate/tests/src/Kernel/Plugin/id_map/SqlDeprecationTest.php diff --git a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php index 2133382fdb8b..4b76e4fbc68f 100644 --- a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php +++ b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php @@ -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') ); } @@ -997,13 +1014,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; } /** @@ -1024,13 +1045,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(); } } diff --git a/core/modules/migrate/tests/src/Kernel/Plugin/id_map/SqlDeprecationTest.php b/core/modules/migrate/tests/src/Kernel/Plugin/id_map/SqlDeprecationTest.php new file mode 100644 index 000000000000..4bdab83ec4b3 --- /dev/null +++ b/core/modules/migrate/tests/src/Kernel/Plugin/id_map/SqlDeprecationTest.php @@ -0,0 +1,36 @@ +<?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') + ); + } + +} diff --git a/core/modules/migrate/tests/src/Kernel/Plugin/id_map/SqlTest.php b/core/modules/migrate/tests/src/Kernel/Plugin/id_map/SqlTest.php index 4073829f28a0..dad421fbdc43 100644 --- a/core/modules/migrate/tests/src/Kernel/Plugin/id_map/SqlTest.php +++ b/core/modules/migrate/tests/src/Kernel/Plugin/id_map/SqlTest.php @@ -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:"); diff --git a/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapEnsureTablesTest.php b/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapEnsureTablesTest.php index d5a185fa78da..f4a4dc25960f 100644 --- a/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapEnsureTablesTest.php +++ b/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapEnsureTablesTest.php @@ -232,7 +232,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(); } diff --git a/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php b/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php index 7908fd912dce..f05ed19c782f 100644 --- a/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php +++ b/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php @@ -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(); + } + } diff --git a/core/modules/migrate/tests/src/Unit/TestSqlIdMap.php b/core/modules/migrate/tests/src/Unit/TestSqlIdMap.php index 1716a878581f..433b3907ee14 100644 --- a/core/modules/migrate/tests/src/Unit/TestSqlIdMap.php +++ b/core/modules/migrate/tests/src/Unit/TestSqlIdMap.php @@ -6,6 +6,7 @@ use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\MigrateException; use Drupal\migrate\Plugin\migrate\id_map\Sql; +use Drupal\migrate\Plugin\MigrationPluginManagerInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** @@ -28,10 +29,12 @@ class TestSqlIdMap extends Sql implements \Iterator { * The migration to do. * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher * The event dispatcher service. + * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_manager + * The migration manager. */ - public function __construct(Connection $database, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher) { + public function __construct(Connection $database, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher, MigrationPluginManagerInterface $migration_manager) { $this->database = $database; - parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $event_dispatcher); + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $event_dispatcher, $migration_manager); } /** @@ -88,4 +91,11 @@ public function ensureTables() { parent::ensureTables(); } + /** + * {@inheritdoc} + */ + public function getMigrationPluginManager() { + return parent::getMigrationPluginManager(); + } + } -- GitLab