summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/bootstrap.inc8
-rw-r--r--modules/simpletest/tests/bootstrap.test44
2 files changed, 50 insertions, 2 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 81598b5c9..bc13133ca 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 18d671b39..e4ef2d717 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;
+ }
+}