Merge pull request #14534 from pradpnayak/EventTypeQuery
[civicrm-core.git] / tests / phpunit / CRM / Report / Form / TestCaseTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 protected $_tablesToTruncate = [
42 'civicrm_contact',
43 'civicrm_email',
44 'civicrm_phone',
45 'civicrm_address',
46 'civicrm_contribution',
47 ];
48
49 /**
50 * @return array
51 */
52 public function dataProvider() {
53 $testCaseA = [
54 'CRM_Report_Form_Contribute_Detail',
55 [
56 'fields' => [
57 'sort_name',
58 'first_name',
59 'email',
60 'total_amount',
61 ],
62 'filters' => [
63 'total_amount_op' => 'gte',
64 'total_amount_value' => 50,
65 ],
66 // FIXME: add filters
67 ],
68 'Contribute/fixtures/dataset-ascii.sql',
69 'Contribute/fixtures/report-ascii.csv',
70 ];
71
72 return [
73 $testCaseA,
74 $testCaseA,
75 $testCaseA,
76 // We repeat the test a second time to
77 // ensure that CiviReportTestCase can
78 // clean up sufficiently to run
79 // multiple tests.
80 ];
81 }
82
83 /**
84 * @return array
85 */
86 public function badDataProvider() {
87 return [
88 // This test-case is bad because the dataset-ascii.sql does not match the
89 // report.csv (due to differences in international chars)
90 [
91 'CRM_Report_Form_Contribute_Detail',
92 [
93 'fields' => [
94 'sort_name',
95 'first_name',
96 'email',
97 'total_amount',
98 ],
99 'filters' => [
100 'total_amount_op' => 'gte',
101 'total_amount_value' => 50,
102 ],
103 // FIXME: add filters
104 ],
105 'Contribute/fixtures/dataset-ascii.sql',
106 'Contribute/fixtures/report.csv',
107 ],
108 // This test-case is bad because the filters check for
109 // an amount >= $100, but the test data includes records
110 // for $50.
111 [
112 'CRM_Report_Form_Contribute_Detail',
113 [
114 'fields' => [
115 'sort_name',
116 'first_name',
117 'email',
118 'total_amount',
119 ],
120 'filters' => [
121 'total_amount_op' => 'gte',
122 'total_amount_value' => 100,
123 ],
124 // FIXME: add filters
125 ],
126 'Contribute/fixtures/dataset-ascii.sql',
127 'Contribute/fixtures/report.csv',
128 ],
129 ];
130 }
131
132 public function setUp() {
133 parent::setUp();
134 $this->quickCleanup($this->_tablesToTruncate);
135 }
136
137 /**
138 * @dataProvider dataProvider
139 * @param $reportClass
140 * @param $inputParams
141 * @param $dataSet
142 * @param $expectedOutputCsvFile
143 * @throws \Exception
144 */
145 public function testReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) {
146 $config = CRM_Core_Config::singleton();
147 CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}");
148
149 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
150 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
151
152 $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}");
153 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
154 }
155
156 /**
157 * @dataProvider badDataProvider
158 * @param $reportClass
159 * @param $inputParams
160 * @param $dataSet
161 * @param $expectedOutputCsvFile
162 * @throws \Exception
163 */
164 public function testBadReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) {
165 $config = CRM_Core_Config::singleton();
166 CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}");
167
168 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
169 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
170
171 $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}");
172 try {
173 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
174 }
175 catch (PHPUnit\Framework\AssertionFailedError $e) {
176 /* OK */
177 }
178 catch (PHPUnit_Framework_AssertionFailedError $e) {
179 /* OK */
180 }
181 }
182
183 /**
184 * Test processReportMode() Function in Reports
185 */
186 public function testOutputMode() {
187 $clazz = new ReflectionClass('CRM_Report_Form');
188 $reportForm = new CRM_Report_Form();
189
190 $params = $clazz->getProperty('_params');
191 $params->setAccessible(TRUE);
192 $outputMode = $clazz->getProperty('_outputMode');
193 $outputMode->setAccessible(TRUE);
194
195 $params->setValue($reportForm, ['groups' => 4]);
196 $reportForm->processReportMode();
197 $this->assertEquals('group', $outputMode->getValue($reportForm));
198
199 $params->setValue($reportForm, ['task' => 'copy']);
200 $reportForm->processReportMode();
201 $this->assertEquals('copy', $outputMode->getValue($reportForm));
202
203 $params->setValue($reportForm, ['task' => 'print']);
204 $reportForm->processReportMode();
205 $this->assertEquals('print', $outputMode->getValue($reportForm));
206 }
207
208 }