mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-10 13:28:55 +00:00
[ticket/11162] Extract existing behavior into a function and add a test.
PHPBB3-11162
This commit is contained in:
parent
88b100d818
commit
fd6ee50e06
3 changed files with 150 additions and 1 deletions
|
@ -1297,6 +1297,28 @@ function tz_select($default = '', $truncate = false)
|
|||
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
|
||||
|
||||
/**
|
||||
|
@ -3355,7 +3377,7 @@ function parse_cfg_file($filename, $lines = false)
|
|||
|
||||
$parsed_items[$key] = $value;
|
||||
}
|
||||
|
||||
|
||||
if (isset($parsed_items['inherit_from']) && isset($parsed_items['name']) && $parsed_items['inherit_from'] == $parsed_items['name'])
|
||||
{
|
||||
unset($parsed_items['inherit_from']);
|
||||
|
|
56
tests/functions/fixtures/duplicates.xml
Normal file
56
tests/functions/fixtures/duplicates.xml
Normal 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>
|
71
tests/functions/update_rows_avoiding_duplicates_test.php
Normal file
71
tests/functions/update_rows_avoiding_duplicates_test.php
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue