1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-06-07 23:16:13 +02:00

[ticket/11162] Extract existing behavior into a function and add a test.

PHPBB3-11162
This commit is contained in:
Oleg Pudeyev 2012-11-02 16:05:53 -04:00
parent 88b100d818
commit fd6ee50e06
3 changed files with 150 additions and 1 deletions

View File

@ -1297,6 +1297,28 @@ function tz_select($default = '', $truncate = false)
return $tz_select; return $tz_select;
} }
/**
* Updates rows in given table from a set of values to a new value.
* If this results in rows violating uniqueness constraints, the duplicate
* rows are eliminated.
*
* @param dbal $db Database object
* @param string $table Table on which to perform the update
* @param string $column Column whose values to change
* @param array $from_values An array of values that should be changed
* @param int $to_value The new value
* @return null
*/
function phpbb_update_rows_avoiding_duplicates($db, $table, $column, $from_values, $to_value)
{
$db->sql_return_on_error(true);
$condition = $db->sql_in_set($column, $from_values);
$db->sql_query('UPDATE ' . $table . ' SET ' . $column . ' = ' . (int) $to_value. ' WHERE ' . $condition);
$db->sql_return_on_error(false);
$db->sql_query('DELETE FROM ' . $table . ' WHERE ' . $condition);
}
// Functions handling topic/post tracking/marking // Functions handling topic/post tracking/marking
/** /**

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_topics_watch">
<column>user_id</column>
<column>topic_id</column>
<column>notify_status</column>
<!-- one entry for this topic -->
<row>
<value>1</value>
<value>1</value>
<value>1</value>
</row>
<!-- non-conflicting entries -->
<row>
<value>2</value>
<value>2</value>
<value>1</value>
</row>
<row>
<value>3</value>
<value>3</value>
<value>1</value>
</row>
<!-- conflicting entries -->
<row>
<value>1</value>
<value>4</value>
<value>1</value>
</row>
<row>
<value>1</value>
<value>5</value>
<value>1</value>
</row>
<!-- conflicting and non-conflicting entries -->
<row>
<value>1</value>
<value>6</value>
<value>1</value>
</row>
<row>
<value>1</value>
<value>7</value>
<value>1</value>
</row>
<row>
<value>2</value>
<value>6</value>
<value>1</value>
</row>
</table>
</dataset>

View File

@ -0,0 +1,71 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_update_rows_avoiding_duplicates_test extends phpbb_database_test_case
{
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/duplicates.xml');
}
public static function fixture_data()
{
return array(
// description
// from array
// to value
// expected count with to value post update
array(
'trivial',
array(1),
10,
1,
),
array(
'no conflict',
array(2),
3,
2,
),
array(
'conflict',
array(4),
5,
1,
),
array(
'conflict and no conflict',
array(6),
7,
2,
),
);
}
/**
* @dataProvider fixture_data
*/
public function test_trivial_update($description, $from, $to, $expected_result_count)
{
$db = $this->new_dbal();
phpbb_update_rows_avoiding_duplicates($db, TOPICS_WATCH_TABLE, 'topic_id', $from, $to);
$sql = 'SELECT count(*) AS count
FROM ' . TOPICS_WATCH_TABLE . '
WHERE topic_id = ' . $db->sql_escape($to);
$result = $db->sql_query($sql);
$result_count = $db->sql_fetchfield('count');
$db->sql_freeresult($result);
$this->assertEquals($expected_result_count, $result_count);
}
}