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