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