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