From e1d4dc7d6e37749e4642cbcaf236f26d01c9a0bc Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Sun, 15 Nov 2009 21:41:06 +0000
Subject: [PATCH] #620688 follow-up by chx and effulgentsia: Fixed
 drupal_static_reset() is broken, with expanded test coverage.

---
 includes/bootstrap.inc                  |  8 +++--
 modules/simpletest/tests/bootstrap.test | 44 +++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 81598b5c9a01..bc13133ca1f0 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -2100,8 +2100,12 @@ function registry_rebuild() {
 function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
   static $data = array(), $default = array();
   if (!isset($name)) {
-    // All variables are reset.
-    $data = $default;
+    // All variables are reset. This needs to be done one at a time so that
+    // references returned by earlier invocations of drupal_static() also get
+    // reset.
+    foreach ($default as $name => $value) {
+      $data[$name] = $value;
+    }
     // As the function returns a reference, the return should always be a
     // variable.
     return $data;
diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test
index 18d671b39873..e4ef2d717b47 100644
--- a/modules/simpletest/tests/bootstrap.test
+++ b/modules/simpletest/tests/bootstrap.test
@@ -374,3 +374,47 @@ class BootstrapTimerTestCase extends DrupalUnitTestCase {
     $this->assertEqual($timer['count'], 2, t('Timer counted 2 instances of being started.'));
   }
 }
+
+/**
+ * Test that resetting static variables works.
+ */
+class BootstrapResettableStaticTestCase extends DrupalUnitTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Resettable static variables test',
+      'description' => 'Test that drupal_static() and drupal_static_reset() work.',
+      'group' => 'Bootstrap',
+    );
+  }
+
+  /**
+   * Test that a variable reference returned by drupal_static() gets reset when
+   * drupal_static_reset() is called. 
+   */
+  function testDrupalStatic() {
+    $name = __CLASS__ . '_' . __METHOD__;
+    $var = &drupal_static($name, 'foo');
+    $this->assertEqual($var, 'foo', t('Variable returned by drupal_static() was set to its default.'));
+
+    // Call the specific reset and the global reset each twice to ensure that
+    // multiple resets can be issued without odd side effects.
+    $var = 'bar';
+    drupal_static_reset($name);
+    $this->assertEqual($var, 'foo', t('Variable was reset after first invocation of name-specific reset.'));
+    $var = 'bar';
+    drupal_static_reset($name);
+    $this->assertEqual($var, 'foo', t('Variable was reset after second invocation of name-specific reset.'));
+
+    // Ensure that batch processing doesn't get reset.
+    $batch = &batch_get();
+    $batch_saved = $batch;
+    $var = 'bar';
+    drupal_static_reset();
+    $this->assertEqual($var, 'foo', t('Variable was reset after first invocation of global reset.'));
+    $var = 'bar';
+    drupal_static_reset();
+    $this->assertEqual($var, 'foo', t('Variable was reset after second invocation of global reset.'));
+    $batch = $batch_saved;
+  }
+}
-- 
GitLab