3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CiviReportTestCase
15 class CiviReportTestCase
extends CiviUnitTestCase
{
17 public function setUp() {
19 $this->_sethtmlGlobals();
22 public function tearDown() {
23 // TODO Figure out how to automatically drop all temporary tables.
24 // Note that MySQL doesn't provide a way to list them, so we would need
25 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
26 // the MySQL connection between test runs.
28 $this->quickCleanup($this->_tablesToTruncate
);
34 * @param array $inputParams
39 public function getReportOutputAsCsv($reportClass, $inputParams) {
41 $reportObj = $this->getReportObject($reportClass, $inputParams);
42 $rows = $reportObj->getResultSet();
43 $tmpFile = $this->createTempDir() . CRM_Utils_File
::makeFileName('CiviReport.csv');
44 $csvContent = CRM_Report_Utils_Report
::makeCsv($reportObj, $rows);
45 file_put_contents($tmpFile, $csvContent);
50 * @param string $reportClass
51 * @param array $inputParams
53 * @return CRM_Report_Form
56 public function getReportObject($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];
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();
89 catch (Exception
$e) {
90 // print_r($e->getCause()->getUserInfo());
91 CRM_Utils_GlobalStack
::singleton()->pop();
94 CRM_Utils_GlobalStack
::singleton()->pop();
104 public function getArrayFromCsv($csvFile) {
106 if (($handle = fopen($csvFile, "r")) !== FALSE) {
107 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
116 * @param array $expectedCsvArray
117 * Two-dimensional array representing a CSV table.
118 * @param array $actualCsvArray
119 * Two-dimensional array representing a CSV table.
121 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
122 // TODO provide better debug output
124 $flatData = "\n===== EXPECTED DATA ====\n"
125 . $this->flattenCsvArray($expectedCsvArray)
126 . "\n===== ACTUAL DATA ====\n"
127 . $this->flattenCsvArray($actualCsvArray);
130 count($actualCsvArray),
131 count($expectedCsvArray),
132 'Arrays have different number of rows; in line ' . __LINE__
. '; data: ' . $flatData
135 foreach ($actualCsvArray as $intKey => $strVal) {
136 $rowData = var_export(array(
137 'expected' => $expectedCsvArray[$intKey],
138 'actual' => $actualCsvArray[$intKey],
140 $this->assertNotNull($expectedCsvArray[$intKey]);
142 count($actualCsvArray[$intKey]),
143 count($expectedCsvArray[$intKey]),
144 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
. '; data: ' . $rowData
146 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
155 public function flattenCsvArray($rows) {
157 foreach ($rows as $row) {
158 $result .= implode(',', $row) . "\n";