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