CRM-12622 - CiviReportTestCase - Set missing global variables
authorTim Otten <totten@civicrm.org>
Tue, 30 Jul 2013 03:18:47 +0000 (20:18 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 30 Jul 2013 03:18:47 +0000 (20:18 -0700)
----------------------------------------
* CRM-12622: Create framework for example report data files
  http://issues.civicrm.org/jira/browse/CRM-12622

CRM/Utils/GlobalStack.php [new file with mode: 0644]
tests/phpunit/CRM/Utils/GlobalStackTest.php [new file with mode: 0644]
tests/phpunit/CiviTest/CiviReportTestCase.php

diff --git a/CRM/Utils/GlobalStack.php b/CRM/Utils/GlobalStack.php
new file mode 100644 (file)
index 0000000..6dedf15
--- /dev/null
@@ -0,0 +1,97 @@
+<?php /*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.3                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2013                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+*/
+
+/**
+ * Temporarily change a global variable.
+ *
+ * @code
+ * $globals = CRM_Utils_GlobalStack::singleton();
+ * $globals->push(array(
+ *   '_GET' => array(
+ *     'q' => 'some-value
+ *   ),
+ * ));
+ * ...do stuff...
+ * $globals->pop();
+ * @endcode
+ *
+ * Note: for purposes of this class, we'll refer to the array passed into
+ * push() as a frame.
+ */
+class CRM_Utils_GlobalStack {
+  /**
+   * We don't have a container or dependency-injection, so use singleton instead
+   *
+   * @var object
+   * @static
+   */
+  private static $_singleton = NULL;
+
+  private $backups = array();
+
+  /**
+   * Get or set the single instance of CRM_Utils_GlobalStack
+   *
+   * @return CRM_Utils_GlobalStack
+   */
+  static public function singleton() {
+    if (self::$_singleton === NULL) {
+      self::$_singleton = new CRM_Utils_GlobalStack();
+    }
+    return self::$_singleton;
+  }
+
+  public function push($newFrame) {
+    $this->backups[] = $this->createBackup($newFrame);
+    $this->applyFrame($newFrame);
+  }
+
+  public function pop() {
+    $this->applyFrame(array_pop($this->backups));
+  }
+
+  /**
+   * @param array $new the new, incoming frame
+   * @return array frame
+   */
+  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]);
+      }
+    }
+    return $frame;
+  }
+
+  public function applyFrame($newFrame) {
+    foreach ($newFrame as $globalKey => $values) {
+      foreach ($values as $key => $value) {
+        $GLOBALS[$globalKey][$key] = $value;
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/tests/phpunit/CRM/Utils/GlobalStackTest.php b/tests/phpunit/CRM/Utils/GlobalStackTest.php
new file mode 100644 (file)
index 0000000..2100830
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+class CRM_Utils_GlobalStackTest extends CiviUnitTestCase {
+
+  public function testPushPop() {
+    global $FOO;
+
+    $FOO['bar'] = 1;
+    $FOO['whiz'] = 1;
+
+    $this->assertEquals(1, $FOO['bar']);
+    $this->assertEquals(1, $FOO['whiz']);
+    $this->assertFalse(isset($FOO['bang']));
+
+    CRM_Utils_GlobalStack::singleton()->push(array(
+      'FOO' => array(
+        'bar' => 2,
+        'bang' => 2,
+      ),
+    ));
+
+    $this->assertEquals(2, $FOO['bar']);
+    $this->assertEquals(1, $FOO['whiz']);
+    $this->assertEquals(2, $FOO['bang']);
+
+    CRM_Utils_GlobalStack::singleton()->pop();
+
+    $this->assertEquals(1, $FOO['bar']);
+    $this->assertEquals(1, $FOO['whiz']);
+    $this->assertEquals(NULL, $FOO['bang']);
+  }
+}
index 5814721645565a7e2ea0dd90510c3d3ea9e9d1b0..9a4c7b8530a547079ab63983ba108fdd40c16583 100644 (file)
@@ -32,29 +32,45 @@ class CiviReportTestCase extends CiviUnitTestCase {
     parent::setUp();
   }
 
+  function tearDown() {
+    parent::tearDown();
+  }
+
   function getReportOutputAsCsv($reportClass, $inputParams) {
     $config = CRM_Core_Config::singleton();
     $config->keyDisable = TRUE;
     $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
     $reportObj =& $controller->_pages['Detail'];//FIXME - Detail is going to change
-    $_REQUEST['force'] = 1;
+
+    $tmpGlobals = array();
+    $tmpGlobals['_REQUEST']['force'] = 1;
+    $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
+    $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
     if (!empty($inputParams['fields'])) {
       $fields = implode(',', $inputParams['fields']);
-      $_GET['fld']  = $fields;
-      $_GET['ufld'] = 1;
+      $tmpGlobals['_GET']['fld']  = $fields;
+      $tmpGlobals['_GET']['ufld'] = 1;
     }
     if (!empty($inputParams['filters'])) {
       foreach ($inputParams['filters'] as $key => $val) {
-        $_GET[$key] = $val;
+        $tmpGlobals['_GET'][$key] = $val;
       }
     }
-    $reportObj->storeResultSet();
-    $reportObj->buildForm();
-    $rows = $reportObj->getResultSet();
+    CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
 
-    $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
-    $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
-    file_put_contents($tmpFile, $csvContent);
+    try {
+      $reportObj->storeResultSet();
+      $reportObj->buildForm();
+      $rows = $reportObj->getResultSet();
+
+      $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
+      $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
+      file_put_contents($tmpFile, $csvContent);
+    } catch (Exception $e) {
+      CRM_Utils_GlobalStack::singleton()->pop();
+      throw $e;
+    }
+    CRM_Utils_GlobalStack::singleton()->pop();
 
     return $tmpFile;
   }