aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functions/obtain_online_test.php
blob: e6929b15495295cf3bdd507a816227bcc0e4ed0f (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
<?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.
*
*/

class phpbb_functions_obtain_online_test extends phpbb_database_test_case
{
	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/obtain_online.xml');
	}

	protected function setUp(): void
	{
		parent::setUp();

		global $config, $db, $user;

		$user = new StdClass;
		$db = $this->db = $this->new_dbal();
		$config = array(
			'load_online_time'	=> 5,
		);
	}

	static public function obtain_guest_count_data()
	{
		return array(
			array(0, 2),
			array(1, 1),
		);
	}

	/**
	* @dataProvider obtain_guest_count_data
	*/
	public function test_obtain_guest_count($forum_id, $expected)
	{
		$this->db->sql_query('DELETE FROM phpbb_sessions');

		$time = time();
		$this->create_guest_sessions($time);
		$this->assertEquals($expected, obtain_guest_count($forum_id));
	}

	static public function obtain_users_online_data()
	{
		return array(
			array(0, false, array(
				'online_users'			=> array(2 => 2, 3 => 3, 6 => 6, 7 => 7, 10 => 10),
				'hidden_users'			=> array(6 => 6, 7 => 7, 10 => 10),
				'total_online'			=> 5,
				'visible_online'		=> 2,
				'hidden_online'			=> 3,
				'guests_online'			=> 0,
			)),
			array(0, true, array(
				'online_users'			=> array(2 => 2, 3 => 3, 6 => 6, 7 => 7, 10 => 10),
				'hidden_users'			=> array(6 => 6, 7 => 7, 10 => 10),
				'total_online'			=> 7,
				'visible_online'		=> 2,
				'hidden_online'			=> 3,
				'guests_online'			=> 2,
			)),
			array(1, false, array(
				'online_users'			=> array(3 => 3, 7 => 7),
				'hidden_users'			=> array(7 => 7),
				'total_online'			=> 2,
				'visible_online'		=> 1,
				'hidden_online'			=> 1,
				'guests_online'			=> 0,
			)),
			array(1, true, array(
				'online_users'			=> array(3 => 3, 7 => 7),
				'hidden_users'			=> array(7 => 7),
				'total_online'			=> 3,
				'visible_online'		=> 1,
				'hidden_online'			=> 1,
				'guests_online'			=> 1,
			)),
			array(2, false, array(
				'online_users'			=> array(),
				'hidden_users'			=> array(),
				'total_online'			=> 0,
				'visible_online'		=> 0,
				'hidden_online'			=> 0,
				'guests_online'			=> 0,
			)),
			array(2, true, array(
				'online_users'			=> array(),
				'hidden_users'			=> array(),
				'total_online'			=> 0,
				'visible_online'		=> 0,
				'hidden_online'			=> 0,
				'guests_online'			=> 0,
			)),
		);
	}

	/**
	* @dataProvider obtain_users_online_data
	*/
	public function test_obtain_users_online($forum_id, $display_guests, $expected)
	{
		$this->db->sql_query('DELETE FROM phpbb_sessions');

		global $config;
		$config['load_online_guests'] = $display_guests;

		$time = time();
		$this->create_guest_sessions($time);
		$this->create_user_sessions($time);
		$this->assertEquals($expected, obtain_users_online($forum_id));
	}

	static public function obtain_users_online_string_data()
	{
		return array(
			array(0, false, array(
				'online_userlist'	=> 'REGISTERED_USERS <span class="username">2</span>, <span class="username">3</span>',
				'l_online_users'	=> 'ONLINE_USERS_TOTAL 5 REG_USERS_TOTAL 2 HIDDEN_USERS_TOTAL 3',
			)),
			array(0, true, array(
				'online_userlist'	=> 'REGISTERED_USERS <span class="username">2</span>, <span class="username">3</span>',
				'l_online_users'	=> 'ONLINE_USERS_TOTAL_GUESTS 7 REG_USERS_TOTAL 2 HIDDEN_USERS_TOTAL 3 GUEST_USERS_TOTAL 2',
			)),
			array(1, false, array(
				'online_userlist'	=> 'BROWSING_FORUM <span class="username">3</span>',
				'l_online_users'	=> 'ONLINE_USERS_TOTAL 2 REG_USERS_TOTAL 1 HIDDEN_USERS_TOTAL 1',
			)),
			array(1, true, array(
				'online_userlist'	=> 'BROWSING_FORUM_GUESTS 1 <span class="username">3</span>',
				'l_online_users'	=> 'ONLINE_USERS_TOTAL_GUESTS 3 REG_USERS_TOTAL 1 HIDDEN_USERS_TOTAL 1 GUEST_USERS_TOTAL 1',
			)),
			array(2, false, array(
				'online_userlist'	=> 'BROWSING_FORUM NO_ONLINE_USERS',
				'l_online_users'	=> 'ONLINE_USERS_TOTAL 0 REG_USERS_TOTAL 0 HIDDEN_USERS_TOTAL 0',
			)),
			array(2, true, array(
				'online_userlist'	=> 'BROWSING_FORUM_GUESTS 0 NO_ONLINE_USERS',
				'l_online_users'	=> 'ONLINE_USERS_TOTAL_GUESTS 0 REG_USERS_TOTAL 0 HIDDEN_USERS_TOTAL 0 GUEST_USERS_TOTAL 0',
			)),
		);
	}

	/**
	* @dataProvider obtain_users_online_string_data
	*/
	public function test_obtain_users_online_string($forum_id, $display_guests, $expected)
	{
		$this->db->sql_query('DELETE FROM phpbb_sessions');

		global $config, $user, $auth, $phpbb_dispatcher;
		$config['load_online_guests'] = $display_guests;
		$user = new phpbb_mock_lang();
		$user->data['user_id'] = 100;
		$user->lang = $this->load_language();
		$auth = $this->createMock('\phpbb\auth\auth');
		$acl_get_map = array(
			array('u_viewonline', true),
			array('u_viewprofile', true),
		);
		$auth->expects($this->any())
			->method('acl_get')
			->with($this->stringContains('_'),
				$this->anything())
			->will($this->returnValueMap($acl_get_map));
		$phpbb_dispatcher = new phpbb_mock_event_dispatcher();

		$time = time();
		$this->create_guest_sessions($time);
		$this->create_user_sessions($time);

		$online_users = obtain_users_online($forum_id);
		$this->assertEquals($expected, obtain_users_online_string($online_users, $forum_id));
	}

	protected function create_guest_sessions($time)
	{
		$this->add_session(1, '0001', '192.168.0.1', 0, true, $time);
		$this->add_session(1, '0002', '192.168.0.2', 1, true, $time);
		$this->add_session(1, '0003', '192.168.0.3', 0, true, $time, 10);
		$this->add_session(1, '0004', '192.168.0.4', 1, true, $time, 10);
	}

	protected function create_user_sessions($time)
	{
		$this->add_session(2, '0005', '192.168.0.5', 0, true, $time);
		$this->add_session(3, '0006', '192.168.0.6', 1, true, $time);
		$this->add_session(4, '0007', '192.168.0.7', 0, true, $time, 10);
		$this->add_session(5, '0008', '192.168.0.8', 1, true, $time, 10);
		$this->add_session(6, '0005', '192.168.0.9', 0, false, $time);
		$this->add_session(7, '0006', '192.168.0.10', 1, false, $time);
		$this->add_session(8, '0007', '192.168.0.11', 0, false, $time, 10);
		$this->add_session(9, '0008', '192.168.0.12', 1, false, $time, 10);
		$this->add_session(10, '009', '192.168.0.13', 0, false, $time);
	}

	protected function add_session($user_id, $session_id, $user_ip, $forum_id, $view_online, $time, $time_delta = 0)
	{
		$sql_ary = array(
			'session_id'			=> $user_id . '_' . $forum_id . '_session00000000000000000' . $session_id,
			'session_user_id'		=> $user_id,
			'session_ip'			=> $user_ip,
			'session_forum_id'		=> $forum_id,
			'session_time'			=> $time - $time_delta * 60,
			'session_viewonline'	=> $view_online,
		);
		$this->db->sql_query('INSERT INTO phpbb_sessions ' . $this->db->sql_build_array('INSERT', $sql_ary));
	}

	protected function load_language()
	{
		return array(
			'NO_ONLINE_USERS'	=> 'NO_ONLINE_USERS',
			'REGISTERED_USERS'	=> 'REGISTERED_USERS',
			'BROWSING_FORUM'	=> 'BROWSING_FORUM %s',
			'BROWSING_FORUM_GUEST'	=> 'BROWSING_FORUM_GUEST %s %d',
			'BROWSING_FORUM_GUESTS'	=> 'BROWSING_FORUM_GUESTS %s %d',
		);
	}
}