Commit | Line | Data |
---|---|---|
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 | 15 | class 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 | |
d5d08fab | 32 | * @throws \CRM_Core_Exception |
4cbe18b8 | 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 |
d5d08fab | 49 | * @throws \CRM_Core_Exception |
2caf0feb | 50 | */ |
d5d08fab | 51 | public function getReportObject(string $reportClass, array $inputParams): CRM_Report_Form { |
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 | 83 | } |
d5d08fab | 84 | catch (CRM_Core_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 | ||
d5d08fab EM |
124 | $this->assertCount( |
125 | count($actualCsvArray), $expectedCsvArray, 'Arrays have different number of rows; data: ' . $flatData | |
3b608e05 TO |
126 | ); |
127 | ||
128 | foreach ($actualCsvArray as $intKey => $strVal) { | |
64aeb844 TO |
129 | $rowData = var_export(array( |
130 | 'expected' => $expectedCsvArray[$intKey], | |
92915c55 | 131 | 'actual' => $actualCsvArray[$intKey], |
64aeb844 | 132 | ), TRUE); |
ba4a1892 | 133 | $this->assertNotNull($expectedCsvArray[$intKey]); |
3b608e05 TO |
134 | $this->assertEquals( |
135 | count($actualCsvArray[$intKey]), | |
136 | count($expectedCsvArray[$intKey]), | |
86bfa4f6 | 137 | 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData |
3b608e05 TO |
138 | ); |
139 | $this->assertEquals($expectedCsvArray[$intKey], $strVal); | |
140 | } | |
141 | } | |
64aeb844 | 142 | |
4cbe18b8 EM |
143 | /** |
144 | * @param $rows | |
145 | * | |
146 | * @return string | |
147 | */ | |
64aeb844 TO |
148 | public function flattenCsvArray($rows) { |
149 | $result = ''; | |
150 | foreach ($rows as $row) { | |
151 | $result .= implode(',', $row) . "\n"; | |
152 | } | |
153 | return $result; | |
154 | } | |
96025800 | 155 | |
ae555e90 | 156 | } |