Merge pull request #17806 from demeritcowboy/mail-report-csv-bom
[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
00be9182 17 public function setUp() {
ae555e90 18 parent::setUp();
5c2e58ae 19 $this->_sethtmlGlobals();
ae555e90
DS
20 }
21
00be9182 22 public function tearDown() {
2d71e99f
TO
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.
745bc660
EM
27
28 $this->quickCleanup($this->_tablesToTruncate);
6d4b9264
TO
29 parent::tearDown();
30 }
31
4cbe18b8
EM
32 /**
33 * @param $reportClass
100fef9d 34 * @param array $inputParams
4cbe18b8
EM
35 *
36 * @return string
37 * @throws Exception
38 */
00be9182 39 public function getReportOutputAsCsv($reportClass, $inputParams) {
2caf0feb
JM
40
41 $reportObj = $this->getReportObject($reportClass, $inputParams);
97754826 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);
2caf0feb
JM
46 return $tmpFile;
47 }
48
49 /**
c86f74b1 50 * @param string $reportClass
2caf0feb
JM
51 * @param array $inputParams
52 *
c86f74b1 53 * @return CRM_Report_Form
2caf0feb
JM
54 * @throws Exception
55 */
56 public function getReportObject($reportClass, $inputParams) {
ae555e90
DS
57 $config = CRM_Core_Config::singleton();
58 $config->keyDisable = TRUE;
59 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
a76480e7
RN
60 $tmpReportVal = explode('_', $reportClass);
61 $reportName = array_pop($tmpReportVal);
62 $reportObj =& $controller->_pages[$reportName];
6d4b9264 63
affcc9d2 64 $tmpGlobals = [];
6d4b9264
TO
65 $tmpGlobals['_REQUEST']['force'] = 1;
66 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
67 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
ae555e90
DS
68 if (!empty($inputParams['fields'])) {
69 $fields = implode(',', $inputParams['fields']);
4be6c8f8 70 $tmpGlobals['_GET']['fld'] = $fields;
6d4b9264 71 $tmpGlobals['_GET']['ufld'] = 1;
ae555e90 72 }
ec24e302
DS
73 if (!empty($inputParams['filters'])) {
74 foreach ($inputParams['filters'] as $key => $val) {
6d4b9264 75 $tmpGlobals['_GET'][$key] = $val;
ec24e302
DS
76 }
77 }
a76480e7
RN
78 if (!empty($inputParams['group_bys'])) {
79 $groupByFields = implode(' ', $inputParams['group_bys']);
80 $tmpGlobals['_GET']['gby'] = $groupByFields;
81 }
82
6d4b9264 83 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
ae555e90 84
6d4b9264
TO
85 try {
86 $reportObj->storeResultSet();
87 $reportObj->buildForm();
0db6c3e1
TO
88 }
89 catch (Exception $e) {
2d71e99f 90 // print_r($e->getCause()->getUserInfo());
6d4b9264
TO
91 CRM_Utils_GlobalStack::singleton()->pop();
92 throw $e;
93 }
94 CRM_Utils_GlobalStack::singleton()->pop();
ae555e90 95
2caf0feb 96 return $reportObj;
ae555e90
DS
97 }
98
4cbe18b8
EM
99 /**
100 * @param $csvFile
101 *
102 * @return array
103 */
00be9182 104 public function getArrayFromCsv($csvFile) {
affcc9d2 105 $arrFile = [];
ae555e90
DS
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 }
3b608e05
TO
114
115 /**
e16033b4
TO
116 * @param array $expectedCsvArray
117 * Two-dimensional array representing a CSV table.
118 * @param array $actualCsvArray
119 * Two-dimensional array representing a CSV table.
3b608e05
TO
120 */
121 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
122 // TODO provide better debug output
123
64aeb844
TO
124 $flatData = "\n===== EXPECTED DATA ====\n"
125 . $this->flattenCsvArray($expectedCsvArray)
126 . "\n===== ACTUAL DATA ====\n"
127 . $this->flattenCsvArray($actualCsvArray);
128
3b608e05
TO
129 $this->assertEquals(
130 count($actualCsvArray),
131 count($expectedCsvArray),
64aeb844 132 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
3b608e05
TO
133 );
134
135 foreach ($actualCsvArray as $intKey => $strVal) {
64aeb844
TO
136 $rowData = var_export(array(
137 'expected' => $expectedCsvArray[$intKey],
92915c55 138 'actual' => $actualCsvArray[$intKey],
64aeb844 139 ), TRUE);
ba4a1892 140 $this->assertNotNull($expectedCsvArray[$intKey]);
3b608e05
TO
141 $this->assertEquals(
142 count($actualCsvArray[$intKey]),
143 count($expectedCsvArray[$intKey]),
86bfa4f6 144 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData
3b608e05
TO
145 );
146 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
147 }
148 }
64aeb844 149
4cbe18b8
EM
150 /**
151 * @param $rows
152 *
153 * @return string
154 */
64aeb844
TO
155 public function flattenCsvArray($rows) {
156 $result = '';
157 foreach ($rows as $row) {
158 $result .= implode(',', $row) . "\n";
159 }
160 return $result;
161 }
96025800 162
ae555e90 163}