5e4920cd2b2f417dbc0f81b63d6179a4a7de1346
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
30 class CiviReportTestCase
extends CiviUnitTestCase
{
33 $this->_sethtmlGlobals();
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.
42 $this->quickCleanup($this->_tablesToTruncate
);
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'));
57 $tmpReportVal = explode('_', $reportClass);
58 $reportName = array_pop($tmpReportVal);
59 $reportObj =& $controller->_pages
[$reportName];
61 $tmpGlobals = array();
62 $tmpGlobals['_REQUEST']['force'] = 1;
63 $tmpGlobals['_GET'][$config->userFrameworkURLVar
] = 'civicrm/placeholder';
64 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
65 if (!empty($inputParams['fields'])) {
66 $fields = implode(',', $inputParams['fields']);
67 $tmpGlobals['_GET']['fld'] = $fields;
68 $tmpGlobals['_GET']['ufld'] = 1;
70 if (!empty($inputParams['filters'])) {
71 foreach ($inputParams['filters'] as $key => $val) {
72 $tmpGlobals['_GET'][$key] = $val;
75 if (!empty($inputParams['group_bys'])) {
76 $groupByFields = implode(' ', $inputParams['group_bys']);
77 $tmpGlobals['_GET']['gby'] = $groupByFields;
80 CRM_Utils_GlobalStack
::singleton()->push($tmpGlobals);
83 $reportObj->storeResultSet();
84 $reportObj->buildForm();
85 $rows = $reportObj->getResultSet();
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) {
91 // print_r($e->getCause()->getUserInfo());
92 CRM_Utils_GlobalStack
::singleton()->pop();
95 CRM_Utils_GlobalStack
::singleton()->pop();
105 function getArrayFromCsv($csvFile) {
107 if (($handle = fopen($csvFile, "r")) !== FALSE) {
108 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
117 * @param array $expectedCsvArray two-dimensional array representing a CSV table
118 * @param array $actualCsvArray two-dimensional array representing a CSV table
120 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
121 // TODO provide better debug output
123 $flatData = "\n===== EXPECTED DATA ====\n"
124 . $this->flattenCsvArray($expectedCsvArray)
125 . "\n===== ACTUAL DATA ====\n"
126 . $this->flattenCsvArray($actualCsvArray);
129 count($actualCsvArray),
130 count($expectedCsvArray),
131 'Arrays have different number of rows; in line ' . __LINE__
. '; data: ' . $flatData
134 foreach ($actualCsvArray as $intKey => $strVal) {
135 $rowData = var_export(array(
136 'expected' => $expectedCsvArray[$intKey],
137 'actual' => $actualCsvArray[$intKey],
139 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__
);
141 count($actualCsvArray[$intKey]),
142 count($expectedCsvArray[$intKey]),
143 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
. '; data: ' . $rowData
145 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
154 public function flattenCsvArray($rows) {
156 foreach ($rows as $row) {
157 $result .= implode(',', $row) . "\n";