aboutsummaryrefslogtreecommitdiffstats
path: root/tests/datetime/from_format_test.php
blob: 7ecb546768d73694715a0adeaf1c84a3813aa5d0 (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
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

require_once dirname(__FILE__) . '/../mock/lang.php';

class phpbb_datetime_from_format_test extends phpbb_test_case
{
	public function from_format_data()
	{
		return array(
			array(
				'UTC',
				'Y-m-d',
				'2012-06-08',
			),

			array(
				'Europe/Berlin',
				'Y-m-d H:i:s',
				'2012-06-08 14:01:02',
			),
		);
	}

	/**
	* @dataProvider from_format_data()
	*/
	public function test_from_format($timezone, $format, $expected)
	{
		global $phpbb_root_path, $phpEx;

		$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
		$lang = new \phpbb\language\language($lang_loader);
		$user = new \phpbb\user($lang, '\phpbb\datetime');
		$user->timezone = new DateTimeZone($timezone);
		$user->lang['datetime'] = array(
			'TODAY'		=> 'Today',
			'TOMORROW'	=> 'Tomorrow',
			'YESTERDAY'	=> 'Yesterday',
			'AGO'		=> array(
				0		=> 'less than a minute ago',
				1		=> '%d minute ago',
				2		=> '%d minutes ago',
			),
		);

		$timestamp = $user->get_timestamp_from_format($format, $expected, new DateTimeZone($timezone));
		$this->assertEquals($expected, $user->format_date($timestamp, $format, true));
	}


	public function relative_format_date_data()
	{
		// If the current time is too close to the testing time,
		// the relative time will use "x minutes ago" instead of "today ..."
		// So we use 18:01 in the morning and 06:01 in the afternoon.
		$testing_time = gmdate('H') <= 12 ? '18:01' : '06:01';

		return array(
			array(
				gmdate('Y-m-d', time() + 2 * 86400) . ' ' . $testing_time, false,
				gmdate('Y-m-d', time() + 2 * 86400) . ' ' . $testing_time,
			),

			array(
				gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time, false,
				'Tomorrow ' . $testing_time,
			),
			array(
				gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time, true,
				gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time,
			),

			array(
				gmdate('Y-m-d') . ' ' . $testing_time, false,
				'Today ' . $testing_time,
			),
			array(
				gmdate('Y-m-d') . ' ' . $testing_time, true,
				gmdate('Y-m-d') . ' ' . $testing_time,
			),

			array(
				gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time, false,
				'Yesterday ' . $testing_time,
			),
			array(
				gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time, true,
				gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time,
			),

			array(
				gmdate('Y-m-d', time() - 2 * 86400) . ' ' . $testing_time, false,
				gmdate('Y-m-d', time() - 2 * 86400) . ' ' . $testing_time,
			),
		);
	}

	/**
	 * @dataProvider relative_format_date_data()
	 */
	public function test_relative_format_date($timestamp, $forcedate, $expected)
	{
		global $phpbb_root_path, $phpEx;

		$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
		$lang = new \phpbb\language\language($lang_loader);
		$user = new \phpbb\user($lang, '\phpbb\datetime');
		$user->timezone = new DateTimeZone('UTC');
		$user->lang['datetime'] = array(
			'TODAY'		=> 'Today',
			'TOMORROW'	=> 'Tomorrow',
			'YESTERDAY'	=> 'Yesterday',
			'AGO'		=> array(
				0		=> 'less than a minute ago',
				1		=> '%d minute ago',
				2		=> '%d minutes ago',
			),
		);

		$timestamp = $user->get_timestamp_from_format('Y-m-d H:i', $timestamp, new DateTimeZone('UTC'));
		$this->assertEquals($expected, $user->format_date($timestamp, '|Y-m-d| H:i', $forcedate));
	}
}