Commit | Line | Data |
---|---|---|
ae555e90 DS |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
ae555e90 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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) { |
ae555e90 DS |
55 | $config = CRM_Core_Config::singleton(); |
56 | $config->keyDisable = TRUE; | |
57 | $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title')); | |
a76480e7 RN |
58 | $tmpReportVal = explode('_', $reportClass); |
59 | $reportName = array_pop($tmpReportVal); | |
60 | $reportObj =& $controller->_pages[$reportName]; | |
6d4b9264 TO |
61 | |
62 | $tmpGlobals = array(); | |
63 | $tmpGlobals['_REQUEST']['force'] = 1; | |
64 | $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder'; | |
65 | $tmpGlobals['_SERVER']['QUERY_STRING'] = ''; | |
ae555e90 DS |
66 | if (!empty($inputParams['fields'])) { |
67 | $fields = implode(',', $inputParams['fields']); | |
4be6c8f8 | 68 | $tmpGlobals['_GET']['fld'] = $fields; |
6d4b9264 | 69 | $tmpGlobals['_GET']['ufld'] = 1; |
ae555e90 | 70 | } |
ec24e302 DS |
71 | if (!empty($inputParams['filters'])) { |
72 | foreach ($inputParams['filters'] as $key => $val) { | |
6d4b9264 | 73 | $tmpGlobals['_GET'][$key] = $val; |
ec24e302 DS |
74 | } |
75 | } | |
a76480e7 RN |
76 | if (!empty($inputParams['group_bys'])) { |
77 | $groupByFields = implode(' ', $inputParams['group_bys']); | |
78 | $tmpGlobals['_GET']['gby'] = $groupByFields; | |
79 | } | |
80 | ||
6d4b9264 | 81 | CRM_Utils_GlobalStack::singleton()->push($tmpGlobals); |
ae555e90 | 82 | |
6d4b9264 TO |
83 | try { |
84 | $reportObj->storeResultSet(); | |
85 | $reportObj->buildForm(); | |
86 | $rows = $reportObj->getResultSet(); | |
87 | ||
88 | $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv'); | |
89 | $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows); | |
90 | file_put_contents($tmpFile, $csvContent); | |
0db6c3e1 TO |
91 | } |
92 | catch (Exception $e) { | |
2d71e99f | 93 | // print_r($e->getCause()->getUserInfo()); |
6d4b9264 TO |
94 | CRM_Utils_GlobalStack::singleton()->pop(); |
95 | throw $e; | |
96 | } | |
97 | CRM_Utils_GlobalStack::singleton()->pop(); | |
ae555e90 DS |
98 | |
99 | return $tmpFile; | |
100 | } | |
101 | ||
4cbe18b8 EM |
102 | /** |
103 | * @param $csvFile | |
104 | * | |
105 | * @return array | |
106 | */ | |
00be9182 | 107 | public function getArrayFromCsv($csvFile) { |
ae555e90 DS |
108 | $arrFile = array(); |
109 | if (($handle = fopen($csvFile, "r")) !== FALSE) { | |
110 | while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { | |
111 | $arrFile[] = $data; | |
112 | } | |
113 | fclose($handle); | |
114 | } | |
115 | return $arrFile; | |
116 | } | |
3b608e05 TO |
117 | |
118 | /** | |
e16033b4 TO |
119 | * @param array $expectedCsvArray |
120 | * Two-dimensional array representing a CSV table. | |
121 | * @param array $actualCsvArray | |
122 | * Two-dimensional array representing a CSV table. | |
3b608e05 TO |
123 | */ |
124 | public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) { | |
125 | // TODO provide better debug output | |
126 | ||
64aeb844 TO |
127 | $flatData = "\n===== EXPECTED DATA ====\n" |
128 | . $this->flattenCsvArray($expectedCsvArray) | |
129 | . "\n===== ACTUAL DATA ====\n" | |
130 | . $this->flattenCsvArray($actualCsvArray); | |
131 | ||
3b608e05 TO |
132 | $this->assertEquals( |
133 | count($actualCsvArray), | |
134 | count($expectedCsvArray), | |
64aeb844 | 135 | 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData |
3b608e05 TO |
136 | ); |
137 | ||
138 | foreach ($actualCsvArray as $intKey => $strVal) { | |
64aeb844 TO |
139 | $rowData = var_export(array( |
140 | 'expected' => $expectedCsvArray[$intKey], | |
92915c55 | 141 | 'actual' => $actualCsvArray[$intKey], |
64aeb844 | 142 | ), TRUE); |
ba4a1892 | 143 | $this->assertNotNull($expectedCsvArray[$intKey]); |
3b608e05 TO |
144 | $this->assertEquals( |
145 | count($actualCsvArray[$intKey]), | |
146 | count($expectedCsvArray[$intKey]), | |
86bfa4f6 | 147 | 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData |
3b608e05 TO |
148 | ); |
149 | $this->assertEquals($expectedCsvArray[$intKey], $strVal); | |
150 | } | |
151 | } | |
64aeb844 | 152 | |
4cbe18b8 EM |
153 | /** |
154 | * @param $rows | |
155 | * | |
156 | * @return string | |
157 | */ | |
64aeb844 TO |
158 | public function flattenCsvArray($rows) { |
159 | $result = ''; | |
160 | foreach ($rows as $row) { | |
161 | $result .= implode(',', $row) . "\n"; | |
162 | } | |
163 | return $result; | |
164 | } | |
96025800 | 165 | |
ae555e90 | 166 | } |