diff --git a/core/modules/comment/src/Tests/Migrate/d7/MigrateCommentTest.php b/core/modules/comment/src/Tests/Migrate/d7/MigrateCommentTest.php index 6c7277e..8d6e668 100644 --- a/core/modules/comment/src/Tests/Migrate/d7/MigrateCommentTest.php +++ b/core/modules/comment/src/Tests/Migrate/d7/MigrateCommentTest.php @@ -11,7 +11,6 @@ use Drupal\comment\Entity\Comment; use Drupal\migrate_drupal\Tests\d7\MigrateDrupal7TestBase; use Drupal\node\NodeInterface; -use Drupal\user\Entity\User; /** * Tests migration of comments from Drupal 7. @@ -37,12 +36,6 @@ protected function setUp() { 'd7_user_role', 'd7_user', ]); - // The test database doesn't include uid 1, so we'll need to create it. - User::create(array( - 'uid' => 1, - 'name' => 'admin', - 'mail' => 'admin@local.host', - ))->save(); $this->executeMigration('d7_node_type'); // We only need the test_content_type node migration to run for real, so // mock all the others. diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php index 69c9e66..2bd3b1c 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php @@ -109,6 +109,17 @@ public function getIds() { * The row object to update from. */ protected function updateEntity(EntityInterface $entity, Row $row) { + $clear = array(); + // Clear all properties except those listed in overwrite_properties. + if (isset($this->configuration['overwrite_properties'])) { + $clear = array_diff(array_keys($row->getDestination()), $this->configuration['overwrite_properties']); + } + // Clear all properties listed in preserve_properties. + elseif (isset($this->configuration['preserve_properties'])) { + $clear = $this->configuration['preserve_properties']; + } + array_walk($clear, [$row, 'clearDestinationProperty']); + foreach ($row->getDestination() as $field_name => $values) { $field = $entity->$field_name; if ($field instanceof TypedDataInterface) { diff --git a/core/modules/migrate/src/Row.php b/core/modules/migrate/src/Row.php index 8774976..f02cf8d 100644 --- a/core/modules/migrate/src/Row.php +++ b/core/modules/migrate/src/Row.php @@ -253,6 +253,19 @@ public function getDestinationProperty($property) { } /** + * Unsets a destination property. + * + * @param string $property + * The name of a destination property. + * + * @return $this + */ + public function clearDestinationProperty($property) { + NestedArray::unsetValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property)); + return $this; + } + + /** * Sets the Migrate ID mappings. * * @param array $id_map diff --git a/core/modules/migrate_drupal/src/Tests/d6/EntityContentBaseTest.php b/core/modules/migrate_drupal/src/Tests/d6/EntityContentBaseTest.php new file mode 100644 index 0000000..3f0a438 --- /dev/null +++ b/core/modules/migrate_drupal/src/Tests/d6/EntityContentBaseTest.php @@ -0,0 +1,79 @@ + 2, + 'name' => 'Michael Palin', + 'mail' => 'mike@monty.py', + ])->save(); + + $this->executeMigrations(['d6_filter_format', 'd6_user_role']); + } + + /** + * Tests overwriting all properties in the destination entity (default + * behavior). + */ + public function testOverwriteAllProperties() { + $this->executeMigration('d6_user'); + /** @var \Drupal\user\UserInterface $account */ + $account = User::load(2); + $this->assertIdentical('john.doe', $account->label()); + $this->assertIdentical('john.doe@example.com', $account->getEmail()); + } + + /** + * Tests overwriting selected properties in the destination entity, specified + * in the destination configuration. + */ + public function testOverwriteSelectedProperties() { + $migration = Migration::load('d6_user'); + $destination = $migration->get('destination'); + $destination['overwrite_properties'][] = 'mail'; + $migration->set('destination', $destination); + $this->executeMigration($migration); + + /** @var \Drupal\user\UserInterface $account */ + $account = User::load(2); + $this->assertIdentical('Michael Palin', $account->label()); + $this->assertIdentical('john.doe@example.com', $account->getEmail()); + } + + /** + * Tests preserving selected properties in the destination entity, specified + * in the destination configuration. + */ + public function testPreserveSelectedProperties() { + $migration = Migration::load('d6_user'); + $destination = $migration->get('destination'); + $destination['preserve_properties'][] = 'name'; + $migration->set('destination', $destination); + $this->executeMigration($migration); + + /** @var \Drupal\user\UserInterface $account */ + $account = User::load(2); + $this->assertIdentical('Michael Palin', $account->label()); + $this->assertIdentical('john.doe@example.com', $account->getEmail()); + } + +} diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal7.php b/core/modules/migrate_drupal/tests/fixtures/drupal7.php index 3a9d266..0804334 100644 --- a/core/modules/migrate_drupal/tests/fixtures/drupal7.php +++ b/core/modules/migrate_drupal/tests/fixtures/drupal7.php @@ -39895,9 +39895,9 @@ )) ->values(array( 'uid' => '1', - 'name' => 'root', + 'name' => 'admin', 'pass' => '$S$D/HVkgCg1Hvi7DN5KVSgNl.2C5g8W6oe/OoIRMUlyjkmPugQRhoB', - 'mail' => '', + 'mail' => 'admin@local.host', 'theme' => '', 'signature' => '', 'signature_format' => NULL, diff --git a/core/modules/node/src/Tests/Migrate/d7/MigrateNodeTest.php b/core/modules/node/src/Tests/Migrate/d7/MigrateNodeTest.php index b2537f5..9a9024b 100644 --- a/core/modules/node/src/Tests/Migrate/d7/MigrateNodeTest.php +++ b/core/modules/node/src/Tests/Migrate/d7/MigrateNodeTest.php @@ -123,7 +123,7 @@ protected function assertRevision($id, $title, $uid, $log, $timestamp) { */ public function testNode() { $this->assertEntity(1, 'test_content_type', 'en', 'A Node', '2', TRUE, '1421727515', '1441032132', TRUE, FALSE); - $this->assertRevision(1, 'A Node', '2', NULL, '1441032132'); + $this->assertRevision(1, 'A Node', '1', NULL, '1441032132'); $node = Node::load(1); $this->assertTrue($node->field_boolean->value); diff --git a/core/modules/user/src/Plugin/migrate/source/d6/User.php b/core/modules/user/src/Plugin/migrate/source/d6/User.php index 59c3229..e344d06 100644 --- a/core/modules/user/src/Plugin/migrate/source/d6/User.php +++ b/core/modules/user/src/Plugin/migrate/source/d6/User.php @@ -25,7 +25,7 @@ class User extends DrupalSqlBase { public function query() { return $this->select('users', 'u') ->fields('u', array_keys($this->baseFields())) - ->condition('uid', 1, '>'); + ->condition('uid', 0, '>'); } /** diff --git a/core/modules/user/src/Plugin/migrate/source/d7/User.php b/core/modules/user/src/Plugin/migrate/source/d7/User.php index fc05bf3..fa872c6 100755 --- a/core/modules/user/src/Plugin/migrate/source/d7/User.php +++ b/core/modules/user/src/Plugin/migrate/source/d7/User.php @@ -25,7 +25,7 @@ class User extends FieldableEntity { public function query() { return $this->select('users', 'u') ->fields('u') - ->condition('uid', 1, '>'); + ->condition('uid', 0, '>'); } /**