Commit | Line | Data |
---|---|---|
7ec58d2b J |
1 | <?php |
2 | ||
3 | /* | |
7d61e75f TO |
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 | +--------------------------------------------------------------------+ | |
7ec58d2b J |
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 | public function setUp() { | |
30 | // There are only unit tests here at present, we can skip database loading. | |
31 | return TRUE; | |
32 | } | |
33 | ||
34 | public function tearDown() { | |
35 | // There are only unit tests here at present, we can skip database loading. | |
36 | return TRUE; | |
37 | } | |
38 | ||
f212762e | 39 | /** |
40 | * Used by testGetFromTo | |
41 | */ | |
42 | private function fromToData() { | |
9099cab3 | 43 | $cases = []; |
7ec58d2b | 44 | // Absolute dates |
f212762e | 45 | $cases['absolute'] = [ |
46 | 'expectedFrom' => '20170901000000', | |
47 | 'expectedTo' => '20170913235959', | |
48 | 'relative' => 0, | |
49 | 'from' => '09/01/2017', | |
50 | 'to' => '09/13/2017', | |
51 | ]; | |
7ec58d2b J |
52 | // "Today" relative date filter |
53 | $date = new DateTime(); | |
f212762e | 54 | $cases['today'] = [ |
55 | 'expectedFrom' => $date->format('Ymd') . '000000', | |
56 | 'expectedTo' => $date->format('Ymd') . '235959', | |
57 | 'relative' => 'this.day', | |
58 | 'from' => '', | |
59 | 'to' => '', | |
60 | ]; | |
7ec58d2b J |
61 | // "yesterday" relative date filter |
62 | $date = new DateTime(); | |
63 | $date->sub(new DateInterval('P1D')); | |
f212762e | 64 | $cases['yesterday'] = [ |
65 | 'expectedFrom' => $date->format('Ymd') . '000000', | |
66 | 'expectedTo' => $date->format('Ymd') . '235959', | |
67 | 'relative' => 'previous.day', | |
68 | 'from' => '', | |
69 | 'to' => '', | |
70 | ]; | |
7ec58d2b J |
71 | return $cases; |
72 | } | |
73 | ||
74 | /** | |
75 | * Test that getFromTo returns the correct dates. | |
7ec58d2b | 76 | */ |
f212762e | 77 | public function testGetFromTo() { |
78 | $cases = $this->fromToData(); | |
79 | foreach ($cases as $caseDescription => $case) { | |
80 | $obj = new CRM_Report_Form(); | |
81 | list($calculatedFrom, $calculatedTo) = $obj->getFromTo($case['relative'], $case['from'], $case['to']); | |
82 | $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()')); | |
659e3f52 | 83 | } |
7ec58d2b J |
84 | } |
85 | ||
d45fdad6 | 86 | /** |
87 | * Test the processReportMode function. | |
88 | * | |
89 | * @dataProvider reportModeProvider | |
90 | * | |
91 | * @param array $input | |
92 | * @param array $expected | |
93 | */ | |
94 | public function testProcessReportMode($input, $expected) { | |
95 | // This is a helper in the tests tree, not a real class in the main tree. | |
96 | $form = new CRM_Report_Form_SampleForm(); | |
97 | ||
98 | $_REQUEST['output'] = $input['format']; | |
99 | $_REQUEST['sendmail'] = $input['sendmail']; | |
100 | ||
101 | $form->processReportMode(); | |
102 | ||
103 | unset($_REQUEST['output']); | |
104 | unset($_REQUEST['sendmail']); | |
105 | ||
106 | $this->assertEquals($expected, [ | |
107 | $form->getOutputMode(), | |
108 | $form->getAddPaging(), | |
109 | $form->printOnly, | |
110 | $form->_absoluteUrl, | |
111 | ]); | |
112 | } | |
113 | ||
114 | /** | |
115 | * dataprovider for testProcessReportMode | |
116 | * | |
117 | * @return array | |
118 | */ | |
119 | public function reportModeProvider() { | |
120 | return [ | |
121 | 'print no mail' => [ | |
122 | [ | |
123 | 'format' => 'report_instance.print', | |
124 | 'sendmail' => NULL, | |
125 | ], | |
126 | [ | |
127 | // _outputMode | |
128 | 'print', | |
129 | // addPaging | |
130 | FALSE, | |
131 | // printOnly | |
132 | TRUE, | |
133 | // _absoluteUrl | |
134 | FALSE, | |
135 | ], | |
136 | ], | |
137 | 'print and mail' => [ | |
138 | [ | |
139 | 'format' => 'report_instance.print', | |
140 | 'sendmail' => '1', | |
141 | ], | |
142 | ['print', FALSE, TRUE, TRUE], | |
143 | ], | |
144 | 'csv no mail' => [ | |
145 | [ | |
146 | 'format' => 'report_instance.csv', | |
147 | 'sendmail' => NULL, | |
148 | ], | |
149 | ['csv', FALSE, TRUE, TRUE], | |
150 | ], | |
151 | 'csv and mail' => [ | |
152 | [ | |
153 | 'format' => 'report_instance.csv', | |
154 | 'sendmail' => '1', | |
155 | ], | |
156 | ['csv', FALSE, TRUE, TRUE], | |
157 | ], | |
158 | 'pdf no mail' => [ | |
159 | [ | |
160 | 'format' => 'report_instance.pdf', | |
161 | 'sendmail' => NULL, | |
162 | ], | |
163 | ['pdf', FALSE, TRUE, TRUE], | |
164 | ], | |
165 | 'pdf and mail' => [ | |
166 | [ | |
167 | 'format' => 'report_instance.pdf', | |
168 | 'sendmail' => '1', | |
169 | ], | |
170 | ['pdf', FALSE, TRUE, TRUE], | |
171 | ], | |
172 | 'unknown format no mail' => [ | |
173 | [ | |
174 | 'format' => NULL, | |
175 | 'sendmail' => NULL, | |
176 | ], | |
177 | [NULL, TRUE, FALSE, FALSE], | |
178 | ], | |
179 | 'unknown format and mail' => [ | |
180 | [ | |
181 | 'format' => NULL, | |
182 | 'sendmail' => '1', | |
183 | ], | |
184 | // This is a bit inconsistent with the mail_report job which defaults | |
185 | // to pdf when you don't specify a format. But for now this is what | |
186 | // processReportMode does. | |
187 | ['print', FALSE, TRUE, TRUE], | |
188 | ], | |
189 | ]; | |
190 | } | |
191 | ||
7ec58d2b | 192 | } |