summaryrefslogtreecommitdiff
path: root/tests/units/Core/DateParserTest.php
blob: fbde8bd5815d9aa59f01304b73d2e8975d8dbce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php

require_once __DIR__.'/../Base.php';

use Kanboard\Core\DateParser;

class DateParserTest extends Base
{
    public function testGetTimeFormats()
    {
        $dateParser = new DateParser($this->container);
        $this->assertCount(2, $dateParser->getTimeFormats());
        $this->assertContains('H:i', $dateParser->getTimeFormats());
        $this->assertContains('g:i a', $dateParser->getTimeFormats());
    }

    public function testGetDateFormats()
    {
        $dateParser = new DateParser($this->container);
        $this->assertCount(4, $dateParser->getDateFormats());
        $this->assertCount(6, $dateParser->getDateFormats(true));
        $this->assertContains('d/m/Y', $dateParser->getDateFormats());
        $this->assertNotContains('Y-m-d', $dateParser->getDateFormats());
        $this->assertContains('Y-m-d', $dateParser->getDateFormats(true));
    }

    public function testThatUserDateFormatIsReturnedFirst()
    {
        $dateParser = new DateParser($this->container);

        $dates = $dateParser->getDateFormats();
        $this->assertEquals('m/d/Y', $dates[0]);

        $dates = $dateParser->getDateFormats(true);
        $this->assertEquals('m/d/Y', $dates[0]);

        $this->container['configModel']->save(array('application_date_format' => 'd/m/Y'));
        $this->container['memoryCache']->flush();

        $dates = $dateParser->getDateFormats();
        $this->assertEquals('d/m/Y', $dates[0]);

        $dates = $dateParser->getDateFormats(true);
        $this->assertEquals('d/m/Y', $dates[0]);
    }

    public function testGetDateTimeFormats()
    {
        $dateParser = new DateParser($this->container);
        $this->assertCount(8, $dateParser->getDateTimeFormats());
        $this->assertCount(12, $dateParser->getDateTimeFormats(true));
        $this->assertContains('d/m/Y H:i', $dateParser->getDateTimeFormats());
        $this->assertNotContains('Y-m-d H:i', $dateParser->getDateTimeFormats());
        $this->assertContains('Y-m-d g:i a', $dateParser->getDateTimeFormats(true));
    }

    public function testThatUserDateTimeFormatIsReturnedFirst()
    {
        $dateParser = new DateParser($this->container);

        $dates = $dateParser->getDateTimeFormats();
        $this->assertEquals('m/d/Y H:i', $dates[0]);

        $dates = $dateParser->getDateTimeFormats(true);
        $this->assertEquals('m/d/Y H:i', $dates[0]);

        $this->container['configModel']->save(array('application_datetime_format' => 'd/m/Y g:i a'));
        $this->container['memoryCache']->flush();

        $dates = $dateParser->getDateTimeFormats();
        $this->assertEquals('d/m/Y g:i a', $dates[0]);

        $dates = $dateParser->getDateTimeFormats(true);
        $this->assertEquals('d/m/Y g:i a', $dates[0]);
    }

    public function testGetAllDateFormats()
    {
        $dateParser = new DateParser($this->container);
        $this->assertCount(12, $dateParser->getAllDateFormats());
        $this->assertCount(18, $dateParser->getAllDateFormats(true));
        $this->assertContains('d/m/Y', $dateParser->getAllDateFormats());
        $this->assertContains('d/m/Y H:i', $dateParser->getAllDateFormats());
        $this->assertNotContains('Y-m-d H:i', $dateParser->getAllDateFormats());
        $this->assertContains('Y-m-d g:i a', $dateParser->getAllDateFormats(true));
        $this->assertContains('Y-m-d', $dateParser->getAllDateFormats(true));
    }

    public function testGetAllAvailableFormats()
    {
        $dateParser = new DateParser($this->container);

        $formats = $dateParser->getAvailableFormats($dateParser->getDateFormats());
        $this->assertArrayHasKey('d/m/Y', $formats);
        $this->assertContains(date('d/m/Y').' (d/m/Y)', $formats);

        $formats = $dateParser->getAvailableFormats($dateParser->getDateTimeFormats());
        $this->assertArrayHasKey('d/m/Y H:i', $formats);
        $this->assertContains(date('d/m/Y H:i').' (d/m/Y H:i)', $formats);

        $formats = $dateParser->getAvailableFormats($dateParser->getAllDateFormats());
        $this->assertArrayHasKey('d/m/Y', $formats);
        $this->assertContains(date('d/m/Y').' (d/m/Y)', $formats);
        $this->assertArrayHasKey('d/m/Y H:i', $formats);
        $this->assertContains(date('d/m/Y H:i').' (d/m/Y H:i)', $formats);
    }

