Merge pull request #14443 from eileenmcnaughton/export
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 /**
29 * Class CiviReportTestCase
30 */
31 class CiviReportTestCase extends CiviUnitTestCase {
32
33 public function setUp() {
34 parent::setUp();
35 $this->_sethtmlGlobals();
36 }
37
38 public function tearDown() {
39 // TODO Figure out how to automatically drop all temporary tables.
40 // Note that MySQL doesn't provide a way to list them, so we would need
41 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
42 // the MySQL connection between test runs.
43
44 $this->quickCleanup($this->_tablesToTruncate);
45 parent::tearDown();
46 }
47
48 /**
49 * @param $reportClass
50 * @param array $inputParams
51 *
52 * @return string
53 * @throws Exception
54 */
55 public function getReportOutputAsCsv($reportClass, $inputParams) {
56
57 $reportObj = $this->getReportObject($reportClass, $inputParams);
58 try {
59 $rows = $reportObj->getResultSet();
60 $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
61 $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
62 file_put_contents($tmpFile, $csvContent);
63 }
64 catch (Exception $e) {
65 throw $e;
66 }
67 return $tmpFile;
68 }
69
70 /**
71 * @param $reportClass
72 * @param array $inputParams
73 *
74 * @return array
75 * @throws Exception
76 */
77 public function getReportObject($reportClass, $inputParams) {
78 $config = CRM_Core_Config::singleton();
79 $config->keyDisable = TRUE;
80 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
81 $tmpReportVal = explode('_', $reportClass);
82 $reportName = array_pop($tmpReportVal);
83 $reportObj =& $controller->_pages[$reportName];
84
85 $tmpGlobals = array();
86 $tmpGlobals['_REQUEST']['force'] = 1;
87 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
88 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
89 if (!empty($inputParams['fields'])) {
90 $fields = implode(',', $inputParams['fields']);
91 $tmpGlobals['_GET']['fld'] = $fields;
92 $tmpGlobals['_GET']['ufld'] = 1;
93 }
94 if (!empty($inputParams['filters'])) {
95 foreach ($inputParams['filters'] as $key => $val) {
96 $tmpGlobals['_GET'][$key] = $val;
97 }
98 }
99 if (!empty($inputParams['group_bys'])) {
100 $groupByFields = implode(' ', $inputParams['group_bys']);
101 $tmpGlobals['_GET']['gby'] = $groupByFields;
102 }
103
104 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
105
106 try {
107 $reportObj->storeResultSet();
108 $reportObj->buildForm();
109 }
110 catch (Exception $e) {
111 // print_r($e->getCause()->getUserInfo());
112 CRM_Utils_GlobalStack::singleton()->pop();
113 throw $e;
114 }
115 CRM_Utils_GlobalStack::singleton()->pop();
116
117 return $reportObj;
118 }
119
120 /**
121 * @param $csvFile
122 *
123 * @return array
124 */
125 public function getArrayFromCsv($csvFile) {
126 $arrFile = array();
127 if (($handle = fopen($csvFile, "r")) !== FALSE) {
128 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
129 $arrFile[] = $data;
130 }
131 fclose($handle);
132 }
133 return $arrFile;
134 }
135
136 /**
137 * @param array $expectedCsvArray
138 * Two-dimensional array representing a CSV table.
139 * @param array $actualCsvArray
140 * Two-dimensional array representing a CSV table.
141 */
142 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
143 // TODO provide better debug output
144
145 $flatData = "\n===== EXPECTED DATA ====\n"
146 . $this->flattenCsvArray($expectedCsvArray)
147 . "\n===== ACTUAL DATA ====\n"
148 . $this->flattenCsvArray($actualCsvArray);
149
150 $this->assertEquals(
151 count($actualCsvArray),
152 count($expectedCsvArray),
153 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
154 );
155
156 foreach ($actualCsvArray as $intKey => $strVal) {
157 $rowData = var_export(array(
158 'expected' => $expectedCsvArray[$intKey],
159 'actual' => $actualCsvArray[$intKey],
160 ), TRUE);
161 $this->assertNotNull($expectedCsvArray[$intKey]);
162 $this->assertEquals(
163 count($actualCsvArray[$intKey]),
164 count($expectedCsvArray[$intKey]),
165 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData
166 );
167 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
168 }
169 }
170
171 /**
172 * @param $rows
173 *
174 * @return string
175 */
176 public function flattenCsvArray($rows) {
177 $result = '';
178 foreach ($rows as $row) {
179 $result .= implode(',', $row) . "\n";
180 }
181 return $result;
182 }
183
184 }