5e4920cd2b2f417dbc0f81b63d6179a4a7de1346
[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
42 $this->quickCleanup($this->_tablesToTruncate);
43 parent::tearDown();
44 }
45
46 /**
47 * @param $reportClass
48 * @param $inputParams
49 *
50 * @return string
51 * @throws Exception
52 */
53 function getReportOutputAsCsv($reportClass, $inputParams) {
54 $config = CRM_Core_Config::singleton();
55 $config->keyDisable = TRUE;
56 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
57 $tmpReportVal = explode('_', $reportClass);
58 $reportName = array_pop($tmpReportVal);
59 $reportObj =& $controller->_pages[$reportName];
60
61 $tmpGlobals = array();
62 $tmpGlobals['_REQUEST']['force'] = 1;
63 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
64 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
65 if (!empty($inputParams['fields'])) {
66 $fields = implode(',', $inputParams['fields']);
67 $tmpGlobals['_GET']['fld'] = $fields;
68 $tmpGlobals['_GET']['ufld'] = 1;
69 }
70 if (!empty($inputParams['filters'])) {
71 foreach ($inputParams['filters'] as $key => $val) {
72 $tmpGlobals['_GET'][$key] = $val;
73 }
74 }
75 if (!empty($inputParams['group_bys'])) {
76 $groupByFields = implode(' ', $inputParams['group_bys']);
77 $tmpGlobals['_GET']['gby'] = $groupByFields;
78 }
79
80 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
81
82 try {
83 $reportObj->storeResultSet();
84 $reportObj->buildForm();
85 $rows = $reportObj->getResultSet();
86
87 $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
88 $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
89 file_put_contents($tmpFile, $csvContent);
90 } catch (Exception $e) {
91 // print_r($e->getCause()->getUserInfo());
92 CRM_Utils_GlobalStack::singleton()->pop();
93 throw $e;
94 }
95 CRM_Utils_GlobalStack::singleton()->pop();
96
97 return $tmpFile;
98 }
99
100 /**
101 * @param $csvFile
102 *
103 * @return array
104 */
105 function getArrayFromCsv($csvFile) {
106 $arrFile = array();
107 if (($handle = fopen($csvFile, "r")) !== FALSE) {
108 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
109 $arrFile[] = $data;
110 }
111 fclose($handle);
112 }
113 return $arrFile;
114 }
115
116 /**
117 * @param array $expectedCsvArray two-dimensional array representing a CSV table
118 * @param array $actualCsvArray two-dimensional array representing a CSV table
119 */
120 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
121 // TODO provide better debug output
122
123 $flatData = "\n===== EXPECTED DATA ====\n"
124 . $this->flattenCsvArray($expectedCsvArray)
125 . "\n===== ACTUAL DATA ====\n"
126 . $this->flattenCsvArray($actualCsvArray);
127
128 $this->assertEquals(
129 count($actualCsvArray),
130 count($expectedCsvArray),
131 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
132 );
133
134 foreach ($actualCsvArray as $intKey => $strVal) {
135 $rowData = var_export(array(
136 'expected' => $expectedCsvArray[$intKey],
137 'actual' => $actualCsvArray[$intKey],
138 ), TRUE);
139 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
140 $this->assertEquals(
141 count($actualCsvArray[$intKey]),
142 count($expectedCsvArray[$intKey]),
143 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__. '; data: ' . $rowData
144 );
145 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
146 }
147 }
148
149 /**
150 * @param $rows
151 *
152 * @return string
153 */
154 public function flattenCsvArray($rows) {
155 $result = '';
156 foreach ($rows as $row) {
157 $result .= implode(',', $row) . "\n";
158 }
159 return $result;
160 }
161 }