    public function testGetTimestampFromDefaultFormats()
    {
        $dateParser = new DateParser($this->container);

        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('06/09/2016')));
        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('2016-06-09')));
        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('2016_06_09')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('2016-06-09 21:15')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('2016_06_09 21:15')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('06/09/2016 21:15')));
    }

    public function testGetTimestampFromUserDateFormats()
    {
        $this->container['configModel']->save(array(
            'application_date_format' => 'd/m/Y',
            'application_datetime_format' => 'd/m/Y g:i a',
        ));

        $dateParser = new DateParser($this->container);

        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('09/06/2016')));
        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('2016-06-09')));
        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('2016_06_09')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('2016-06-09 21:15')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('2016_06_09 21:15')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('09/06/2016 9:15 pm')));
    }

    public function testGetTimestampFromAnotherUserDateFormats()
    {
        $this->container['configModel']->save(array(
            'application_date_format' => 'd.m.Y',
            'application_datetime_format' => 'd.m.Y H:i',
        ));

        $dateParser = new DateParser($this->container);

        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('09.06.2016')));
        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('2016-06-09')));
        $this->assertEquals('2016-06-09', date('Y-m-d', $dateParser->getTimestamp('2016_06_09')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('2016-06-09 21:15')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('2016_06_09 21:15')));
        $this->assertEquals('2016-06-09 21:15', date('Y-m-d H:i', $dateParser->getTimestamp('09.06.2016 21:15')));
    }

    public function testDateRange()
    {
        $dateParser = new DateParser($this->container);

        $this->assertTrue($dateParser->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00')));
        $this->assertFalse($dateParser->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 16:00:00'), new DateTime('2015-03-14 17:00:00')));
    }

    public function testGetHours()
    {
        $dateParser = new DateParser($this->container);

        $this->assertEquals(1, $dateParser->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00')));
        $this->assertEquals(2.5, $dateParser->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:30:00')));
        $this->assertEquals(2.75, $dateParser->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:45:00')));
        $this->assertEquals(3, $dateParser->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 17:58:00')));
        $this->assertEquals(3, $dateParser->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 11:58:00')));
    }

    public function testRoundSeconds()
    {
        $dateParser = new DateParser($this->container);
        $this->assertEquals('16:30', date('H:i', $dateParser->getRoundedSeconds(strtotime('16:28'))));
        $this->assertEquals('16:00', date('H:i', $dateParser->getRoundedSeconds(strtotime('16:02'))));
        $this->assertEquals('16:15', date('H:i', $dateParser->getRoundedSeconds(strtotime('16:14'))));
        $this->assertEquals('17:00', date('H:i', $dateParser->getRoundedSeconds(strtotime('16:58'))));
    }

    public function testGetIsoDate()
    {
        $dateParser = new DateParser($this->container);

        $this->assertEquals('2016-02-06', $dateParser->getIsoDate(1454786217));
        $this->assertEquals('2014-03-05', $dateParser->getIsoDate('2014-03-05'));
        $this->assertEquals('2014-03-05', $dateParser->getIsoDate('2014_03_05'));
        $this->assertEquals('2014-03-05', $dateParser->getIsoDate('03/05/2014'));
        $this->assertEquals('2014-03-25', $dateParser->getIsoDate('03/25/2014 23:14'));
        $this->assertEquals('2014-03-29', $dateParser->getIsoDate('2014_03_29 23:14'));
        $this->assertEquals('2014-03-29', $dateParser->getIsoDate('2014-03-29 23:14'));
    }

    public function testGetTimestampFromIsoFormat()
    {
        $dateParser = new DateParser($this->container);
        $this->assertEquals('2014-03-05 00:00', date('Y-m-d H:i', $dateParser->getTimestampFromIsoFormat('2014-03-05')));
        $this->assertEquals(date('Y-m-d 00:00', strtotime('+2 days')), date('Y-m-d H:i', $dateParser->getTimestampFromIsoFormat(strtotime('+2 days'))));
    }

    public function testRemoveTimeFromTimestamp()
    {
        $dateParser = new DateParser($this->container);
        $this->assertEquals('2016-02-06 00:00', date('Y-m-d H:i', $dateParser->removeTimeFromTimestamp(1454786217)));
    }

    public function testFormat()
    {
        $dateParser = new DateParser($this->container);
        $values['date'] = '1454787006';

        $this->assertEquals(array('date' => '06/02/2016'), $dateParser->format($values, array('date'), 'd/m/Y'));
        $this->assertEquals(array('date' => '02/06/2016 7:30 pm'), $dateParser->format($values, array('date'), 'm/d/Y g:i a'));
    }

    public function testConvert()
    {
        $dateParser = new DateParser($this->container);
        $values = array(
            'date_due' => '2015-01-25',
            'date_started' => '2015-01-25 17:25',
        );

        $this->assertEquals(
            array('date_due' => 1422144000, 'date_started' => 1422144000),
            $dateParser->convert($values, array('date_due', 'date_started'))
        );

        $values = array(
            'date_started' => '2015-01-25 17:25',
        );

        $this->assertEquals(
            array('date_started' => 1422206700),
            $dateParser->convert($values, array('date_due', 'date_started'), true)
        );
    }
}