Merge pull request #20456 from eileenmcnaughton/group2
[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 use Civi\Test\Invasive;
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 * Financial data used in these tests is invalid - do not validate.
35 *
36 * Note ideally it would be fixed and we would always use valid data.
37 *
38 * @var bool
39 */
40 protected $isValidateFinancialsOnPostAssert = FALSE;
41
42 /**
43 * @return array
44 */
45 public function dataProvider(): array {
46 $testCaseA = [
47 'CRM_Report_Form_Contribute_Detail',
48 [
49 'fields' => [
50 'sort_name',
51 'first_name',
52 'email',
53 'total_amount',
54 ],
55 'filters' => [
56 'total_amount_op' => 'gte',
57 'total_amount_value' => 50,
58 ],
59 // FIXME: add filters
60 ],
61 'Contribute/fixtures/dataset-ascii.sql',
62 'Contribute/fixtures/report-ascii.csv',
63 ];
64
65 return [
66 $testCaseA,
67 $testCaseA,
68 $testCaseA,
69 // We repeat the test a second time to
70 // ensure that CiviReportTestCase can
71 // clean up sufficiently to run
72 // multiple tests.
73 ];
74 }
75
76 /**
77 * @return array
78 */
79 public function badDataProvider(): array {
80 return [
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 [
84 'CRM_Report_Form_Contribute_Detail',
85 [
86 'fields' => [
87 'sort_name',
88 'first_name',
89 'email',
90 'total_amount',
91 ],
92 'filters' => [
93 'total_amount_op' => 'gte',
94 'total_amount_value' => 50,
95 ],
96 // FIXME: add filters
97 ],
98 'Contribute/fixtures/dataset-ascii.sql',
99 'Contribute/fixtures/report.csv',
100 ],
101 // This test-case is bad because the filters check for
102 // an amount >= $100, but the test data includes records
103 // for $50.
104 [
105 'CRM_Report_Form_Contribute_Detail',
106 [
107 'fields' => [
108 'sort_name',
109 'first_name',
110 'email',
111 'total_amount',
112 ],
113 'filters' => [
114 'total_amount_op' => 'gte',
115 'total_amount_value' => 100,
116 ],
117 // FIXME: add filters
118 ],
119 'Contribute/fixtures/dataset-ascii.sql',
120 'Contribute/fixtures/report.csv',
121 ],
122 ];
123 }
124
125 /**
126 * @throws \CRM_Core_Exception
127 */
128 public function setUp(): void {
129 parent::setUp();
130 $this->quickCleanup($this->_tablesToTruncate);
131 }
132
133 /**
134 * @dataProvider dataProvider
135 * @param $reportClass
136 * @param $inputParams
137 * @param $dataSet
138 * @param $expectedOutputCsvFile
139 * @throws \Exception
140 */
141 public function testReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile): void {
142 $config = CRM_Core_Config::singleton();
143 CRM_Utils_File::sourceSQLFile($config->dsn, __DIR__ . "/$dataSet");
144
145 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
146 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
147
148 $expectedOutputCsvArray = $this->getArrayFromCsv(__DIR__ . "/$expectedOutputCsvFile");
149 $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray);
150 }
151
152 /**
153 * @dataProvider badDataProvider
154 *
155 * @param $reportClass
156 * @param $inputParams
157 * @param $dataSet
158 * @param $expectedOutputCsvFile
159 *
160 * @throws \CRM_Core_Exception
161 */
162 public function testBadReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile): void {
163 CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, __DIR__ . "/$dataSet");
164
165 $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams);
166 $reportCsvArray = $this->getArrayFromCsv($reportCsvFile);
167
168 $expectedOutputCsvArray = $this->getArrayFromCsv(__DIR__ . "/$expectedOutputCsvFile");
169 $this->assertNotEquals($expectedOutputCsvArray[1], $reportCsvArray[1]);
170 }
171
172 /**
173 * Test processReportMode() Function in Reports
174 */
175 public function testOutputMode(): void {
176 $reportForm = new CRM_Report_Form();
177
178 Invasive::set([$reportForm, '_params'], ['groups' => 4]);
179 $reportForm->processReportMode();
180 $this->assertEquals('group', Invasive::get([$reportForm, '_outputMode']));
181
182 Invasive::set([$reportForm, '_params'], ['task' => 'copy']);
183 $reportForm->processReportMode();
184 $this->assertEquals('copy', Invasive::get([$reportForm, '_outputMode']));
185
186 Invasive::set([$reportForm, '_params'], ['task' => 'print']);
187 $reportForm->processReportMode();
188 $this->assertEquals('print', Invasive::get([$reportForm, '_outputMode']));
189 }
190
191 }