Commit 9d068fac authored by alexpott's avatar alexpott Committed by andypost

Issue #2833562 by alexpott, das-peter, tstoeckler, andypost, larowlan,...

Issue #2833562 by alexpott, das-peter, tstoeckler, andypost, larowlan, hatznie: Installing modules using the ConfigImporter does not work
parent 5f491b0c
...@@ -9,3 +9,7 @@ services: ...@@ -9,3 +9,7 @@ services:
default_content.exporter: default_content.exporter:
class: Drupal\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%'] arguments: ['@serializer', '@entity_type.manager', '@entity.repository', '@rest.link_manager', '@event_dispatcher', '@module_handler', '@info_parser', '%default_content.link_domain%']
default_content.config_subscriber:
class: Drupal\default_content\Config\DefaultContentConfigSubscriber
arguments: ['@default_content.importer']
tags: [{ name: event_subscriber }]
<?php
namespace Drupal\default_content\Config;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigImporterEvent;
use Drupal\default_content\ImporterInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Reacts to configuration events for the Default Content module.
*/
class DefaultContentConfigSubscriber implements EventSubscriberInterface {
/**
* The default content importer.
*
* @var \Drupal\default_content\ImporterInterface
*/
protected $defaultContentImporter;
/**
* Constructs a DefaultContentConfigSubscriber object.
*
* @param \Drupal\default_content\ImporterInterface $default_content_importer
* The default content importer.
*/
public function __construct(ImporterInterface $default_content_importer) {
$this->defaultContentImporter = $default_content_importer;
}
/**
* Creates default content after config synchronization.
*
* @param \Drupal\Core\Config\ConfigImporterEvent $event
* The config importer event.
*/
public function onConfigImport(ConfigImporterEvent $event) {
$modules = $event->getConfigImporter()->getExtensionChangelist('module', 'install');
foreach ($modules as $module) {
$this->defaultContentImporter->importContent($module);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [ConfigEvents::IMPORT => 'onConfigImport'];
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\Tests\default_content\Functional; namespace Drupal\Tests\default_content\Functional;
use Drupal\Core\Config\FileStorage;
use Drupal\simpletest\BrowserTestBase; use Drupal\simpletest\BrowserTestBase;
use Drupal\simpletest\ContentTypeCreationTrait; use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\simpletest\NodeCreationTrait; use Drupal\simpletest\NodeCreationTrait;
...@@ -35,11 +36,44 @@ class DefaultContentTest extends BrowserTestBase { ...@@ -35,11 +36,44 @@ class DefaultContentTest extends BrowserTestBase {
* Test importing default content. * Test importing default content.
*/ */
public function testImport() { public function testImport() {
// Login as admin.
$this->drupalLogin($this->drupalCreateUser(array_keys(\Drupal::moduleHandler()->invokeAll(('permission')))));
// Enable the module and import the content. // Enable the module and import the content.
\Drupal::service('module_installer')->install(array('default_content_test'), TRUE); \Drupal::service('module_installer')->install(array('default_content_test'), TRUE);
$this->rebuildContainer(); $this->rebuildContainer();
$this->doPostInstallTests();
}
/**
* Test importing default content via ConfigImporter.
*/
public function testImportViaConfigImporter() {
$sync = $this->container->get('config.storage.sync');
$this->copyConfig($this->container->get('config.storage'), $sync);
// Enable the module using the ConfigImporter.
$extensions = $sync->read('core.extension');
$extensions['module']['default_content_test'] = 0;
$extensions['module'] = module_config_sort($extensions['module']);
$sync->write('core.extension', $extensions);
// Slightly hacky but we need the config from the test module too.
$module_storage = new FileStorage(drupal_get_path('module', 'default_content_test') . '/config/install');
foreach ($module_storage->listAll() as $name) {
$sync->write($name, $module_storage->read($name));
}
$this->configImporter()->import();
$this->doPostInstallTests();
}
/**
* Makes assertions post the install of the default_content_test module.
*/
protected function doPostInstallTests() {
// Login as admin.
$this->drupalLogin($this->drupalCreateUser([], NULL, TRUE));
// Ensure the content contained in the default_content_test module has been
// created correctly.
$node = $this->getNodeByTitle('Imported node'); $node = $this->getNodeByTitle('Imported node');
$this->assertEquals($node->body->value, 'Crikey it works!'); $this->assertEquals($node->body->value, 'Crikey it works!');
$this->assertEquals($node->getType(), 'page'); $this->assertEquals($node->getType(), 'page');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment