INFRA-132 - Drupal.Classes.ClassCreateInstance.ParenthesisMissing
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
CommitLineData
ae555e90
DS
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
ae555e90
DS
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
e9479dcf
EM
30/**
31 * Class CiviReportTestCase
32 */
ae555e90 33class CiviReportTestCase extends CiviUnitTestCase {
00be9182 34 public function setUp() {
ae555e90 35 parent::setUp();
5c2e58ae 36 $this->_sethtmlGlobals();
ae555e90
DS
37 }
38
00be9182 39 public 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
100fef9d 51 * @param array $inputParams
4cbe18b8
EM
52 *
53 * @return string
54 * @throws Exception
55 */
00be9182 56 public function getReportOutputAsCsv($reportClass, $inputParams) {
ae555e90
DS
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);
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
DS
100
101 return $tmpFile;
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);
3b608e05
TO
145 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
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}