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 tearDown(): void
{
18 // TODO Figure out how to automatically drop all temporary tables.
19 // Note that MySQL doesn't provide a way to list them, so we would need
20 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
21 // the MySQL connection between test runs.
23 $this->quickCleanup($this->_tablesToTruncate
);
29 * @param array $inputParams
34 public function getReportOutputAsCsv($reportClass, $inputParams) {
36 $reportObj = $this->getReportObject($reportClass, $inputParams);
37 $rows = $reportObj->getResultSet();
38 $tmpFile = $this->createTempDir() . CRM_Utils_File
::makeFileName('CiviReport.csv');
39 $csvContent = CRM_Report_Utils_Report
::makeCsv($reportObj, $rows);
40 file_put_contents($tmpFile, $csvContent);
45 * @param string $reportClass
46 * @param array $inputParams
48 * @return CRM_Report_Form
51 public function getReportObject($reportClass, $inputParams) {
52 $config = CRM_Core_Config
::singleton();
53 $config->keyDisable
= TRUE;
54 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
55 $tmpReportVal = explode('_', $reportClass);
56 $reportName = array_pop($tmpReportVal);
57 $reportObj =& $controller->_pages
[$reportName];
60 $tmpGlobals['_REQUEST']['force'] = 1;
61 $tmpGlobals['_GET'][$config->userFrameworkURLVar
] = 'civicrm/placeholder';
62 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
63 if (!empty($inputParams['fields'])) {
64 $fields = implode(',', $inputParams['fields']);
65 $tmpGlobals['_GET']['fld'] = $fields;
66 $tmpGlobals['_GET']['ufld'] = 1;
68 if (!empty($inputParams['filters'])) {
69 foreach ($inputParams['filters'] as $key => $val) {
70 $tmpGlobals['_GET'][$key] = $val;
73 if (!empty($inputParams['group_bys'])) {
74 $groupByFields = implode(' ', $inputParams['group_bys']);
75 $tmpGlobals['_GET']['gby'] = $groupByFields;
78 CRM_Utils_GlobalStack
::singleton()->push($tmpGlobals);
81 $reportObj->storeResultSet();
82 $reportObj->buildForm();
84 catch (Exception
$e) {
85 // print_r($e->getCause()->getUserInfo());
86 CRM_Utils_GlobalStack
::singleton()->pop();
89 CRM_Utils_GlobalStack
::singleton()->pop();
99 public function getArrayFromCsv($csvFile) {
101 if (($handle = fopen($csvFile, "r")) !== FALSE) {
102 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
111 * @param array $expectedCsvArray
112 * Two-dimensional array representing a CSV table.
113 * @param array $actualCsvArray
114 * Two-dimensional array representing a CSV table.
116 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
117 // TODO provide better debug output
119 $flatData = "\n===== EXPECTED DATA ====\n"
120 . $this->flattenCsvArray($expectedCsvArray)
121 . "\n===== ACTUAL DATA ====\n"
122 . $this->flattenCsvArray($actualCsvArray);
125 count($actualCsvArray),
126 count($expectedCsvArray),
127 'Arrays have different number of rows; in line ' . __LINE__
. '; data: ' . $flatData
130 foreach ($actualCsvArray as $intKey => $strVal) {
131 $rowData = var_export(array(
132 'expected' => $expectedCsvArray[$intKey],
133 'actual' => $actualCsvArray[$intKey],
135 $this->assertNotNull($expectedCsvArray[$intKey]);
137 count($actualCsvArray[$intKey]),
138 count($expectedCsvArray[$intKey]),
139 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
. '; data: ' . $rowData
141 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
150 public function flattenCsvArray($rows) {
152 foreach ($rows as $row) {
153 $result .= implode(',', $row) . "\n";