diff options
| author | Joas Schilling <nickvergessen@gmx.de> | 2013-06-06 22:58:26 +0200 | 
|---|---|---|
| committer | Joas Schilling <nickvergessen@gmx.de> | 2013-06-06 22:58:26 +0200 | 
| commit | 74aec79551f24bf3365cb7060e8781eda10ec318 (patch) | |
| tree | e500d0011fbe758456174030846301e9fb69c232 | |
| parent | b4b1704a9b2f2c6e7770f478e27c5c408772f3ff (diff) | |
| parent | ba665412ea4342da24b70408335e82a8c455f258 (diff) | |
| download | forums-74aec79551f24bf3365cb7060e8781eda10ec318.tar forums-74aec79551f24bf3365cb7060e8781eda10ec318.tar.gz forums-74aec79551f24bf3365cb7060e8781eda10ec318.tar.bz2 forums-74aec79551f24bf3365cb7060e8781eda10ec318.tar.xz forums-74aec79551f24bf3365cb7060e8781eda10ec318.zip  | |
Merge branch 'ticket/11543' into ticket/develop/11543
* ticket/11543:
  [ticket/11543] Use correct IP addresses and inject time for correct values
  [ticket/11543] Add unit tests for obtain_users_online_string()
  [ticket/11543] Add unit tests for obtain_users_online() with empty forum
  [ticket/11543] Add unit tests for obtain_users_online()
  [ticket/11543] Add unit tests for obtain_guest_count()
| -rw-r--r-- | tests/functions/fixtures/obtain_online.xml | 111 | ||||
| -rw-r--r-- | tests/functions/obtain_online_test.php | 237 | 
2 files changed, 348 insertions, 0 deletions
diff --git a/tests/functions/fixtures/obtain_online.xml b/tests/functions/fixtures/obtain_online.xml new file mode 100644 index 0000000000..1c5a4454f2 --- /dev/null +++ b/tests/functions/fixtures/obtain_online.xml @@ -0,0 +1,111 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> +	<table name="phpbb_sessions"> +		<column>session_id</column> +		<column>session_user_id</column> +		<column>session_forum_id</column> +		<column>session_time</column> +		<column>session_ip</column> +		<column>session_viewonline</column> +	</table> +	<table name="phpbb_users"> +		<column>user_id</column> +		<column>username_clean</column> +		<column>username</column> +		<column>user_allow_viewonline</column> +		<column>user_permissions</column> +		<column>user_sig</column> +		<column>user_occ</column> +		<column>user_interests</column> +		<row> +			<value>1</value> +			<value>anonymous</value> +			<value>anonymous</value> +			<value>1</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>2</value> +			<value>2</value> +			<value>2</value> +			<value>1</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>3</value> +			<value>3</value> +			<value>3</value> +			<value>1</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>4</value> +			<value>4</value> +			<value>4</value> +			<value>1</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>5</value> +			<value>5</value> +			<value>5</value> +			<value>1</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>6</value> +			<value>6</value> +			<value>6</value> +			<value>0</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>7</value> +			<value>7</value> +			<value>7</value> +			<value>0</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>8</value> +			<value>8</value> +			<value>8</value> +			<value>0</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>9</value> +			<value>9</value> +			<value>9</value> +			<value>0</value> +			<value></value> +			<value></value> +			<value></value> +			<value></value> +		</row> +	</table> +</dataset> diff --git a/tests/functions/obtain_online_test.php b/tests/functions/obtain_online_test.php new file mode 100644 index 0000000000..941c20de92 --- /dev/null +++ b/tests/functions/obtain_online_test.php @@ -0,0 +1,237 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/auth.php'; + +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() +	{ +		parent::setUp(); + +		global $config, $db; + +		$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), +				'hidden_users'			=> array(6 => 6, 7 => 7), +				'total_online'			=> 4, +				'visible_online'		=> 2, +				'hidden_online'			=> 2, +				'guests_online'			=> 0, +			)), +			array(0, true, array( +				'online_users'			=> array(2 => 2, 3 => 3, 6 => 6, 7 => 7), +				'hidden_users'			=> array(6 => 6, 7 => 7), +				'total_online'			=> 6, +				'visible_online'		=> 2, +				'hidden_online'			=> 2, +				'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 2, 3', +				'l_online_users'	=> 'ONLINE_USERS_TOTAL 4REG_USERS_TOTAL_AND 2HIDDEN_USERS_TOTAL 2', +			)), +			array(0, true, array( +				'online_userlist'	=> 'REGISTERED_USERS 2, 3', +				'l_online_users'	=> 'ONLINE_USERS_TOTAL 6REG_USERS_TOTAL 2HIDDEN_USERS_TOTAL_AND 2GUEST_USERS_TOTAL 2', +			)), +			array(1, false, array( +				'online_userlist'	=> 'BROWSING_FORUM 3', +				'l_online_users'	=> 'ONLINE_USERS_TOTAL 2REG_USER_TOTAL_AND 1HIDDEN_USER_TOTAL 1', +			)), +			array(1, true, array( +				'online_userlist'	=> 'BROWSING_FORUM_GUEST 3 1', +				'l_online_users'	=> 'ONLINE_USERS_TOTAL 3REG_USER_TOTAL 1HIDDEN_USER_TOTAL_AND 1GUEST_USER_TOTAL 1', +			)), +			array(2, false, array( +				'online_userlist'	=> 'BROWSING_FORUM NO_ONLINE_USERS', +				'l_online_users'	=> 'ONLINE_USERS_ZERO_TOTAL 0REG_USERS_ZERO_TOTAL_AND 0HIDDEN_USERS_ZERO_TOTAL 0', +			)), +			array(2, true, array( +				'online_userlist'	=> 'BROWSING_FORUM_GUESTS NO_ONLINE_USERS 0', +				'l_online_users'	=> 'ONLINE_USERS_ZERO_TOTAL 0REG_USERS_ZERO_TOTAL 0HIDDEN_USERS_ZERO_TOTAL_AND 0GUEST_USERS_ZERO_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; +		$config['load_online_guests'] = $display_guests; +		$user->lang = $this->load_language(); +		$auth = $this->getMock('auth'); +		$acl_get_map = array( +			array('u_viewonline', true), +		); +		$auth->expects($this->any()) +			->method('acl_get') +			->with($this->stringContains('_'), +				$this->anything()) +			->will($this->returnValueMap($acl_get_map)); + +		$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); +	} + +	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() +	{ +		$lang = 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', +		); +		$vars_online = array('ONLINE', 'REG', 'HIDDEN', 'GUEST'); +		foreach ($vars_online as $online) +		{ +			$lang = array_merge($lang, array( +				$online . '_USERS_ZERO_TOTAL'	=> $online . '_USERS_ZERO_TOTAL %d', +				$online . '_USER_TOTAL'			=> $online . '_USER_TOTAL %d', +				$online . '_USERS_TOTAL'		=> $online . '_USERS_TOTAL %d', +				$online . '_USERS_ZERO_TOTAL_AND'	=> $online . '_USERS_ZERO_TOTAL_AND %d', +				$online . '_USER_TOTAL_AND'			=> $online . '_USER_TOTAL_AND %d', +				$online . '_USERS_TOTAL_AND'		=> $online . '_USERS_TOTAL_AND %d', +			)); +		} +		return $lang; +	} +}  | 
