#620688 follow-up by chx and effulgentsia: Fixed drupal_static_reset() is broken, with expanded test coverage.
parent
7fb94bda0d
commit
e1d4dc7d6e
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue