Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-10-02-11-18-44
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
index 77a8c2ae59e2da857f5ac30e8d3eccba92923a75..f43dc77ab87cc0a95f90aa98fdbf664e409fc416 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -30,27 +30,60 @@ require_once 'CiviTest/CiviUnitTestCase.php';
 class CiviReportTestCase extends CiviUnitTestCase {
   function setUp() {
     parent::setUp();
+    $this->_sethtmlGlobals();
+  }
+
+  function tearDown() {
+    // TODO Figure out how to automatically drop all temporary tables.
+    // Note that MySQL doesn't provide a way to list them, so we would need
+    // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
+    // the MySQL connection between test runs.
+    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;
+    $tmpReportVal = explode('_', $reportClass);
+    $reportName = array_pop($tmpReportVal);
+    $reportObj =& $controller->_pages[$reportName];
+
+    $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) {
+        $tmpGlobals['_GET'][$key] = $val;
+      }
+    }
+    if (!empty($inputParams['group_bys'])) {
+      $groupByFields = implode(' ', $inputParams['group_bys']);
+      $tmpGlobals['_GET']['gby'] = $groupByFields;
     }
 
-    $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) {
+      // print_r($e->getCause()->getUserInfo());
+      CRM_Utils_GlobalStack::singleton()->pop();
+      throw $e;
+    }
+    CRM_Utils_GlobalStack::singleton()->pop();
 
     return $tmpFile;
   }
@@ -66,20 +99,27 @@ class CiviReportTestCase extends CiviUnitTestCase {
     return $arrFile;
   }
 
-  function compareCsvFiles($csvFile1, $csvFile2) {
-    $arrFile1 = $this->getArrayFromCsv($csvFile1);
-    $arrFile2 = $this->getArrayFromCsv($csvFile2);
+  /**
+   * @param array $expectedCsvArray two-dimensional array representing a CSV table
+   * @param array $actualCsvArray two-dimensional array representing a CSV table
+   */
+  public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
+    // TODO provide better debug output
 
-    $intRow = 0;
-    foreach($arrFile1 as $intKey => $strVal) {
-      if (count($strVal) != count($arrFile2[$intKey])) {
-        //FIXME : exit("Column count doesn't match\n");
-      }
-      if (!isset($arrFile2[$intKey]) || ($arrFile2[$intKey] != $strVal)) {
-        //FIXME: exit("Column $intKey, row $intRow of $strFile1 doesn't match\n");
-      }
-      $intRow++;
+    $this->assertEquals(
+      count($actualCsvArray),
+      count($expectedCsvArray),
+      'Arrays have different number of rows; in line ' . __LINE__
+    );
+
+    foreach ($actualCsvArray as $intKey => $strVal) {
+      $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
+      $this->assertEquals(
+        count($actualCsvArray[$intKey]),
+        count($expectedCsvArray[$intKey]),
+        'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
+      );
+      $this->assertEquals($expectedCsvArray[$intKey], $strVal);
     }
-    // FIXME: print "All rows match fine.\n";
   }
 }