Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
30 / 90
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_datetime_from_format_test
33.33% covered (danger)
33.33%
30 / 90
40.00% covered (danger)
40.00%
2 / 5
39.63
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
20 / 20
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_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
31        // Set up language data for testing
32        $reflection_class = new ReflectionClass('\phpbb\language\language');
33        // Set default language files loaded flag to true
34        $common_language_files_loaded_flag = $reflection_class->getProperty('common_language_files_loaded');
35        $common_language_files_loaded_flag->setValue($this->lang, true);
36        // Set up test language data
37        $lang_array = $reflection_class->getProperty('lang');
38        $lang_array->setValue($this->lang, [
39            'datetime' => [
40                'TODAY'        => 'Today',
41                'TOMORROW'    => 'Tomorrow',
42                'YESTERDAY'    => 'Yesterday',
43                'AGO'        => [
44                    0        => 'less than a minute ago',
45                    1        => '%d minute ago',
46                    2        => '%d minutes ago',
47                ],        
48            ],
49        ]);
50
51        $this->user = new \phpbb\user($this->lang, '\phpbb\datetime');
52    }
53
54    public static function from_format_data()
55    {
56        return [
57            [
58                'UTC',
59                'Y-m-d',
60                '2012-06-08',
61            ],
62
63            [
64                'Europe/Berlin',
65                'Y-m-d H:i:s',
66                '2012-06-08 14:01:02',
67            ],
68        ];
69    }
70
71    /**
72    * @dataProvider from_format_data()
73    */
74    public function test_from_format($timezone, $format, $expected)
75    {
76        $this->user->timezone = new DateTimeZone($timezone);
77        $timestamp = $this->user->get_timestamp_from_format($format, $expected, new DateTimeZone($timezone));
78        $this->assertEquals($expected, $this->user->format_date($timestamp, $format, true));
79    }
80
81    public static function relative_format_date_data()
82    {
83        // If the current time is too close to the testing time,
84        // the relative time will use "x minutes ago" instead of "today ..."
85        // So we use 18:01 in the morning and 06:01 in the afternoon.
86        $testing_time = gmdate('H') <= 12 ? '18:01' : '06:01';
87
88        return [
89            [
90                gmdate('Y-m-d', time() + 2 * 86400) . ' ' . $testing_time, false,
91                gmdate('Y-m-d', time() + 2 * 86400) . ' ' . $testing_time,
92            ],
93            [
94                gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time, false,
95                'Tomorrow ' . $testing_time,
96            ],
97            [
98                gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time, true,
99                gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time,
100            ],
101            [
102                gmdate('Y-m-d') . ' ' . $testing_time, false,
103                'Today ' . $testing_time,
104            ],
105            [
106                gmdate('Y-m-d') . ' ' . $testing_time, true,
107                gmdate('Y-m-d') . ' ' . $testing_time,
108            ],
109            [
110                gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time, false,
111                'Yesterday ' . $testing_time,
112            ],
113            [
114                gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time, true,
115                gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time,
116            ],
117            [
118                gmdate('Y-m-d', time() - 2 * 86400) . ' ' . $testing_time, false,
119                gmdate('Y-m-d', time() - 2 * 86400) . ' ' . $testing_time,
120            ],
121
122            // Test edge cases: Yesterday 00:00, Today 00:00, Tomorrow 00:00
123            [
124                gmdate('Y-m-d', strtotime('yesterday')) . ' 00:00', false,
125                'Yesterday 00:00',
126            ],
127            [
128                gmdate('Y-m-d', strtotime('today')) . ' 00:00', false,
129                'Today 00:00',
130            ],
131            [
132                gmdate('Y-m-d', strtotime('tomorrow')) . ' 00:00', false,
133                'Tomorrow 00:00',
134            ],
135        ];
136    }
137
138    /**
139     * @dataProvider relative_format_date_data()
140     */
141    public function test_relative_format_date($timestamp, $forcedate, $expected)
142    {
143        $this->user->timezone = new DateTimeZone('UTC');
144        $timestamp = $this->user->get_timestamp_from_format('Y-m-d H:i', $timestamp, new DateTimeZone('UTC'));
145
146        /* This code is equal to the one from \phpbb\datetime function format()
147         * If the delta is less than or equal to 1 hour
148         * and the delta not more than a minute in the past
149         * and the delta is either greater than -5 seconds
150         * or timestamp and current time are of the same minute
151         * format_date() will return relative date format using "... ago" options
152         */
153        $now_ts = strtotime('now');
154        $delta = $now_ts - $timestamp;
155        if ($delta <= 3600 && $delta > -60 &&
156            ($delta >= -5 || (($now_ts/ 60) % 60) == (($timestamp / 60) % 60))
157        )
158        {
159            $expected = $this->lang->lang(['datetime', 'AGO'], max(0, (int) floor($delta / 60)));
160        }
161
162        $this->assertEquals($expected, $this->user->format_date($timestamp, '|Y-m-d| H:i', $forcedate));
163    }
164}