X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCiviTest%2FCiviReportTestCase.php;h=5d54fee994796a39ced8e5dfc1ab0392c560fdda;hb=e70a7fc01e4d73954614f33ff4ba352ec41c2334;hp=5814721645565a7e2ea0dd90510c3d3ea9e9d1b0;hpb=10f4dff8a3a83f09af30ec2624c0db5d27372cea;p=civicrm-core.git diff --git a/tests/phpunit/CiviTest/CiviReportTestCase.php b/tests/phpunit/CiviTest/CiviReportTestCase.php index 5814721645..5d54fee994 100644 --- a/tests/phpunit/CiviTest/CiviReportTestCase.php +++ b/tests/phpunit/CiviTest/CiviReportTestCase.php @@ -1,9 +1,9 @@ _sethtmlGlobals(); } - function getReportOutputAsCsv($reportClass, $inputParams) { + public function tearDown() { + // TODO Figure out how to automatically drop all temporary tables. + // Note that MySQL doesn't provide a way to list them, so we would need + // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset + // the MySQL connection between test runs. + + $this->quickCleanup($this->_tablesToTruncate); + parent::tearDown(); + } + + /** + * @param $reportClass + * @param array $inputParams + * + * @return string + * @throws Exception + */ + public function getReportOutputAsCsv($reportClass, $inputParams) { $config = CRM_Core_Config::singleton(); $config->keyDisable = TRUE; $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title')); - $reportObj =& $controller->_pages['Detail'];//FIXME - Detail is going to change - $_REQUEST['force'] = 1; + $tmpReportVal = explode('_', $reportClass); + $reportName = array_pop($tmpReportVal); + $reportObj =& $controller->_pages[$reportName]; + + $tmpGlobals = array(); + $tmpGlobals['_REQUEST']['force'] = 1; + $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder'; + $tmpGlobals['_SERVER']['QUERY_STRING'] = ''; if (!empty($inputParams['fields'])) { $fields = implode(',', $inputParams['fields']); - $_GET['fld'] = $fields; - $_GET['ufld'] = 1; + $tmpGlobals['_GET']['fld'] = $fields; + $tmpGlobals['_GET']['ufld'] = 1; } if (!empty($inputParams['filters'])) { foreach ($inputParams['filters'] as $key => $val) { - $_GET[$key] = $val; + $tmpGlobals['_GET'][$key] = $val; } } - $reportObj->storeResultSet(); - $reportObj->buildForm(); - $rows = $reportObj->getResultSet(); + if (!empty($inputParams['group_bys'])) { + $groupByFields = implode(' ', $inputParams['group_bys']); + $tmpGlobals['_GET']['gby'] = $groupByFields; + } - $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv'); - $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows); - file_put_contents($tmpFile, $csvContent); + CRM_Utils_GlobalStack::singleton()->push($tmpGlobals); + + try { + $reportObj->storeResultSet(); + $reportObj->buildForm(); + $rows = $reportObj->getResultSet(); + + $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv'); + $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows); + file_put_contents($tmpFile, $csvContent); + } + catch (Exception $e) { + // print_r($e->getCause()->getUserInfo()); + CRM_Utils_GlobalStack::singleton()->pop(); + throw $e; + } + CRM_Utils_GlobalStack::singleton()->pop(); return $tmpFile; } - function getArrayFromCsv($csvFile) { + /** + * @param $csvFile + * + * @return array + */ + public function getArrayFromCsv($csvFile) { $arrFile = array(); if (($handle = fopen($csvFile, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { @@ -69,4 +116,53 @@ class CiviReportTestCase extends CiviUnitTestCase { } return $arrFile; } + + /** + * @param array $expectedCsvArray + * Two-dimensional array representing a CSV table. + * @param array $actualCsvArray + * Two-dimensional array representing a CSV table. + */ + public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) { + // TODO provide better debug output + + $flatData = "\n===== EXPECTED DATA ====\n" + . $this->flattenCsvArray($expectedCsvArray) + . "\n===== ACTUAL DATA ====\n" + . $this->flattenCsvArray($actualCsvArray); + + $this->assertEquals( + count($actualCsvArray), + count($expectedCsvArray), + 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData + ); + + foreach ($actualCsvArray as $intKey => $strVal) { + $rowData = var_export(array( + 'expected' => $expectedCsvArray[$intKey], + 'actual' => $actualCsvArray[$intKey], + ), TRUE); + $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__); + $this->assertEquals( + count($actualCsvArray[$intKey]), + count($expectedCsvArray[$intKey]), + 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData + ); + $this->assertEquals($expectedCsvArray[$intKey], $strVal); + } + } + + /** + * @param $rows + * + * @return string + */ + public function flattenCsvArray($rows) { + $result = ''; + foreach ($rows as $row) { + $result .= implode(',', $row) . "\n"; + } + return $result; + } + }