Commit | Line | Data |
---|---|---|
ae555e90 DS |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
ae555e90 | 5 | +--------------------------------------------------------------------+ |
15a4309a | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
ae555e90 DS |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
ae555e90 | 27 | |
e9479dcf EM |
28 | /** |
29 | * Class CiviReportTestCase | |
30 | */ | |
ae555e90 | 31 | class CiviReportTestCase extends CiviUnitTestCase { |
00be9182 | 32 | public function setUp() { |
ae555e90 | 33 | parent::setUp(); |
5c2e58ae | 34 | $this->_sethtmlGlobals(); |
ae555e90 DS |
35 | } |
36 | ||
00be9182 | 37 | public function tearDown() { |
2d71e99f TO |
38 | // TODO Figure out how to automatically drop all temporary tables. |
39 | // Note that MySQL doesn't provide a way to list them, so we would need | |
40 | // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset | |
41 | // the MySQL connection between test runs. | |
745bc660 EM |
42 | |
43 | $this->quickCleanup($this->_tablesToTruncate); | |
6d4b9264 TO |
44 | parent::tearDown(); |
45 | } | |
46 | ||
4cbe18b8 EM |
47 | /** |
48 | * @param $reportClass | |
100fef9d | 49 | * @param array $inputParams |
4cbe18b8 EM |
50 | * |
51 | * @return string | |
52 | * @throws Exception | |
53 | */ | |
00be9182 | 54 | public function getReportOutputAsCsv($reportClass, $inputParams) { |
2caf0feb JM |
55 | |
56 | $reportObj = $this->getReportObject($reportClass, $inputParams); | |
57 | try { | |
58 | $rows = $reportObj->getResultSet(); | |
59 | $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv'); | |
60 | $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows); | |
61 | file_put_contents($tmpFile, $csvContent); | |
62 | } | |
63 | catch (Exception $e) { | |
64 | throw $e; | |
65 | } | |
66 | return $tmpFile; | |
67 | } | |
68 | ||
69 | /** | |
70 | * @param $reportClass | |
71 | * @param array $inputParams | |
72 | * | |
73 | * @return array | |
74 | * @throws Exception | |
75 | */ | |
76 | public function getReportObject($reportClass, $inputParams) { | |
ae555e90 DS |
77 | $config = CRM_Core_Config::singleton(); |
78 | $config->keyDisable = TRUE; | |
79 | $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title')); | |
a76480e7 RN |
80 | $tmpReportVal = explode('_', $reportClass); |
81 | $reportName = array_pop($tmpReportVal); | |
82 | $reportObj =& $controller->_pages[$reportName]; | |
6d4b9264 TO |
83 | |
84 | $tmpGlobals = array(); | |
85 | $tmpGlobals['_REQUEST']['force'] = 1; | |
86 | $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder'; | |
87 | $tmpGlobals['_SERVER']['QUERY_STRING'] = ''; | |
ae555e90 DS |
88 | if (!empty($inputParams['fields'])) { |
89 | $fields = implode(',', $inputParams['fields']); | |
4be6c8f8 | 90 | $tmpGlobals['_GET']['fld'] = $fields; |
6d4b9264 | 91 | $tmpGlobals['_GET']['ufld'] = 1; |
ae555e90 | 92 | } |
ec24e302 DS |
93 | if (!empty($inputParams['filters'])) { |
94 | foreach ($inputParams['filters'] as $key => $val) { | |
6d4b9264 | 95 | $tmpGlobals['_GET'][$key] = $val; |
ec24e302 DS |
96 | } |
97 | } | |
a76480e7 RN |
98 | if (!empty($inputParams['group_bys'])) { |
99 | $groupByFields = implode(' ', $inputParams['group_bys']); | |
100 | $tmpGlobals['_GET']['gby'] = $groupByFields; | |
101 | } | |
102 | ||
6d4b9264 | 103 | CRM_Utils_GlobalStack::singleton()->push($tmpGlobals); |
ae555e90 | 104 | |
6d4b9264 TO |
105 | try { |
106 | $reportObj->storeResultSet(); | |
107 | $reportObj->buildForm(); | |
0db6c3e1 TO |
108 | } |
109 | catch (Exception $e) { | |
2d71e99f | 110 | // print_r($e->getCause()->getUserInfo()); |
6d4b9264 TO |
111 | CRM_Utils_GlobalStack::singleton()->pop(); |
112 | throw $e; | |
113 | } | |
114 | CRM_Utils_GlobalStack::singleton()->pop(); | |
ae555e90 | 115 | |
2caf0feb | 116 | return $reportObj; |
ae555e90 DS |
117 | } |
118 | ||
4cbe18b8 EM |
119 | /** |
120 | * @param $csvFile | |
121 | * | |
122 | * @return array | |
123 | */ | |
00be9182 | 124 | public function getArrayFromCsv($csvFile) { |
ae555e90 DS |
125 | $arrFile = array(); |
126 | if (($handle = fopen($csvFile, "r")) !== FALSE) { | |
127 | while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { | |
128 | $arrFile[] = $data; | |
129 | } | |
130 | fclose($handle); | |
131 | } | |
132 | return $arrFile; | |
133 | } | |
3b608e05 TO |
134 | |
135 | /** | |
e16033b4 TO |
136 | * @param array $expectedCsvArray |
137 | * Two-dimensional array representing a CSV table. | |
138 | * @param array $actualCsvArray | |
139 | * Two-dimensional array representing a CSV table. | |
3b608e05 TO |
140 | */ |
141 | public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) { | |
142 | // TODO provide better debug output | |
143 | ||
64aeb844 TO |
144 | $flatData = "\n===== EXPECTED DATA ====\n" |
145 | . $this->flattenCsvArray($expectedCsvArray) | |
146 | . "\n===== ACTUAL DATA ====\n" | |
147 | . $this->flattenCsvArray($actualCsvArray); | |
148 | ||
3b608e05 TO |
149 | $this->assertEquals( |
150 | count($actualCsvArray), | |
151 | count($expectedCsvArray), | |
64aeb844 | 152 | 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData |
3b608e05 TO |
153 | ); |
154 | ||
155 | foreach ($actualCsvArray as $intKey => $strVal) { | |
64aeb844 TO |
156 | $rowData = var_export(array( |
157 | 'expected' => $expectedCsvArray[$intKey], | |
92915c55 | 158 | 'actual' => $actualCsvArray[$intKey], |
64aeb844 | 159 | ), TRUE); |
ba4a1892 | 160 | $this->assertNotNull($expectedCsvArray[$intKey]); |
3b608e05 TO |
161 | $this->assertEquals( |
162 | count($actualCsvArray[$intKey]), | |
163 | count($expectedCsvArray[$intKey]), | |
86bfa4f6 | 164 | 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData |
3b608e05 TO |
165 | ); |
166 | $this->assertEquals($expectedCsvArray[$intKey], $strVal); | |
167 | } | |
168 | } | |
64aeb844 | 169 | |
4cbe18b8 EM |
170 | /** |
171 | * @param $rows | |
172 | * | |
173 | * @return string | |
174 | */ | |
64aeb844 TO |
175 | public function flattenCsvArray($rows) { |
176 | $result = ''; | |
177 | foreach ($rows as $row) { | |
178 | $result .= implode(',', $row) . "\n"; | |
179 | } | |
180 | return $result; | |
181 | } | |
96025800 | 182 | |
ae555e90 | 183 | } |