Passed
Pull Request — 4.1 (#8170)
by Damian
06:18
created

DBDatetimeTest::testHindiNumerals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\i18n\i18n;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
9
/**
10
 * Tests for {@link Datetime} class.
11
 */
12
class DBDatetimeTest extends SapphireTest
13
{
14
    protected function setUp()
15
    {
16
        parent::setUp();
17
        i18n::set_locale('en_NZ');
18
    }
19
20
    public function testNowWithSystemDate()
21
    {
22
        $systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
23
        $nowDatetime = DBDatetime::now();
24
25
        $this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
0 ignored issues
show
Bug introduced by
The method Date() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $this->assertEquals($systemDatetime->/** @scrutinizer ignore-call */ Date(), $nowDatetime->Date());
Loading history...
26
    }
27
28
    public function testNowWithMockDate()
29
    {
30
        // Test setting
31
        $mockDate = '2001-12-31 22:10:59';
32
        DBDatetime::set_mock_now($mockDate);
33
        $systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
34
        $nowDatetime = DBDatetime::now();
35
        $this->assertNotEquals($systemDatetime->Date(), $nowDatetime->Date());
36
        $this->assertEquals($nowDatetime->getValue(), $mockDate);
37
38
        // Test clearing
39
        DBDatetime::clear_mock_now();
40
        $systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
41
        $nowDatetime = DBDatetime::now();
42
        $this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
43
    }
44
45
    public function testSetNullAndZeroValues()
46
    {
47
        $date = DBDatetime::create_field('Datetime', '');
48
        $this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
49
50
        $date = DBDatetime::create_field('Datetime', null);
51
        $this->assertNull($date->getValue(), 'NULL is set as NULL');
52
53
        $date = DBDatetime::create_field('Datetime', false);
54
        $this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
55
56
        $date = DBDatetime::create_field('Datetime', '0');
57
        $this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time');
58
59
        $date = DBDatetime::create_field('Datetime', 0);
60
        $this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time');
61
    }
62
63
    public function testExtendedDateTimes()
64
    {
65
        $date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
66
        $this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s'));
0 ignored issues
show
Bug introduced by
The method Format() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $this->assertEquals('10 Oct 1600 15 32 24', $date->/** @scrutinizer ignore-call */ Format('d MMM y H m s'));
Loading history...
67
68
        $date = DBDatetime::create_field('Datetime', '3000-10-10 15:32:24');
69
        $this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d MMM y H m s'));
70
    }
71
72
    /**
73
     * Coverage for dates using hindi-numerals
74
     */
75
    public function testHindiNumerals()
76
    {
77
        // Parent locale is english; Can be localised to arabic
78
        $date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
79
        $this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s'));
80
        $this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s', 'ar'));
81
82
        // Parent locale is arabic; Datavalue uses ISO date
83
        i18n::set_locale('ar');
84
        $date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
85
        $this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s'));
86
        $this->assertEquals('1600-10-10 15:32:24', $date->getValue());
87
    }
88
89
    public function testNice()
90
    {
91
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
92
        // note: Some localisation packages exclude the ',' in default medium format
93
        $this->assertRegExp('#31/12/2001(,)? 10:10:59 PM#i', $date->Nice());
0 ignored issues
show
Bug introduced by
The method Nice() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        $this->assertRegExp('#31/12/2001(,)? 10:10:59 PM#i', $date->/** @scrutinizer ignore-call */ Nice());
Loading history...
94
    }
95
96
    public function testDate()
97
    {
98
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
99
        $this->assertEquals('31/12/2001', $date->Date());
100
    }
101
102
    public function testTime()
103
    {
104
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
105
        $this->assertRegexp('#10:10:59 PM#i', $date->Time());
0 ignored issues
show
Bug introduced by
The method Time() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        $this->assertRegexp('#10:10:59 PM#i', $date->/** @scrutinizer ignore-call */ Time());
Loading history...
106
    }
107
108
    public function testTime24()
109
    {
110
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
111
        $this->assertEquals('22:10', $date->Time24());
0 ignored issues
show
Bug introduced by
The method Time24() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $this->assertEquals('22:10', $date->/** @scrutinizer ignore-call */ Time24());
Loading history...
112
    }
113
114
    public function testURLDateTime()
115
    {
116
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
117
        $this->assertEquals('2001-12-31%2022%3A10%3A59', $date->URLDateTime());
0 ignored issues
show
Bug introduced by
The method URLDateTime() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        $this->assertEquals('2001-12-31%2022%3A10%3A59', $date->/** @scrutinizer ignore-call */ URLDateTime());
Loading history...
118
    }
119
120
    public function testAgoInPast()
121
    {
122
        DBDatetime::set_mock_now('2000-12-31 12:00:00');
123
124
        $this->assertEquals(
125
            '10 years ago',
126
            DBDatetime::create_field('Datetime', '1990-12-31 12:00:00')->Ago(),
0 ignored issues
show
Bug introduced by
The method Ago() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
            DBDatetime::create_field('Datetime', '1990-12-31 12:00:00')->/** @scrutinizer ignore-call */ Ago(),
Loading history...
127
            'Exact past match on years'
128
        );
129
130
        $this->assertEquals(
131
            '10 years ago',
132
            DBDatetime::create_field('Datetime', '1990-12-30 12:00:00')->Ago(),
133
            'Approximate past match on years'
134
        );
135
136
        $this->assertEquals(
137
            '1 year ago',
138
            DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(true, 1),
139
            'Approximate past match in singular, significance=1'
140
        );
141
142
        $this->assertEquals(
143
            '12 months ago',
144
            DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(),
145
            'Approximate past match in singular'
146
        );
147
148
        $this->assertEquals(
149
            '50 mins ago',
150
            DBDatetime::create_field('Datetime', '2000-12-31 11:10:11')->Ago(),
151
            'Approximate past match on minutes'
152
        );
153
154
        $this->assertEquals(
155
            '59 secs ago',
156
            DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(),
157
            'Approximate past match on seconds'
158
        );
159
160
        $this->assertEquals(
161
            'less than a minute ago',
162
            DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(false),
163
            'Approximate past match on seconds with $includeSeconds=false'
164
        );
165
166
        $this->assertEquals(
167
            '1 min ago',
168
            DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(false),
169
            'Test between 1 and 2 minutes with includeSeconds=false'
170
        );
171
172
        $this->assertEquals(
173
            '70 secs ago',
174
            DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(true),
175
            'Test between 1 and 2 minutes with includeSeconds=true'
176
        );
177
178
        $this->assertEquals(
179
            '4 mins ago',
180
            DBDatetime::create_field('Datetime', '2000-12-31 11:55:50')->Ago(),
181
            'Past match on minutes'
182
        );
183
184
        $this->assertEquals(
185
            '1 hour ago',
186
            DBDatetime::create_field('Datetime', '2000-12-31 10:50:58')->Ago(true, 1),
187
            'Past match on hours, significance=1'
188
        );
189
190
        $this->assertEquals(
191
            '3 hours ago',
192
            DBDatetime::create_field('Datetime', '2000-12-31 08:50:58')->Ago(),
193
            'Past match on hours'
194
        );
195
196
        DBDatetime::clear_mock_now();
197
    }
198
199
    public function testAgoInFuture()
200
    {
201
        DBDatetime::set_mock_now('2000-12-31 00:00:00');
202
203
        $this->assertEquals(
204
            'in 10 years',
205
            DBDatetime::create_field('Datetime', '2010-12-31 12:00:00')->Ago(),
206
            'Exact past match on years'
207
        );
208
209
        $this->assertEquals(
210
            'in 1 hour',
211
            DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(true, 1),
212
            'Approximate past match on minutes, significance=1'
213
        );
214
215
        $this->assertEquals(
216
            'in 61 mins',
217
            DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(),
218
            'Approximate past match on minutes'
219
        );
220
221
        DBDatetime::clear_mock_now();
222
    }
223
224
    public function testRfc3999()
225
    {
226
        // Dates should be formatted as: 2018-01-24T14:05:53+00:00
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
227
        $date = DBDatetime::create_field('Datetime', '2010-12-31 16:58:59');
228
        $this->assertEquals('2010-12-31T16:58:59+00:00', $date->Rfc3339());
0 ignored issues
show
Bug introduced by
The method Rfc3339() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

228
        $this->assertEquals('2010-12-31T16:58:59+00:00', $date->/** @scrutinizer ignore-call */ Rfc3339());
Loading history...
229
    }
230
}
231