Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
34.07% covered (danger)
34.07%
31 / 91
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_datetime_from_format_test
34.07% covered (danger)
34.07%
31 / 91
40.00% covered (danger)
40.00%
2 / 5
38.66
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 from_format_data
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 test_from_format
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 relative_format_date_data
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
6
 test_relative_format_date
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14require_once __DIR__ . '/../mock/lang.php';
15
16class phpbb_datetime_from_format_test extends phpbb_test_case
17{
18    /** @var \phpbb\language\language */
19    protected $lang;
20
21    /** @var \phpbb\user */
22    protected $user;
23
24    protected function setUp(): void
25    {
26        global $phpbb_dispatcher, $phpbb_root_path, $phpEx;
27
28        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
29        $this->lang = new \phpbb\language\language($lang_loader);
30        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
31
32        // Set up language data for testing
33        $reflection_class = new ReflectionClass('\phpbb\language\language');
34        // Set default language files loaded flag to true
35        $common_language_files_loaded_flag = $reflection_class->getProperty('common_language_files_loaded');
36        $common_language_files_loaded_flag->setValue($this->lang, true);
37        // Set up test language data
38        $lang_array = $reflection_class->getProperty('lang');
39        $lang_array->setValue($this->lang, [
40            'datetime' => [
41                'TODAY'        => 'Today',
42                'TOMORROW'    => 'Tomorrow',
43                'YESTERDAY'    => 'Yesterday',
44                'AGO'        => [
45                    0        => 'less than a minute ago',
46                    1        => '%d minute ago',
47                    2        => '%d minutes ago',
48                ],
49            ],
50        ]);
51
52        $this->user = new \phpbb\user($this->lang, '\phpbb\datetime');
53    }
54
55    public static function from_format_data()
56    {
57        return [
58            [
59                'UTC',
60                'Y-m-d',
61                '2012-06-08',
62            ],
63
64            [
65                'Europe/Berlin',
66                'Y-m-d H:i:s',
67                '2012-06-08 14:01:02',
68            ],
69        ];
70    }
71
72    /**
73    * @dataProvider from_format_data()
74    */
75    public function test_from_format($timezone, $format, $expected)
76    {
77        $this->user->timezone = new DateTimeZone($timezone);
78        $timestamp = $this->user->get_timestamp_from_format($format, $expected, new DateTimeZone($timezone));
79        $this->assertEquals($expected, $this->user->format_date($timestamp, $format, true));
80    }
81
82    public static function relative_format_date_data()
83    {
84        // If the current time is too close to the testing time,
85        // the relative time will use "x minutes ago" instead of "today ..."
86        // So we use 18:01 in the morning and 06:01 in the afternoon.
87        $testing_time = gmdate('H') <= 12 ? '18:01' : '06:01';
88
89        return [
90            [
91                gmdate('Y-m-d', strtotime('+2day')) . ' ' . $testing_time, false,
92                gmdate('Y-m-d', strtotime('+2day')) . ' ' . $testing_time,
93            ],
94            [
95                gmdate('Y-m-d', strtotime('+1day')) . ' ' . $testing_time, false,
96                'Tomorrow ' . $testing_time,
97            ],
98            [
99                gmdate('Y-m-d', strtotime('+1day')) . ' ' . $testing_time, true,
100                gmdate('Y-m-d', strtotime('+1day')) . ' ' . $testing_time,
101            ],
102            [
103                gmdate('Y-m-d') . ' ' . $testing_time, false,
104                'Today ' . $testing_time,
105            ],
106            [
107                gmdate('Y-m-d') . ' ' . $testing_time, true,
108                gmdate('Y-m-d') . ' ' . $testing_time,
109            ],
110            [
111                gmdate('Y-m-d', strtotime('-1day')) . ' ' . $testing_time, false,
112                'Yesterday ' . $testing_time,
113            ],
114            [
115                gmdate('Y-m-d', strtotime('-1day')) . ' ' . $testing_time, true,
116                gmdate('Y-m-d', strtotime('-1day')) . ' ' . $testing_time,
117            ],
118            [
119                gmdate('Y-m-d', strtotime('-2day')) . ' ' . $testing_time, false,
120                gmdate('Y-m-d', strtotime('-2day')) . ' ' . $testing_time,
121            ],
122
123            // Test edge cases: Yesterday 00:00, Today 00:00, Tomorrow 00:00
124            [
125                gmdate('Y-m-d', strtotime('-1day')) . ' 00:00', false,
126                'Yesterday 00:00',
127            ],
128            [
129                gmdate('Y-m-d', strtotime('now')) . ' 00:00', false,
130                'Today 00:00',
131            ],
132            [
133                gmdate('Y-m-d', strtotime('+1day')) . ' 00:00', false,
134                'Tomorrow 00:00',
135            ],
136        ];
137    }
138
139    /**
140     * @dataProvider relative_format_date_data()
141     */
142    public function test_relative_format_date($timestamp, $forcedate, $expected)
143    {
144        $this->user->timezone = new DateTimeZone('UTC');
145        $timestamp = $this->user->get_timestamp_from_format('Y-m-d H:i', $timestamp, new DateTimeZone('UTC'));
146
147        /* This code is equal to the one from \phpbb\datetime function format()
148         * If the delta is less than or equal to 1 hour
149         * and the delta not more than a minute in the past
150         * and the delta is either greater than -5 seconds
151         * or timestamp and current time are of the same minute
152         * format_date() will return relative date format using "... ago" options
153         */
154        $now_ts = strtotime('now');
155        $delta = $now_ts - $timestamp;
156        if ($delta <= 3600 && $delta > -60 &&
157            ($delta >= -5 || (($now_ts/ 60) % 60) == (($timestamp / 60) % 60))
158        )
159        {
160            $expected = $this->lang->lang(['datetime', 'AGO'], max(0, (int) floor($delta / 60)));
161        }
162
163        $this->assertEquals($expected, $this->user->format_date($timestamp, '|Y-m-d| H:i', $forcedate));
164    }
165}