diff -u b/src/Plugin/migrate/destination/EntityReferenceRevisions.php b/src/Plugin/migrate/destination/EntityReferenceRevisions.php --- b/src/Plugin/migrate/destination/EntityReferenceRevisions.php +++ b/src/Plugin/migrate/destination/EntityReferenceRevisions.php @@ -46,12 +46,25 @@ * {@inheritdoc} */ public function getIds() { - $ids = parent::getIds(); + if ($revision_key = $this->getKey('revision')) { + // TODO: Improve after https://www.drupal.org/node/2783715 is finished. + $ids[$revision_key]['type'] = 'integer'; - // TODO: Improve after https://www.drupal.org/node/2783715 is finished. - $ids[$this->getKey('revision')]['type'] = 'string'; + $id_key = $this->getKey('id'); + $ids[$id_key]['type'] = 'integer'; - return $ids; + if ($this->isTranslationDestination()) { + if ($revision_key = $this->getKey('langcode')) { + $ids[$revision_key]['type'] = 'string'; + } + else { + throw new MigrateException('This entity type does not support translation.'); + } + } + + return $ids; + } + throw new MigrateException('This entity type does not support revisions.'); } /** @@ -64,7 +77,8 @@ $revisionId = $oldDestinationIdValues ? array_pop($oldDestinationIdValues) : $row->getDestinationProperty($this->getKey('revision')); - if (!empty($entityId) && !empty($revisionId) && ($entity = $this->storage->loadByProperties([$this->getKey('id') => $entityId, $this->getKey('revision') => $revisionId]))) { + if (!empty($entityId) && !empty($revisionId) && ($entities = $this->storage->loadByProperties([$this->getKey('id') => $entityId, $this->getKey('revision') => $revisionId]))) { + $entity = reset($entities); $entity->setNewRevision(FALSE); } else { diff -u b/tests/modules/err_migration_test/migration_templates/err_migration_test.yml b/tests/modules/err_migration_test/migration_templates/err_migration_test.yml --- b/tests/modules/err_migration_test/migration_templates/err_migration_test.yml +++ b/tests/modules/err_migration_test/migration_templates/err_migration_test.yml @@ -2,9 +2,11 @@ migration_tags: {} label: 'ERR Migration Test' source: - plugin: dummy + plugin: err_dummy process: - id + id: id + revision_id: revision_id + name: name destination: plugin: 'entity_reference_revisions:entity_test_composite' migration_dependencies: { } diff -u b/tests/modules/err_migration_test/src/Plugin/migrate/source/DummySource.php b/tests/modules/err_migration_test/src/Plugin/migrate/source/DummySource.php --- b/tests/modules/err_migration_test/src/Plugin/migrate/source/DummySource.php +++ b/tests/modules/err_migration_test/src/Plugin/migrate/source/DummySource.php @@ -1,12 +1,12 @@ [ + 'type' => 'integer', + ] + ]; } /** * {@inheritdoc} */ protected function initializeIterator() { - return [ + return new \ArrayIterator([ ['id' => 1, 'revision_id' => 1, 'name' => 'content item 1'], - ]; + ['id' => '', 'revision_id' => '', 'name' => 'content item 2'], + ]); } } diff -u b/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php b/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php --- b/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php +++ b/tests/src/Kernel/Plugin/migrate/destination/EntityReferenceRevisionsDestinationTest.php @@ -5,6 +5,8 @@ use Drupal\Core\Entity\EntityStorageBase; use Drupal\entity_reference_revisions\Plugin\migrate\destination\EntityReferenceRevisions; use Drupal\KernelTests\KernelTestBase; +use Drupal\migrate\MigrateExecutable; +use Drupal\migrate\MigrateMessageInterface; use Drupal\migrate\Plugin\Migration; use Drupal\migrate\Plugin\MigrationPluginManager; use Drupal\migrate\Plugin\MigrateDestinationPluginManager; @@ -15,17 +17,36 @@ * @coversDefaultClass \Drupal\entity_reference_revisions\Plugin\migrate\destination\EntityReferenceRevisions * @group entity_reference_revisions */ -class EntityReferenceRevisionsDestinationTest extends KernelTestBase { +class EntityReferenceRevisionsDestinationTest extends KernelTestBase implements MigrateMessageInterface { /** * {@inheritdoc} */ - public static $modules = ['migrate', 'entity_reference_revisions', 'err_migration_test', 'entity_composite_relationship_test']; + public static $modules = [ + 'migrate', + 'err_migration_test', + 'entity_reference_revisions', + 'entity_composite_relationship_test', + 'user', + 'system', + ]; /** - * Tests deriver + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + $this->installEntitySchema('entity_test_composite'); + $this->installSchema('system', ['sequences']); + $this->installConfig($this->modules); + } + + /** + * Tests get entity type id. + * + * @covers ::getEntityTypeId */ - public function testDestinationDeriver() { + public function testGetEntityTypeId() { /** @var MigrationPluginManager $migrationManager */ $migrationManager = \Drupal::service('plugin.manager.migration'); $definition = $migrationManager->getDefinition('err_migration_test'); @@ -44,2 +65,33 @@ + /** + * Tests get entity. + * + * @covers ::getEntity + */ + public function testGetEntity() { + /** @var MigrationPluginManager $migrationManager */ + $migrationManager = \Drupal::service('plugin.manager.migration'); + $definition = $migrationManager->getDefinition('err_migration_test'); + /** @var Migration $migration */ + $migration = $migrationManager->createStubMigration($definition); + $migrationExecutable = (new MigrateExecutable($migration, $this)); + for ($i = 0; $i < 2; $i++) { + $migrationExecutable->import(); + $migration->getIdMap()->prepareUpdate(); + $entities = \Drupal::entityManager()->getStorage('entity_test_composite')->loadByProperties(['id' => 1, 'revision_id' => 1]); + $entity = reset($entities); + $this->assertEquals('content item 1', $entity->label()); + $entities = \Drupal::entityManager()->getStorage('entity_test_composite')->loadByProperties(['id' => 2, 'revision_id' => 2]); + $entity = reset($entities); + $this->assertEquals('content item 2', $entity->label()); + } + } + + /** + * {@inheritdoc} + */ + public function display($message, $type = 'status') { + $this->assertTrue($type == 'status', $message); + } + }