From f92cc9d2194864bc9ac6b72cd4fccba714559b10 Mon Sep 17 00:00:00 2001
From: Joris van Eijden <joris.vaneijden@oneshoe.nl>
Date: Fri, 7 Nov 2014 15:55:50 +0100
Subject: [PATCH] Clear all static caches on module install/uninstall.

---
 core/lib/Drupal/Core/Extension/ModuleHandler.php   | 10 +++++---
 .../src/Tests/Module/InstallUninstallTest.php      | 29 ++++++++++++++++++++++
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php
index 028d7d4..0caa90a 100644
--- a/core/lib/Drupal/Core/Extension/ModuleHandler.php
+++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php
@@ -859,6 +859,10 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
         }
         drupal_set_installed_schema_version($module, $version);
 
+        // Clear static caches to ensure that they are refilled including any
+        // data the enabled module provides.
+        drupal_static_reset();
+
         // Record the fact that it was installed.
         $modules_installed[] = $module;
 
@@ -998,10 +1002,8 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
       // Remove any potential cache bins provided by the module.
       $this->removeCacheBins($module);
 
-      // Clear the static cache of system_rebuild_module_data() to pick up the
-      // new module, since it merges the installation status of modules into
-      // its statically cached list.
-      drupal_static_reset('system_rebuild_module_data');
+      // Empty static caches so any data from the disabled modules is gone.
+      drupal_static_reset();
 
       // Clear plugin manager caches and flag router to rebuild if requested.
       \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
diff --git a/core/modules/system/src/Tests/Module/InstallUninstallTest.php b/core/modules/system/src/Tests/Module/InstallUninstallTest.php
index 43e16f4..8e96c85 100644
--- a/core/modules/system/src/Tests/Module/InstallUninstallTest.php
+++ b/core/modules/system/src/Tests/Module/InstallUninstallTest.php
@@ -181,4 +181,33 @@ protected function assertSuccessfullUninstall($module, $package = 'Core') {
     $this->assertNoModuleConfig($module);
   }
 
+  /**
+   * Tests refreshing of static data when modules are installed or uninstalled.
+   */
+  public function testStaticReset() {
+    // Get data from hook_test.
+    $first = $this->retrieveStaticTestHookData();
+    // Enable a module that implements that hook.
+    $this->container->get('module_handler')->install(array('module_test'));
+    // Get the data again.
+    $second = $this->retrieveStaticTestHookData();
+    // Assert that the data has changed.
+    $this->assertNotEqual($first, $second, 'Static data was reset when installing a module.');
+    $this->container->get('module_handler')->uninstall(array('module_test'));
+    $third = $this->retrieveStaticTestHookData();
+    // Assert that the data has changed again.
+    $this->assertNotEqual($second, $third, 'Static data was reset when uninstalling a module.');
+  }
+
+  /**
+   * Return the statically cached result of the test_hook implementations.
+   */
+  protected function retrieveStaticTestHookData() {
+    $data = & drupal_static(__FUNCTION__);
+    if (!isset($data)) {
+      $data = \Drupal::moduleHandler()->invokeAll('test_hook');
+    }
+    return $data;
+  }
+
 }
-- 
1.9.4.msysgit.0

