summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2017-09-28 11:00:13 (GMT)
committerNathaniel Catchpole2017-09-28 11:00:13 (GMT)
commit3d524aaae56b67e74e873b8c024a70e5ff2f60d2 (patch)
treef114a34c778daa25b9dc3b939cddd5e47773ad03
parent2d0182426a9d0ecb42cc7f69c87291b6a5d3ef00 (diff)
Issue #2711353 by rakesh.gectcr, Jo Fitzgerald, quietone, chx, Yogesh Pawar, Pavan B S, biguzis, phenaproxima, mikeryan, vasi, iMiksu, Berdir, dawehner, benjy: Migrate never unsets existing data for content entitites
-rw-r--r--core/modules/migrate/src/MigrateExecutable.php11
-rw-r--r--core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php5
-rw-r--r--core/modules/migrate/src/Row.php27
-rw-r--r--core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php62
-rw-r--r--core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php27
5 files changed, 129 insertions, 3 deletions
diff --git a/core/modules/migrate/src/MigrateExecutable.php b/core/modules/migrate/src/MigrateExecutable.php
index 94075b7..cbe5f2f 100644
--- a/core/modules/migrate/src/MigrateExecutable.php
+++ b/core/modules/migrate/src/MigrateExecutable.php
@@ -386,9 +386,14 @@ class MigrateExecutable implements MigrateExecutableInterface {
$multiple = $plugin->multiple();
}
}
- // No plugins or no value means do not set.
- if ($plugins && !is_null($value)) {
- $row->setDestinationProperty($destination, $value);
+ // Ensure all values, including nulls, are migrated.
+ if ($plugins) {
+ if (isset($value)) {
+ $row->setDestinationProperty($destination, $value);
+ }
+ else {
+ $row->setEmptyDestinationProperty($destination);
+ }
}
// Reset the value.
$value = NULL;
diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php
index d4f9bd7..cf3ae1d 100644
--- a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php
+++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php
@@ -149,6 +149,7 @@ class EntityContentBase extends Entity {
* An updated entity, or NULL if it's the same as the one passed in.
*/
protected function updateEntity(EntityInterface $entity, Row $row) {
+ $empty_destinations = $row->getEmptyDestinationProperties();
// By default, an update will be preserved.
$rollback_action = MigrateIdMapInterface::ROLLBACK_PRESERVE;
@@ -171,6 +172,7 @@ class EntityContentBase extends Entity {
// clone the row with an empty set of destination values, and re-add only
// the specified properties.
if (isset($this->configuration['overwrite_properties'])) {
+ $empty_destinations = array_intersect($empty_destinations, $this->configuration['overwrite_properties']);
$clone = $row->cloneWithoutDestination();
foreach ($this->configuration['overwrite_properties'] as $property) {
$clone->setDestinationProperty($property, $row->getDestinationProperty($property));
@@ -184,6 +186,9 @@ class EntityContentBase extends Entity {
$field->setValue($values);
}
}
+ foreach ($empty_destinations as $field_name) {
+ $entity->$field_name = NULL;
+ }
$this->setRollbackAction($row->getIdMap(), $rollback_action);
diff --git a/core/modules/migrate/src/Row.php b/core/modules/migrate/src/Row.php
index 95e7f28..83e97b1 100644
--- a/core/modules/migrate/src/Row.php
+++ b/core/modules/migrate/src/Row.php
@@ -78,6 +78,13 @@ class Row {
protected $isStub = FALSE;
/**
+ * The empty destination properties.
+ *
+ * @var array
+ */
+ protected $emptyDestinationProperties = [];
+
+ /**
* Constructs a \Drupal\Migrate\Row object.
*
* @param array $values
@@ -230,6 +237,26 @@ class Row {
}
/**
+ * Sets a destination to be empty.
+ *
+ * @param string $property
+ * The destination property.
+ */
+ public function setEmptyDestinationProperty($property) {
+ $this->emptyDestinationProperties[] = $property;
+ }
+
+ /**
+ * Gets the empty destination properties.
+ *
+ * @return array
+ * An array of destination properties.
+ */
+ public function getEmptyDestinationProperties() {
+ return $this->emptyDestinationProperties;
+ }
+
+ /**
* Returns the whole destination array.
*
* @return array
diff --git a/core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php b/core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php
index 54c60ba..e2f02ae 100644
--- a/core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php
+++ b/core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php
@@ -9,6 +9,7 @@ use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
+use Drupal\migrate_entity_test\Entity\StringIdEntityTest;
/**
* Tests the EntityContentBase destination.
@@ -203,4 +204,65 @@ class MigrateEntityContentBaseTest extends KernelTestBase {
$this->assertEquals(123456789012, $map_row['destid1']);
}
+ /**
+ * Tests empty destinations.
+ */
+ public function testEmptyDestinations() {
+ $this->enableModules(['migrate_entity_test']);
+ $this->installEntitySchema('migrate_string_id_entity_test');
+
+ $definition = [
+ 'source' => [
+ 'plugin' => 'embedded_data',
+ 'data_rows' => [
+ ['id' => 123, 'version' => 'foo'],
+ // This integer needs an 'int' schema with 'big' size. If 'destid1'
+ // is not correctly taking the definition from the destination entity
+ // type, the import will fail with an SQL exception.
+ ['id' => 123456789012, 'version' => 'bar'],
+ ],
+ 'ids' => [
+ 'id' => ['type' => 'integer', 'size' => 'big'],
+ 'version' => ['type' => 'string'],
+ ],
+ 'constants' => ['null' => NULL],
+ ],
+ 'process' => [
+ 'id' => 'id',
+ 'version' => 'version',
+ ],
+ 'destination' => [
+ 'plugin' => 'entity:migrate_string_id_entity_test',
+ ],
+ ];
+
+ $migration = \Drupal::service('plugin.manager.migration')
+ ->createStubMigration($definition);
+ $executable = new MigrateExecutable($migration);
+ $executable->import();
+
+ /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */
+ $entity = StringIdEntityTest::load('123');
+ $this->assertSame('foo', $entity->version->value);
+ $entity = StringIdEntityTest::load('123456789012');
+ $this->assertSame('bar', $entity->version->value);
+
+ // Rerun the migration forcing the version to NULL.
+ $definition['process'] = [
+ 'id' => 'id',
+ 'version' => 'constants/null',
+ ];
+
+ $migration = \Drupal::service('plugin.manager.migration')
+ ->createStubMigration($definition);
+ $executable = new MigrateExecutable($migration);
+ $executable->import();
+
+ /** @var \Drupal\migrate_entity_test\Entity\StringIdEntityTest $entity */
+ $entity = StringIdEntityTest::load('123');
+ $this->assertNull($entity->version->value);
+ $entity = StringIdEntityTest::load('123456789012');
+ $this->assertNull($entity->version->value);
+ }
+
}
diff --git a/core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php b/core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php
index 5e7cbac..f199562 100644
--- a/core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php
+++ b/core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php
@@ -440,6 +440,33 @@ class MigrateExecutableTest extends MigrateTestCase {
}
/**
+ * Tests the processRow method.
+ */
+ public function testProcessRowEmptyDestination() {
+ $expected = [
+ 'test' => 'test destination',
+ 'test1' => 'test1 destination',
+ 'test2' => NULL,
+ ];
+ $row = new Row();
+ $plugins = [];
+ foreach ($expected as $key => $value) {
+ $plugin = $this->prophesize(MigrateProcessInterface::class);
+ $plugin->getPluginDefinition()->willReturn([]);
+ $plugin->transform(NULL, $this->executable, $row, $key)->willReturn($value);
+ $plugin->multiple()->willReturn(TRUE);
+ $plugins[$key][0] = $plugin->reveal();
+ }
+ $this->migration->method('getProcessPlugins')->willReturn($plugins);
+ $this->executable->processRow($row);
+ foreach ($expected as $key => $value) {
+ $this->assertSame($value, $row->getDestinationProperty($key));
+ }
+ $this->assertCount(2, $row->getDestination());
+ $this->assertSame(['test2'], $row->getEmptyDestinationProperties());
+ }
+
+ /**
* Returns a mock migration source instance.
*
* @return \Drupal\migrate\Plugin\MigrateSourceInterface|\PHPUnit_Framework_MockObject_MockObject