From: Tim Otten Date: Tue, 30 Jul 2013 04:15:28 +0000 (-0700) Subject: CRM_Utils_GlobalStack - Allow top-level vars X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=25c8f5c66c277bcb918c9d9fca8910189e38fb8d;p=civicrm-core.git CRM_Utils_GlobalStack - Allow top-level vars --- diff --git a/CRM/Utils/GlobalStack.php b/CRM/Utils/GlobalStack.php index 6dedf152a0..09dcccc7d9 100644 --- a/CRM/Utils/GlobalStack.php +++ b/CRM/Utils/GlobalStack.php @@ -80,8 +80,12 @@ class CRM_Utils_GlobalStack { public function createBackup($new) { $frame = array(); foreach ($new as $globalKey => $values) { - foreach ($values as $key => $value) { - $frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]); + if (is_array($values)) { + foreach ($values as $key => $value) { + $frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]); + } + } else { + $frame[$globalKey] = CRM_Utils_Array::value($globalKey, $GLOBALS); } } return $frame; @@ -89,8 +93,12 @@ class CRM_Utils_GlobalStack { public function applyFrame($newFrame) { foreach ($newFrame as $globalKey => $values) { - foreach ($values as $key => $value) { - $GLOBALS[$globalKey][$key] = $value; + if (is_array($values)) { + foreach ($values as $key => $value) { + $GLOBALS[$globalKey][$key] = $value; + } + } else { + $GLOBALS[$globalKey] = $values; } } } diff --git a/tests/phpunit/CRM/Utils/GlobalStackTest.php b/tests/phpunit/CRM/Utils/GlobalStackTest.php index 21008307c6..685cfce14b 100644 --- a/tests/phpunit/CRM/Utils/GlobalStackTest.php +++ b/tests/phpunit/CRM/Utils/GlobalStackTest.php @@ -5,30 +5,35 @@ require_once 'CiviTest/CiviUnitTestCase.php'; class CRM_Utils_GlobalStackTest extends CiviUnitTestCase { public function testPushPop() { - global $FOO; + global $FOO, $EXTRA; $FOO['bar'] = 1; $FOO['whiz'] = 1; + $EXTRA = 1; $this->assertEquals(1, $FOO['bar']); $this->assertEquals(1, $FOO['whiz']); $this->assertFalse(isset($FOO['bang'])); + $this->assertEquals(1, $EXTRA); CRM_Utils_GlobalStack::singleton()->push(array( 'FOO' => array( 'bar' => 2, 'bang' => 2, ), + 'EXTRA' => 2, )); $this->assertEquals(2, $FOO['bar']); $this->assertEquals(1, $FOO['whiz']); $this->assertEquals(2, $FOO['bang']); + $this->assertEquals(2, $EXTRA); CRM_Utils_GlobalStack::singleton()->pop(); $this->assertEquals(1, $FOO['bar']); $this->assertEquals(1, $FOO['whiz']); $this->assertEquals(NULL, $FOO['bang']); + $this->assertEquals(1, $EXTRA); } }