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