Commit | Line | Data |
---|---|---|
819e6707 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 | +--------------------------------------------------------------------+ | |
819e6707 J |
11 | */ |
12 | ||
13 | /** | |
14 | * File for the DateTest class | |
15 | * | |
16 | * (PHP 5) | |
17 | * | |
18 | * @author Jon Goldberg <jon@megaphonetech.com> | |
19 | */ | |
20 | ||
21 | /** | |
22 | * Test CRM_Utils_Date functions. | |
23 | * | |
24 | * @package CiviCRM | |
25 | * @group headless | |
26 | */ | |
27 | class CRM_Utils_DateTest 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 | ||
39 | public function fromToData() { | |
9099cab3 | 40 | $cases = []; |
819e6707 | 41 | // Absolute dates |
9099cab3 | 42 | $cases[] = ['20170901000000', '20170913235959', 0, '09/01/2017', '09/13/2017']; |
819e6707 J |
43 | // "Today" relative date filter |
44 | $date = new DateTime(); | |
45 | $expectedFrom = $date->format('Ymd') . '000000'; | |
46 | $expectedTo = $date->format('Ymd') . '235959'; | |
9099cab3 | 47 | $cases[] = [$expectedFrom, $expectedTo, 'this.day', '', '']; |
819e6707 J |
48 | // "yesterday" relative date filter |
49 | $date = new DateTime(); | |
50 | $date->sub(new DateInterval('P1D')); | |
51 | $expectedFrom = $date->format('Ymd') . '000000'; | |
52 | $expectedTo = $date->format('Ymd') . '235959'; | |
9099cab3 | 53 | $cases[] = [$expectedFrom, $expectedTo, 'previous.day', '', '']; |
819e6707 J |
54 | return $cases; |
55 | } | |
56 | ||
57 | /** | |
58 | * Test that getFromTo returns the correct dates. | |
59 | * | |
60 | * @dataProvider fromToData | |
61 | * @param $expectedFrom | |
62 | * @param $expectedTo | |
63 | * @param $relative | |
64 | * @param $from | |
65 | * @param $to | |
66 | */ | |
67 | public function testGetFromTo($expectedFrom, $expectedTo, $relative, $from, $to) { | |
68 | $obj = new CRM_Utils_Date(); | |
69 | list($calculatedFrom, $calculatedTo) = $obj->getFromTo($relative, $from, $to); | |
70 | $this->assertEquals($expectedFrom, $calculatedFrom); | |
71 | $this->assertEquals($expectedTo, $calculatedTo); | |
72 | } | |
73 | ||
1e60a380 | 74 | /** |
75 | * Test relativeToAbsolute function on a range of fiscal year options. | |
76 | * | |
77 | * Go backwards one year at a time through the sequence. | |
78 | */ | |
79 | public function testRelativeToAbsoluteFiscalYear() { | |
80 | $sequence = ['this', 'previous', 'previous_before']; | |
81 | Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]); | |
82 | $fiscalYearStartYear = (strtotime('now') > strtotime((date('Y-07-01')))) ? date('Y') : (date('Y') - 1); | |
83 | ||
e34c9274 | 84 | // this_2 = 'These 2 Fiscal years' |
85 | $date = CRM_Utils_Date::relativeToAbsolute('this_2', 'fiscal_year'); | |
86 | $this->assertEquals([ | |
87 | 'from' => ($fiscalYearStartYear - 1) . '0701000000', | |
88 | 'to' => ($fiscalYearStartYear + 1) . '0630235959', | |
89 | ], $date, 'relative term is this_2.fiscal_year'); | |
90 | ||
1e60a380 | 91 | foreach ($sequence as $relativeString) { |
92 | $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year'); | |
93 | $this->assertEquals([ | |
94 | 'from' => $fiscalYearStartYear . '0701', | |
e34c9274 | 95 | 'to' => ($fiscalYearStartYear + 1) . '0630235959', |
1e60a380 | 96 | ], $date, 'relative term is ' . $relativeString); |
97 | ||
98 | $fiscalYearStartYear--; | |
99 | } | |
e34c9274 | 100 | |
1e60a380 | 101 | } |
102 | ||
103 | /** | |
104 | * Test relativeToAbsolute function on a range of year options. | |
105 | * | |
106 | * Go backwards one year at a time through the sequence. | |
107 | */ | |
108 | public function testRelativeToAbsoluteYear() { | |
109 | $sequence = ['this', 'previous', 'previous_before']; | |
110 | $year = date('Y'); | |
111 | ||
112 | foreach ($sequence as $relativeString) { | |
113 | $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year'); | |
114 | $this->assertEquals([ | |
115 | 'from' => $year . '0101', | |
116 | 'to' => $year . '1231', | |
117 | ], $date, 'relative term is ' . $relativeString); | |
118 | ||
119 | $year--; | |
120 | } | |
e34c9274 | 121 | |
122 | // this_2 = 'These 2 years' | |
123 | $date = CRM_Utils_Date::relativeToAbsolute('this_2', 'year'); | |
124 | $thisYear = date('Y'); | |
125 | $this->assertEquals([ | |
126 | 'from' => ($thisYear - 1) . '0101', | |
127 | 'to' => $thisYear . '1231', | |
128 | ], $date, 'relative term is this_2 year'); | |
1e60a380 | 129 | } |
130 | ||
ad336d61 | 131 | /** |
132 | * Test relativeToAbsolute function on a range of year options. | |
133 | * | |
134 | * Go backwards one year at a time through the sequence. | |
135 | */ | |
136 | public function testRelativeEnding() { | |
137 | $relativeDateValues = [ | |
138 | 'ending.week' => '- 6 days', | |
139 | 'ending_30.day' => '- 29 days', | |
140 | 'ending.year' => '- 1 year + 1 day', | |
141 | 'ending_90.day' => '- 89 days', | |
142 | 'ending_60.day' => '- 59 days', | |
143 | 'ending_2.year' => '- 2 years + 1 day', | |
144 | 'ending_3.year' => '- 3 years + 1 day', | |
145 | 'ending_18.year' => '- 18 years + 1 day', | |
146 | 'ending_18.quarter' => '- 54 months + 1 day', | |
147 | 'ending_18.week' => '- 18 weeks + 1 day', | |
148 | 'ending_18.month' => '- 18 months + 1 day', | |
149 | 'ending_18.day' => '- 17 days', | |
150 | ]; | |
151 | ||
152 | foreach ($relativeDateValues as $key => $value) { | |
153 | $parts = explode('.', $key); | |
154 | $date = CRM_Utils_Date::relativeToAbsolute($parts[0], $parts[1]); | |
155 | $this->assertEquals([ | |
156 | 'from' => date('Ymd000000', strtotime($value)), | |
157 | 'to' => date('Ymd235959'), | |
158 | ], $date, 'relative term is ' . $key); | |
159 | } | |
160 | ||
161 | $date = CRM_Utils_Date::relativeToAbsolute('ending', 'month'); | |
162 | $this->assertEquals([ | |
163 | 'from' => date('Ymd000000', strtotime('- 29 days')), | |
164 | 'to' => date('Ymd235959'), | |
165 | ], $date, 'relative term is ending.week'); | |
166 | } | |
167 | ||
e34c9274 | 168 | /** |
169 | * Test relativeToAbsolute function on a range of year options. | |
170 | * | |
171 | * Go backwards one year at a time through the sequence. | |
172 | */ | |
173 | public function testRelativeThisFiscal() { | |
174 | $relativeDateValues = [ | |
175 | 'ending.week' => '- 6 days', | |
176 | 'ending_30.day' => '- 29 days', | |
177 | 'ending.year' => '- 1 year + 1 day', | |
178 | 'ending_90.day' => '- 89 days', | |
179 | 'ending_60.day' => '- 59 days', | |
180 | 'ending_2.year' => '- 2 years + 1 day', | |
181 | 'ending_3.year' => '- 3 years + 1 day', | |
182 | 'ending_18.year' => '- 18 years + 1 day', | |
183 | 'ending_18.quarter' => '- 54 months + 1 day', | |
184 | 'ending_18.week' => '- 18 weeks + 1 day', | |
185 | 'ending_18.month' => '- 18 months + 1 day', | |
186 | 'ending_18.day' => '- 17 days', | |
187 | ]; | |
188 | ||
189 | foreach ($relativeDateValues as $key => $value) { | |
190 | $parts = explode('.', $key); | |
191 | $date = CRM_Utils_Date::relativeToAbsolute($parts[0], $parts[1]); | |
192 | $this->assertEquals([ | |
193 | 'from' => date('Ymd000000', strtotime($value)), | |
194 | 'to' => date('Ymd235959'), | |
195 | ], $date, 'relative term is ' . $key); | |
196 | } | |
197 | ||
198 | $date = CRM_Utils_Date::relativeToAbsolute('ending', 'month'); | |
199 | $this->assertEquals([ | |
200 | 'from' => date('Ymd000000', strtotime('- 29 days')), | |
201 | 'to' => date('Ymd235959'), | |
202 | ], $date, 'relative term is ending.week'); | |
203 | } | |
204 | ||
1e60a380 | 205 | /** |
206 | * Test relativeToAbsolute function on a range of year options. | |
207 | * | |
208 | * Go backwards one year at a time through the sequence. | |
209 | */ | |
210 | public function testRelativeToAbsoluteYearRange() { | |
211 | $sequence = ['previous_2']; | |
212 | $lastYear = (date('Y') - 1); | |
213 | ||
214 | foreach ($sequence as $relativeString) { | |
215 | $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year'); | |
216 | // For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract | |
217 | // one from the range count to reflect the calendar year being one less apart due | |
218 | // to it being from the beginning of one to the end of the next. | |
219 | $offset = (substr($relativeString, -1, 1)) - 1; | |
220 | $this->assertEquals([ | |
221 | 'from' => $lastYear - $offset . '0101', | |
39b959db | 222 | 'to' => $lastYear . '1231', |
1e60a380 | 223 | ], $date, 'relative term is ' . $relativeString); |
224 | } | |
225 | } | |
226 | ||
227 | /** | |
228 | * Test relativeToAbsolute function on a range of year options. | |
229 | * | |
230 | * Go backwards one year at a time through the sequence. | |
231 | */ | |
232 | public function testRelativeToAbsoluteFiscalYearRange() { | |
233 | $sequence = ['previous_2', 'previous_3', 'previous_4']; | |
234 | Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]); | |
235 | $lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ? (date('Y')) : (date('Y') - 1); | |
236 | ||
237 | foreach ($sequence as $relativeString) { | |
238 | $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year'); | |
239 | // For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract | |
240 | // one from the range count to reflect the calendar year being one less apart due | |
241 | // to it being from the beginning of one to the end of the next. | |
242 | $offset = (substr($relativeString, -1, 1)); | |
243 | $this->assertEquals([ | |
244 | 'from' => $lastFiscalYearEnd - $offset . '0701', | |
e34c9274 | 245 | 'to' => $lastFiscalYearEnd . '0630235959', |
1e60a380 | 246 | ], $date, 'relative term is ' . $relativeString); |
247 | } | |
248 | } | |
249 | ||
d2f262da AS |
250 | /** |
251 | * Test customFormat() function | |
252 | */ | |
253 | public function testCustomFormat() { | |
254 | $dateTime = "2018-11-08 21:46:44"; | |
255 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%b"), "Nov"); | |
256 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%B"), "November"); | |
257 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%d"), "08"); | |
258 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%e"), " 8"); | |
259 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%E"), "8"); | |
260 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%f"), "th"); | |
261 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%H"), "21"); | |
262 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%I"), "09"); | |
263 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%k"), "21"); | |
264 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%l"), " 9"); | |
265 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%m"), "11"); | |
266 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%M"), "46"); | |
267 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%p"), "pm"); | |
268 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%P"), "PM"); | |
269 | $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%Y"), "2018"); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Test customFormat() function | |
274 | */ | |
275 | public function testCustomFormatTs() { | |
276 | $ts = mktime(21, 46, 44, 11, 8, 2018); | |
277 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%b"), "Nov"); | |
278 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%B"), "November"); | |
279 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%d"), "08"); | |
280 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%e"), " 8"); | |
281 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%E"), "8"); | |
282 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%f"), "th"); | |
283 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%H"), "21"); | |
284 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%I"), "09"); | |
285 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%k"), "21"); | |
286 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%l"), " 9"); | |
287 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%m"), "11"); | |
288 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%M"), "46"); | |
289 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%p"), "pm"); | |
290 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%P"), "PM"); | |
291 | $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%Y"), "2018"); | |
292 | } | |
293 | ||
819e6707 | 294 | } |