Merge branch '4.4' into master
[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 class CiviReportTestCase extends CiviUnitTestCase {
31 function setUp() {
32 parent::setUp();
33 $this->_sethtmlGlobals();
34 }
35
36 function tearDown() {
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.
41
42 $this->quickCleanup($this->_tablesToTruncate);
43 parent::tearDown();
44 }
45
46 function getReportOutputAsCsv($reportClass, $inputParams) {
47 $config = CRM_Core_Config::singleton();
48 $config->keyDisable = TRUE;
49 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
50 $tmpReportVal = explode('_', $reportClass);
51 $reportName = array_pop($tmpReportVal);
52 $reportObj =& $controller->_pages[$reportName];
53
54 $tmpGlobals = array();
55 $tmpGlobals['_REQUEST']['force'] = 1;
56 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
57 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
58 if (!empty($inputParams['fields'])) {
59 $fields = implode(',', $inputParams['fields']);
60 $tmpGlobals['_GET']['fld'] = $fields;
61 $tmpGlobals['_GET']['ufld'] = 1;
62 }
63 if (!empty($inputParams['filters'])) {
64 foreach ($inputParams['filters'] as $key => $val) {
65 $tmpGlobals['_GET'][$key] = $val;
66 }
67 }
68 if (!empty($inputParams['group_bys'])) {
69 $groupByFields = implode(' ', $inputParams['group_bys']);
70 $tmpGlobals['_GET']['gby'] = $groupByFields;
71 }
72
73 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
74
75 try {
76 $reportObj->storeResultSet();
77 $reportObj->buildForm();
78 $rows = $reportObj->getResultSet();
79
80 $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
81 $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
82 file_put_contents($tmpFile, $csvContent);
83 } catch (Exception $e) {
84 // print_r($e->getCause()->getUserInfo());
85 CRM_Utils_GlobalStack::singleton()->pop();
86 throw $e;
87 }
88 CRM_Utils_GlobalStack::singleton()->pop();
89
90 return $tmpFile;
91 }
92
93 function getArrayFromCsv($csvFile) {
94 $arrFile = array();
95 if (($handle = fopen($csvFile, "r")) !== FALSE) {
96 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
97 $arrFile[] = $data;
98 }
99 fclose($handle);
100 }
101 return $arrFile;
102 }
103
104 /**
105 * @param array $expectedCsvArray two-dimensional array representing a CSV table
106 * @param array $actualCsvArray two-dimensional array representing a CSV table
107 */
108 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
109 // TODO provide better debug output
110
111 $flatData = "\n===== EXPECTED DATA ====\n"
112 . $this->flattenCsvArray($expectedCsvArray)
113 . "\n===== ACTUAL DATA ====\n"
114 . $this->flattenCsvArray($actualCsvArray);
115
116 $this->assertEquals(
117 count($actualCsvArray),
118 count($expectedCsvArray),
119 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData
120 );
121
122 foreach ($actualCsvArray as $intKey => $strVal) {
123 $rowData = var_export(array(
124 'expected' => $expectedCsvArray[$intKey],
125 'actual' => $actualCsvArray[$intKey],
126 ), TRUE);
127 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
128 $this->assertEquals(
129 count($actualCsvArray[$intKey]),
130 count($expectedCsvArray[$intKey]),
131 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__. '; data: ' . $rowData
132 );
133 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
134 }
135 }
136
137 public function flattenCsvArray($rows) {
138 $result = '';
139 foreach ($rows as $row) {
140 $result .= implode(',', $row) . "\n";
141 }
142 return $result;
143 }
144 }