1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\i18n\i18n; |
6
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\Security\Member; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Tests for {@link Datetime} class. |
12
|
|
|
*/ |
13
|
|
|
class DBDatetimeTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
protected function setUp() |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
i18n::set_locale('en_NZ'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testNowWithSystemDate() |
22
|
|
|
{ |
23
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s')); |
24
|
|
|
$nowDatetime = DBDatetime::now(); |
25
|
|
|
|
26
|
|
|
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date()); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testNowWithMockDate() |
30
|
|
|
{ |
31
|
|
|
// Test setting |
32
|
|
|
$mockDate = '2001-12-31 22:10:59'; |
33
|
|
|
DBDatetime::set_mock_now($mockDate); |
34
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s')); |
35
|
|
|
$nowDatetime = DBDatetime::now(); |
36
|
|
|
$this->assertNotEquals($systemDatetime->Date(), $nowDatetime->Date()); |
37
|
|
|
$this->assertEquals($nowDatetime->getValue(), $mockDate); |
38
|
|
|
|
39
|
|
|
// Test clearing |
40
|
|
|
DBDatetime::clear_mock_now(); |
41
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s')); |
42
|
|
|
$nowDatetime = DBDatetime::now(); |
43
|
|
|
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testSetNullAndZeroValues() |
47
|
|
|
{ |
48
|
|
|
$date = DBDatetime::create_field('Datetime', ''); |
49
|
|
|
$this->assertNull($date->getValue(), 'Empty string evaluates to NULL'); |
50
|
|
|
|
51
|
|
|
$date = DBDatetime::create_field('Datetime', null); |
52
|
|
|
$this->assertNull($date->getValue(), 'NULL is set as NULL'); |
53
|
|
|
|
54
|
|
|
$date = DBDatetime::create_field('Datetime', false); |
55
|
|
|
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL'); |
56
|
|
|
|
57
|
|
|
$date = DBDatetime::create_field('Datetime', '0'); |
58
|
|
|
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time'); |
59
|
|
|
|
60
|
|
|
$date = DBDatetime::create_field('Datetime', 0); |
61
|
|
|
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testExtendedDateTimes() |
65
|
|
|
{ |
66
|
|
|
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24'); |
67
|
|
|
$this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s')); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$date = DBDatetime::create_field('Datetime', '3000-10-10 15:32:24'); |
70
|
|
|
$this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d MMM y H m s')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testNice() |
74
|
|
|
{ |
75
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
76
|
|
|
// note: Some localisation packages exclude the ',' in default medium format |
77
|
|
|
$this->assertRegExp('#31/12/2001(,)? 10:10:59 PM#i', $date->Nice()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testDate() |
81
|
|
|
{ |
82
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
83
|
|
|
$this->assertEquals('31/12/2001', $date->Date()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testTime() |
87
|
|
|
{ |
88
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
89
|
|
|
$this->assertRegexp('#10:10:59 PM#i', $date->Time()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testTime24() |
93
|
|
|
{ |
94
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
95
|
|
|
$this->assertEquals('22:10', $date->Time24()); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testURLDateTime() |
99
|
|
|
{ |
100
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
101
|
|
|
$this->assertEquals('2001-12-31%2022%3A10%3A59', $date->URLDateTime()); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testAgoInPast() |
105
|
|
|
{ |
106
|
|
|
DBDatetime::set_mock_now('2000-12-31 12:00:00'); |
107
|
|
|
|
108
|
|
|
$this->assertEquals( |
109
|
|
|
'10 years ago', |
110
|
|
|
DBDatetime::create_field('Datetime', '1990-12-31 12:00:00')->Ago(), |
|
|
|
|
111
|
|
|
'Exact past match on years' |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$this->assertEquals( |
115
|
|
|
'10 years ago', |
116
|
|
|
DBDatetime::create_field('Datetime', '1990-12-30 12:00:00')->Ago(), |
117
|
|
|
'Approximate past match on years' |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->assertEquals( |
121
|
|
|
'1 year ago', |
122
|
|
|
DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(true, 1), |
123
|
|
|
'Approximate past match in singular, significance=1' |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$this->assertEquals( |
127
|
|
|
'12 months ago', |
128
|
|
|
DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(), |
129
|
|
|
'Approximate past match in singular' |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->assertEquals( |
133
|
|
|
'50 mins ago', |
134
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:10:11')->Ago(), |
135
|
|
|
'Approximate past match on minutes' |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$this->assertEquals( |
139
|
|
|
'59 secs ago', |
140
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(), |
141
|
|
|
'Approximate past match on seconds' |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$this->assertEquals( |
145
|
|
|
'less than a minute ago', |
146
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(false), |
147
|
|
|
'Approximate past match on seconds with $includeSeconds=false' |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$this->assertEquals( |
151
|
|
|
'1 min ago', |
152
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(false), |
153
|
|
|
'Test between 1 and 2 minutes with includeSeconds=false' |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$this->assertEquals( |
157
|
|
|
'70 secs ago', |
158
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(true), |
159
|
|
|
'Test between 1 and 2 minutes with includeSeconds=true' |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
$this->assertEquals( |
163
|
|
|
'4 mins ago', |
164
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:55:50')->Ago(), |
165
|
|
|
'Past match on minutes' |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$this->assertEquals( |
169
|
|
|
'1 hour ago', |
170
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 10:50:58')->Ago(true, 1), |
171
|
|
|
'Past match on hours, significance=1' |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$this->assertEquals( |
175
|
|
|
'3 hours ago', |
176
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 08:50:58')->Ago(), |
177
|
|
|
'Past match on hours' |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
DBDatetime::clear_mock_now(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testAgoInFuture() |
184
|
|
|
{ |
185
|
|
|
DBDatetime::set_mock_now('2000-12-31 00:00:00'); |
186
|
|
|
|
187
|
|
|
$this->assertEquals( |
188
|
|
|
'in 10 years', |
189
|
|
|
DBDatetime::create_field('Datetime', '2010-12-31 12:00:00')->Ago(), |
190
|
|
|
'Exact past match on years' |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$this->assertEquals( |
194
|
|
|
'in 1 hour', |
195
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(true, 1), |
196
|
|
|
'Approximate past match on minutes, significance=1' |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$this->assertEquals( |
200
|
|
|
'in 61 mins', |
201
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(), |
202
|
|
|
'Approximate past match on minutes' |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
DBDatetime::clear_mock_now(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testRfc3999() |
209
|
|
|
{ |
210
|
|
|
// Dates should be formatted as: 2018-01-24T14:05:53+00:00 |
|
|
|
|
211
|
|
|
$date = DBDatetime::create_field('Datetime', '2010-12-31 16:58:59'); |
212
|
|
|
$this->assertEquals('2010-12-31T16:58:59+00:00', $date->Rfc3339()); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|