3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
29 * Class CiviReportTestCase
31 class CiviReportTestCase
extends CiviUnitTestCase
{
32 public function setUp() {
34 $this->_sethtmlGlobals();
37 public function tearDown() {
38 // TODO Figure out how to automatically drop all temporary tables.
39 // Note that MySQL doesn't provide a way to list them, so we would need
40 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
41 // the MySQL connection between test runs.
43 $this->quickCleanup($this->_tablesToTruncate
);
49 * @param array $inputParams
54 public function getReportOutputAsCsv($reportClass, $inputParams) {
56 $reportObj = $this->getReportObject($reportClass, $inputParams);
58 $rows = $reportObj->getResultSet();
59 $tmpFile = $this->createTempDir() . CRM_Utils_File
::makeFileName('CiviReport.csv');
60 $csvContent = CRM_Report_Utils_Report
::makeCsv($reportObj, $rows);
61 file_put_contents($tmpFile, $csvContent);
63 catch (Exception
$e) {
71 * @param array $inputParams
76 public function getReportObject($reportClass, $inputParams) {
77 $config = CRM_Core_Config
::singleton();
78 $config->keyDisable
= TRUE;
79 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
80 $tmpReportVal = explode('_', $reportClass);
81 $reportName = array_pop($tmpReportVal);
82 $reportObj =& $controller->_pages
[$reportName];
84 $tmpGlobals = array();
85 $tmpGlobals['_REQUEST']['force'] = 1;
86 $tmpGlobals['_GET'][$config->userFrameworkURLVar
] = 'civicrm/placeholder';
87 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
88 if (!empty($inputParams['fields'])) {
89 $fields = implode(',', $inputParams['fields']);
90 $tmpGlobals['_GET']['fld'] = $fields;
91 $tmpGlobals['_GET']['ufld'] = 1;
93 if (!empty($inputParams['filters'])) {
94 foreach ($inputParams['filters'] as $key => $val) {
95 $tmpGlobals['_GET'][$key] = $val;
98 if (!empty($inputParams['group_bys'])) {
99 $groupByFields = implode(' ', $inputParams['group_bys']);
100 $tmpGlobals['_GET']['gby'] = $groupByFields;
103 CRM_Utils_GlobalStack
::singleton()->push($tmpGlobals);
106 $reportObj->storeResultSet();
107 $reportObj->buildForm();
109 catch (Exception
$e) {
110 // print_r($e->getCause()->getUserInfo());
111 CRM_Utils_GlobalStack
::singleton()->pop();
114 CRM_Utils_GlobalStack
::singleton()->pop();
124 public function getArrayFromCsv($csvFile) {
126 if (($handle = fopen($csvFile, "r")) !== FALSE) {
127 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
136 * @param array $expectedCsvArray
137 * Two-dimensional array representing a CSV table.
138 * @param array $actualCsvArray
139 * Two-dimensional array representing a CSV table.
141 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
142 // TODO provide better debug output
144 $flatData = "\n===== EXPECTED DATA ====\n"
145 . $this->flattenCsvArray($expectedCsvArray)
146 . "\n===== ACTUAL DATA ====\n"
147 . $this->flattenCsvArray($actualCsvArray);
150 count($actualCsvArray),
151 count($expectedCsvArray),
152 'Arrays have different number of rows; in line ' . __LINE__
. '; data: ' . $flatData
155 foreach ($actualCsvArray as $intKey => $strVal) {
156 $rowData = var_export(array(
157 'expected' => $expectedCsvArray[$intKey],
158 'actual' => $actualCsvArray[$intKey],
160 $this->assertNotNull($expectedCsvArray[$intKey]);
162 count($actualCsvArray[$intKey]),
163 count($expectedCsvArray[$intKey]),
164 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
. '; data: ' . $rowData
166 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
175 public function flattenCsvArray($rows) {
177 foreach ($rows as $row) {
178 $result .= implode(',', $row) . "\n";