From 0085972f1e62162b9c010237e5bd5b9b3bfccc4a Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Tue, 29 Sep 2009 18:08:28 +0000
Subject: [PATCH] #557542 follow-up by catch: Fixed module_implements() caching
 for authenticated users.

---
 includes/module.inc                  | 10 ++++------
 modules/simpletest/tests/module.test | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/includes/module.inc b/includes/module.inc
index 1b6d04cf7e0e..43bd83e1dc0c 100644
--- a/includes/module.inc
+++ b/includes/module.inc
@@ -416,12 +416,10 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) {
  */
 function module_implements_write_cache() {
   $implementations = &drupal_static('module_implements');
-  // Check whether we need to write the cache. We do not want to cache hooks,
-  // which are only invoked on HTTP POST requests. Maybe the page is not
-  // cacheable for other reasons than the HTTP request type as well, but this
-  // does not matter for the hook implementation cache, because nothing breaks
-  // if hook implementations are not cached.
-  if (isset($implementations['#write_cache']) && drupal_page_is_cacheable()) {
+  // Check whether we need to write the cache. We do not want to cache hooks
+  // which are only invoked on HTTP POST requests since these do not need to be
+  // optimized as tightly, and not doing so keeps the cache entry smaller.
+  if (isset($implementations['#write_cache']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD')) {
     unset($implementations['#write_cache']);
     cache_set('module_implements', $implementations);
   }
diff --git a/modules/simpletest/tests/module.test b/modules/simpletest/tests/module.test
index a157c957a775..059672e5c13b 100644
--- a/modules/simpletest/tests/module.test
+++ b/modules/simpletest/tests/module.test
@@ -81,6 +81,24 @@ class ModuleUnitTest extends DrupalWebTestCase {
     ksort($expected_values);
     $this->assertIdentical($expected_values, module_list(FALSE, FALSE, TRUE), t('@condition: module_list() returns correctly sorted results', array('@condition' => $condition)));
   }
+
+  /**
+   * Test module_implements() caching.
+   */
+  function testModuleImplements() {
+    // Clear the cache.
+    cache_clear_all('module_implements', 'cache');
+    $this->assertFalse(cache_get('module_implements'), t('The module implements cache is empty.'));
+    $this->drupalGet('');
+    $this->assertTrue(cache_get('module_implements'), t('The module implements cache is populated after requesting a page.'));
+
+    // Test again with an authenticated user.
+    $this->user = $this->drupalCreateUser();
+    $this->drupalLogin($this->user);
+    cache_clear_all('module_implements', 'cache');
+    $this->drupalGet('');
+    $this->assertTrue(cache_get('module_implements'), t('The module implements cache is populated after requesting a page.'));
+  }
 }
 
 /**
-- 
GitLab