diff --git a/default_content.services.yml b/default_content.services.yml index 6827fa3..2a88056 100644 --- a/default_content.services.yml +++ b/default_content.services.yml @@ -5,7 +5,7 @@ services: class: Drupal\default_content\Scanner default_content.importer: class: Drupal\default_content\Importer - arguments: ['@serializer', '@plugin.manager.rest', '@entity_type.manager', '@rest.link_manager', '@event_dispatcher', '@default_content.scanner', '@module_handler', '%default_content.link_domain%'] + arguments: ['@serializer', '@plugin.manager.rest', '@entity_type.manager', '@rest.link_manager', '@event_dispatcher', '@default_content.scanner', '@module_handler', '%default_content.link_domain%', '%install_profile%'] default_content.exporter: class: Drupal\default_content\Exporter arguments: ['@serializer', '@entity_type.manager', '@entity.repository', '@rest.link_manager', '@event_dispatcher', '@module_handler', '@info_parser', '%default_content.link_domain%'] diff --git a/drush/default_content.drush.inc b/drush/default_content.drush.inc index 46e090e..a3597dd 100644 --- a/drush/default_content.drush.inc +++ b/drush/default_content.drush.inc @@ -132,7 +132,7 @@ function drush_default_content_export_module($module_name) { /** * Imports content provided in enabled modules and default profile. * - * @param string $module_name + * @param string $module * The import will be limited to this module or profile. */ function drush_default_content_import_module($module) { diff --git a/src/Importer.php b/src/Importer.php index 18c24df..a4cc933 100644 --- a/src/Importer.php +++ b/src/Importer.php @@ -92,6 +92,13 @@ class Importer implements ImporterInterface { protected $scanner; /** + * The installation profile. + * + * @var string + */ + protected $installProfile; + + /** * Constructs the default content manager. * * @param \Symfony\Component\Serializer\Serializer $serializer @@ -110,8 +117,10 @@ class Importer implements ImporterInterface { * The module handler. * @param string $link_domain * Defines relation domain URI for entity links. + * @param string $install_profile + * The install profile. */ - public function __construct(Serializer $serializer, ResourcePluginManager $resource_plugin_manager, EntityTypeManagerInterface $entity_type_manager, LinkManagerInterface $link_manager, EventDispatcherInterface $event_dispatcher, ScannerInterface $scanner, ModuleHandlerInterface $module_handler, $link_domain) { + public function __construct(Serializer $serializer, ResourcePluginManager $resource_plugin_manager, EntityTypeManagerInterface $entity_type_manager, LinkManagerInterface $link_manager, EventDispatcherInterface $event_dispatcher, ScannerInterface $scanner, ModuleHandlerInterface $module_handler, $link_domain, $install_profile) { $this->serializer = $serializer; $this->resourcePluginManager = $resource_plugin_manager; $this->entityTypeManager = $entity_type_manager; @@ -120,6 +129,7 @@ class Importer implements ImporterInterface { $this->scanner = $scanner; $this->moduleHandler = $module_handler; $this->linkDomain = $link_domain; + $this->installProfile = $install_profile; } /** @@ -216,8 +226,8 @@ class Importer implements ImporterInterface { */ public function importAllContent() { $module_names = array_keys($this->moduleHandler->getModuleList()); - $profile_name = drupal_get_profile(); - return array_reduce(array_unique($module_names + [$profile_name]), function (array $entities, $module_name) { + // Flatten the created entities into one array. + return array_reduce(array_unique($module_names + [$this->installProfile]), function (array $entities, $module_name) { return array_merge($entities, $this->importContent($module_name)); }, []); } diff --git a/src/ImporterInterface.php b/src/ImporterInterface.php index 0b60960..bd56f68 100644 --- a/src/ImporterInterface.php +++ b/src/ImporterInterface.php @@ -21,7 +21,7 @@ interface ImporterInterface { /** * Imports all default content across the site. * - * @return array[\Drupal\Core\Entity\EntityInterface] + * @return \Drupal\Core\Entity\EntityInterface[] * The created entities. */ public function importAllContent(); diff --git a/tests/src/Kernel/ImporterIntegrationTest.php b/tests/src/Kernel/ImporterIntegrationTest.php new file mode 100644 index 0000000..b0b9c00 --- /dev/null +++ b/tests/src/Kernel/ImporterIntegrationTest.php @@ -0,0 +1,108 @@ +installSchema('system', ['router', 'sequences']); + } + + protected function setupImport() { + \Drupal::service('module_installer')->install(['node', 'taxonomy', 'field']); + $this->installEntitySchema('node'); + $this->installEntitySchema('taxonomy_term'); + + NodeType::create([ + 'type' => 'page', + ])->save(); + + FieldConfig::create([ + 'entity_type' => 'node', + 'field_name' => 'body', + 'bundle' => 'page', + ])->save(); + + \Drupal::service('module_installer')->install(['rest', 'serialization']); + \Drupal::service('module_installer')->install([ + 'default_content', + ]); + + // Install the module but remove its content. + \Drupal::service('module_installer')->install([ + 'default_content_test', + ]); + + // Cleanup previously installed content. + \Drupal::service('entity.repository')->loadEntityByUuid('node', '65c412a3-b83f-4efb-8a05-5a6ecea10ad4')->delete(); + \Drupal::service('entity.repository')->loadEntityByUuid('taxonomy_term', '550f86ad-aa11-4047-953f-636d42889f85')->delete(); + } + + /** + * Tests the import mechanism. + */ + public function testSingleImport() { + $this->setupImport(); + + $this->importer = \Drupal::service('default_content.importer'); + + $entities = $this->importer->importContent('default_content_test'); + $this->assertInstanceOf(TermInterface::class, $entities['550f86ad-aa11-4047-953f-636d42889f85']); + $this->assertInstanceOf(NodeInterface::class, $entities['65c412a3-b83f-4efb-8a05-5a6ecea10ad4']); + + $this->assertCount(2, $entities); + // Ensure the content can be actually loaded. + $this->assertEquals('A tag', Term::load($entities['550f86ad-aa11-4047-953f-636d42889f85']->id())->label()); + $this->assertEquals('Imported node', Node::load($entities['65c412a3-b83f-4efb-8a05-5a6ecea10ad4']->id())->label()); + } + + /** + * Tests the import mechanism of all modules. + */ + public function testImportAllContent() { + $this->setupImport(); + + $this->importer = \Drupal::service('default_content.importer'); + + $entities = $this->importer->importAllContent(); + $this->assertInstanceOf(TermInterface::class, $entities['550f86ad-aa11-4047-953f-636d42889f85']); + $this->assertInstanceOf(NodeInterface::class, $entities['65c412a3-b83f-4efb-8a05-5a6ecea10ad4']); + + $this->assertCount(2, $entities); + // Ensure the content can be actually loaded. + $this->assertEquals('A tag', Term::load($entities['550f86ad-aa11-4047-953f-636d42889f85']->id())->label()); + $this->assertEquals('Imported node', Node::load($entities['65c412a3-b83f-4efb-8a05-5a6ecea10ad4']->id())->label()); + } + +}