Merge pull request #22316 from braders/core-3003-preserve-tab-between-pageloads
[civicrm-core.git] / tests / phpunit / CRM / Report / FormTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 * File for the FormTest class
15 *
16 * (PHP 5)
17 *
18 * @author Jon Goldberg <jon@megaphonetech.com>
19 */
20
21 /**
22 * Test CRM_Report_Form functions.
23 *
24 * @package CiviCRM
25 * @group headless
26 */
27 class CRM_Report_FormTest extends CiviUnitTestCase {
28
29 /**
30 * Used by testGetFromTo
31 */
32 private function fromToData() {
33 $cases = [];
34 // Absolute dates
35 $cases['absolute'] = [
36 'expectedFrom' => '20170901000000',
37 'expectedTo' => '20170913235959',
38 'relative' => 0,
39 'from' => '09/01/2017',
40 'to' => '09/13/2017',
41 ];
42 // "Today" relative date filter
43 $date = new DateTime();
44 $cases['today'] = [
45 'expectedFrom' => $date->format('Ymd') . '000000',
46 'expectedTo' => $date->format('Ymd') . '235959',
47 'relative' => 'this.day',
48 'from' => '',
49 'to' => '',
50 ];
51 // "yesterday" relative date filter
52 $date = new DateTime();
53 $date->sub(new DateInterval('P1D'));
54 $cases['yesterday'] = [
55 'expectedFrom' => $date->format('Ymd') . '000000',
56 'expectedTo' => $date->format('Ymd') . '235959',
57 'relative' => 'previous.day',
58 'from' => '',
59 'to' => '',
60 ];
61 return $cases;
62 }
63
64 /**
65 * Test that getFromTo returns the correct dates.
66 */
67 public function testGetFromTo() {
68 $cases = $this->fromToData();
69 foreach ($cases as $caseDescription => $case) {
70 $obj = new CRM_Report_Form();
71 list($calculatedFrom, $calculatedTo) = $obj->getFromTo($case['relative'], $case['from'], $case['to']);
72 $this->assertEquals([$case['expectedFrom'], $case['expectedTo']], [$calculatedFrom, $calculatedTo], "fail on data set '{$caseDescription}'. Local php time is " . date('Y-m-d H:i:s') . ' and mysql time is ' . CRM_Core_DAO::singleValueQuery('SELECT NOW()'));
73 }
74 }
75
76 /**
77 * Test the processReportMode function.
78 *
79 * @dataProvider reportModeProvider
80 *
81 * @param array $input
82 * @param array $expected
83 */
84 public function testProcessReportMode($input, $expected) {
85 // This is a helper in the tests tree, not a real class in the main tree.
86 $form = new CRM_Report_Form_SampleForm();
87
88 $_REQUEST['output'] = $input['format'];
89 $_REQUEST['sendmail'] = $input['sendmail'];
90
91 $form->processReportMode();
92
93 unset($_REQUEST['output']);
94 unset($_REQUEST['sendmail']);
95
96 $this->assertEquals($expected, [
97 $form->getOutputMode(),
98 $form->getAddPaging(),
99 $form->printOnly,
100 $form->_absoluteUrl,
101 ]);
102 }
103
104 /**
105 * dataprovider for testProcessReportMode
106 *
107 * @return array
108 */
109 public function reportModeProvider() {
110 return [
111 'print no mail' => [
112 [
113 'format' => 'report_instance.print',
114 'sendmail' => NULL,
115 ],
116 [
117 // _outputMode
118 'print',
119 // addPaging
120 FALSE,
121 // printOnly
122 TRUE,
123 // _absoluteUrl
124 FALSE,
125 ],
126 ],
127 'print and mail' => [
128 [
129 'format' => 'report_instance.print',
130 'sendmail' => '1',
131 ],
132 ['print', FALSE, TRUE, TRUE],
133 ],
134 'csv no mail' => [
135 [
136 'format' => 'report_instance.csv',
137 'sendmail' => NULL,
138 ],
139 ['csv', FALSE, TRUE, TRUE],
140 ],
141 'csv and mail' => [
142 [
143 'format' => 'report_instance.csv',
144 'sendmail' => '1',
145 ],
146 ['csv', FALSE, TRUE, TRUE],
147 ],
148 'pdf no mail' => [
149 [
150 'format' => 'report_instance.pdf',
151 'sendmail' => NULL,
152 ],
153 ['pdf', FALSE, TRUE, TRUE],
154 ],
155 'pdf and mail' => [
156 [
157 'format' => 'report_instance.pdf',
158 'sendmail' => '1',
159 ],
160 ['pdf', FALSE, TRUE, TRUE],
161 ],
162 'unknown format no mail' => [
163 [
164 'format' => NULL,
165 'sendmail' => NULL,
166 ],
167 [NULL, TRUE, FALSE, FALSE],
168 ],
169 'unknown format and mail' => [
170 [
171 'format' => NULL,
172 'sendmail' => '1',
173 ],
174 // This is a bit inconsistent with the mail_report job which defaults
175 // to pdf when you don't specify a format. But for now this is what
176 // processReportMode does.
177 ['print', FALSE, TRUE, TRUE],
178 ],
179 ];
180 }
181
182 }