Commit | Line | Data |
---|---|---|
2d71e99f TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
2d71e99f | 5 | | | |
7d61e75f TO |
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 | | |
2d71e99f | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
2d71e99f | 11 | |
4dca4ad7 TO |
12 | use Civi\Test\Invasive; |
13 | ||
2d71e99f TO |
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 { | |
9099cab3 | 25 | protected $_tablesToTruncate = [ |
2d71e99f TO |
26 | 'civicrm_contact', |
27 | 'civicrm_email', | |
28 | 'civicrm_phone', | |
29 | 'civicrm_address', | |
30 | 'civicrm_contribution', | |
9099cab3 | 31 | ]; |
2d71e99f | 32 | |
d5d08fab EM |
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 | ||
4cbe18b8 EM |
42 | /** |
43 | * @return array | |
44 | */ | |
d5d08fab | 45 | public function dataProvider(): array { |
9099cab3 | 46 | $testCaseA = [ |
2d71e99f | 47 | 'CRM_Report_Form_Contribute_Detail', |
9099cab3 CW |
48 | [ |
49 | 'fields' => [ | |
f81c4bbb | 50 | 'sort_name', |
2d71e99f TO |
51 | 'first_name', |
52 | 'email', | |
53 | 'total_amount', | |
9099cab3 CW |
54 | ], |
55 | 'filters' => [ | |
2d71e99f TO |
56 | 'total_amount_op' => 'gte', |
57 | 'total_amount_value' => 50, | |
9099cab3 | 58 | ], |
2d71e99f | 59 | // FIXME: add filters |
9099cab3 | 60 | ], |
2d71e99f TO |
61 | 'Contribute/fixtures/dataset-ascii.sql', |
62 | 'Contribute/fixtures/report-ascii.csv', | |
9099cab3 | 63 | ]; |
2d71e99f | 64 | |
9099cab3 | 65 | return [ |
2d71e99f TO |
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. | |
9099cab3 | 73 | ]; |
2d71e99f TO |
74 | } |
75 | ||
4cbe18b8 EM |
76 | /** |
77 | * @return array | |
78 | */ | |
d5d08fab | 79 | public function badDataProvider(): array { |
9099cab3 | 80 | return [ |
2d71e99f TO |
81 | // This test-case is bad because the dataset-ascii.sql does not match the |
82 | // report.csv (due to differences in international chars) | |
9099cab3 | 83 | [ |
2d71e99f | 84 | 'CRM_Report_Form_Contribute_Detail', |
9099cab3 CW |
85 | [ |
86 | 'fields' => [ | |
f81c4bbb | 87 | 'sort_name', |
2d71e99f TO |
88 | 'first_name', |
89 | 'email', | |
90 | 'total_amount', | |
9099cab3 CW |
91 | ], |
92 | 'filters' => [ | |
2d71e99f TO |
93 | 'total_amount_op' => 'gte', |
94 | 'total_amount_value' => 50, | |
9099cab3 | 95 | ], |
2d71e99f | 96 | // FIXME: add filters |
9099cab3 | 97 | ], |
2d71e99f TO |
98 | 'Contribute/fixtures/dataset-ascii.sql', |
99 | 'Contribute/fixtures/report.csv', | |
9099cab3 | 100 | ], |
2d71e99f TO |
101 | // This test-case is bad because the filters check for |
102 | // an amount >= $100, but the test data includes records | |
103 | // for $50. | |
9099cab3 | 104 | [ |
2d71e99f | 105 | 'CRM_Report_Form_Contribute_Detail', |
9099cab3 CW |
106 | [ |
107 | 'fields' => [ | |
f81c4bbb | 108 | 'sort_name', |
2d71e99f TO |
109 | 'first_name', |
110 | 'email', | |
111 | 'total_amount', | |
9099cab3 CW |
112 | ], |
113 | 'filters' => [ | |
2d71e99f TO |
114 | 'total_amount_op' => 'gte', |
115 | 'total_amount_value' => 100, | |
9099cab3 | 116 | ], |
2d71e99f | 117 | // FIXME: add filters |
9099cab3 | 118 | ], |
2d71e99f TO |
119 | 'Contribute/fixtures/dataset-ascii.sql', |
120 | 'Contribute/fixtures/report.csv', | |
9099cab3 CW |
121 | ], |
122 | ]; | |
2d71e99f TO |
123 | } |
124 | ||
d5d08fab EM |
125 | /** |
126 | * @throws \CRM_Core_Exception | |
127 | */ | |
0b49aa04 | 128 | public function setUp(): void { |
2d71e99f | 129 | parent::setUp(); |
745bc660 | 130 | $this->quickCleanup($this->_tablesToTruncate); |
2d71e99f TO |
131 | } |
132 | ||
2d71e99f TO |
133 | /** |
134 | * @dataProvider dataProvider | |
1e1fdcf6 EM |
135 | * @param $reportClass |
136 | * @param $inputParams | |
137 | * @param $dataSet | |
138 | * @param $expectedOutputCsvFile | |
139 | * @throws \Exception | |
2d71e99f | 140 | */ |
d5d08fab | 141 | public function testReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile): void { |
2d71e99f | 142 | $config = CRM_Core_Config::singleton(); |
d5d08fab | 143 | CRM_Utils_File::sourceSQLFile($config->dsn, __DIR__ . "/$dataSet"); |
2d71e99f TO |
144 | |
145 | $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams); | |
146 | $reportCsvArray = $this->getArrayFromCsv($reportCsvFile); | |
147 | ||
d5d08fab | 148 | $expectedOutputCsvArray = $this->getArrayFromCsv(__DIR__ . "/$expectedOutputCsvFile"); |
2d71e99f TO |
149 | $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray); |
150 | } | |
151 | ||
152 | /** | |
2d71e99f | 153 | * @dataProvider badDataProvider |
d5d08fab | 154 | * |
1e1fdcf6 EM |
155 | * @param $reportClass |
156 | * @param $inputParams | |
157 | * @param $dataSet | |
158 | * @param $expectedOutputCsvFile | |
d5d08fab EM |
159 | * |
160 | * @throws \CRM_Core_Exception | |
2d71e99f | 161 | */ |
d5d08fab EM |
162 | public function testBadReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile): void { |
163 | CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, __DIR__ . "/$dataSet"); | |
2d71e99f TO |
164 | |
165 | $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams); | |
166 | $reportCsvArray = $this->getArrayFromCsv($reportCsvFile); | |
167 | ||
d5d08fab EM |
168 | $expectedOutputCsvArray = $this->getArrayFromCsv(__DIR__ . "/$expectedOutputCsvFile"); |
169 | $this->assertNotEquals($expectedOutputCsvArray[1], $reportCsvArray[1]); | |
2d71e99f | 170 | } |
96025800 | 171 | |
7343d8a7 | 172 | /** |
173 | * Test processReportMode() Function in Reports | |
174 | */ | |
d5d08fab | 175 | public function testOutputMode(): void { |
7343d8a7 | 176 | $reportForm = new CRM_Report_Form(); |
177 | ||
4dca4ad7 | 178 | Invasive::set([$reportForm, '_params'], ['groups' => 4]); |
7343d8a7 | 179 | $reportForm->processReportMode(); |
4dca4ad7 | 180 | $this->assertEquals('group', Invasive::get([$reportForm, '_outputMode'])); |
7343d8a7 | 181 | |
4dca4ad7 | 182 | Invasive::set([$reportForm, '_params'], ['task' => 'copy']); |
7343d8a7 | 183 | $reportForm->processReportMode(); |
4dca4ad7 | 184 | $this->assertEquals('copy', Invasive::get([$reportForm, '_outputMode'])); |
7343d8a7 | 185 | |
4dca4ad7 | 186 | Invasive::set([$reportForm, '_params'], ['task' => 'print']); |
7343d8a7 | 187 | $reportForm->processReportMode(); |
4dca4ad7 | 188 | $this->assertEquals('print', Invasive::get([$reportForm, '_outputMode'])); |
7343d8a7 | 189 | } |
190 | ||
2d71e99f | 191 | } |