tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CiviReportTestCase
30 */
31 class CiviReportTestCase extends CiviUnitTestCase {
32 public function setUp() {
33 parent::setUp();
34 $this->_sethtmlGlobals();
35 }
36
37 public function tearDown() {
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.
42
43 $this->quickCleanup($this->_tablesToTruncate);
44 parent::tearDown();
45 }
46
47 /**
48 * @param $reportClass
49 * @param array $inputParams
50 *
51 * @return string
52 * @throws Exception
53 */
54 public function getReportOutputAsCsv($reportClass, $inputParams) {
55 $config = CRM_Core_Config::singleton();
56 $config->keyDisable = TRUE;
57 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
58 $tmpReportVal = explode('_', $reportClass);
59 $reportName = array_pop($tmpReportVal);
60 $reportObj =& $controller->_pages[$reportName];
61
62 $tmpGlobals = array();
63 $tmpGlobals['_REQUEST']['force'] = 1;
64 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
65 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
66 if (!empty($inputParams['fields'])) {
67 $fields = implode(',', $inputParams['fields']);
68 $tmpGlobals['_GET']['fld'] = $fields;
69 $tmpGlobals['_GET']['ufld'] = 1;
70 }
71 if (!empty($inputParams['filters'])) {
72 foreach ($inputParams['filters'] as $key => $val) {
73 $tmpGlobals['_GET'][$key] = $val;
74 }
75 }
76 if (!empty($inputParams['group_bys'])) {
77 $groupByFields = implode(' ', $inputParams['group_bys']);
78 $tmpGlobals['_GET']['gby'] = $groupByFields;
79 }
80
81 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
82
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);
91 }
92 catch (Exception $e) {
93 // print_r($e->getCause()->getUserInfo());
94 CRM_Utils_GlobalStack::singleton()->pop();
95 throw $e;
96 }
97 CRM_Utils_GlobalStack::singleton()->pop();
98
99 return $tmpFile;
100 }
101
102 /**
103 * @param $csvFile
104 *
105 * @return array
106 */
107 public function getArrayFromCsv($csvFile) {
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 }
117
118 /**
119 * @param array $expectedCsvArray
120 * Two-dimensional array representing a CSV table.
121 * @param array $actualCsvArray
122 * Two-dimensional array representing a CSV table.
123 */
124 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
125 // TODO provide better debug output
126
127 $flatData = "\n===== EXPECTED DATA ====\n"
128 . $this->flattenCsvArray($expectedCsvArray)
129 . "\n===== ACTUAL DATA ====\n"
130 . $this->flattenCsvArray($actualCsvArray);
131
132 $this->assertEquals(
133 count($actualCsvArray),
134 count($expectedCsvArray),
135 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
136 );
137
138 foreach ($actualCsvArray as $intKey => $strVal) {
139 $rowData = var_export(array(
140 'expected' => $expectedCsvArray[$intKey],
141 'actual' => $actualCsvArray[$intKey],
142 ), TRUE);
143 $this->assertNotNull($expectedCsvArray[$intKey]);
144 $this->assertEquals(
145 count($actualCsvArray[$intKey]),
146 count($expectedCsvArray[$intKey]),
147 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData
148 );
149 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
150 }
151 }
152
153 /**
154 * @param $rows
155 *
156 * @return string
157 */
158 public function flattenCsvArray($rows) {
159 $result = '';
160 foreach ($rows as $row) {
161 $result .= implode(',', $row) . "\n";
162 }
163 return $result;
164 }
165
166 }