CRM-12622 - Report testing - Multiple fixes/enhancements:
[civicrm-core.git] / tests / phpunit / CRM / Report / Form / TestCaseTest.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/CiviReportTestCase.php';
29
30 /**
31 * Verify that the CiviReportTestCase provides a working set of
32 * primitives for tests. Do this by running various scenarios
33 * that should yield positive and negative results.
34 *
35 * Note: We need some report class to use as an example.
36 * CRM_Report_Form_Contribute_DetailTest is chosen arbitrarily.
37 *
38 * @package CiviCRM
39 */
40 class CRM_Report_Form_TestCaseTest extends CiviReportTestCase {
41 static $_tablesToTruncate = array(
42 'civicrm_contact',
43 'civicrm_email',
44 'civicrm_phone',
45 'civicrm_address',
46 'civicrm_contribution',
47 );
48
49 public function dataProvider() {
50 $testCaseA = array(
51 'CRM_Report_Form_Contribute_Detail',
52 array(
53 'fields' => array(
54 'first_name',
55 'email',
56 'total_amount',
57 ),
58 'filters' => array(
59 'total_amount_op' => 'gte',
60 'total_amount_value' => 50,
61 ),
62 // FIXME: add filters
63 ),
64 'Contribute/fixtures/dataset-ascii.sql',
65 'Contribute/fixtures/report-ascii.csv',
66 );
67
68 return array(
69 $testCaseA,
70 $testCaseA,
71 $testCaseA,
72 // We repeat the test a second time to
73 // ensure that CiviReportTestCase can
74 // clean up sufficiently to run
75 // multiple tests.
76 );
77 }
78
79 public function badDataProvider() {
80 return array(
81 // This test-case is bad because the dataset-ascii.sql does not match the
82 // report.csv (due to differences in international chars)
83 array(
84 'CRM_Report_Form_Contribute_Detail',
85 array(
86 'fields' => array(
87 'first_name',
88 'email',
89 'total_amount',
90 ),
91 'filters' => array(
92 'total_amount_op' => 'gte',
93 'total_amount_value' => 50,
94 ),
95 // FIXME: add filters
96 ),
97 'Contribute/fixtures/dataset-ascii.sql',
98 'Contribute/fixtures/report.csv',
99 ),
100 // This test-case is bad because the filters check for
101 // an amount >= $100, but the test data includes records
102 // for $50.
103 array(
104 'CRM_Report_Form_Contribute_Detail',
105 array(
106 'fields' => array(
107 'first_name',
108 'email',
109 'total_amount',
110 ),
111 'filters' => array(
112 'total_amount_op' => 'gte',
113 'total_amount_value' => 100,
114 ),
115 // FIXME: add filters
116 ),
117 'Contribute/fixtures/dataset-ascii.sql',
118 'Contribute/fixtures/report.csv',
119 ),
120 );
121 }
122
123 function setUp() {
124 parent::setUp();
125 $this->foreignKeyChecksOff();
126 $this->quickCleanup(self::$_tablesToTruncate);
127 }
128
129 function tearDown() {
130 parent::tearDown();
131 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp1');
132 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp2');
133 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp3');
134 }
135
136 /**
137 * @dataProvider dataProvider
138 */
139 public function testReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) {
140 $config = CRM_Core_Config::singleton();
141 CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}");
142
143 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
144 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
145
146 $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}");
147 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
148 }
149
150 /**
151 * @expectedException PHPUnit_Framework_AssertionFailedError
152 * @dataProvider badDataProvider
153 */
154 public function testBadReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) {
155 $config = CRM_Core_Config::singleton();
156 CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}");
157
158 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
159 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
160
161 $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}");
162 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
163 }
164 }