add missing comments - tests directory
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
CommitLineData
ae555e90
DS
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
ae555e90 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30class CiviReportTestCase extends CiviUnitTestCase {
31 function setUp() {
32 parent::setUp();
5c2e58ae 33 $this->_sethtmlGlobals();
ae555e90
DS
34 }
35
6d4b9264 36 function tearDown() {
2d71e99f
TO
37 // TODO Figure out how to automatically drop all temporary tables.
38 // Note that MySQL doesn't provide a way to list them, so we would need
39 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
40 // the MySQL connection between test runs.
745bc660
EM
41
42 $this->quickCleanup($this->_tablesToTruncate);
6d4b9264
TO
43 parent::tearDown();
44 }
45
4cbe18b8
EM
46 /**
47 * @param $reportClass
48 * @param $inputParams
49 *
50 * @return string
51 * @throws Exception
52 */
ae555e90
DS
53 function getReportOutputAsCsv($reportClass, $inputParams) {
54 $config = CRM_Core_Config::singleton();
55 $config->keyDisable = TRUE;
56 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
a76480e7
RN
57 $tmpReportVal = explode('_', $reportClass);
58 $reportName = array_pop($tmpReportVal);
59 $reportObj =& $controller->_pages[$reportName];
6d4b9264
TO
60
61 $tmpGlobals = array();
62 $tmpGlobals['_REQUEST']['force'] = 1;
63 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
64 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
ae555e90
DS
65 if (!empty($inputParams['fields'])) {
66 $fields = implode(',', $inputParams['fields']);
4be6c8f8 67 $tmpGlobals['_GET']['fld'] = $fields;
6d4b9264 68 $tmpGlobals['_GET']['ufld'] = 1;
ae555e90 69 }
ec24e302
DS
70 if (!empty($inputParams['filters'])) {
71 foreach ($inputParams['filters'] as $key => $val) {
6d4b9264 72 $tmpGlobals['_GET'][$key] = $val;
ec24e302
DS
73 }
74 }
a76480e7
RN
75 if (!empty($inputParams['group_bys'])) {
76 $groupByFields = implode(' ', $inputParams['group_bys']);
77 $tmpGlobals['_GET']['gby'] = $groupByFields;
78 }
79
6d4b9264 80 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
ae555e90 81
6d4b9264
TO
82 try {
83 $reportObj->storeResultSet();
84 $reportObj->buildForm();
85 $rows = $reportObj->getResultSet();
86
87 $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
88 $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
89 file_put_contents($tmpFile, $csvContent);
90 } catch (Exception $e) {
2d71e99f 91 // print_r($e->getCause()->getUserInfo());
6d4b9264
TO
92 CRM_Utils_GlobalStack::singleton()->pop();
93 throw $e;
94 }
95 CRM_Utils_GlobalStack::singleton()->pop();
ae555e90
DS
96
97 return $tmpFile;
98 }
99
4cbe18b8
EM
100 /**
101 * @param $csvFile
102 *
103 * @return array
104 */
ae555e90
DS
105 function getArrayFromCsv($csvFile) {
106 $arrFile = array();
107 if (($handle = fopen($csvFile, "r")) !== FALSE) {
108 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
109 $arrFile[] = $data;
110 }
111 fclose($handle);
112 }
113 return $arrFile;
114 }
3b608e05
TO
115
116 /**
117 * @param array $expectedCsvArray two-dimensional array representing a CSV table
118 * @param array $actualCsvArray two-dimensional array representing a CSV table
119 */
120 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
121 // TODO provide better debug output
122
64aeb844
TO
123 $flatData = "\n===== EXPECTED DATA ====\n"
124 . $this->flattenCsvArray($expectedCsvArray)
125 . "\n===== ACTUAL DATA ====\n"
126 . $this->flattenCsvArray($actualCsvArray);
127
3b608e05
TO
128 $this->assertEquals(
129 count($actualCsvArray),
130 count($expectedCsvArray),
64aeb844 131 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
3b608e05
TO
132 );
133
134 foreach ($actualCsvArray as $intKey => $strVal) {
64aeb844
TO
135 $rowData = var_export(array(
136 'expected' => $expectedCsvArray[$intKey],
137 'actual' => $actualCsvArray[$intKey],
138 ), TRUE);
3b608e05
TO
139 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
140 $this->assertEquals(
141 count($actualCsvArray[$intKey]),
142 count($expectedCsvArray[$intKey]),
64aeb844 143 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__. '; data: ' . $rowData
3b608e05
TO
144 );
145 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
146 }
147 }
64aeb844 148
4cbe18b8
EM
149 /**
150 * @param $rows
151 *
152 * @return string
153 */
64aeb844
TO
154 public function flattenCsvArray($rows) {
155 $result = '';
156 foreach ($rows as $row) {
157 $result .= implode(',', $row) . "\n";
158 }
159 return $result;
160 }
ae555e90 161}