Merge pull request #1 from civicrm/master
[civicrm-core.git] / tests / phpunit / CRM / Utils / DateTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * File for the DateTest class
31 *
32 * (PHP 5)
33 *
34 * @author Jon Goldberg <jon@megaphonetech.com>
35 */
36
37 /**
38 * Test CRM_Utils_Date functions.
39 *
40 * @package CiviCRM
41 * @group headless
42 */
43 class CRM_Utils_DateTest extends CiviUnitTestCase {
44
45 public function setUp() {
46 // There are only unit tests here at present, we can skip database loading.
47 return TRUE;
48 }
49
50 public function tearDown() {
51 // There are only unit tests here at present, we can skip database loading.
52 return TRUE;
53 }
54
55 public function fromToData() {
56 $cases = [];
57 // Absolute dates
58 $cases[] = ['20170901000000', '20170913235959', 0, '09/01/2017', '09/13/2017'];
59 // "Today" relative date filter
60 $date = new DateTime();
61 $expectedFrom = $date->format('Ymd') . '000000';
62 $expectedTo = $date->format('Ymd') . '235959';
63 $cases[] = [$expectedFrom, $expectedTo, 'this.day', '', ''];
64 // "yesterday" relative date filter
65 $date = new DateTime();
66 $date->sub(new DateInterval('P1D'));
67 $expectedFrom = $date->format('Ymd') . '000000';
68 $expectedTo = $date->format('Ymd') . '235959';
69 $cases[] = [$expectedFrom, $expectedTo, 'previous.day', '', ''];
70 return $cases;
71 }
72
73 /**
74 * Test that getFromTo returns the correct dates.
75 *
76 * @dataProvider fromToData
77 * @param $expectedFrom
78 * @param $expectedTo
79 * @param $relative
80 * @param $from
81 * @param $to
82 */
83 public function testGetFromTo($expectedFrom, $expectedTo, $relative, $from, $to) {
84 $obj = new CRM_Utils_Date();
85 list($calculatedFrom, $calculatedTo) = $obj->getFromTo($relative, $from, $to);
86 $this->assertEquals($expectedFrom, $calculatedFrom);
87 $this->assertEquals($expectedTo, $calculatedTo);
88 }
89
90 /**
91 * Test relativeToAbsolute function on a range of fiscal year options.
92 *
93 * Go backwards one year at a time through the sequence.
94 */
95 public function testRelativeToAbsoluteFiscalYear() {
96 $sequence = ['this', 'previous', 'previous_before'];
97 Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
98 $fiscalYearStartYear = (strtotime('now') > strtotime((date('Y-07-01')))) ? date('Y') : (date('Y') - 1);
99
100 // this_2 = 'These 2 Fiscal years'
101 $date = CRM_Utils_Date::relativeToAbsolute('this_2', 'fiscal_year');
102 $this->assertEquals([
103 'from' => ($fiscalYearStartYear - 1) . '0701000000',
104 'to' => ($fiscalYearStartYear + 1) . '0630235959',
105 ], $date, 'relative term is this_2.fiscal_year');
106
107 foreach ($sequence as $relativeString) {
108 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
109 $this->assertEquals([
110 'from' => $fiscalYearStartYear . '0701',
111 'to' => ($fiscalYearStartYear + 1) . '0630235959',
112 ], $date, 'relative term is ' . $relativeString);
113
114 $fiscalYearStartYear--;
115 }
116
117 }
118
119 /**
120 * Test relativeToAbsolute function on a range of year options.
121 *
122 * Go backwards one year at a time through the sequence.
123 */
124 public function testRelativeToAbsoluteYear() {
125 $sequence = ['this', 'previous', 'previous_before'];
126 $year = date('Y');
127
128 foreach ($sequence as $relativeString) {
129 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
130 $this->assertEquals([
131 'from' => $year . '0101',
132 'to' => $year . '1231',
133 ], $date, 'relative term is ' . $relativeString);
134
135 $year--;
136 }
137
138 // this_2 = 'These 2 years'
139 $date = CRM_Utils_Date::relativeToAbsolute('this_2', 'year');
140 $thisYear = date('Y');
141 $this->assertEquals([
142 'from' => ($thisYear - 1) . '0101',
143 'to' => $thisYear . '1231',
144 ], $date, 'relative term is this_2 year');
145 }
146
147 /**
148 * Test relativeToAbsolute function on a range of year options.
149 *
150 * Go backwards one year at a time through the sequence.
151 */
152 public function testRelativeEnding() {
153 $relativeDateValues = [
154 'ending.week' => '- 6 days',
155 'ending_30.day' => '- 29 days',
156 'ending.year' => '- 1 year + 1 day',
157 'ending_90.day' => '- 89 days',
158 'ending_60.day' => '- 59 days',
159 'ending_2.year' => '- 2 years + 1 day',
160 'ending_3.year' => '- 3 years + 1 day',
161 'ending_18.year' => '- 18 years + 1 day',
162 'ending_18.quarter' => '- 54 months + 1 day',
163 'ending_18.week' => '- 18 weeks + 1 day',
164 'ending_18.month' => '- 18 months + 1 day',
165 'ending_18.day' => '- 17 days',
166 ];
167
168 foreach ($relativeDateValues as $key => $value) {
169 $parts = explode('.', $key);
170 $date = CRM_Utils_Date::relativeToAbsolute($parts[0], $parts[1]);
171 $this->assertEquals([
172 'from' => date('Ymd000000', strtotime($value)),
173 'to' => date('Ymd235959'),
174 ], $date, 'relative term is ' . $key);
175 }
176
177 $date = CRM_Utils_Date::relativeToAbsolute('ending', 'month');
178 $this->assertEquals([
179 'from' => date('Ymd000000', strtotime('- 29 days')),
180 'to' => date('Ymd235959'),
181 ], $date, 'relative term is ending.week');
182 }
183
184 /**
185 * Test relativeToAbsolute function on a range of year options.
186 *
187 * Go backwards one year at a time through the sequence.
188 */
189 public function testRelativeThisFiscal() {
190 $relativeDateValues = [
191 'ending.week' => '- 6 days',
192 'ending_30.day' => '- 29 days',
193 'ending.year' => '- 1 year + 1 day',
194 'ending_90.day' => '- 89 days',
195 'ending_60.day' => '- 59 days',
196 'ending_2.year' => '- 2 years + 1 day',
197 'ending_3.year' => '- 3 years + 1 day',
198 'ending_18.year' => '- 18 years + 1 day',
199 'ending_18.quarter' => '- 54 months + 1 day',
200 'ending_18.week' => '- 18 weeks + 1 day',
201 'ending_18.month' => '- 18 months + 1 day',
202 'ending_18.day' => '- 17 days',
203 ];
204
205 foreach ($relativeDateValues as $key => $value) {
206 $parts = explode('.', $key);
207 $date = CRM_Utils_Date::relativeToAbsolute($parts[0], $parts[1]);
208 $this->assertEquals([
209 'from' => date('Ymd000000', strtotime($value)),
210 'to' => date('Ymd235959'),
211 ], $date, 'relative term is ' . $key);
212 }
213
214 $date = CRM_Utils_Date::relativeToAbsolute('ending', 'month');
215 $this->assertEquals([
216 'from' => date('Ymd000000', strtotime('- 29 days')),
217 'to' => date('Ymd235959'),
218 ], $date, 'relative term is ending.week');
219 }
220
221 /**
222 * Test relativeToAbsolute function on a range of year options.
223 *
224 * Go backwards one year at a time through the sequence.
225 */
226 public function testRelativeToAbsoluteYearRange() {
227 $sequence = ['previous_2'];
228 $lastYear = (date('Y') - 1);
229
230 foreach ($sequence as $relativeString) {
231 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
232 // For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract
233 // one from the range count to reflect the calendar year being one less apart due
234 // to it being from the beginning of one to the end of the next.
235 $offset = (substr($relativeString, -1, 1)) - 1;
236 $this->assertEquals([
237 'from' => $lastYear - $offset . '0101',
238 'to' => $lastYear . '1231',
239 ], $date, 'relative term is ' . $relativeString);
240 }
241 }
242
243 /**
244 * Test relativeToAbsolute function on a range of year options.
245 *
246 * Go backwards one year at a time through the sequence.
247 */
248 public function testRelativeToAbsoluteFiscalYearRange() {
249 $sequence = ['previous_2', 'previous_3', 'previous_4'];
250 Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
251 $lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ? (date('Y')) : (date('Y') - 1);
252
253 foreach ($sequence as $relativeString) {
254 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
255 // For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract
256 // one from the range count to reflect the calendar year being one less apart due
257 // to it being from the beginning of one to the end of the next.
258 $offset = (substr($relativeString, -1, 1));
259 $this->assertEquals([
260 'from' => $lastFiscalYearEnd - $offset . '0701',
261 'to' => $lastFiscalYearEnd . '0630235959',
262 ], $date, 'relative term is ' . $relativeString);
263 }
264 }
265
266 /**
267 * Test customFormat() function
268 */
269 public function testCustomFormat() {
270 $dateTime = "2018-11-08 21:46:44";
271 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%b"), "Nov");
272 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%B"), "November");
273 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%d"), "08");
274 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%e"), " 8");
275 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%E"), "8");
276 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%f"), "th");
277 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%H"), "21");
278 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%I"), "09");
279 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%k"), "21");
280 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%l"), " 9");
281 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%m"), "11");
282 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%M"), "46");
283 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%p"), "pm");
284 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%P"), "PM");
285 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%Y"), "2018");
286 }
287
288 /**
289 * Test customFormat() function
290 */
291 public function testCustomFormatTs() {
292 $ts = mktime(21, 46, 44, 11, 8, 2018);
293 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%b"), "Nov");
294 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%B"), "November");
295 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%d"), "08");
296 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%e"), " 8");
297 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%E"), "8");
298 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%f"), "th");
299 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%H"), "21");
300 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%I"), "09");
301 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%k"), "21");
302 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%l"), " 9");
303 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%m"), "11");
304 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%M"), "46");
305 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%p"), "pm");
306 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%P"), "PM");
307 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%Y"), "2018");
308 }
309
310 }