Merge pull request #16447 from samuelsov/lab#1319
[civicrm-core.git] / tests / phpunit / CRM / Report / Form / TestCaseTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 require_once 'CiviTest/CiviReportTestCase.php';
13
14 /**
15 * Verify that the CiviReportTestCase provides a working set of
16 * primitives for tests. Do this by running various scenarios
17 * that should yield positive and negative results.
18 *
19 * Note: We need some report class to use as an example.
20 * CRM_Report_Form_Contribute_DetailTest is chosen arbitrarily.
21 *
22 * @package CiviCRM
23 */
24 class CRM_Report_Form_TestCaseTest extends CiviReportTestCase {
25 protected $_tablesToTruncate = [
26 'civicrm_contact',
27 'civicrm_email',
28 'civicrm_phone',
29 'civicrm_address',
30 'civicrm_contribution',
31 ];
32
33 /**
34 * @return array
35 */
36 public function dataProvider() {
37 $testCaseA = [
38 'CRM_Report_Form_Contribute_Detail',
39 [
40 'fields' => [
41 'sort_name',
42 'first_name',
43 'email',
44 'total_amount',
45 ],
46 'filters' => [
47 'total_amount_op' => 'gte',
48 'total_amount_value' => 50,
49 ],
50 // FIXME: add filters
51 ],
52 'Contribute/fixtures/dataset-ascii.sql',
53 'Contribute/fixtures/report-ascii.csv',
54 ];
55
56 return [
57 $testCaseA,
58 $testCaseA,
59 $testCaseA,
60 // We repeat the test a second time to
61 // ensure that CiviReportTestCase can
62 // clean up sufficiently to run
63 // multiple tests.
64 ];
65 }
66
67 /**
68 * @return array
69 */
70 public function badDataProvider() {
71 return [
72 // This test-case is bad because the dataset-ascii.sql does not match the
73 // report.csv (due to differences in international chars)
74 [
75 'CRM_Report_Form_Contribute_Detail',
76 [
77 'fields' => [
78 'sort_name',
79 'first_name',
80 'email',
81 'total_amount',
82 ],
83 'filters' => [
84 'total_amount_op' => 'gte',
85 'total_amount_value' => 50,
86 ],
87 // FIXME: add filters
88 ],
89 'Contribute/fixtures/dataset-ascii.sql',
90 'Contribute/fixtures/report.csv',
91 ],
92 // This test-case is bad because the filters check for
93 // an amount >= $100, but the test data includes records
94 // for $50.
95 [
96 'CRM_Report_Form_Contribute_Detail',
97 [
98 'fields' => [
99 'sort_name',
100 'first_name',
101 'email',
102 'total_amount',
103 ],
104 'filters' => [
105 'total_amount_op' => 'gte',
106 'total_amount_value' => 100,
107 ],
108 // FIXME: add filters
109 ],
110 'Contribute/fixtures/dataset-ascii.sql',
111 'Contribute/fixtures/report.csv',
112 ],
113 ];
114 }
115
116 public function setUp() {
117 parent::setUp();
118 $this->quickCleanup($this->_tablesToTruncate);
119 }
120
121 /**
122 * @dataProvider dataProvider
123 * @param $reportClass
124 * @param $inputParams
125 * @param $dataSet
126 * @param $expectedOutputCsvFile
127 * @throws \Exception
128 */
129 public function testReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) {
130 $config = CRM_Core_Config::singleton();
131 CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}");
132
133 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
134 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
135
136 $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}");
137 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
138 }
139
140 /**
141 * @dataProvider badDataProvider
142 * @param $reportClass
143 * @param $inputParams
144 * @param $dataSet
145 * @param $expectedOutputCsvFile
146 * @throws \Exception
147 */
148 public function testBadReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) {
149 $config = CRM_Core_Config::singleton();
150 CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}");
151
152 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
153 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
154
155 $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}");
156 try {
157 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
158 }
159 catch (PHPUnit\Framework\AssertionFailedError $e) {
160 /* OK */
161 }
162 catch (PHPUnit_Framework_AssertionFailedError $e) {
163 /* OK */
164 }
165 }
166
167 /**
168 * Test processReportMode() Function in Reports
169 */
170 public function testOutputMode() {
171 $clazz = new ReflectionClass('CRM_Report_Form');
172 $reportForm = new CRM_Report_Form();
173
174 $params = $clazz->getProperty('_params');
175 $params->setAccessible(TRUE);
176 $outputMode = $clazz->getProperty('_outputMode');
177 $outputMode->setAccessible(TRUE);
178
179 $params->setValue($reportForm, ['groups' => 4]);
180 $reportForm->processReportMode();
181 $this->assertEquals('group', $outputMode->getValue($reportForm));
182
183 $params->setValue($reportForm, ['task' => 'copy']);
184 $reportForm->processReportMode();
185 $this->assertEquals('copy', $outputMode->getValue($reportForm));
186
187 $params->setValue($reportForm, ['task' => 'print']);
188 $reportForm->processReportMode();
189 $this->assertEquals('print', $outputMode->getValue($reportForm));
190 }
191
192 }