summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2016-11-03 11:54:42 +0000
committerNathaniel Catchpole2016-11-03 11:54:42 +0000
commit5112179badce4924f37e6d637c035b3e0f166602 (patch)
tree65095b8f6d08ed8bf8e6b75335f9a901d79162fa
parent5a88934ee9e309e13b6d29d4a0b58b8bddf0c913 (diff)
Issue #2690001 by quietone, vasi, mikeryan, infopicard, phenaproxima: Migrate: Don't deduplicate forum vocabulary
-rw-r--r--core/modules/migrate/src/Plugin/migrate/process/DedupeEntity.php32
-rw-r--r--core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php44
-rw-r--r--core/modules/migrate_drupal_ui/src/Tests/d6/MigrateUpgrade6Test.php2
-rw-r--r--core/modules/taxonomy/migration_templates/d6_taxonomy_vocabulary.yml1
4 files changed, 74 insertions, 5 deletions
diff --git a/core/modules/migrate/src/Plugin/migrate/process/DedupeEntity.php b/core/modules/migrate/src/Plugin/migrate/process/DedupeEntity.php
index d2afe8f..a616c86 100644
--- a/core/modules/migrate/src/Plugin/migrate/process/DedupeEntity.php
+++ b/core/modules/migrate/src/Plugin/migrate/process/DedupeEntity.php
@@ -10,6 +10,9 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Ensures value is not duplicated against an entity field.
*
+ * If the 'migrated' configuration value is true, an entity will only be
+ * considered a duplicate if it was migrated by the current migration.
+ *
* @link https://www.drupal.org/node/2135325 Online handbook documentation for dedupe_entity process plugin @endlink
*
* @MigrateProcessPlugin(
@@ -26,10 +29,18 @@ class DedupeEntity extends DedupeBase implements ContainerFactoryPluginInterface
protected $entityQueryFactory;
/**
+ * The current migration.
+ *
+ * @var \Drupal\migrate\Plugin\MigrationInterface
+ */
+ protected $migration;
+
+ /**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, QueryFactory $entity_query_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
+ $this->migration = $migration;
$this->entityQueryFactory = $entity_query_factory;
}
@@ -51,12 +62,25 @@ class DedupeEntity extends DedupeBase implements ContainerFactoryPluginInterface
*/
protected function exists($value) {
// Plugins are cached so for every run we need a new query object.
- return $this
+ $query = $this
->entityQueryFactory
->get($this->configuration['entity_type'], 'AND')
- ->condition($this->configuration['field'], $value)
- ->count()
- ->execute();
+ ->condition($this->configuration['field'], $value);
+ if (!empty($this->configuration['migrated'])) {
+ // Check if each entity is in the ID map.
+ $idMap = $this->migration->getIdMap();
+ foreach ($query->execute() as $id) {
+ $dest_id_values[$this->configuration['field']] = $id;
+ if ($idMap->lookupSourceID($dest_id_values)) {
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+ else {
+ // Just check if any such entity exists.
+ return $query->count()->execute();
+ }
}
}
diff --git a/core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php b/core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php
index a1960de..4fe8db3 100644
--- a/core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php
+++ b/core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php
@@ -2,6 +2,7 @@
namespace Drupal\Tests\migrate\Unit\process;
+use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
use Drupal\Component\Utility\Unicode;
@@ -161,4 +162,47 @@ class DedupeEntityTest extends MigrateProcessTestCase {
->will($this->returnCallback(function () use (&$count) { return $count--;}));
}
+ /**
+ * Test deduplicating only migrated entities.
+ */
+ public function testDedupeMigrated() {
+ $configuration = array(
+ 'entity_type' => 'test_entity_type',
+ 'field' => 'test_field',
+ 'migrated' => TRUE,
+ );
+ $plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this->getMigration(), $this->entityQueryFactory);
+
+ // Setup the entityQuery used in DedupeEntity::exists. The map, $map, is
+ // an array consisting of the four input parameters to the query condition
+ // method and then the query to return. Both 'forum' and
+ // 'test_vocab' are existing entities. There is no 'test_vocab1'.
+ $map = [];
+ foreach (['forums', 'test_vocab', 'test_vocab1'] as $id) {
+ $query = $this->prophesize(QueryInterface::class);
+ $query->willBeConstructedWith([]);
+ $query->execute()->willReturn($id === 'test_vocab1' ? [] : [$id]);
+ $map[] = ['test_field', $id, NULL, NULL, $query->reveal()];
+ }
+ $this->entityQuery
+ ->method('condition')
+ ->will($this->returnValueMap($map));
+
+ // Entity 'forums' is pre-existing, entity 'test_vocab' was migrated.
+ $this->idMap
+ ->method('lookupSourceID')
+ ->will($this->returnValueMap([
+ [['test_field' => 'forums'], FALSE],
+ [['test_field' => 'test_vocab'], ['source_id' => 42]],
+ ]));
+
+ // Existing entity 'forums' was not migrated, it should not be deduplicated.
+ $actual = $plugin->transform('forums', $this->migrateExecutable, $this->row, 'testproperty');
+ $this->assertEquals('forums', $actual, 'Pre-existing name is re-used');
+
+ // Entity 'test_vocab' was migrated, should be deduplicated.
+ $actual = $plugin->transform('test_vocab', $this->migrateExecutable, $this->row, 'testproperty');
+ $this->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');
+ }
+
}
diff --git a/core/modules/migrate_drupal_ui/src/Tests/d6/MigrateUpgrade6Test.php b/core/modules/migrate_drupal_ui/src/Tests/d6/MigrateUpgrade6Test.php
index 2a806de..3b53992 100644
--- a/core/modules/migrate_drupal_ui/src/Tests/d6/MigrateUpgrade6Test.php
+++ b/core/modules/migrate_drupal_ui/src/Tests/d6/MigrateUpgrade6Test.php
@@ -58,7 +58,7 @@ class MigrateUpgrade6Test extends MigrateUpgradeTestBase {
'action' => 22,
'menu' => 8,
'taxonomy_term' => 6,
- 'taxonomy_vocabulary' => 6,
+ 'taxonomy_vocabulary' => 5,
'tour' => 4,
'user' => 7,
'user_role' => 6,
diff --git a/core/modules/taxonomy/migration_templates/d6_taxonomy_vocabulary.yml b/core/modules/taxonomy/migration_templates/d6_taxonomy_vocabulary.yml
index 654a076..f7f5884 100644
--- a/core/modules/taxonomy/migration_templates/d6_taxonomy_vocabulary.yml
+++ b/core/modules/taxonomy/migration_templates/d6_taxonomy_vocabulary.yml
@@ -14,6 +14,7 @@ process:
entity_type: taxonomy_vocabulary
field: vid
length: 32
+ migrated: true
label: name
name: name
description: description