3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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';
31 * Class CiviReportTestCase
33 class CiviReportTestCase
extends CiviUnitTestCase
{
34 public function setUp() {
36 $this->_sethtmlGlobals();
39 public function tearDown() {
40 // TODO Figure out how to automatically drop all temporary tables.
41 // Note that MySQL doesn't provide a way to list them, so we would need
42 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
43 // the MySQL connection between test runs.
45 $this->quickCleanup($this->_tablesToTruncate
);
51 * @param array $inputParams
56 public function getReportOutputAsCsv($reportClass, $inputParams) {
57 $config = CRM_Core_Config
::singleton();
58 $config->keyDisable
= TRUE;
59 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
60 $tmpReportVal = explode('_', $reportClass);
61 $reportName = array_pop($tmpReportVal);
62 $reportObj =& $controller->_pages
[$reportName];
64 $tmpGlobals = array();
65 $tmpGlobals['_REQUEST']['force'] = 1;
66 $tmpGlobals['_GET'][$config->userFrameworkURLVar
] = 'civicrm/placeholder';
67 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
68 if (!empty($inputParams['fields'])) {
69 $fields = implode(',', $inputParams['fields']);
70 $tmpGlobals['_GET']['fld'] = $fields;
71 $tmpGlobals['_GET']['ufld'] = 1;
73 if (!empty($inputParams['filters'])) {
74 foreach ($inputParams['filters'] as $key => $val) {
75 $tmpGlobals['_GET'][$key] = $val;
78 if (!empty($inputParams['group_bys'])) {
79 $groupByFields = implode(' ', $inputParams['group_bys']);
80 $tmpGlobals['_GET']['gby'] = $groupByFields;
83 CRM_Utils_GlobalStack
::singleton()->push($tmpGlobals);
86 $reportObj->storeResultSet();
87 $reportObj->buildForm();
88 $rows = $reportObj->getResultSet();
90 $tmpFile = $this->createTempDir() . CRM_Utils_File
::makeFileName('CiviReport.csv');
91 $csvContent = CRM_Report_Utils_Report
::makeCsv($reportObj, $rows);
92 file_put_contents($tmpFile, $csvContent);
94 catch (Exception
$e) {
95 // print_r($e->getCause()->getUserInfo());
96 CRM_Utils_GlobalStack
::singleton()->pop();
99 CRM_Utils_GlobalStack
::singleton()->pop();
109 public function getArrayFromCsv($csvFile) {
111 if (($handle = fopen($csvFile, "r")) !== FALSE) {
112 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
121 * @param array $expectedCsvArray
122 * Two-dimensional array representing a CSV table.
123 * @param array $actualCsvArray
124 * Two-dimensional array representing a CSV table.
126 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
127 // TODO provide better debug output
129 $flatData = "\n===== EXPECTED DATA ====\n"
130 . $this->flattenCsvArray($expectedCsvArray)
131 . "\n===== ACTUAL DATA ====\n"
132 . $this->flattenCsvArray($actualCsvArray);
135 count($actualCsvArray),
136 count($expectedCsvArray),
137 'Arrays have different number of rows; in line ' . __LINE__
. '; data: ' . $flatData
140 foreach ($actualCsvArray as $intKey => $strVal) {
141 $rowData = var_export(array(
142 'expected' => $expectedCsvArray[$intKey],
143 'actual' => $actualCsvArray[$intKey],
145 $this->assertNotNull($expectedCsvArray[$intKey]);
147 count($actualCsvArray[$intKey]),
148 count($expectedCsvArray[$intKey]),
149 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
. '; data: ' . $rowData
151 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
160 public function flattenCsvArray($rows) {
162 foreach ($rows as $row) {
163 $result .= implode(',', $row) . "\n";