Merge pull request #17688 from artfulrobot/artfulrobot-trxn-id-and-date
[civicrm-core.git] / tests / phpunit / CRM / Utils / DateTest.php
CommitLineData
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 */
27class 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
f212762e 39 /**
40 * Used by testGetFromTo
41 */
42 private function fromToData() {
9099cab3 43 $cases = [];
819e6707 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 ];
819e6707
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 ];
819e6707
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 ];
819e6707
J
71 return $cases;
72 }
73
74 /**
75 * Test that getFromTo returns the correct dates.
819e6707 76 */
f212762e 77 public function testGetFromTo() {
78 $cases = $this->fromToData();
79 foreach ($cases as $caseDescription => $case) {
80 $obj = new CRM_Utils_Date();
81 list($calculatedFrom, $calculatedTo) = $obj->getFromTo($case['relative'], $case['from'], $case['to']);
82 $this->assertEquals($case['expectedFrom'], $calculatedFrom, "Expected From failed for case $caseDescription");
83 $this->assertEquals($case['expectedTo'], $calculatedTo, "Expected To failed for case $caseDescription");
84 }
819e6707
J
85 }
86
1e60a380 87 /**
88 * Test relativeToAbsolute function on a range of fiscal year options.
89 *
90 * Go backwards one year at a time through the sequence.
91 */
92 public function testRelativeToAbsoluteFiscalYear() {
93 $sequence = ['this', 'previous', 'previous_before'];
94 Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
95 $fiscalYearStartYear = (strtotime('now') > strtotime((date('Y-07-01')))) ? date('Y') : (date('Y') - 1);
96
e34c9274 97 // this_2 = 'These 2 Fiscal years'
98 $date = CRM_Utils_Date::relativeToAbsolute('this_2', 'fiscal_year');
99 $this->assertEquals([
100 'from' => ($fiscalYearStartYear - 1) . '0701000000',
101 'to' => ($fiscalYearStartYear + 1) . '0630235959',
102 ], $date, 'relative term is this_2.fiscal_year');
103
1e60a380 104 foreach ($sequence as $relativeString) {
105 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
106 $this->assertEquals([
107 'from' => $fiscalYearStartYear . '0701',
e34c9274 108 'to' => ($fiscalYearStartYear + 1) . '0630235959',
1e60a380 109 ], $date, 'relative term is ' . $relativeString);
110
111 $fiscalYearStartYear--;
112 }
e34c9274 113
1e60a380 114 }
115
116 /**
117 * Test relativeToAbsolute function on a range of year options.
118 *
119 * Go backwards one year at a time through the sequence.
120 */
121 public function testRelativeToAbsoluteYear() {
122 $sequence = ['this', 'previous', 'previous_before'];
123 $year = date('Y');
124
125 foreach ($sequence as $relativeString) {
126 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
127 $this->assertEquals([
128 'from' => $year . '0101',
129 'to' => $year . '1231',
130 ], $date, 'relative term is ' . $relativeString);
131
132 $year--;
133 }
e34c9274 134
135 // this_2 = 'These 2 years'
136 $date = CRM_Utils_Date::relativeToAbsolute('this_2', 'year');
137 $thisYear = date('Y');
138 $this->assertEquals([
139 'from' => ($thisYear - 1) . '0101',
140 'to' => $thisYear . '1231',
141 ], $date, 'relative term is this_2 year');
1e60a380 142 }
143
ad336d61 144 /**
145 * Test relativeToAbsolute function on a range of year options.
146 *
147 * Go backwards one year at a time through the sequence.
148 */
149 public function testRelativeEnding() {
150 $relativeDateValues = [
151 'ending.week' => '- 6 days',
152 'ending_30.day' => '- 29 days',
153 'ending.year' => '- 1 year + 1 day',
154 'ending_90.day' => '- 89 days',
155 'ending_60.day' => '- 59 days',
156 'ending_2.year' => '- 2 years + 1 day',
157 'ending_3.year' => '- 3 years + 1 day',
158 'ending_18.year' => '- 18 years + 1 day',
159 'ending_18.quarter' => '- 54 months + 1 day',
160 'ending_18.week' => '- 18 weeks + 1 day',
161 'ending_18.month' => '- 18 months + 1 day',
162 'ending_18.day' => '- 17 days',
163 ];
164
165 foreach ($relativeDateValues as $key => $value) {
166 $parts = explode('.', $key);
167 $date = CRM_Utils_Date::relativeToAbsolute($parts[0], $parts[1]);
168 $this->assertEquals([
169 'from' => date('Ymd000000', strtotime($value)),
170 'to' => date('Ymd235959'),
171 ], $date, 'relative term is ' . $key);
172 }
173
174 $date = CRM_Utils_Date::relativeToAbsolute('ending', 'month');
175 $this->assertEquals([
176 'from' => date('Ymd000000', strtotime('- 29 days')),
177 'to' => date('Ymd235959'),
178 ], $date, 'relative term is ending.week');
179 }
180
e34c9274 181 /**
182 * Test relativeToAbsolute function on a range of year options.
183 *
184 * Go backwards one year at a time through the sequence.
185 */
186 public function testRelativeThisFiscal() {
187 $relativeDateValues = [
188 'ending.week' => '- 6 days',
189 'ending_30.day' => '- 29 days',
190 'ending.year' => '- 1 year + 1 day',
191 'ending_90.day' => '- 89 days',
192 'ending_60.day' => '- 59 days',
193 'ending_2.year' => '- 2 years + 1 day',
194 'ending_3.year' => '- 3 years + 1 day',
195 'ending_18.year' => '- 18 years + 1 day',
196 'ending_18.quarter' => '- 54 months + 1 day',
197 'ending_18.week' => '- 18 weeks + 1 day',
198 'ending_18.month' => '- 18 months + 1 day',
199 'ending_18.day' => '- 17 days',
200 ];
201
202 foreach ($relativeDateValues as $key => $value) {
203 $parts = explode('.', $key);
204 $date = CRM_Utils_Date::relativeToAbsolute($parts[0], $parts[1]);
205 $this->assertEquals([
206 'from' => date('Ymd000000', strtotime($value)),
207 'to' => date('Ymd235959'),
208 ], $date, 'relative term is ' . $key);
209 }
210
211 $date = CRM_Utils_Date::relativeToAbsolute('ending', 'month');
212 $this->assertEquals([
213 'from' => date('Ymd000000', strtotime('- 29 days')),
214 'to' => date('Ymd235959'),
215 ], $date, 'relative term is ending.week');
216 }
217
1e60a380 218 /**
219 * Test relativeToAbsolute function on a range of year options.
220 *
221 * Go backwards one year at a time through the sequence.
222 */
223 public function testRelativeToAbsoluteYearRange() {
224 $sequence = ['previous_2'];
225 $lastYear = (date('Y') - 1);
226
227 foreach ($sequence as $relativeString) {
228 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
229 // For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract
230 // one from the range count to reflect the calendar year being one less apart due
231 // to it being from the beginning of one to the end of the next.
232 $offset = (substr($relativeString, -1, 1)) - 1;
233 $this->assertEquals([
234 'from' => $lastYear - $offset . '0101',
39b959db 235 'to' => $lastYear . '1231',
1e60a380 236 ], $date, 'relative term is ' . $relativeString);
237 }
238 }
239
240 /**
241 * Test relativeToAbsolute function on a range of year options.
242 *
243 * Go backwards one year at a time through the sequence.
244 */
245 public function testRelativeToAbsoluteFiscalYearRange() {
246 $sequence = ['previous_2', 'previous_3', 'previous_4'];
247 Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
248 $lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ? (date('Y')) : (date('Y') - 1);
249
250 foreach ($sequence as $relativeString) {
251 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
252 // For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract
253 // one from the range count to reflect the calendar year being one less apart due
254 // to it being from the beginning of one to the end of the next.
255 $offset = (substr($relativeString, -1, 1));
256 $this->assertEquals([
257 'from' => $lastFiscalYearEnd - $offset . '0701',
e34c9274 258 'to' => $lastFiscalYearEnd . '0630235959',
1e60a380 259 ], $date, 'relative term is ' . $relativeString);
260 }
261 }
262
d2f262da
AS
263 /**
264 * Test customFormat() function
265 */
266 public function testCustomFormat() {
267 $dateTime = "2018-11-08 21:46:44";
268 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%b"), "Nov");
269 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%B"), "November");
270 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%d"), "08");
271 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%e"), " 8");
272 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%E"), "8");
273 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%f"), "th");
274 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%H"), "21");
275 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%I"), "09");
276 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%k"), "21");
277 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%l"), " 9");
278 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%m"), "11");
279 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%M"), "46");
280 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%p"), "pm");
281 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%P"), "PM");
282 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%Y"), "2018");
283 }
284
285 /**
286 * Test customFormat() function
287 */
288 public function testCustomFormatTs() {
289 $ts = mktime(21, 46, 44, 11, 8, 2018);
290 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%b"), "Nov");
291 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%B"), "November");
292 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%d"), "08");
293 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%e"), " 8");
294 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%E"), "8");
295 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%f"), "th");
296 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%H"), "21");
297 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%I"), "09");
298 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%k"), "21");
299 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%l"), " 9");
300 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%m"), "11");
301 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%M"), "46");
302 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%p"), "pm");
303 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%P"), "PM");
304 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%Y"), "2018");
305 }
306
b886f2bf
SS
307 /**
308 * Test Earlier Day Relative term to absolute
309 */
310 public function testRelativeEarlierDay() {
145bb8e4 311 $date = CRM_Utils_Date::relativeToAbsolute('earlier', 'day');
b886f2bf
SS
312
313 $this->assertEquals([
145bb8e4 314 'from' => NULL,
eb2fe97a 315 'to' => date('Ymd000000', strtotime('-1 day')),
b886f2bf
SS
316 ], $date);
317 }
318
819e6707 319}