| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 29 | |
| 30 | /** |
| 31 | * Class CiviReportTestCase |
| 32 | */ |
| 33 | class CiviReportTestCase extends CiviUnitTestCase { |
| 34 | public function setUp() { |
| 35 | parent::setUp(); |
| 36 | $this->_sethtmlGlobals(); |
| 37 | } |
| 38 | |
| 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. |
| 44 | |
| 45 | $this->quickCleanup($this->_tablesToTruncate); |
| 46 | parent::tearDown(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param $reportClass |
| 51 | * @param array $inputParams |
| 52 | * |
| 53 | * @return string |
| 54 | * @throws Exception |
| 55 | */ |
| 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]; |
| 63 | |
| 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; |
| 72 | } |
| 73 | if (!empty($inputParams['filters'])) { |
| 74 | foreach ($inputParams['filters'] as $key => $val) { |
| 75 | $tmpGlobals['_GET'][$key] = $val; |
| 76 | } |
| 77 | } |
| 78 | if (!empty($inputParams['group_bys'])) { |
| 79 | $groupByFields = implode(' ', $inputParams['group_bys']); |
| 80 | $tmpGlobals['_GET']['gby'] = $groupByFields; |
| 81 | } |
| 82 | |
| 83 | CRM_Utils_GlobalStack::singleton()->push($tmpGlobals); |
| 84 | |
| 85 | try { |
| 86 | $reportObj->storeResultSet(); |
| 87 | $reportObj->buildForm(); |
| 88 | $rows = $reportObj->getResultSet(); |
| 89 | |
| 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); |
| 93 | } |
| 94 | catch (Exception $e) { |
| 95 | // print_r($e->getCause()->getUserInfo()); |
| 96 | CRM_Utils_GlobalStack::singleton()->pop(); |
| 97 | throw $e; |
| 98 | } |
| 99 | CRM_Utils_GlobalStack::singleton()->pop(); |
| 100 | |
| 101 | return $tmpFile; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @param $csvFile |
| 106 | * |
| 107 | * @return array |
| 108 | */ |
| 109 | public function getArrayFromCsv($csvFile) { |
| 110 | $arrFile = array(); |
| 111 | if (($handle = fopen($csvFile, "r")) !== FALSE) { |
| 112 | while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { |
| 113 | $arrFile[] = $data; |
| 114 | } |
| 115 | fclose($handle); |
| 116 | } |
| 117 | return $arrFile; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param array $expectedCsvArray |
| 122 | * Two-dimensional array representing a CSV table. |
| 123 | * @param array $actualCsvArray |
| 124 | * Two-dimensional array representing a CSV table. |
| 125 | */ |
| 126 | public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) { |
| 127 | // TODO provide better debug output |
| 128 | |
| 129 | $flatData = "\n===== EXPECTED DATA ====\n" |
| 130 | . $this->flattenCsvArray($expectedCsvArray) |
| 131 | . "\n===== ACTUAL DATA ====\n" |
| 132 | . $this->flattenCsvArray($actualCsvArray); |
| 133 | |
| 134 | $this->assertEquals( |
| 135 | count($actualCsvArray), |
| 136 | count($expectedCsvArray), |
| 137 | 'Arrays have different number of rows; in line ' . __LINE__ . '; data: ' . $flatData |
| 138 | ); |
| 139 | |
| 140 | foreach ($actualCsvArray as $intKey => $strVal) { |
| 141 | $rowData = var_export(array( |
| 142 | 'expected' => $expectedCsvArray[$intKey], |
| 143 | 'actual' => $actualCsvArray[$intKey], |
| 144 | ), TRUE); |
| 145 | $this->assertNotNull($expectedCsvArray[$intKey]); |
| 146 | $this->assertEquals( |
| 147 | count($actualCsvArray[$intKey]), |
| 148 | count($expectedCsvArray[$intKey]), |
| 149 | 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__ . '; data: ' . $rowData |
| 150 | ); |
| 151 | $this->assertEquals($expectedCsvArray[$intKey], $strVal); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param $rows |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | public function flattenCsvArray($rows) { |
| 161 | $result = ''; |
| 162 | foreach ($rows as $row) { |
| 163 | $result .= implode(',', $row) . "\n"; |
| 164 | } |
| 165 | return $result; |
| 166 | } |
| 167 | |
| 168 | } |