| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Class CiviReportTestCase |
| 14 | */ |
| 15 | class CiviReportTestCase extends CiviUnitTestCase { |
| 16 | |
| 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. |
| 22 | |
| 23 | $this->quickCleanup($this->_tablesToTruncate); |
| 24 | parent::tearDown(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @param $reportClass |
| 29 | * @param array $inputParams |
| 30 | * |
| 31 | * @return string |
| 32 | * @throws Exception |
| 33 | */ |
| 34 | public function getReportOutputAsCsv($reportClass, $inputParams) { |
| 35 | |
| 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); |
| 41 | return $tmpFile; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param string $reportClass |
| 46 | * @param array $inputParams |
| 47 | * |
| 48 | * @return CRM_Report_Form |
| 49 | * @throws Exception |
| 50 | */ |
| 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]; |
| 58 | |
| 59 | $tmpGlobals = []; |
| 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; |
| 67 | } |
| 68 | if (!empty($inputParams['filters'])) { |
| 69 | foreach ($inputParams['filters'] as $key => $val) { |
| 70 | $tmpGlobals['_GET'][$key] = $val; |
| 71 | } |
| 72 | } |
| 73 | if (!empty($inputParams['group_bys'])) { |
| 74 | $groupByFields = implode(' ', $inputParams['group_bys']); |
| 75 | $tmpGlobals['_GET']['gby'] = $groupByFields; |
| 76 | } |
| 77 | |
| 78 | CRM_Utils_GlobalStack::singleton()->push($tmpGlobals); |
| 79 | |
| 80 | try { |
| 81 | $reportObj->storeResultSet(); |
| 82 | $reportObj->buildForm(); |
| 83 | } |
| 84 | catch (Exception $e) { |
| 85 | // print_r($e->getCause()->getUserInfo()); |
| 86 | CRM_Utils_GlobalStack::singleton()->pop(); |
| 87 | throw $e; |
| 88 | } |
| 89 | CRM_Utils_GlobalStack::singleton()->pop(); |
| 90 | |
| 91 | return $reportObj; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param $csvFile |
| 96 | * |
| 97 | * @return array |
| 98 | */ |
| 99 | public function getArrayFromCsv($csvFile) { |
| 100 | $arrFile = []; |
| 101 | if (($handle = fopen($csvFile, "r")) !== FALSE) { |
| 102 | while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { |
| 103 | $arrFile[] = $data; |
| 104 | } |
| 105 | fclose($handle); |
| 106 | } |
| 107 | return $arrFile; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param array $expectedCsvArray |
| 112 | * Two-dimensional array representing a CSV table. |
| 113 | * @param array $actualCsvArray |
| 114 | * Two-dimensional array representing a CSV table. |
| 115 | */ |
| 116 | public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) { |
| 117 | // TODO provide better debug output |
| 118 | |
| 119 | $flatData = "\n===== EXPECTED DATA ====\n" |
| 120 | . $this->flattenCsvArray($expectedCsvArray) |
| 121 | . "\n===== ACTUAL DATA ====\n" |
| 122 | . $this->flattenCsvArray($actualCsvArray); |
| 123 | |
| 124 | $this->assertEquals( |
| 125 | count($actualCsvArray), |
| 126 | count($expectedCsvArray), |
| 127 | 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData |
| 128 | ); |
| 129 | |
| 130 | foreach ($actualCsvArray as $intKey => $strVal) { |
| 131 | $rowData = var_export(array( |
| 132 | 'expected' => $expectedCsvArray[$intKey], |
| 133 | 'actual' => $actualCsvArray[$intKey], |
| 134 | ), TRUE); |
| 135 | $this->assertNotNull($expectedCsvArray[$intKey]); |
| 136 | $this->assertEquals( |
| 137 | count($actualCsvArray[$intKey]), |
| 138 | count($expectedCsvArray[$intKey]), |
| 139 | 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData |
| 140 | ); |
| 141 | $this->assertEquals($expectedCsvArray[$intKey], $strVal); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param $rows |
| 147 | * |
| 148 | * @return string |
| 149 | */ |
| 150 | public function flattenCsvArray($rows) { |
| 151 | $result = ''; |
| 152 | foreach ($rows as $row) { |
| 153 | $result .= implode(',', $row) . "\n"; |
| 154 | } |
| 155 | return $result; |
| 156 | } |
| 157 | |
| 158 | } |