diff --git a/core/modules/action/tests/src/Kernel/Plugin/migrate/source/ActionTest.php b/core/modules/action/tests/src/Kernel/Plugin/migrate/source/ActionTest.php index 7f7b7ef8cd..938752ad4d 100644 --- a/core/modules/action/tests/src/Kernel/Plugin/migrate/source/ActionTest.php +++ b/core/modules/action/tests/src/Kernel/Plugin/migrate/source/ActionTest.php @@ -15,7 +15,7 @@ class ActionTest extends MigrateSqlSourceTestBase { /** * {@inheritdoc} */ - public static $modules = ['action', 'migrate_drupal']; + public static $modules = ['action', 'migrate_drupal', 'system']; /** * {@inheritdoc} diff --git a/core/modules/book/tests/src/Kernel/Plugin/migrate/source/d6/BookTest.php b/core/modules/book/tests/src/Kernel/Plugin/migrate/source/d6/BookTest.php index feece1f7bc..f8a362eef2 100644 --- a/core/modules/book/tests/src/Kernel/Plugin/migrate/source/d6/BookTest.php +++ b/core/modules/book/tests/src/Kernel/Plugin/migrate/source/d6/BookTest.php @@ -13,7 +13,7 @@ class BookTest extends MigrateSqlSourceTestBase { /** * {@inheritdoc} */ - public static $modules = ['book', 'migrate_drupal']; + public static $modules = ['user', 'node', 'book', 'migrate_drupal']; /** * {@inheritdoc} diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntity.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntity.php new file mode 100644 index 0000000000..b9826be3ed --- /dev/null +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntity.php @@ -0,0 +1,230 @@ +entityTypeManager = $entity_type_manager; + $this->entityFieldManager = $entity_field_manager; + $this->entityType = $plugin_definition['entity_type']; + $entityDefinition = $this->entityTypeManager->getDefinition($this->entityType); + $this->bundleKey = $entityDefinition->getKey('bundle'); + if (!empty($this->configuration['bundle'])) { + if (!$this->bundleKey) { + throw new InvalidPluginDefinitionException('A bundle was provided but entity is not bundleable.'); + } + } + parent::__construct($configuration + ['bundle' => NULL], $plugin_id, $plugin_definition, $migration); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + $container->get('entity_type.manager'), + $container->get('entity_field.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function __toString() { + return $this->entityType; + } + + /** + * Initializes the iterator with the source data. + * + * @return \Generator + * An generator of the data for this source. + */ + protected function initializeIterator() { + $ids = $this->query()->execute(); + return $this->yield($ids); + } + + /** + * Loads and yields a single entity one at a time. + * + * @param array $ids + * The entity IDs. + * + * @return \Generator + * And iterable of loaded entities. + */ + protected function yield(array $ids) { + $storage = $this->entityTypeManager->getStorage($this->entityType); + foreach ($ids as $id) { + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $storage->load($id); + yield $this->toArray($entity); + foreach ($entity->getTranslationLanguages(FALSE) as $language) { + yield $this->toArray($entity->getTranslation($language->getId())); + } + } + } + + /** + * Convert entity to array. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity to convert. + * + * @return array + * The entity converted to array. + */ + protected function toArray(ContentEntityInterface $entity) { + $return = []; + foreach ($entity->toArray() as $field => $values) { + $return[$field] = $values; + } + // The ids must be flat, they cannot be nested for the id map. + foreach (array_keys($this->getIds()) as $id) { + $reset = reset($return[$id]); + $return[$id] = reset($reset); + } + return $return; + } + + /** + * Query to retrieve entities. + * + * @return \Drupal\Core\Entity\Query\QueryInterface + */ + public function query() { + $query = $this->entityTypeManager + ->getStorage($this->entityType) + ->getQuery(); + if (!empty($this->configuration['bundle'])) { + $query->condition($this->bundleKey, $this->configuration['bundle']); + } + return $query; + } + + /** + * {@inheritdoc} + */ + protected function doCount() { + return $this->query()->count()->execute(); + } + + /** + * {@inheritdoc} + */ + public function fields() { + $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType); + $fields = []; + foreach ($field_definitions as $field_name => $definition) { + $fields[$field_name] = (string) $definition->getLabel(); + } + if (!empty($this->configuration['bundle'])) { + foreach ($this->entityFieldManager->getFieldDefinitions($this->entityType, $this->configuration['bundle']) as $field_name => $definition) { + $fields[$field_name] = (string) $definition->getLabel(); + } + } + return $fields; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $entity_definition = $this->entityTypeManager->getDefinition($this->entityType); + $idKey = $entity_definition->getKey('id'); + $ids[$idKey] = $this->getDefinitionFromEntity($idKey); + if ($entity_definition->isTranslatable()) { + $langcode_key = $entity_definition->getKey('langcode'); + $ids[$langcode_key] = $this->getDefinitionFromEntity($langcode_key); + } + return $ids; + } + + /** + * Gets the field definition from a specific entity base field. + * + * @param string $key + * The field ID key. + * + * @return array + * An associative array with a structure that contains the field type, keyed + * as 'type', together with field storage settings as they are returned by + * FieldStorageDefinitionInterface::getSettings(). + * + * @see \Drupal\migrate\Plugin\migrate\destination\EntityContentBase::getDefinitionFromEntity() + */ + protected function getDefinitionFromEntity($key) { + /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ + $field_definition = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType)[$key]; + return [ + 'alias' => 'b', + 'type' => $field_definition->getType(), + ] + $field_definition->getSettings(); + } + +} diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntityDeriver.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntityDeriver.php new file mode 100644 index 0000000000..33052bedfa --- /dev/null +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntityDeriver.php @@ -0,0 +1,59 @@ +entityTypeManager = $entityTypeManager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, $base_plugin_id) { + // Translations don't make sense unless we have content_translation. + return new static( + $base_plugin_id, + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions($base_plugin_definition) { + $this->derivatives = []; + foreach ($this->entityTypeManager->getDefinitions() as $id => $definition) { + // The source plugin only supports entities that are a content entity. + if ($definition instanceof ContentEntityTypeInterface) { + $this->derivatives[$id] = $base_plugin_definition; + // Provide this so the source can be used separate from a deriver. + $this->derivatives[$id]['entity_type'] = $id; + } + } + return parent::getDerivativeDefinitions($base_plugin_definition); + } + +} diff --git a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ContentEntityTest.php b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ContentEntityTest.php new file mode 100755 index 0000000000..c096f72892 --- /dev/null +++ b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ContentEntityTest.php @@ -0,0 +1,375 @@ +installEntitySchema('node'); + $this->installEntitySchema('file'); + $this->installEntitySchema('media'); + $this->installEntitySchema('taxonomy_term'); + $this->installEntitySchema('taxonomy_vocabulary'); + $this->installEntitySchema('user'); + $this->installSchema('system', ['sequences']); + $this->installSchema('user', 'users_data'); + $this->installSchema('file', 'file_usage'); + $this->installSchema('node', ['node_access']); + $this->installConfig($this->modules); + + ConfigurableLanguage::createFromLangcode('fr')->save(); + + // Create article content type. + $nodeType = NodeType::create(['type' => $this->bundle, 'name' => 'Article']); + $nodeType->save(); + + // Create a vocabulary. + $vocabulary = Vocabulary::create([ + 'name' => $this->vocabulary, + 'description' => $this->vocabulary, + 'vid' => $this->vocabulary, + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + ]); + $vocabulary->save(); + + // Create a term reference field on node. + $this->createEntityReferenceField( + 'node', + $this->bundle, + $this->fieldName, + 'Term reference', + 'taxonomy_term', + 'default', + ['target_bundles' => [$this->vocabulary]], + FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED + ); + // Create a term reference field on user. + $this->createEntityReferenceField( + 'user', + 'user', + $this->fieldName, + 'Term reference', + 'taxonomy_term', + 'default', + ['target_bundles' => [$this->vocabulary]], + FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED + ); + + // Create some data. + $this->user = User::create([ + 'name' => 'user123', + 'uid' => 1, + 'mail' => 'example@example.com', + ]); + $this->user->save(); + + $term = Term::create([ + 'vid' => $this->vocabulary, + 'name' => 'Apples', + 'uid' => $this->user->id(), + ]); + $term->save(); + $this->user->set($this->fieldName, $term->id()); + $this->user->save(); + $node = Node::create([ + 'type' => $this->bundle, + 'title' => 'Apples', + $this->fieldName => $term->id(), + 'uid' => $this->user->id(), + ]); + $node->save(); + $node->addTranslation('fr', [ + 'title' => 'Pommes', + $this->fieldName => $term->id(), + ])->save(); + + $this->sourcePluginManager = $this->container->get('plugin.manager.migrate.source'); + $this->migration = $this->createMock(MigrationInterface::class); + $idMap = $this->createMock(MigrateIdMapInterface::class); + $idMap->expects(self::any())->method('getRowBySource')->willReturn(NULL); + $this->migration->expects(self::any())->method('getIdMap')->willReturn($idMap); + } + + /** + * Tests user source plugin. + */ + public function testUserSource() { + $userSource = $this->sourcePluginManager->createInstance('entity:user', [], $this->migration); + $this->migration->expects(self::once())->method('getSourcePlugin')->willReturn($userSource); + $this->migration->expects(self::once())->method('getDestinationIds')->willReturn([1]); + $ids = $userSource->getIds(); + $this->assertArrayHasKey('langcode', $ids); + $this->assertArrayHasKey('uid', $ids); + $fields = $userSource->fields(); + $this->assertArrayHasKey('name', $fields); + $this->assertArrayHasKey('pass', $fields); + $this->assertArrayHasKey('mail', $fields); + $this->assertArrayHasKey('uid', $fields); + $this->assertArrayHasKey('roles', $fields); + $userSource->rewind(); + $values = $userSource->current()->getSource(); + $this->assertEquals('example@example.com', $values['mail'][0]['value']); + $this->assertEquals('user123', $values['name'][0]['value']); + $this->assertEquals(1, $values['uid']); + $this->assertEquals(1, $values['field_entity_reference'][0]['target_id']); + } + + /** + * Tests file source plugin. + */ + public function testFileSource() { + $file = File::create([ + 'filename' => 'foo.txt', + 'uid' => $this->user->id(), + 'uri' => 'public://foo.txt', + ]); + $file->save(); + + $fileSource = $this->sourcePluginManager->createInstance('entity:file', [], $this->migration); + $this->migration->expects(self::once())->method('getSourcePlugin')->willReturn($fileSource); + $this->migration->expects(self::once())->method('getDestinationIds')->willReturn([1]); + $ids = $fileSource->getIds(); + $this->assertArrayHasKey('fid', $ids); + $fields = $fileSource->fields(); + $this->assertArrayHasKey('fid', $fields); + $this->assertArrayHasKey('filemime', $fields); + $this->assertArrayHasKey('filename', $fields); + $this->assertArrayHasKey('uid', $fields); + $this->assertArrayHasKey('uri', $fields); + $fileSource->rewind(); + $values = $fileSource->current()->getSource(); + $this->assertEquals('text/plain', $values['filemime'][0]['value']); + $this->assertEquals('public://foo.txt', $values['uri'][0]['value']); + $this->assertEquals('foo.txt', $values['filename'][0]['value']); + $this->assertEquals(1, $values['fid']); + } + + /** + * Tests node source plugin. + */ + public function testNodeSource() { + $nodeSource = $this->sourcePluginManager->createInstance('entity:node', ['bundle' => $this->bundle], $this->migration); + $this->migration->expects(self::exactly(2))->method('getSourcePlugin')->willReturn($nodeSource); + $this->migration->expects(self::at(0))->method('getDestinationIds')->willReturn([1, 'en']); + $this->migration->expects(self::at(1))->method('getDestinationIds')->willReturn([2, 'fr']); + $ids = $nodeSource->getIds(); + $this->assertArrayHasKey('langcode', $ids); + $this->assertArrayHasKey('nid', $ids); + $fields = $nodeSource->fields(); + $this->assertArrayHasKey('nid', $fields); + $this->assertArrayHasKey('vid', $fields); + $this->assertArrayHasKey('title', $fields); + $this->assertArrayHasKey('uid', $fields); + $this->assertArrayHasKey('sticky', $fields); + $nodeSource->rewind(); + $values = $nodeSource->current()->getSource(); + $this->assertEquals($this->bundle, $values['type'][0]['target_id']); + $this->assertEquals(1, $values['nid']); + $this->assertEquals('en', $values['langcode']); + $this->assertEquals(1, $values['status'][0]['value']); + $this->assertEquals('Apples', $values['title'][0]['value']); + $this->assertEquals(1, $values['default_langcode'][0]['value']); + $this->assertEquals(1, $values['field_entity_reference'][0]['target_id']); + $nodeSource->next(); + $values = $nodeSource->current()->getSource(); + $this->assertEquals($this->bundle, $values['type'][0]['target_id']); + $this->assertEquals(1, $values['nid']); + $this->assertEquals('fr', $values['langcode']); + $this->assertEquals(1, $values['status'][0]['value']); + $this->assertEquals('Pommes', $values['title'][0]['value']); + $this->assertEquals(0, $values['default_langcode'][0]['value']); + $this->assertEquals(1, $values['field_entity_reference'][0]['target_id']); + } + + /** + * Tests media source plugin. + */ + public function testMediaSource() { + $mediaType = $this->createMediaType('test'); + $media = Media::create([ + 'name' => 'Foo media', + 'uid' => $this->user->id(), + 'bundle' => $mediaType->id(), + ]); + $media->save(); + + $mediaSource = $this->sourcePluginManager->createInstance('entity:media', ['bundle' => 'image'], $this->migration); + $this->migration->expects(self::once())->method('getSourcePlugin')->willReturn($mediaSource); + $this->migration->expects(self::once())->method('getDestinationIds')->willReturn([1]); + $ids = $mediaSource->getIds(); + $this->assertArrayHasKey('langcode', $ids); + $this->assertArrayHasKey('mid', $ids); + $fields = $mediaSource->fields(); + $this->assertArrayHasKey('bundle', $fields); + $this->assertArrayHasKey('mid', $fields); + $this->assertArrayHasKey('name', $fields); + $this->assertArrayHasKey('status', $fields); + $mediaSource->rewind(); + $values = $mediaSource->current()->getSource(); + $this->assertEquals(1, $values['mid']); + $this->assertEquals('Foo media', $values['name'][0]['value']); + $this->assertEquals('Foo media', $values['thumbnail'][0]['title']); + $this->assertEquals(1, $values['uid'][0]['target_id']); + $this->assertEquals('image', $values['bundle'][0]['target_id']); + } + + /** + * Tests term source plugin. + */ + public function testTermSource() { + $term2 = Term::create([ + 'vid' => $this->vocabulary, + 'name' => 'Granny Smith', + 'uid' => $this->user->id(), + 'parent' => 1, + ]); + $term2->save(); + + $termSource = $this->sourcePluginManager->createInstance('entity:taxonomy_term', ['bundle' => $this->vocabulary], $this->migration); + $this->migration->expects(self::exactly(2))->method('getSourcePlugin')->willReturn($termSource); + $this->migration->expects(self::at(0))->method('getDestinationIds')->willReturn([1]); + $this->migration->expects(self::at(1))->method('getDestinationIds')->willReturn([2]); + $ids = $termSource->getIds(); + $this->assertArrayHasKey('langcode', $ids); + $this->assertArrayHasKey('tid', $ids); + $fields = $termSource->fields(); + $this->assertArrayHasKey('vid', $fields); + $this->assertArrayHasKey('tid', $fields); + $this->assertArrayHasKey('name', $fields); + $termSource->rewind(); + $values = $termSource->current()->getSource(); + $this->assertEquals($this->vocabulary, $values['vid'][0]['target_id']); + $this->assertEquals(1, $values['tid']); + // @TODO: add test coverage for parent after + // https://www.drupal.org/project/drupal/issues/2543726 + $this->assertEquals('Apples', $values['name'][0]['value']); + $termSource->next(); + $values = $termSource->current()->getSource(); + $this->assertEquals($this->vocabulary, $values['vid'][0]['target_id']); + $this->assertEquals(2, $values['tid']); + // @TODO: add test coverage for parent after + // https://www.drupal.org/project/drupal/issues/2543726 + $this->assertEquals('Granny Smith', $values['name'][0]['value']); + } + + /** + * Create a media type for a source plugin. + * + * @param string $media_source_name + * The name of the media source. + * + * @return \Drupal\media\MediaTypeInterface + * A media type. + */ + protected function createMediaType($media_source_name) { + $media_type = MediaType::create([ + 'id' => 'image', + 'label' => 'Image', + 'source' => $media_source_name, + 'new_revision' => FALSE, + ]); + $media_type->save(); + $source_field = $media_type->getSource()->createSourceField($media_type); + // The media type form creates a source field if it does not exist yet. The + // same must be done in a kernel test, since it does not use that form. + // @see \Drupal\media\MediaTypeForm::save() + $source_field->getFieldStorageDefinition()->save(); + // The source field storage has been created, now the field can be saved. + $source_field->save(); + $media_type->set('source_configuration', [ + 'source_field' => $source_field->getName(), + ])->save(); + return $media_type; + } + +}