3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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) {
55 $config = CRM_Core_Config
::singleton();
56 $config->keyDisable
= TRUE;
57 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
58 $tmpReportVal = explode('_', $reportClass);
59 $reportName = array_pop($tmpReportVal);
60 $reportObj =& $controller->_pages
[$reportName];
62 $tmpGlobals = array();
63 $tmpGlobals['_REQUEST']['force'] = 1;
64 $tmpGlobals['_GET'][$config->userFrameworkURLVar
] = 'civicrm/placeholder';
65 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
66 if (!empty($inputParams['fields'])) {
67 $fields = implode(',', $inputParams['fields']);
68 $tmpGlobals['_GET']['fld'] = $fields;
69 $tmpGlobals['_GET']['ufld'] = 1;
71 if (!empty($inputParams['filters'])) {
72 foreach ($inputParams['filters'] as $key => $val) {
73 $tmpGlobals['_GET'][$key] = $val;
76 if (!empty($inputParams['group_bys'])) {
77 $groupByFields = implode(' ', $inputParams['group_bys']);
78 $tmpGlobals['_GET']['gby'] = $groupByFields;
81 CRM_Utils_GlobalStack
::singleton()->push($tmpGlobals);
84 $reportObj->storeResultSet();
85 $reportObj->buildForm();
86 $rows = $reportObj->getResultSet();
88 $tmpFile = $this->createTempDir() . CRM_Utils_File
::makeFileName('CiviReport.csv');
89 $csvContent = CRM_Report_Utils_Report
::makeCsv($reportObj, $rows);
90 file_put_contents($tmpFile, $csvContent);
92 catch (Exception
$e) {
93 // print_r($e->getCause()->getUserInfo());
94 CRM_Utils_GlobalStack
::singleton()->pop();
97 CRM_Utils_GlobalStack
::singleton()->pop();
107 public function getArrayFromCsv($csvFile) {
109 if (($handle = fopen($csvFile, "r")) !== FALSE) {
110 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
119 * @param array $expectedCsvArray
120 * Two-dimensional array representing a CSV table.
121 * @param array $actualCsvArray
122 * Two-dimensional array representing a CSV table.
124 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
125 // TODO provide better debug output
127 $flatData = "\n===== EXPECTED DATA ====\n"
128 . $this->flattenCsvArray($expectedCsvArray)
129 . "\n===== ACTUAL DATA ====\n"
130 . $this->flattenCsvArray($actualCsvArray);
133 count($actualCsvArray),
134 count($expectedCsvArray),
135 'Arrays have different number of rows; in line ' . __LINE__
. '; data: ' . $flatData
138 foreach ($actualCsvArray as $intKey => $strVal) {
139 $rowData = var_export(array(
140 'expected' => $expectedCsvArray[$intKey],
141 'actual' => $actualCsvArray[$intKey],
143 $this->assertNotNull($expectedCsvArray[$intKey]);
145 count($actualCsvArray[$intKey]),
146 count($expectedCsvArray[$intKey]),
147 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
. '; data: ' . $rowData
149 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
158 public function flattenCsvArray($rows) {
160 foreach ($rows as $row) {
161 $result .= implode(',', $row) . "\n";