Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-07-15-48-59
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class CiviReportTestCase
32 */
33 class CiviReportTestCase extends CiviUnitTestCase {
34 function setUp() {
35 parent::setUp();
36 $this->_sethtmlGlobals();
37 }
38
39 function tearDown() {
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.
44
45 $this->quickCleanup($this->_tablesToTruncate);
46 parent::tearDown();
47 }
48
49 /**
50 * @param $reportClass
51 * @param $inputParams
52 *
53 * @return string
54 * @throws Exception
55 */
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'));
60 $tmpReportVal = explode('_', $reportClass);
61 $reportName = array_pop($tmpReportVal);
62 $reportObj =& $controller->_pages[$reportName];
63
64 $tmpGlobals = array();
65 $tmpGlobals['_REQUEST']['force'] = 1;
66 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
67 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
68 if (!empty($inputParams['fields'])) {
69 $fields = implode(',', $inputParams['fields']);
70 $tmpGlobals['_GET']['fld'] = $fields;
71 $tmpGlobals['_GET']['ufld'] = 1;
72 }
73 if (!empty($inputParams['filters'])) {
74 foreach ($inputParams['filters'] as $key => $val) {
75 $tmpGlobals['_GET'][$key] = $val;
76 }
77 }
78 if (!empty($inputParams['group_bys'])) {
79 $groupByFields = implode(' ', $inputParams['group_bys']);
80 $tmpGlobals['_GET']['gby'] = $groupByFields;
81 }
82
83 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
84
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) {
94 // print_r($e->getCause()->getUserInfo());
95 CRM_Utils_GlobalStack::singleton()->pop();
96 throw $e;
97 }
98 CRM_Utils_GlobalStack::singleton()->pop();
99
100 return $tmpFile;
101 }
102
103 /**
104 * @param $csvFile
105 *
106 * @return array
107 */
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 }
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
126 $flatData = "\n===== EXPECTED DATA ====\n"
127 . $this->flattenCsvArray($expectedCsvArray)
128 . "\n===== ACTUAL DATA ====\n"
129 . $this->flattenCsvArray($actualCsvArray);
130
131 $this->assertEquals(
132 count($actualCsvArray),
133 count($expectedCsvArray),
134 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
135 );
136
137 foreach ($actualCsvArray as $intKey => $strVal) {
138 $rowData = var_export(array(
139 'expected' => $expectedCsvArray[$intKey],
140 'actual' => $actualCsvArray[$intKey],
141 ), TRUE);
142 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
143 $this->assertEquals(
144 count($actualCsvArray[$intKey]),
145 count($expectedCsvArray[$intKey]),
146 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__. '; data: ' . $rowData
147 );
148 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
149 }
150 }
151
152 /**
153 * @param $rows
154 *
155 * @return string
156 */
157 public function flattenCsvArray($rows) {
158 $result = '';
159 foreach ($rows as $row) {
160 $result .= implode(',', $row) . "\n";
161 }
162 return $result;
163 }
164 }