Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-30-12-58-23
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 class CiviReportTestCase extends CiviUnitTestCase {
31 function setUp() {
32 parent::setUp();
33 $this->_sethtmlGlobals();
34 }
35
36 function tearDown() {
37 // TODO Figure out how to automatically drop all temporary tables.
38 // Note that MySQL doesn't provide a way to list them, so we would need
39 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
40 // the MySQL connection between test runs.
41 parent::tearDown();
42 }
43
44 function getReportOutputAsCsv($reportClass, $inputParams) {
45 $config = CRM_Core_Config::singleton();
46 $config->keyDisable = TRUE;
47 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
48 $tmpReportVal = explode('_', $reportClass);
49 $reportName = array_pop($tmpReportVal);
50 $reportObj =& $controller->_pages[$reportName];
51
52 $tmpGlobals = array();
53 $tmpGlobals['_REQUEST']['force'] = 1;
54 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
55 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
56 if (!empty($inputParams['fields'])) {
57 $fields = implode(',', $inputParams['fields']);
58 $tmpGlobals['_GET']['fld'] = $fields;
59 $tmpGlobals['_GET']['ufld'] = 1;
60 }
61 if (!empty($inputParams['filters'])) {
62 foreach ($inputParams['filters'] as $key => $val) {
63 $tmpGlobals['_GET'][$key] = $val;
64 }
65 }
66 if (!empty($inputParams['group_bys'])) {
67 $groupByFields = implode(' ', $inputParams['group_bys']);
68 $tmpGlobals['_GET']['gby'] = $groupByFields;
69 }
70
71 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
72
73 try {
74 $reportObj->storeResultSet();
75 $reportObj->buildForm();
76 $rows = $reportObj->getResultSet();
77
78 $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
79 $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
80 file_put_contents($tmpFile, $csvContent);
81 } catch (Exception $e) {
82 // print_r($e->getCause()->getUserInfo());
83 CRM_Utils_GlobalStack::singleton()->pop();
84 throw $e;
85 }
86 CRM_Utils_GlobalStack::singleton()->pop();
87
88 return $tmpFile;
89 }
90
91 function getArrayFromCsv($csvFile) {
92 $arrFile = array();
93 if (($handle = fopen($csvFile, "r")) !== FALSE) {
94 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
95 $arrFile[] = $data;
96 }
97 fclose($handle);
98 }
99 return $arrFile;
100 }
101
102 /**
103 * @param array $expectedCsvArray two-dimensional array representing a CSV table
104 * @param array $actualCsvArray two-dimensional array representing a CSV table
105 */
106 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
107 // TODO provide better debug output
108
109 $flatData = "\n===== EXPECTED DATA ====\n"
110 . $this->flattenCsvArray($expectedCsvArray)
111 . "\n===== ACTUAL DATA ====\n"
112 . $this->flattenCsvArray($actualCsvArray);
113
114 $this->assertEquals(
115 count($actualCsvArray),
116 count($expectedCsvArray),
117 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
118 );
119
120 foreach ($actualCsvArray as $intKey => $strVal) {
121 $rowData = var_export(array(
122 'expected' => $expectedCsvArray[$intKey],
123 'actual' => $actualCsvArray[$intKey],
124 ), TRUE);
125 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
126 $this->assertEquals(
127 count($actualCsvArray[$intKey]),
128 count($expectedCsvArray[$intKey]),
129 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__. '; data: ' . $rowData
130 );
131 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
132 }
133 }
134
135 public function flattenCsvArray($rows) {
136 $result = '';
137 foreach ($rows as $row) {
138 $result .= implode(',', $row) . "\n";
139 }
140 return $result;
141 }
142 }