]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/DateTimeFormatTest.php
Merge pull request #13375 from MrPetovan/bug/empty-timeline
[friendica.git] / tests / src / Util / DateTimeFormatTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util;
23
24 use Friendica\Test\MockedTest;
25 use Friendica\Util\DateTimeFormat;
26
27 class DateTimeFormatTest extends MockedTest
28 {
29         public function dataYearMonth()
30         {
31                 return [
32                         'validNormal' => [
33                                 'input' => '1990-10',
34                                 'assert' => true,
35                         ],
36                         'validOneCharMonth' => [
37                                 'input' => '1990-1',
38                                 'assert' => true,
39                         ],
40                         'validTwoCharMonth' => [
41                                 'input' => '1990-01',
42                                 'assert' => true,
43                         ],
44                         'invalidFormat' => [
45                                 'input' => '199-11',
46                                 'assert' => false,
47                         ],
48                         'invalidFormat2' => [
49                                 'input' => '1990-15',
50                                 'assert' => false,
51                         ],
52                         'invalidFormat3' => [
53                                 'input' => '99-101',
54                                 'assert' => false,
55                         ],
56                         'invalidFormat4' => [
57                                 'input' => '11-1990',
58                                 'assert' => false,
59                         ],
60                         'invalidFuture' => [
61                                 'input' => '3030-12',
62                                 'assert' => false,
63                         ],
64                         'invalidYear' => [
65                                 'input' => '-100-10',
66                                 'assert' => false,
67                         ],
68                 ];
69         }
70
71         /**
72          * @dataProvider dataYearMonth
73          */
74         public function testIsYearMonth(string $input, bool $assert)
75         {
76                 $dtFormat = new DateTimeFormat();
77
78                 self::assertEquals($assert, $dtFormat->isYearMonth($input));
79         }
80
81         /**
82          * Test the DateTimeFormat::API output.
83          *
84          * @return void
85          */
86         public function testApiDate()
87         {
88                 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', DateTimeFormat::utc('1990-10-10', DateTimeFormat::API));
89         }
90
91         public function dataFix(): array
92         {
93                 return [
94                         'Mo, 19 Sep 2022 14:51:00 +0200' => [
95                                 'expectedDate' => '2022-09-19T14:51:00+02:00',
96                                 'dateString' => 'Mo, 19 Sep 2022 14:51:00 +0200',
97                         ],
98                         '2020-11-21T12:00:13.745339ZZ' => [
99                                 'expectedDate' => '2020-11-21T12:00:13+00:00',
100                                 'dateString' => '2020-11-21T12:00:13.745339ZZ',
101                         ],
102                         '2016-09-09T13:32:00ZZ' => [
103                                 'expectedDate' => '2016-09-09T13:32:00+00:00',
104                                 'dateString' => '2016-09-09T13:32:00ZZ',
105                         ],
106                         'Sun, 10/03/2021 - 12:41' => [
107                                 'expectedDate' => '2021-10-03T12:41:00+00:00',
108                                 'dateString' => 'Sun, 10/03/2021 - 12:41',
109                         ],
110                         '4:30 PM, Sep 13, 2022' => [
111                                 'expectedDate' => '2022-09-13T16:30:00+00:00',
112                                 'dateString' => '4:30 PM, Sep 13, 2022',
113                         ],
114                         'August 27, 2022 - 21:00' => [
115                                 'expectedDate' => '2022-08-27T21:00:00+00:00',
116                                 'dateString' => 'August 27, 2022 - 21:00',
117                         ],
118                         '2021-09-19T14:06:03&#x2B;00:00' => [
119                                 'expectedDate' => '2021-09-19T14:06:03+00:00',
120                                 'dateString' => '2021-09-19T14:06:03&#x2B;00:00',
121                         ],
122                         'Eastern Time timezone' => [
123                                 'expectedDate' => '2022-09-30T00:00:00-05:00',
124                                 'dateString' => 'September 30, 2022, 12:00 a.m. ET',
125                         ],
126                         'German date time string' => [
127                                 'expectedDate' => '2022-10-05T16:34:00+02:00',
128                                 'dateString' => '05 Okt 2022 16:34:00 +0200',
129                         ],
130                         '(Coordinated Universal Time)' => [
131                                 'expectedDate' => '2022-12-30T14:29:10+00:00',
132                                 'dateString' => 'Fri Dec 30 2022 14:29:10 GMT+0000 (Coordinated Universal Time)',
133                         ],
134                         'Double HTML encode' => [
135                                 'expectedDate' => '2015-05-22T08:48:00+12:00',
136                                 'dateString' => '2015-05-22T08:48:00&amp;#43;12:00'
137                         ],
138                         '2023-04-02\T17:22:42+05:30' => [
139                                 'expectedDate' => '2023-04-02T17:22:42+05:30',
140                                 'dateString' => '2023-04-02\T17:22:42+05:30'
141                         ],
142                 ];
143         }
144
145         /**
146          * @dataProvider dataFix
147          *
148          * @param $expectedDate
149          * @param $dateString
150          * @return void
151          * @throws \Exception
152          */
153         public function testFix($expectedDate, $dateString)
154         {
155                 $fixed = DateTimeFormat::fix($dateString);
156
157                 $this->assertEquals($expectedDate, (new \DateTime($fixed))->format('c'));
158         }
159
160         /**
161          * This test is meant to ensure DateTimeFormat::fix() isn't called on relative date/time strings
162          *
163          * @return void
164          * @throws \Exception
165          */
166         public function testConvertRelative()
167         {
168                 $now = DateTimeFormat::utcNow('U');
169                 $date = DateTimeFormat::utc('now - 3 days', 'U');
170
171                 $this->assertEquals(259200, $now - $date);
172         }
173 }