Merge pull request #1278 from totten/master-reporttestcase-2
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 class CiviReportTestCase extends CiviUnitTestCase {
31 function setUp() {
32 parent::setUp();
33 $this->_sethtmlGlobals();
34 }
35
36 function tearDown() {
37 // TODO Figure out how to automatically drop all temporary tables.
38 // Note that MySQL doesn't provide a way to list them, so we would need
39 // to keep track ourselves (eg CRM_Core_TemporaryTableManager) or reset
40 // the MySQL connection between test runs.
41 parent::tearDown();
42 }
43
44 function getReportOutputAsCsv($reportClass, $inputParams) {
45 $config = CRM_Core_Config::singleton();
46 $config->keyDisable = TRUE;
47 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
48 $reportObj =& $controller->_pages['Detail']; //FIXME - Detail is going to change
49
50 $tmpGlobals = array();
51 $tmpGlobals['_REQUEST']['force'] = 1;
52 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
53 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
54 if (!empty($inputParams['fields'])) {
55 $fields = implode(',', $inputParams['fields']);
56 $tmpGlobals['_GET']['fld'] = $fields;
57 $tmpGlobals['_GET']['ufld'] = 1;
58 }
59 if (!empty($inputParams['filters'])) {
60 foreach ($inputParams['filters'] as $key => $val) {
61 $tmpGlobals['_GET'][$key] = $val;
62 }
63 }
64 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
65
66 try {
67 $reportObj->storeResultSet();
68 $reportObj->buildForm();
69 $rows = $reportObj->getResultSet();
70
71 $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
72 $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
73 file_put_contents($tmpFile, $csvContent);
74 } catch (Exception $e) {
75 // print_r($e->getCause()->getUserInfo());
76 CRM_Utils_GlobalStack::singleton()->pop();
77 throw $e;
78 }
79 CRM_Utils_GlobalStack::singleton()->pop();
80
81 return $tmpFile;
82 }
83
84 function getArrayFromCsv($csvFile) {
85 $arrFile = array();
86 if (($handle = fopen($csvFile, "r")) !== FALSE) {
87 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
88 $arrFile[] = $data;
89 }
90 fclose($handle);
91 }
92 return $arrFile;
93 }
94
95 /**
96 * @param array $expectedCsvArray two-dimensional array representing a CSV table
97 * @param array $actualCsvArray two-dimensional array representing a CSV table
98 */
99 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
100 // TODO provide better debug output
101
102 $this->assertEquals(
103 count($actualCsvArray),
104 count($expectedCsvArray),
105 'Arrays have different number of rows; in line ' . __LINE__
106 );
107
108 foreach ($actualCsvArray as $intKey => $strVal) {
109 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
110 $this->assertEquals(
111 count($actualCsvArray[$intKey]),
112 count($expectedCsvArray[$intKey]),
113 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
114 );
115 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
116 }
117 }
118 }