Commit | Line | Data |
---|---|---|
819e6707 J |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
2fe49090 | 5 | | CiviCRM version 5 | |
819e6707 J |
6 | +--------------------------------------------------------------------+ |
7 | | Copyright CiviCRM LLC (c) 2004-2017 | | |
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 = array(); | |
57 | // Absolute dates | |
58 | $cases[] = array('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[] = array($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[] = array($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 | ||
1e60a380 | 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 | foreach ($sequence as $relativeString) { | |
101 | $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year'); | |
102 | $this->assertEquals([ | |
103 | 'from' => $fiscalYearStartYear . '0701', | |
104 | 'to' => ($fiscalYearStartYear + 1) . '0630' | |
105 | ], $date, 'relative term is ' . $relativeString); | |
106 | ||
107 | $fiscalYearStartYear--; | |
108 | } | |
109 | } | |
110 | ||
111 | /** | |
112 | * Test relativeToAbsolute function on a range of year options. | |
113 | * | |
114 | * Go backwards one year at a time through the sequence. | |
115 | */ | |
116 | public function testRelativeToAbsoluteYear() { | |
117 | $sequence = ['this', 'previous', 'previous_before']; | |
118 | $year = date('Y'); | |
119 | ||
120 | foreach ($sequence as $relativeString) { | |
121 | $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year'); | |
122 | $this->assertEquals([ | |
123 | 'from' => $year . '0101', | |
124 | 'to' => $year . '1231', | |
125 | ], $date, 'relative term is ' . $relativeString); | |
126 | ||
127 | $year--; | |
128 | } | |
129 | } | |
130 | ||
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 testRelativeToAbsoluteYearRange() { | |
137 | $sequence = ['previous_2']; | |
138 | $lastYear = (date('Y') - 1); | |
139 | ||
140 | foreach ($sequence as $relativeString) { | |
141 | $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year'); | |
142 | // For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract | |
143 | // one from the range count to reflect the calendar year being one less apart due | |
144 | // to it being from the beginning of one to the end of the next. | |
145 | $offset = (substr($relativeString, -1, 1)) - 1; | |
146 | $this->assertEquals([ | |
147 | 'from' => $lastYear - $offset . '0101', | |
148 | 'to' => $lastYear . '1231', | |
149 | ], $date, 'relative term is ' . $relativeString); | |
150 | } | |
151 | } | |
152 | ||
153 | /** | |
154 | * Test relativeToAbsolute function on a range of year options. | |
155 | * | |
156 | * Go backwards one year at a time through the sequence. | |
157 | */ | |
158 | public function testRelativeToAbsoluteFiscalYearRange() { | |
159 | $sequence = ['previous_2', 'previous_3', 'previous_4']; | |
160 | Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]); | |
161 | $lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ? (date('Y')) : (date('Y') - 1); | |
162 | ||
163 | foreach ($sequence as $relativeString) { | |
164 | $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year'); | |
165 | // For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract | |
166 | // one from the range count to reflect the calendar year being one less apart due | |
167 | // to it being from the beginning of one to the end of the next. | |
168 | $offset = (substr($relativeString, -1, 1)); | |
169 | $this->assertEquals([ | |
170 | 'from' => $lastFiscalYearEnd - $offset . '0701', | |
171 | 'to' => $lastFiscalYearEnd . '0630', | |
172 | ], $date, 'relative term is ' . $relativeString); | |
173 | } | |
174 | } | |
175 | ||
819e6707 | 176 | } |