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