Merge pull request #2763 from colemanw/master
[civicrm-core.git] / tests / phpunit / CRM / Report / Form / TestCaseTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 'sort_name',
55 'first_name',
56 'email',
57 'total_amount',
58 ),
59 'filters' => array(
60 'total_amount_op' => 'gte',
61 'total_amount_value' => 50,
62 ),
63 // FIXME: add filters
64 ),
65 'Contribute/fixtures/dataset-ascii.sql',
66 'Contribute/fixtures/report-ascii.csv',
67 );
68
69 return array(
70 $testCaseA,
71 $testCaseA,
72 $testCaseA,
73 // We repeat the test a second time to
74 // ensure that CiviReportTestCase can
75 // clean up sufficiently to run
76 // multiple tests.
77 );
78 }
79
80 public function badDataProvider() {
81 return array(
82 // This test-case is bad because the dataset-ascii.sql does not match the
83 // report.csv (due to differences in international chars)
84 array(
85 'CRM_Report_Form_Contribute_Detail',
86 array(
87 'fields' => array(
88 'sort_name',
89 'first_name',
90 'email',
91 'total_amount',
92 ),
93 'filters' => array(
94 'total_amount_op' => 'gte',
95 'total_amount_value' => 50,
96 ),
97 // FIXME: add filters
98 ),
99 'Contribute/fixtures/dataset-ascii.sql',
100 'Contribute/fixtures/report.csv',
101 ),
102 // This test-case is bad because the filters check for
103 // an amount >= $100, but the test data includes records
104 // for $50.
105 array(
106 'CRM_Report_Form_Contribute_Detail',
107 array(
108 'fields' => array(
109 'sort_name',
110 'first_name',
111 'email',
112 'total_amount',
113 ),
114 'filters' => array(
115 'total_amount_op' => 'gte',
116 'total_amount_value' => 100,
117 ),
118 // FIXME: add filters
119 ),
120 'Contribute/fixtures/dataset-ascii.sql',
121 'Contribute/fixtures/report.csv',
122 ),
123 );
124 }
125
126 function setUp() {
127 parent::setUp();
128 $this->foreignKeyChecksOff();
129 $this->quickCleanup(self::$_tablesToTruncate);
130 }
131
132 function tearDown() {
133 parent::tearDown();
134 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp1');
135 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp2');
136 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp3');
137 }
138
139 /**
140 * @dataProvider dataProvider
141 */
142 public function testReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) {
143 $config = CRM_Core_Config::singleton();
144 CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}");
145
146 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
147 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
148
149 $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}");
150 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
151 }
152
153 /**
154 * @expectedException PHPUnit_Framework_AssertionFailedError
155 * @dataProvider badDataProvider
156 */
157 public function testBadReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) {
158 $config = CRM_Core_Config::singleton();
159 CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}");
160
161 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
162 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
163
164 $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}");
165 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
166 }
167 }