3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
{
33 public function setUp() {
35 $this->_sethtmlGlobals();
38 public function tearDown() {
39 // TODO Figure out how to automatically drop all temporary tables.
40 // Note that MySQL doesn't provide a way to list them, so we would need
41 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
42 // the MySQL connection between test runs.
44 $this->quickCleanup($this->_tablesToTruncate
);
50 * @param array $inputParams
55 public function getReportOutputAsCsv($reportClass, $inputParams) {
57 $reportObj = $this->getReportObject($reportClass, $inputParams);
59 $rows = $reportObj->getResultSet();
60 $tmpFile = $this->createTempDir() . CRM_Utils_File
::makeFileName('CiviReport.csv');
61 $csvContent = CRM_Report_Utils_Report
::makeCsv($reportObj, $rows);
62 file_put_contents($tmpFile, $csvContent);
64 catch (Exception
$e) {
72 * @param array $inputParams
77 public function getReportObject($reportClass, $inputParams) {
78 $config = CRM_Core_Config
::singleton();
79 $config->keyDisable
= TRUE;
80 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
81 $tmpReportVal = explode('_', $reportClass);
82 $reportName = array_pop($tmpReportVal);
83 $reportObj =& $controller->_pages
[$reportName];
85 $tmpGlobals = array();
86 $tmpGlobals['_REQUEST']['force'] = 1;
87 $tmpGlobals['_GET'][$config->userFrameworkURLVar
] = 'civicrm/placeholder';
88 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
89 if (!empty($inputParams['fields'])) {
90 $fields = implode(',', $inputParams['fields']);
91 $tmpGlobals['_GET']['fld'] = $fields;
92 $tmpGlobals['_GET']['ufld'] = 1;
94 if (!empty($inputParams['filters'])) {
95 foreach ($inputParams['filters'] as $key => $val) {
96 $tmpGlobals['_GET'][$key] = $val;
99 if (!empty($inputParams['group_bys'])) {
100 $groupByFields = implode(' ', $inputParams['group_bys']);
101 $tmpGlobals['_GET']['gby'] = $groupByFields;
104 CRM_Utils_GlobalStack
::singleton()->push($tmpGlobals);
107 $reportObj->storeResultSet();
108 $reportObj->buildForm();
110 catch (Exception
$e) {
111 // print_r($e->getCause()->getUserInfo());
112 CRM_Utils_GlobalStack
::singleton()->pop();
115 CRM_Utils_GlobalStack
::singleton()->pop();
125 public function getArrayFromCsv($csvFile) {
127 if (($handle = fopen($csvFile, "r")) !== FALSE) {
128 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
137 * @param array $expectedCsvArray
138 * Two-dimensional array representing a CSV table.
139 * @param array $actualCsvArray
140 * Two-dimensional array representing a CSV table.
142 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
143 // TODO provide better debug output
145 $flatData = "\n===== EXPECTED DATA ====\n"
146 . $this->flattenCsvArray($expectedCsvArray)
147 . "\n===== ACTUAL DATA ====\n"
148 . $this->flattenCsvArray($actualCsvArray);
151 count($actualCsvArray),
152 count($expectedCsvArray),
153 'Arrays have different number of rows; in line ' . __LINE__
. '; data: ' . $flatData
156 foreach ($actualCsvArray as $intKey => $strVal) {
157 $rowData = var_export(array(
158 'expected' => $expectedCsvArray[$intKey],
159 'actual' => $actualCsvArray[$intKey],
161 $this->assertNotNull($expectedCsvArray[$intKey]);
163 count($actualCsvArray[$intKey]),
164 count($expectedCsvArray[$intKey]),
165 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
. '; data: ' . $rowData
167 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
176 public function flattenCsvArray($rows) {
178 foreach ($rows as $row) {
179 $result .= implode(',', $row) . "\n";