From 6ab577bf28ee606ec8a57affea1f07dff1878d75 Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Fri, 9 Mar 2018 07:42:32 +1000
Subject: [PATCH] Issue #2877119 by alexpott, Dane Powell: Cannot uninstall
 Config module via drush config-import

---
 core/modules/config/src/ConfigSubscriber.php  |  5 ++
 .../ConfigUninstallViaCliImportTest.php       | 71 +++++++++++++++++++
 2 files changed, 76 insertions(+)
 create mode 100644 core/modules/config/tests/src/Kernel/ConfigUninstallViaCliImportTest.php

diff --git a/core/modules/config/src/ConfigSubscriber.php b/core/modules/config/src/ConfigSubscriber.php
index 7e4c51ddac37..779bab67ddcb 100644
--- a/core/modules/config/src/ConfigSubscriber.php
+++ b/core/modules/config/src/ConfigSubscriber.php
@@ -18,6 +18,11 @@ class ConfigSubscriber extends ConfigImportValidateEventSubscriberBase {
    *   The config import event.
    */
   public function onConfigImporterValidate(ConfigImporterEvent $event) {
+    // Make sure config syncs performed via the Config UI don't break, but
+    // don't worry about syncs initiated via the command line.
+    if (PHP_SAPI === 'cli') {
+      return;
+    }
     $importer = $event->getConfigImporter();
     $core_extension = $importer->getStorageComparer()->getSourceStorage()->read('core.extension');
     if (!isset($core_extension['module']['config'])) {
diff --git a/core/modules/config/tests/src/Kernel/ConfigUninstallViaCliImportTest.php b/core/modules/config/tests/src/Kernel/ConfigUninstallViaCliImportTest.php
new file mode 100644
index 000000000000..5f77447f316f
--- /dev/null
+++ b/core/modules/config/tests/src/Kernel/ConfigUninstallViaCliImportTest.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Drupal\Tests\config\Kernel;
+
+use Drupal\Core\Config\ConfigImporter;
+use Drupal\Core\Config\StorageComparer;
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * Tests importing configuration from files into active configuration.
+ *
+ * @group config
+ */
+class ConfigUninstallViaCliImportTest extends KernelTestBase {
+  /**
+   * Config Importer object used for testing.
+   *
+   * @var \Drupal\Core\Config\ConfigImporter
+   */
+  protected $configImporter;
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = ['system', 'config'];
+
+  protected function setUp() {
+    parent::setUp();
+    if (PHP_SAPI !== 'cli') {
+      $this->markTestSkipped('This test has to be run from the CLI');
+    }
+
+    $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
+
+    // Set up the ConfigImporter object for testing.
+    $storage_comparer = new StorageComparer(
+      $this->container->get('config.storage.sync'),
+      $this->container->get('config.storage'),
+      $this->container->get('config.manager')
+    );
+    $this->configImporter = new ConfigImporter(
+      $storage_comparer->createChangelist(),
+      $this->container->get('event_dispatcher'),
+      $this->container->get('config.manager'),
+      $this->container->get('lock'),
+      $this->container->get('config.typed'),
+      $this->container->get('module_handler'),
+      $this->container->get('module_installer'),
+      $this->container->get('theme_handler'),
+      $this->container->get('string_translation')
+    );
+  }
+
+  /**
+   * Tests that the config mopdule can be uninstalled via CLI config import.
+   *
+   * @see \Drupal\config\ConfigSubscriber
+   */
+  public function testConfigUninstallViaCli() {
+    $this->assertTrue($this->container->get('module_handler')->moduleExists('config'));
+    $sync = $this->container->get('config.storage.sync');
+    $extensions = $sync->read('core.extension');
+    unset($extensions['module']['config']);
+    $sync->write('core.extension', $extensions);
+    $this->configImporter->reset()->import();
+    $this->assertFalse($this->container->get('module_handler')->moduleExists('config'));
+  }
+
+}
-- 
GitLab