Merge pull request #14470 from eileenmcnaughton/alike
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
CommitLineData
ae555e90
DS
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
ae555e90 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
ae555e90
DS
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
ae555e90 27
e9479dcf
EM
28/**
29 * Class CiviReportTestCase
30 */
ae555e90 31class CiviReportTestCase extends CiviUnitTestCase {
39b959db 32
00be9182 33 public function setUp() {
ae555e90 34 parent::setUp();
5c2e58ae 35 $this->_sethtmlGlobals();
ae555e90
DS
36 }
37
00be9182 38 public function tearDown() {
2d71e99f
TO
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.
745bc660
EM
43
44 $this->quickCleanup($this->_tablesToTruncate);
6d4b9264
TO
45 parent::tearDown();
46 }
47
4cbe18b8
EM
48 /**
49 * @param $reportClass
100fef9d 50 * @param array $inputParams
4cbe18b8
EM
51 *
52 * @return string
53 * @throws Exception
54 */
00be9182 55 public function getReportOutputAsCsv($reportClass, $inputParams) {
2caf0feb
JM
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) {
ae555e90
DS
78 $config = CRM_Core_Config::singleton();
79 $config->keyDisable = TRUE;
80 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
a76480e7
RN
81 $tmpReportVal = explode('_', $reportClass);
82 $reportName = array_pop($tmpReportVal);
83 $reportObj =& $controller->_pages[$reportName];
6d4b9264
TO
84
85 $tmpGlobals = array();
86 $tmpGlobals['_REQUEST']['force'] = 1;
87 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
88 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
ae555e90
DS
89 if (!empty($inputParams['fields'])) {
90 $fields = implode(',', $inputParams['fields']);
4be6c8f8 91 $tmpGlobals['_GET']['fld'] = $fields;
6d4b9264 92 $tmpGlobals['_GET']['ufld'] = 1;
ae555e90 93 }
ec24e302
DS
94 if (!empty($inputParams['filters'])) {
95 foreach ($inputParams['filters'] as $key => $val) {
6d4b9264 96 $tmpGlobals['_GET'][$key] = $val;
ec24e302
DS
97 }
98 }
a76480e7
RN
99 if (!empty($inputParams['group_bys'])) {
100 $groupByFields = implode(' ', $inputParams['group_bys']);
101 $tmpGlobals['_GET']['gby'] = $groupByFields;
102 }
103
6d4b9264 104 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
ae555e90 105
6d4b9264
TO
106 try {
107 $reportObj->storeResultSet();
108 $reportObj->buildForm();
0db6c3e1
TO
109 }
110 catch (Exception $e) {
2d71e99f 111 // print_r($e->getCause()->getUserInfo());
6d4b9264
TO
112 CRM_Utils_GlobalStack::singleton()->pop();
113 throw $e;
114 }
115 CRM_Utils_GlobalStack::singleton()->pop();
ae555e90 116
2caf0feb 117 return $reportObj;
ae555e90
DS
118 }
119
4cbe18b8
EM
120 /**
121 * @param $csvFile
122 *
123 * @return array
124 */
00be9182 125 public function getArrayFromCsv($csvFile) {
ae555e90
DS
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 }
3b608e05
TO
135
136 /**
e16033b4
TO
137 * @param array $expectedCsvArray
138 * Two-dimensional array representing a CSV table.
139 * @param array $actualCsvArray
140 * Two-dimensional array representing a CSV table.
3b608e05
TO
141 */
142 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
143 // TODO provide better debug output
144
64aeb844
TO
145 $flatData = "\n===== EXPECTED DATA ====\n"
146 . $this->flattenCsvArray($expectedCsvArray)
147 . "\n===== ACTUAL DATA ====\n"
148 . $this->flattenCsvArray($actualCsvArray);
149
3b608e05
TO
150 $this->assertEquals(
151 count($actualCsvArray),
152 count($expectedCsvArray),
64aeb844 153 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
3b608e05
TO
154 );
155
156 foreach ($actualCsvArray as $intKey => $strVal) {
64aeb844
TO
157 $rowData = var_export(array(
158 'expected' => $expectedCsvArray[$intKey],
92915c55 159 'actual' => $actualCsvArray[$intKey],
64aeb844 160 ), TRUE);
ba4a1892 161 $this->assertNotNull($expectedCsvArray[$intKey]);
3b608e05
TO
162 $this->assertEquals(
163 count($actualCsvArray[$intKey]),
164 count($expectedCsvArray[$intKey]),
86bfa4f6 165 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData
3b608e05
TO
166 );
167 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
168 }
169 }
64aeb844 170
4cbe18b8
EM
171 /**
172 * @param $rows
173 *
174 * @return string
175 */
64aeb844
TO
176 public function flattenCsvArray($rows) {
177 $result = '';
178 foreach ($rows as $row) {
179 $result .= implode(',', $row) . "\n";
180 }
181 return $result;
182 }
96025800 183
ae555e90 184}