mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-07 20:08:53 +00:00
[ticket/8116] Improve code and add tests for helper functions
PHPBB3-8116
This commit is contained in:
parent
2edf7056f0
commit
e6a0e4caed
3 changed files with 158 additions and 36 deletions
|
@ -28,9 +28,6 @@ function mcp_post_details($id, $mode, $action)
|
||||||
global $template, $db, $user, $auth, $cache, $phpbb_container;
|
global $template, $db, $user, $auth, $cache, $phpbb_container;
|
||||||
global $phpbb_dispatcher;
|
global $phpbb_dispatcher;
|
||||||
|
|
||||||
/** @var \phpbb\pagination $pagination */
|
|
||||||
$pagination = $phpbb_container->get('pagination');
|
|
||||||
|
|
||||||
$user->add_lang('posting');
|
$user->add_lang('posting');
|
||||||
|
|
||||||
$post_id = request_var('p', 0);
|
$post_id = request_var('p', 0);
|
||||||
|
@ -358,6 +355,9 @@ function mcp_post_details($id, $mode, $action)
|
||||||
// Get IP
|
// Get IP
|
||||||
if ($auth->acl_get('m_info', $post_info['forum_id']))
|
if ($auth->acl_get('m_info', $post_info['forum_id']))
|
||||||
{
|
{
|
||||||
|
/** @var \phpbb\pagination $pagination */
|
||||||
|
$pagination = $phpbb_container->get('pagination');
|
||||||
|
|
||||||
$rdns_ip_num = $request->variable('rdns', '');
|
$rdns_ip_num = $request->variable('rdns', '');
|
||||||
$start_users = $request->variable('start_users', 0);
|
$start_users = $request->variable('start_users', 0);
|
||||||
|
|
||||||
|
@ -383,10 +383,10 @@ function mcp_post_details($id, $mode, $action)
|
||||||
ORDER BY postings DESC, poster_id ASC";
|
ORDER BY postings DESC, poster_id ASC";
|
||||||
$result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_users);
|
$result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_users);
|
||||||
|
|
||||||
$users = 0;
|
$page_users = 0;
|
||||||
while ($row = $db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
$users++;
|
$page_users++;
|
||||||
|
|
||||||
// Fill the user select list with users who have posted under this IP
|
// Fill the user select list with users who have posted under this IP
|
||||||
if ($row['poster_id'] != $post_info['poster_id'])
|
if ($row['poster_id'] != $post_info['poster_id'])
|
||||||
|
@ -396,7 +396,7 @@ function mcp_post_details($id, $mode, $action)
|
||||||
}
|
}
|
||||||
$db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
if ($users == $config['posts_per_page'] || $start_users)
|
if ($page_users == $config['posts_per_page'] || $start_users)
|
||||||
{
|
{
|
||||||
if ($num_users === false)
|
if ($num_users === false)
|
||||||
{
|
{
|
||||||
|
@ -462,10 +462,10 @@ function mcp_post_details($id, $mode, $action)
|
||||||
ORDER BY postings DESC, poster_ip ASC";
|
ORDER BY postings DESC, poster_ip ASC";
|
||||||
$result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_ips);
|
$result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_ips);
|
||||||
|
|
||||||
$ips = 0;
|
$page_ips = 0;
|
||||||
while ($row = $db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
$ips++;
|
$page_ips++;
|
||||||
$hostname = (($rdns_ip_num == $row['poster_ip'] || $rdns_ip_num == 'all') && $row['poster_ip']) ? @gethostbyaddr($row['poster_ip']) : '';
|
$hostname = (($rdns_ip_num == $row['poster_ip'] || $rdns_ip_num == 'all') && $row['poster_ip']) ? @gethostbyaddr($row['poster_ip']) : '';
|
||||||
|
|
||||||
$template->assign_block_vars('iprow', array(
|
$template->assign_block_vars('iprow', array(
|
||||||
|
@ -480,7 +480,7 @@ function mcp_post_details($id, $mode, $action)
|
||||||
}
|
}
|
||||||
$db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
if ($ips == $config['posts_per_page'] || $start_ips)
|
if ($page_ips == $config['posts_per_page'] || $start_ips)
|
||||||
{
|
{
|
||||||
if ($num_ips === false)
|
if ($num_ips === false)
|
||||||
{
|
{
|
||||||
|
@ -517,23 +517,14 @@ function mcp_post_details($id, $mode, $action)
|
||||||
/**
|
/**
|
||||||
* Get the number of posters for a given ip
|
* Get the number of posters for a given ip
|
||||||
*
|
*
|
||||||
* @param \phpbb\db\driver\driver_interface $db
|
* @param \phpbb\db\driver\driver_interface $db DBAL interface
|
||||||
* @param string $poster_ip
|
* @param string $poster_ip IP
|
||||||
* @return int
|
* @return int Number of posters
|
||||||
*/
|
*/
|
||||||
function phpbb_get_num_posters_for_ip(\phpbb\db\driver\driver_interface $db, $poster_ip)
|
function phpbb_get_num_posters_for_ip(\phpbb\db\driver\driver_interface $db, $poster_ip)
|
||||||
{
|
{
|
||||||
if ($db->get_sql_layer() == 'sqlite' || $db->get_sql_layer() == 'sqlite3')
|
$sql = 'SELECT COUNT(DISTINCT poster_id) as num_users
|
||||||
{
|
FROM ' . POSTS_TABLE . "
|
||||||
$sql = 'SELECT COUNT(poster_id) as num_users
|
|
||||||
FROM (SELECT DISTINCT poster_id';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$sql = 'SELECT COUNT(DISTINCT poster_id) as num_users';
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql .= ' FROM ' . POSTS_TABLE . "
|
|
||||||
WHERE poster_ip = '" . $db->sql_escape($poster_ip) . "'";
|
WHERE poster_ip = '" . $db->sql_escape($poster_ip) . "'";
|
||||||
$result = $db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
$num_users = (int) $db->sql_fetchfield('num_users');
|
$num_users = (int) $db->sql_fetchfield('num_users');
|
||||||
|
@ -546,22 +537,13 @@ function phpbb_get_num_posters_for_ip(\phpbb\db\driver\driver_interface $db, $po
|
||||||
* Get the number of ips for a given poster
|
* Get the number of ips for a given poster
|
||||||
*
|
*
|
||||||
* @param \phpbb\db\driver\driver_interface $db
|
* @param \phpbb\db\driver\driver_interface $db
|
||||||
* @param int $poster_id
|
* @param int $poster_id Poster user ID
|
||||||
* @return int
|
* @return int Number of IPs for given poster
|
||||||
*/
|
*/
|
||||||
function phpbb_get_num_ips_for_poster(\phpbb\db\driver\driver_interface $db, $poster_id)
|
function phpbb_get_num_ips_for_poster(\phpbb\db\driver\driver_interface $db, $poster_id)
|
||||||
{
|
{
|
||||||
if ($db->get_sql_layer() == 'sqlite' || $db->get_sql_layer() == 'sqlite3')
|
$sql = 'SELECT COUNT(DISTINCT poster_ip) as num_ips
|
||||||
{
|
FROM ' . POSTS_TABLE . '
|
||||||
$sql = 'SELECT COUNT(poster_ip) as num_ips
|
|
||||||
FROM (SELECT DISTINCT poster_ip';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$sql = 'SELECT COUNT(DISTINCT poster_ip) as num_ips';
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql .= ' FROM ' . POSTS_TABLE . '
|
|
||||||
WHERE poster_id = ' . (int) $poster_id;
|
WHERE poster_id = ' . (int) $poster_id;
|
||||||
$result = $db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
$num_ips = (int) $db->sql_fetchfield('num_ips');
|
$num_ips = (int) $db->sql_fetchfield('num_ips');
|
||||||
|
|
73
tests/mcp/fixtures/post_ip.xml
Normal file
73
tests/mcp/fixtures/post_ip.xml
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<dataset>
|
||||||
|
<table name="phpbb_posts">
|
||||||
|
<column>post_id</column>
|
||||||
|
<column>poster_id</column>
|
||||||
|
<column>post_edit_user</column>
|
||||||
|
<column>post_delete_user</column>
|
||||||
|
<column>post_username</column>
|
||||||
|
<column>topic_id</column>
|
||||||
|
<column>forum_id</column>
|
||||||
|
<column>post_visibility</column>
|
||||||
|
<column>post_time</column>
|
||||||
|
<column>post_text</column>
|
||||||
|
<column>post_reported</column>
|
||||||
|
<column>poster_ip</column>
|
||||||
|
<row>
|
||||||
|
<value>1</value>
|
||||||
|
<value>2</value>
|
||||||
|
<value>2</value>
|
||||||
|
<value>2</value>
|
||||||
|
<value></value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value></value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>127.0.0.1</value>
|
||||||
|
</row>
|
||||||
|
<row>
|
||||||
|
<value>2</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>Other</value>
|
||||||
|
<value>2</value>
|
||||||
|
<value>2</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value></value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>127.0.0.2</value>
|
||||||
|
</row>
|
||||||
|
<row>
|
||||||
|
<value>3</value>
|
||||||
|
<value>2</value>
|
||||||
|
<value>2</value>
|
||||||
|
<value>2</value>
|
||||||
|
<value></value>
|
||||||
|
<value>3</value>
|
||||||
|
<value>3</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value></value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>127.0.0.3</value>
|
||||||
|
</row>
|
||||||
|
<row>
|
||||||
|
<value>4</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>Other</value>
|
||||||
|
<value>4</value>
|
||||||
|
<value>4</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>1</value>
|
||||||
|
<value></value>
|
||||||
|
<value>1</value>
|
||||||
|
<value>127.0.0.1</value>
|
||||||
|
</row>
|
||||||
|
</table>
|
||||||
|
</dataset>
|
67
tests/mcp/post_ip_test.php
Normal file
67
tests/mcp/post_ip_test.php
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
<?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__) . '/../../phpBB/includes/mcp/mcp_post.php';
|
||||||
|
|
||||||
|
class phpbb_mcp_post_ip_test extends phpbb_database_test_case
|
||||||
|
{
|
||||||
|
/** @var \phpbb\db\driver\driver_interface */
|
||||||
|
protected $db;
|
||||||
|
|
||||||
|
public function getDataSet()
|
||||||
|
{
|
||||||
|
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/post_ip.xml');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->db = $this->new_dbal();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function data_get_num_ips()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(2, 1),
|
||||||
|
array(2, 2),
|
||||||
|
array(0, 3),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider data_get_num_ips
|
||||||
|
*/
|
||||||
|
public function test_get_num_ips($expected, $poster_id)
|
||||||
|
{
|
||||||
|
$this->assertSame($expected, phpbb_get_num_ips_for_poster($this->db, $poster_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function data_get_num_posters()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(2, '127.0.0.1'),
|
||||||
|
array(1, '127.0.0.2'),
|
||||||
|
array(1, '127.0.0.3'),
|
||||||
|
array(0, '127.0.0.4'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider data_get_num_posters
|
||||||
|
*/
|
||||||
|
public function test_get_num_posters($expected, $ip)
|
||||||
|
{
|
||||||
|
$this->assertSame($expected, phpbb_get_num_posters_for_ip($this->db, $ip));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue