Merge pull request #1277 from totten/master-reporttestcase
[civicrm-core.git] / tests / phpunit / CiviTest / CiviReportTestCase.php
CommitLineData
ae555e90
DS
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30class CiviReportTestCase extends CiviUnitTestCase {
31 function setUp() {
32 parent::setUp();
33 }
34
6d4b9264
TO
35 function tearDown() {
36 parent::tearDown();
37 }
38
ae555e90
DS
39 function getReportOutputAsCsv($reportClass, $inputParams) {
40 $config = CRM_Core_Config::singleton();
41 $config->keyDisable = TRUE;
42 $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
4be6c8f8 43 $reportObj =& $controller->_pages['Detail']; //FIXME - Detail is going to change
6d4b9264
TO
44
45 $tmpGlobals = array();
46 $tmpGlobals['_REQUEST']['force'] = 1;
47 $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
48 $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
ae555e90
DS
49 if (!empty($inputParams['fields'])) {
50 $fields = implode(',', $inputParams['fields']);
4be6c8f8 51 $tmpGlobals['_GET']['fld'] = $fields;
6d4b9264 52 $tmpGlobals['_GET']['ufld'] = 1;
ae555e90 53 }
ec24e302
DS
54 if (!empty($inputParams['filters'])) {
55 foreach ($inputParams['filters'] as $key => $val) {
6d4b9264 56 $tmpGlobals['_GET'][$key] = $val;
ec24e302
DS
57 }
58 }
6d4b9264 59 CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
ae555e90 60
6d4b9264
TO
61 try {
62 $reportObj->storeResultSet();
63 $reportObj->buildForm();
64 $rows = $reportObj->getResultSet();
65
66 $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
67 $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
68 file_put_contents($tmpFile, $csvContent);
69 } catch (Exception $e) {
70 CRM_Utils_GlobalStack::singleton()->pop();
71 throw $e;
72 }
73 CRM_Utils_GlobalStack::singleton()->pop();
ae555e90
DS
74
75 return $tmpFile;
76 }
77
78 function getArrayFromCsv($csvFile) {
79 $arrFile = array();
80 if (($handle = fopen($csvFile, "r")) !== FALSE) {
81 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
82 $arrFile[] = $data;
83 }
84 fclose($handle);
85 }
86 return $arrFile;
87 }
3b608e05
TO
88
89 /**
90 * @param array $expectedCsvArray two-dimensional array representing a CSV table
91 * @param array $actualCsvArray two-dimensional array representing a CSV table
92 */
93 public function assertCsvArraysEqual($expectedCsvArray, $actualCsvArray) {
94 // TODO provide better debug output
95
96 $this->assertEquals(
97 count($actualCsvArray),
98 count($expectedCsvArray),
99 'Arrays have different number of rows; in line ' . __LINE__
100 );
101
102 foreach ($actualCsvArray as $intKey => $strVal) {
103 $this->assertNotNull($expectedCsvArray[$intKey], 'In line ' . __LINE__);
104 $this->assertEquals(
105 count($actualCsvArray[$intKey]),
106 count($expectedCsvArray[$intKey]),
107 'Arrays have different number of columns at row ' . $intKey . '; in line ' . __LINE__
108 );
109 $this->assertEquals($expectedCsvArray[$intKey], $strVal);
110 }
111 }
ae555e90 112}