diff --git a/src/DefaultContentManager.php b/src/DefaultContentManager.php index 40acca2..6160c48 100644 --- a/src/DefaultContentManager.php +++ b/src/DefaultContentManager.php @@ -219,7 +219,14 @@ class DefaultContentManager implements DefaultContentManagerInterface { $contents = $this->parseFile($file); $class = $definition['serialization_class']; $entity = $this->serializer->deserialize($contents, $class, 'hal_json', array('request_method' => 'POST')); - $entity->enforceIsNew(TRUE); + // Unset entity keys to avoid collisions. UUID will conflict will + // alert us to any true collision. + $entity_type = $this->entityManager->getDefinition($entity_type_id); + $keys = $entity_type->getKeys(); + unset($entity->{$keys['id']}); + if (isset($keys['revision'])) { + unset($entity->{$keys['revision']}); + } $entity->save(); $created[$entity->uuid()] = $entity; } diff --git a/tests/modules/default_content_test/content/node/imported.json b/tests/modules/default_content_test/content/node/imported.json index a5e37bc..1e3421e 100755 --- a/tests/modules/default_content_test/content/node/imported.json +++ b/tests/modules/default_content_test/content/node/imported.json @@ -27,6 +27,16 @@ "value": "65c412a3-b83f-4efb-8a05-5a6ecea10ad4" } ], + "nid": [ + { + "value": "1" + } + ], + "vid": [ + { + "value": "1" + } + ], "type": [ { "target_id": "page" diff --git a/tests/src/Functional/DefaultContentTest.php b/tests/src/Functional/DefaultContentTest.php index caeaf15..eadf73f 100644 --- a/tests/src/Functional/DefaultContentTest.php +++ b/tests/src/Functional/DefaultContentTest.php @@ -5,6 +5,7 @@ namespace Drupal\Tests\default_content\Functional; use Drupal\simpletest\BrowserTestBase; use Drupal\simpletest\ContentTypeCreationTrait; use Drupal\simpletest\NodeCreationTrait; +use Drupal\taxonomy\Entity\Term; /** * Test import of default content. @@ -29,6 +30,7 @@ class DefaultContentTest extends BrowserTestBase { protected function setUp() { parent::setUp(); $this->createContentType(array('type' => 'page')); + $this->createContentType(array('type' => 'article')); } /** @@ -51,4 +53,35 @@ class DefaultContentTest extends BrowserTestBase { $this->assertTrue(!empty($term_id), 'Term reference populated'); } + /** + * Test stripping entity ids during import. + */ + public function testImportWithEntityIdConflict() { + $this->drupalLogin($this->drupalCreateUser(array_keys(\Drupal::moduleHandler()->invokeAll(('permission'))))); + + // Create entities with known-conflicting ids. + $conflict_node = $this->createNode([ + 'title' => 'Node with conflicting nid', + 'type' => 'article', + 'nid' => 1, + 'vid' => 1, + ]); + $conflict_term = Term::create([ + 'name' => 'Tag with conflicting tid', + 'vid' => 'tags', + 'tid' => 1, + ]); + $conflict_term->save(); + + // Enable the module and import the content. + \Drupal::service('module_installer')->install(array('default_content_test'), TRUE); + $this->rebuildContainer(); + $node = $this->getNodeByTitle('Imported node'); + $this->assertNotEquals($node->id(), $conflict_node->id()); + $this->assertNotEquals($node->getRevisionId(), $conflict_node->getRevisionId()); + $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'A tag']); + $term = reset($terms); + $this->assertNotEquals($term->id(), $conflict_term->id()); + } + }