diff -u b/core/modules/migrate/src/Audit/IdAuditor.php b/core/modules/migrate/src/Audit/IdAuditor.php --- b/core/modules/migrate/src/Audit/IdAuditor.php +++ b/core/modules/migrate/src/Audit/IdAuditor.php @@ -32,7 +32,7 @@ throw new AuditException($migration, "ID map does not implement $interface"); } - if ($destination->max() > $id_map->max()) { + if ($destination->getHighestId() > $id_map->getHighestId()) { return static::conflict($migration); } return []; @@ -67,15 +67,8 @@ $conflict = [ $base_id => $base_id, ]; + $conflict[$base_id] = (string) $migration->label(); - $label = $migration->label(); - if (is_string($label)) { - $conflict[$base_id] = $label; - } - elseif ($label instanceof TranslatableMarkup) { - $arguments = $label->getArguments(); - $conflict[$base_id] = isset($arguments['@label']) ? $arguments['@label'] : $label->render(); - } return $conflict; } diff -u b/core/modules/migrate/src/Audit/MaximumValueInterface.php b/core/modules/migrate/src/Audit/MaximumValueInterface.php --- b/core/modules/migrate/src/Audit/MaximumValueInterface.php +++ b/core/modules/migrate/src/Audit/MaximumValueInterface.php @@ -5,13 +5,13 @@ /** * Defines an interface for destination and ID maps which track a highest ID. * - * When implemented by destination plugins, max() should return the highest ID - * of the destination entity type that exists in the system. So, for example, - * the entity:node plugin should return the highest node ID that exists, - * regardless of whether it was created by a migration. + * When implemented by destination plugins, getHighestId() should return the + * highest ID of the destination entity type that exists in the system. So, for + * example, the entity:node plugin should return the highest node ID that + * exists, regardless of whether it was created by a migration. * - * When implemented by an ID map, max() should return the highest migrated ID - * of the destination entity type. + * When implemented by an ID map, getHighestId() should return the highest + * migrated ID of the destination entity type. */ interface MaximumValueInterface { @@ -22,5 +22,5 @@ * The highest ID. */ - public function max(); + public function getHighestId(); } diff -u b/core/modules/migrate/src/Plugin/Migration.php b/core/modules/migrate/src/Plugin/Migration.php --- b/core/modules/migrate/src/Plugin/Migration.php +++ b/core/modules/migrate/src/Plugin/Migration.php @@ -20,9 +20,8 @@ * Configuration options: * * - audit: If set to TRUE, the migration's IDs will be audited. This means - * that, for the entity type this migration creates, if the highest - * destination ID is greater than the highest source ID, a warning will be - * displayed that entities might be overwritten. + * that if the highest destination ID is greater than the highest source ID, + * a warning will be displayed that entities might be overwritten. */ class Migration extends PluginBase implements MigrationInterface, RequirementsInterface, ContainerFactoryPluginInterface { diff -u b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php --- b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php @@ -295,7 +295,7 @@ /** * {@inheritdoc} */ - public function max() { + public function getHighestId() { $query = $this->storage->getQuery() ->sort($this->getKey('id'), 'DESC') ->range(0, 1); diff -u b/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php --- b/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php @@ -81,7 +81,7 @@ /** * {@inheritdoc} */ - public function max() { + public function getHighestId() { $query = $this->storage->getQuery() ->sort($this->getKey('revision'), 'DESC') ->range(0, 1); diff -u b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php --- b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php +++ b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php @@ -13,6 +13,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; @@ -85,6 +86,13 @@ protected $migration; /** + * The migration plugin manager. + * + * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface + */ + protected $migration_plugin_manager; + + /** * The source ID fields. * * @var array @@ -155,12 +163,15 @@ * The migration to do. * @param \Symfony\Component\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) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->migration = $migration; $this->eventDispatcher = $event_dispatcher; $this->message = new MigrateMessage(); + $this->migration_plugin_manager = $migration_plugin_manager; } /** @@ -172,7 +183,8 @@ $plugin_id, $plugin_definition, $migration, - $container->get('event_dispatcher') + $container->get('event_dispatcher'), + $container->get('plugin.manager.migration') ); } @@ -931,11 +943,11 @@ /** * {@inheritdoc} */ - public function max() { + public function getHighestId() { $destination_ids = array_filter( $this->migration->getDestinationPlugin()->getIds(), function (array $id) { - return $id['type'] == 'integer'; + return $id['type'] === 'integer'; } ); if (empty($destination_ids)) { @@ -954,16 +966,12 @@ // 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($migration_id, 0, strpos($migration_id, static::DERIVATIVE_SEPARATOR))) { - // @TODO Inject the plugin manager as a dependency. - /** @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_manager */ - $migration_manager = \Drupal::service('plugin.manager.migration'); - - $migrations = $migration_manager->getDefinitions(); + $migrations = $this->migration_plugin_manager->getDefinitions(); foreach ($migrations as $migration_id => $migration) { - if ($migration['id'] == $base_id) { + 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->migration_plugin_manager->createInstance($migration_id); $map_tables[$migration_id] = $stub->getIdMap()->mapTableName(); } } @@ -983,7 +991,7 @@ $ids[] = $query->execute()->fetchField(); } - // Return the highest of the highest IDs. + // Return the highest of all the mapped IDs. return max($ids); } reverted: --- b/core/modules/migrate/tests/src/Kernel/MigrateTestBase.php +++ a/core/modules/migrate/tests/src/Kernel/MigrateTestBase.php @@ -7,6 +7,7 @@ use Drupal\migrate\MigrateExecutable; use Drupal\migrate\MigrateMessageInterface; use Drupal\migrate\Plugin\MigrateIdMapInterface; +use Drupal\migrate\Plugin\Migration; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Row; @@ -46,9 +47,6 @@ */ protected $sourceDatabase; - /** - * {@inheritdoc} - */ public static $modules = ['migrate']; /** diff -u b/core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php b/core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php --- b/core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php +++ b/core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php @@ -2,17 +2,10 @@ namespace Drupal\Tests\migrate_drupal\Kernel\d6; -use Drupal\aggregator\Entity\Feed; -use Drupal\aggregator\Entity\Item; -use Drupal\block_content\Entity\BlockContent; -use Drupal\comment\Entity\Comment; -use Drupal\file\Entity\File; -use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait; use Drupal\migrate\Audit\IdAuditor; use Drupal\node\Entity\Node; -use Drupal\taxonomy\Entity\Term; -use Drupal\user\Entity\User; +use Drupal\Tests\migrate_drupal\Kernel\CreateContentTrait; /** * Tests the migration auditor for ID conflicts. @@ -22,6 +15,7 @@ class MigrateDrupal6AuditIdsTest extends MigrateDrupal6TestBase { use FileSystemModuleDiscoveryDataProviderTrait; + use CreateContentTrait; /** * {@inheritdoc} @@ -125,71 +119,2 @@ - protected function createContent() { - // Create an aggregator feed. - $feed = Feed::create([ - 'title' => 'feed', - 'url' => 'http://www.example.com', - ]); - $feed->save(); - - // Create an aggregator feed item. - $item = Item::create([ - 'title' => 'feed item', - 'fid' => $feed->id(), - 'link' => 'http://www.example.com', - ]); - $item->save(); - - // Create a block content. - $block = BlockContent::create([ - 'info' => 'block', - 'type' => 'block', - ]); - $block->save(); - - // Create a node. - $node = Node::create([ - 'type' => 'page', - 'title' => 'page', - ]); - $node->save(); - - // Create a comment. - $comment = Comment::create([ - 'comment_type' => 'comment', - 'field_name' => 'comment', - 'entity_type' => 'node', - 'entity_id' => $node->id(), - ]); - $comment->save(); - - // Create a file. - $file = File::create([ - 'uri' => 'public://example.txt', - ]); - $file->save(); - - // Create a menu link. - $menu_link = MenuLinkContent::create([ - 'title' => 'menu link', - 'link' => ['uri' => 'http://www.example.com'], - 'menu_name' => 'tools', - ]); - $menu_link->save(); - - // Create a taxonomy term. - $term = Term::create([ - 'name' => 'term', - 'vid' => 'term', - ]); - $term->save(); - - // Create a user. - $user = User::create([ - 'uid' => 2, - 'name' => 'user', - 'mail' => 'user@example.com', - ]); - $user->save(); - } - } diff -u b/core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php b/core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php --- b/core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php +++ b/core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php @@ -2,17 +2,10 @@ namespace Drupal\Tests\migrate_drupal\Kernel\d7; -use Drupal\aggregator\Entity\Feed; -use Drupal\aggregator\Entity\Item; -use Drupal\block_content\Entity\BlockContent; -use Drupal\comment\Entity\Comment; -use Drupal\file\Entity\File; -use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait; use Drupal\migrate\Audit\IdAuditor; use Drupal\node\Entity\Node; -use Drupal\taxonomy\Entity\Term; -use Drupal\user\Entity\User; +use Drupal\Tests\migrate_drupal\Kernel\CreateContentTrait; /** * Tests the migration auditor for ID conflicts. @@ -22,6 +15,7 @@ class MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase { use FileSystemModuleDiscoveryDataProviderTrait; + use CreateContentTrait; /** * {@inheritdoc} @@ -124,71 +118,2 @@ - protected function createContent() { - // Create an aggregator feed. - $feed = Feed::create([ - 'title' => 'feed', - 'url' => 'http://www.example.com', - ]); - $feed->save(); - - // Create an aggregator feed item. - $item = Item::create([ - 'title' => 'feed item', - 'fid' => $feed->id(), - 'link' => 'http://www.example.com', - ]); - $item->save(); - - // Create a block content. - $block = BlockContent::create([ - 'info' => 'block', - 'type' => 'block', - ]); - $block->save(); - - // Create a node. - $node = Node::create([ - 'type' => 'page', - 'title' => 'page', - ]); - $node->save(); - - // Create a comment. - $comment = Comment::create([ - 'comment_type' => 'comment', - 'field_name' => 'comment', - 'entity_type' => 'node', - 'entity_id' => $node->id(), - ]); - $comment->save(); - - // Create a file. - $file = File::create([ - 'uri' => 'public://example.txt', - ]); - $file->save(); - - // Create a menu link. - $menu_link = MenuLinkContent::create([ - 'title' => 'menu link', - 'link' => ['uri' => 'http://www.example.com'], - 'menu_name' => 'tools', - ]); - $menu_link->save(); - - // Create a taxonomy term. - $term = Term::create([ - 'name' => 'term', - 'vid' => 'term', - ]); - $term->save(); - - // Create a user. - $user = User::create([ - 'uid' => 2, - 'name' => 'user', - 'mail' => 'user@example.com', - ]); - $user->save(); - } - } diff -u b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php --- b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php +++ b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php @@ -8,6 +8,7 @@ use Drupal\Core\Render\RendererInterface; use Drupal\Core\State\StateInterface; use Drupal\Core\Url; +use Drupal\migrate\Audit\AuditorInterface; use Drupal\migrate\Audit\IdAuditor; use Drupal\migrate\Plugin\migrate\destination\EntityContentBase; use Drupal\migrate\Plugin\MigrationPluginManagerInterface; @@ -51,6 +52,13 @@ protected $pluginManager; /** + * The auditor service. + * + * @var \Drupal\migrate\Audit\AuditorInterface + */ + protected $auditor; + + /** * Constructs the MigrateUpgradeForm. * * @param \Drupal\Core\State\StateInterface $state @@ -61,12 +69,15 @@ * The renderer service. * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $plugin_manager * The migration plugin manager. + * @param \Drupal\migrate\Audit\AuditorInterface $auditor + * The auditor service. */ - public function __construct(StateInterface $state, DateFormatterInterface $date_formatter, RendererInterface $renderer, MigrationPluginManagerInterface $plugin_manager) { + public function __construct(StateInterface $state, DateFormatterInterface $date_formatter, RendererInterface $renderer, MigrationPluginManagerInterface $plugin_manager, AuditorInterface $auditor) { $this->state = $state; $this->dateFormatter = $date_formatter; $this->renderer = $renderer; $this->pluginManager = $plugin_manager; + $this->auditor = $auditor; } /** @@ -77,7 +88,8 @@ $container->get('state'), $container->get('date.formatter'), $container->get('renderer'), - $container->get('plugin.manager.migration') + $container->get('plugin.manager.migration'), + $container->get('migrate.audit') ); } @@ -459,10 +471,9 @@ $migration_ids = array_keys($form_state->get('migrations')); $migrations = $this->pluginManager->createInstances($migration_ids); - $auditor = new IdAuditor(); $translated_content_conflicts = $content_conflicts = []; foreach ($migrations as $migration) { - $audit = $auditor->audit($migration); + $audit = $this->auditor->audit($migration); $destination_plugin = $migration->getDestinationPlugin(); if ($destination_plugin instanceof EntityContentBase && $destination_plugin->isTranslationDestination()) { diff -u b/core/modules/user/migration_templates/d7_user.yml b/core/modules/user/migration_templates/d7_user.yml --- b/core/modules/user/migration_templates/d7_user.yml +++ b/core/modules/user/migration_templates/d7_user.yml @@ -3,7 +3,7 @@ audit: true migration_tags: - Drupal 7 -class: \Drupal\user\Plugin\migrate\User +class: Drupal\user\Plugin\migrate\User source: plugin: d7_user process: diff -u b/core/modules/user/src/Plugin/migrate/destination/EntityUser.php b/core/modules/user/src/Plugin/migrate/destination/EntityUser.php --- b/core/modules/user/src/Plugin/migrate/destination/EntityUser.php +++ b/core/modules/user/src/Plugin/migrate/destination/EntityUser.php @@ -130,12 +130,12 @@ /** * {@inheritdoc} */ - public function max() { - $highest_id = parent::max(); + public function getHighestId() { + $highest_id = parent::getHighestId(); // Every Drupal site must have a user with UID of 1 and it's normal for // migrations to overwrite this user. - if ($highest_id == 1) { + if ($highest_id === 1) { return 0; } return $highest_id; only in patch2: unchanged: --- a/core/modules/migrate_drupal/migrate_drupal.services.yml +++ b/core/modules/migrate_drupal/migrate_drupal.services.yml @@ -16,3 +16,5 @@ services: - '@module_handler' - '\Drupal\migrate_drupal\Annotation\MigrateCckField' deprecated: The "%service_id%" service is deprecated. You should use the 'plugin.manager.migrate.field' service instead. See https://www.drupal.org/node/2751897 + migrate.audit: + class: Drupal\migrate\Audit\IdAuditor only in patch2: unchanged: --- /dev/null +++ b/core/modules/migrate_drupal/tests/src/Kernel/CreateContentTrait.php @@ -0,0 +1,92 @@ + 'feed', + 'url' => 'http://www.example.com', + ]); + $feed->save(); + + // Create an aggregator feed item. + $item = Item::create([ + 'title' => 'feed item', + 'fid' => $feed->id(), + 'link' => 'http://www.example.com', + ]); + $item->save(); + + // Create a block content. + $block = BlockContent::create([ + 'info' => 'block', + 'type' => 'block', + ]); + $block->save(); + + // Create a node. + $node = Node::create([ + 'type' => 'page', + 'title' => 'page', + ]); + $node->save(); + + // Create a comment. + $comment = Comment::create([ + 'comment_type' => 'comment', + 'field_name' => 'comment', + 'entity_type' => 'node', + 'entity_id' => $node->id(), + ]); + $comment->save(); + + // Create a file. + $file = File::create([ + 'uri' => 'public://example.txt', + ]); + $file->save(); + + // Create a menu link. + $menu_link = MenuLinkContent::create([ + 'title' => 'menu link', + 'link' => ['uri' => 'http://www.example.com'], + 'menu_name' => 'tools', + ]); + $menu_link->save(); + + // Create a taxonomy term. + $term = Term::create([ + 'name' => 'term', + 'vid' => 'term', + ]); + $term->save(); + + // Create a user. + $user = User::create([ + 'uid' => 2, + 'name' => 'user', + 'mail' => 'user@example.com', + ]); + $user->save(); + } + +}