1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-03-13 20:28:44 +01:00

Merge pull request #1348 from nickvergessen/ticket/11495

Ticket/11495 Nested Set Implementation
This commit is contained in:
Nils Adermann 2013-05-02 15:04:19 -07:00
commit 7320e4a535
14 changed files with 2164 additions and 0 deletions

View File

@ -116,6 +116,17 @@ class phpbb_lock_db
return $this->locked;
}
/**
* Does this process own the lock?
*
* @return bool true if lock is owned
* false otherwise
*/
public function owns_lock()
{
return (bool) $this->locked;
}
/**
* Releases the lock.
*

View File

@ -110,6 +110,17 @@ class phpbb_lock_flock
return (bool) $this->lock_fp;
}
/**
* Does this process own the lock?
*
* @return bool true if lock is owned
* false otherwise
*/
public function owns_lock()
{
return (bool) $this->lock_fp;
}
/**
* Releases the lock.
*

View File

@ -0,0 +1,122 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
interface phpbb_tree_interface
{
/**
* Inserts an item into the database table and into the tree.
*
* @param array $item The item to be added
* @return array Array with item data as set in the database
*/
public function insert(array $additional_data);
/**
* Delete an item from the tree and from the database table
*
* Also deletes the subtree from the tree and from the database table
*
* @param int $item_id The item to be deleted
* @return array Item ids that have been deleted
*/
public function delete($item_id);
/**
* Move an item by a given delta
*
* An item is only moved up/down within the same parent. If the delta is
* larger then the number of children, the item is moved to the top/bottom
* of the list of children within this parent.
*
* @param int $item_id The item to be moved
* @param int $delta Number of steps to move this item, < 0 => down, > 0 => up
* @return bool True if the item was moved
*/
public function move($item_id, $delta);
/**
* Move an item down by 1
*
* @param int $item_id The item to be moved
* @return bool True if the item was moved
*/
public function move_down($item_id);
/**
* Move an item up by 1
*
* @param int $item_id The item to be moved
* @return bool True if the item was moved
*/
public function move_up($item_id);
/**
* Moves all children of one item to another item
*
* If the new parent already has children, the new children are appended
* to the list.
*
* @param int $current_parent_id The current parent item
* @param int $new_parent_id The new parent item
* @return bool True if any items where moved
*/
public function move_children($current_parent_id, $new_parent_id);
/**
* Change parent item
*
* Moves the item to the bottom of the new parent's list of children
*
* @param int $item_id The item to be moved
* @param int $new_parent_id The new parent item
* @return bool True if the parent was set successfully
*/
public function change_parent($item_id, $new_parent_id);
/**
* Get all items that are either ancestors or descendants of the item
*
* @param int $item_id Id of the item to retrieve the ancestors/descendants from
* @param bool $order_asc Order the items ascendingly (most outer ancestor first)
* @param bool $include_item Should the item matching the given item id be included in the list as well
* @return array Array of items (containing all columns from the item table)
* ID => Item data
*/
public function get_path_and_subtree_data($item_id, $order_asc, $include_item);
/**
* Get all of the item's ancestors
*
* @param int $item_id Id of the item to retrieve the ancestors from
* @param bool $order_asc Order the items ascendingly (most outer ancestor first)
* @param bool $include_item Should the item matching the given item id be included in the list as well
* @return array Array of items (containing all columns from the item table)
* ID => Item data
*/
public function get_path_data($item_id, $order_asc, $include_item);
/**
* Get all of the item's descendants
*
* @param int $item_id Id of the item to retrieve the descendants from
* @param bool $order_asc Order the items ascendingly
* @param bool $include_item Should the item matching the given item id be included in the list as well
* @return array Array of items (containing all columns from the item table)
* ID => Item data
*/
public function get_subtree_data($item_id, $order_asc, $include_item);
}

View File

@ -0,0 +1,850 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
abstract class phpbb_tree_nestedset implements phpbb_tree_interface
{
/** @var phpbb_db_driver */
protected $db;
/** @var phpbb_lock_db */
protected $lock;
/** @var string */
protected $table_name;
/**
* Prefix for the language keys returned by exceptions
* @var string
*/
protected $message_prefix = '';
/**
* Column names in the table
* @var string
*/
protected $column_item_id = 'item_id';
protected $column_left_id = 'left_id';
protected $column_right_id = 'right_id';
protected $column_parent_id = 'parent_id';
protected $column_item_parents = 'item_parents';
/**
* Additional SQL restrictions
* Allows to have multiple nested sets in one table
* @var string
*/
protected $sql_where = '';
/**
* List of item properties to be cached in the item_parents column
* @var array
*/
protected $item_basic_data = array('*');
/**
* Construct
*
* @param phpbb_db_driver $db Database connection
* @param phpbb_lock_db $lock Lock class used to lock the table when moving forums around
* @param string $table_name Table name
* @param string $message_prefix Prefix for the messages thrown by exceptions
* @param string $sql_where Additional SQL restrictions for the queries
* @param array $item_basic_data Array with basic item data that is stored in item_parents
* @param array $columns Array with column names to overwrite
*/
public function __construct(phpbb_db_driver $db, phpbb_lock_db $lock, $table_name, $message_prefix = '', $sql_where = '', $item_basic_data = array(), $columns = array())
{
$this->db = $db;
$this->lock = $lock;
$this->table_name = $table_name;
$this->message_prefix = $message_prefix;
$this->sql_where = $sql_where;
$this->item_basic_data = (!empty($item_basic_data)) ? $item_basic_data : array('*');
if (!empty($columns))
{
foreach ($columns as $column => $name)
{
$column_name = 'column_' . $column;
$this->$column_name = $name;
}
}
}
/**
* Returns additional sql where restrictions
*
* @param string $operator SQL operator that needs to be prepended to sql_where,
* if it is not empty.
* @param string $column_prefix Prefix that needs to be prepended to column names
* @return string Returns additional where statements to narrow down the tree,
* prefixed with operator and prepended column_prefix to column names
*/
public function get_sql_where($operator = 'AND', $column_prefix = '')
{
return (!$this->sql_where) ? '' : $operator . ' ' . sprintf($this->sql_where, $column_prefix);
}
/**
* Acquires a lock on the item table
*
* @return bool True if the lock was acquired, false if it has been acquired previously
*
* @throws RuntimeException If the lock could not be acquired
*/
protected function acquire_lock()
{
if ($this->lock->owns_lock())
{
return false;
}
if (!$this->lock->acquire())
{
throw new RuntimeException($this->message_prefix . 'LOCK_FAILED_ACQUIRE');
}
return true;
}
/**
* @inheritdoc
*/
public function insert(array $additional_data)
{
$item_data = $this->reset_nestedset_values($additional_data);
$sql = 'INSERT INTO ' . $this->table_name . ' ' . $this->db->sql_build_array('INSERT', $item_data);
$this->db->sql_query($sql);
$item_data[$this->column_item_id] = (int) $this->db->sql_nextid();
return array_merge($item_data, $this->add_item_to_nestedset($item_data[$this->column_item_id]));
}
/**
* Add an item which already has a database row at the end of the tree
*
* @param int $item_id The item to be added
* @return array Array with updated data, if the item was added successfully
* Empty array otherwise
*/
protected function add_item_to_nestedset($item_id)
{
$sql = 'SELECT MAX(' . $this->column_right_id . ') AS ' . $this->column_right_id . '
FROM ' . $this->table_name . '
' . $this->get_sql_where('WHERE');
$result = $this->db->sql_query($sql);
$current_max_right_id = (int) $this->db->sql_fetchfield($this->column_right_id);
$this->db->sql_freeresult($result);
$update_item_data = array(
$this->column_parent_id => 0,
$this->column_left_id => $current_max_right_id + 1,
$this->column_right_id => $current_max_right_id + 2,
$this->column_item_parents => '',
);
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->db->sql_build_array('UPDATE', $update_item_data) . '
WHERE ' . $this->column_item_id . ' = ' . (int) $item_id . '
AND ' . $this->column_parent_id . ' = 0
AND ' . $this->column_left_id . ' = 0
AND ' . $this->column_right_id . ' = 0';
$this->db->sql_query($sql);
return ($this->db->sql_affectedrows() == 1) ? $update_item_data : array();
}
/**
* Remove an item from the tree without deleting it from the database
*
* Also removes all subitems from the tree without deleting them from the database either
*
* @param int $item_id The item to be deleted
* @return array Item ids that have been removed
*/
protected function remove_item_from_nestedset($item_id)
{
$item_id = (int) $item_id;
if (!$item_id)
{
throw new OutOfBoundsException($this->message_prefix . 'INVALID_ITEM');
}
$items = $this->get_subtree_data($item_id);
$item_ids = array_keys($items);
if (empty($items) || !isset($items[$item_id]))
{
throw new OutOfBoundsException($this->message_prefix . 'INVALID_ITEM');
}
$this->remove_subset($item_ids, $items[$item_id]);
return $item_ids;
}
/**
* @inheritdoc
*/
public function delete($item_id)
{
$removed_items = $this->remove_item_from_nestedset($item_id);
$sql = 'DELETE FROM ' . $this->table_name . '
WHERE ' . $this->db->sql_in_set($this->column_item_id, $removed_items) . '
' . $this->get_sql_where('AND');
$this->db->sql_query($sql);
return $removed_items;
}
/**
* @inheritdoc
*/
public function move($item_id, $delta)
{
if ($delta == 0)
{
return false;
}
$this->acquire_lock();
$action = ($delta > 0) ? 'move_up' : 'move_down';
$delta = abs($delta);
// Keep $this->get_sql_where() here, to ensure we are in the right tree.
$sql = 'SELECT *
FROM ' . $this->table_name . '
WHERE ' . $this->column_item_id . ' = ' . (int) $item_id . '
' . $this->get_sql_where();
$result = $this->db->sql_query_limit($sql, $delta);
$item = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$item)
{
$this->lock->release();
throw new OutOfBoundsException($this->message_prefix . 'INVALID_ITEM');
}
/**
* Fetch all the siblings between the item's current spot
* and where we want to move it to. If there are less than $delta
* siblings between the current spot and the target then the
* item will move as far as possible
*/
$sql = "SELECT {$this->column_item_id}, {$this->column_parent_id}, {$this->column_left_id}, {$this->column_right_id}, {$this->column_item_parents}
FROM " . $this->table_name . '
WHERE ' . $this->column_parent_id . ' = ' . (int) $item[$this->column_parent_id] . '
' . $this->get_sql_where() . '
AND ';
if ($action == 'move_up')
{
$sql .= $this->column_right_id . ' < ' . (int) $item[$this->column_right_id] . ' ORDER BY ' . $this->column_right_id . ' DESC';
}
else
{
$sql .= $this->column_left_id . ' > ' . (int) $item[$this->column_left_id] . ' ORDER BY ' . $this->column_left_id . ' ASC';
}
$result = $this->db->sql_query_limit($sql, $delta);
$target = false;
while ($row = $this->db->sql_fetchrow($result))
{
$target = $row;
}
$this->db->sql_freeresult($result);
if (!$target)
{
$this->lock->release();
// The item is already on top or bottom
return false;
}
/**
* $left_id and $right_id define the scope of the items that are affected by the move.
* $diff_up and $diff_down are the values to substract or add to each item's left_id
* and right_id in order to move them up or down.
* $move_up_left and $move_up_right define the scope of the items that are moving
* up. Other items in the scope of ($left_id, $right_id) are considered to move down.
*/
if ($action == 'move_up')
{
$left_id = (int) $target[$this->column_left_id];
$right_id = (int) $item[$this->column_right_id];
$diff_up = (int) $item[$this->column_left_id] - (int) $target[$this->column_left_id];
$diff_down = (int) $item[$this->column_right_id] + 1 - (int) $item[$this->column_left_id];
$move_up_left = (int) $item[$this->column_left_id];
$move_up_right = (int) $item[$this->column_right_id];
}
else
{
$left_id = (int) $item[$this->column_left_id];
$right_id = (int) $target[$this->column_right_id];
$diff_up = (int) $item[$this->column_right_id] + 1 - (int) $item[$this->column_left_id];
$diff_down = (int) $target[$this->column_right_id] - (int) $item[$this->column_right_id];
$move_up_left = (int) $item[$this->column_right_id] + 1;
$move_up_right = (int) $target[$this->column_right_id];
}
// Now do the dirty job
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->column_left_id . ' = ' . $this->column_left_id . ' + CASE
WHEN ' . $this->column_left_id . " BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
ELSE {$diff_down}
END,
" . $this->column_right_id . ' = ' . $this->column_right_id . ' + CASE
WHEN ' . $this->column_right_id . " BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
ELSE {$diff_down}
END
WHERE
" . $this->column_left_id . " BETWEEN {$left_id} AND {$right_id}
AND " . $this->column_right_id . " BETWEEN {$left_id} AND {$right_id}
" . $this->get_sql_where();
$this->db->sql_query($sql);
$this->lock->release();
return true;
}
/**
* @inheritdoc
*/
public function move_down($item_id)
{
return $this->move($item_id, -1);
}
/**
* @inheritdoc
*/
public function move_up($item_id)
{
return $this->move($item_id, 1);
}
/**
* @inheritdoc
*/
public function move_children($current_parent_id, $new_parent_id)
{
$current_parent_id = (int) $current_parent_id;
$new_parent_id = (int) $new_parent_id;
if ($current_parent_id == $new_parent_id)
{
return false;
}
if (!$current_parent_id)
{
throw new OutOfBoundsException($this->message_prefix . 'INVALID_ITEM');
}
$this->acquire_lock();
$item_data = $this->get_subtree_data($current_parent_id);
if (!isset($item_data[$current_parent_id]))
{
$this->lock->release();
throw new OutOfBoundsException($this->message_prefix . 'INVALID_ITEM');
}
$current_parent = $item_data[$current_parent_id];
unset($item_data[$current_parent_id]);
$move_items = array_keys($item_data);
if (($current_parent[$this->column_right_id] - $current_parent[$this->column_left_id]) <= 1)
{
$this->lock->release();
return false;
}
if (in_array($new_parent_id, $move_items))
{
$this->lock->release();
throw new OutOfBoundsException($this->message_prefix . 'INVALID_PARENT');
}
$diff = sizeof($move_items) * 2;
$sql_exclude_moved_items = $this->db->sql_in_set($this->column_item_id, $move_items, true);
$this->db->sql_transaction('begin');
$this->remove_subset($move_items, $current_parent, false, true);
if ($new_parent_id)
{
// Retrieve new-parent again, it may have been changed...
$sql = 'SELECT *
FROM ' . $this->table_name . '
WHERE ' . $this->column_item_id . ' = ' . $new_parent_id;
$result = $this->db->sql_query($sql);
$new_parent = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$new_parent)
{
$this->db->sql_transaction('rollback');
$this->lock->release();
throw new OutOfBoundsException($this->message_prefix . 'INVALID_PARENT');
}
$new_right_id = $this->prepare_adding_subset($move_items, $new_parent, true);
if ($new_right_id > $current_parent[$this->column_right_id])
{
$diff = ' + ' . ($new_right_id - $current_parent[$this->column_right_id]);
}
else
{
$diff = ' - ' . abs($new_right_id - $current_parent[$this->column_right_id]);
}
}
else
{
$sql = 'SELECT MAX(' . $this->column_right_id . ') AS ' . $this->column_right_id . '
FROM ' . $this->table_name . '
WHERE ' . $sql_exclude_moved_items . '
' . $this->get_sql_where('AND');
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
$diff = ' + ' . ($row[$this->column_right_id] - $current_parent[$this->column_left_id]);
}
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->column_left_id . ' = ' . $this->column_left_id . $diff . ',
' . $this->column_right_id . ' = ' . $this->column_right_id . $diff . ',
' . $this->column_parent_id . ' = ' . $this->db->sql_case($this->column_parent_id . ' = ' . $current_parent_id, $new_parent_id, $this->column_parent_id) . ',
' . $this->column_item_parents . " = ''
WHERE " . $this->db->sql_in_set($this->column_item_id, $move_items) . '
' . $this->get_sql_where('AND');
$this->db->sql_query($sql);
$this->db->sql_transaction('commit');
$this->lock->release();
return true;
}
/**
* @inheritdoc
*/
public function change_parent($item_id, $new_parent_id)
{
$item_id = (int) $item_id;
$new_parent_id = (int) $new_parent_id;
if ($item_id == $new_parent_id)
{
return false;
}
if (!$item_id)
{
throw new OutOfBoundsException($this->message_prefix . 'INVALID_ITEM');
}
$this->acquire_lock();
$item_data = $this->get_subtree_data($item_id);
if (!isset($item_data[$item_id]))
{
$this->lock->release();
throw new OutOfBoundsException($this->message_prefix . 'INVALID_ITEM');
}
$item = $item_data[$item_id];
$move_items = array_keys($item_data);
if (in_array($new_parent_id, $move_items))
{
$this->lock->release();
throw new OutOfBoundsException($this->message_prefix . 'INVALID_PARENT');
}
$diff = sizeof($move_items) * 2;
$sql_exclude_moved_items = $this->db->sql_in_set($this->column_item_id, $move_items, true);
$this->db->sql_transaction('begin');
$this->remove_subset($move_items, $item, false, true);
if ($new_parent_id)
{
// Retrieve new-parent again, it may have been changed...
$sql = 'SELECT *
FROM ' . $this->table_name . '
WHERE ' . $this->column_item_id . ' = ' . $new_parent_id;
$result = $this->db->sql_query($sql);
$new_parent = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$new_parent)
{
$this->db->sql_transaction('rollback');
$this->lock->release();
throw new OutOfBoundsException($this->message_prefix . 'INVALID_PARENT');
}
$new_right_id = $this->prepare_adding_subset($move_items, $new_parent, true);
if ($new_right_id > (int) $item[$this->column_right_id])
{
$diff = ' + ' . ($new_right_id - (int) $item[$this->column_right_id] - 1);
}
else
{
$diff = ' - ' . abs($new_right_id - (int) $item[$this->column_right_id] - 1);
}
}
else
{
$sql = 'SELECT MAX(' . $this->column_right_id . ') AS ' . $this->column_right_id . '
FROM ' . $this->table_name . '
WHERE ' . $sql_exclude_moved_items . '
' . $this->get_sql_where('AND');
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
$diff = ' + ' . ($row[$this->column_right_id] - (int) $item[$this->column_left_id] + 1);
}
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->column_left_id . ' = ' . $this->column_left_id . $diff . ',
' . $this->column_right_id . ' = ' . $this->column_right_id . $diff . ',
' . $this->column_parent_id . ' = ' . $this->db->sql_case($this->column_item_id . ' = ' . $item_id, $new_parent_id, $this->column_parent_id) . ',
' . $this->column_item_parents . " = ''
WHERE " . $this->db->sql_in_set($this->column_item_id, $move_items) . '
' . $this->get_sql_where('AND');
$this->db->sql_query($sql);
$this->db->sql_transaction('commit');
$this->lock->release();
return true;
}
/**
* @inheritdoc
*/
public function get_path_and_subtree_data($item_id, $order_asc = true, $include_item = true)
{
$condition = 'i2.' . $this->column_left_id . ' BETWEEN i1.' . $this->column_left_id . ' AND i1.' . $this->column_right_id . '
OR i1.' . $this->column_left_id . ' BETWEEN i2.' . $this->column_left_id . ' AND i2.' . $this->column_right_id;
return $this->get_set_of_nodes_data($item_id, $condition, $order_asc, $include_item);
}
/**
* @inheritdoc
*/
public function get_path_data($item_id, $order_asc = true, $include_item = true)
{
$condition = 'i1.' . $this->column_left_id . ' BETWEEN i2.' . $this->column_left_id . ' AND i2.' . $this->column_right_id . '';
return $this->get_set_of_nodes_data($item_id, $condition, $order_asc, $include_item);
}
/**
* @inheritdoc
*/
public function get_subtree_data($item_id, $order_asc = true, $include_item = true)
{
$condition = 'i2.' . $this->column_left_id . ' BETWEEN i1.' . $this->column_left_id . ' AND i1.' . $this->column_right_id . '';
return $this->get_set_of_nodes_data($item_id, $condition, $order_asc, $include_item);
}
/**
* Get items that are related to the given item by the condition
*
* @param int $item_id Id of the item to retrieve the node set from
* @param string $condition Query string restricting the item list
* @param bool $order_asc Order the items ascending by their left_id
* @param bool $include_item Should the item matching the given item id be included in the list as well
* @return array Array of items (containing all columns from the item table)
* ID => Item data
*/
protected function get_set_of_nodes_data($item_id, $condition, $order_asc = true, $include_item = true)
{
$rows = array();
$sql = 'SELECT i2.*
FROM ' . $this->table_name . ' i1
LEFT JOIN ' . $this->table_name . " i2
ON (($condition) " . $this->get_sql_where('AND', 'i2.') . ')
WHERE i1.' . $this->column_item_id . ' = ' . (int) $item_id . '
' . $this->get_sql_where('AND', 'i1.') . '
ORDER BY i2.' . $this->column_left_id . ' ' . ($order_asc ? 'ASC' : 'DESC');
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
if (!$include_item && $item_id == $row[$this->column_item_id])
{
continue;
}
$rows[(int) $row[$this->column_item_id]] = $row;
}
$this->db->sql_freeresult($result);
return $rows;
}
/**
* Get basic data of all parent items
*
* Basic data is defined in the $item_basic_data property.
* Data is cached in the item_parents column in the item table
*
* @param array $item The item to get the path from
* @return array Array of items (containing basic columns from the item table)
* ID => Item data
*/
public function get_path_basic_data(array $item)
{
$parents = array();
if ($item[$this->column_parent_id])
{
if (!$item[$this->column_item_parents])
{
$sql = 'SELECT ' . implode(', ', $this->item_basic_data) . '
FROM ' . $this->table_name . '
WHERE ' . $this->column_left_id . ' < ' . (int) $item[$this->column_left_id] . '
AND ' . $this->column_right_id . ' > ' . (int) $item[$this->column_right_id] . '
' . $this->get_sql_where('AND') . '
ORDER BY ' . $this->column_left_id . ' ASC';
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$parents[$row[$this->column_item_id]] = $row;
}
$this->db->sql_freeresult($result);
$item_parents = serialize($parents);
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->column_item_parents . " = '" . $this->db->sql_escape($item_parents) . "'
WHERE " . $this->column_parent_id . ' = ' . (int) $item[$this->column_parent_id];
$this->db->sql_query($sql);
}
else
{
$parents = unserialize($item[$this->column_item_parents]);
}
}
return $parents;
}
/**
* Remove a subset from the nested set
*
* @param array $subset_items Subset of items to remove
* @param array $bounding_item Item containing the right bound of the subset
* @param bool $set_subset_zero Should the parent, left and right id of the items be set to 0, or kept unchanged?
* In case of removing an item from the tree, we should the values to 0
* In case of moving an item, we shouldkeep the original values, in order to allow "+ diff" later
* @return null
*/
protected function remove_subset(array $subset_items, array $bounding_item, $set_subset_zero = true)
{
$acquired_new_lock = $this->acquire_lock();
$diff = sizeof($subset_items) * 2;
$sql_subset_items = $this->db->sql_in_set($this->column_item_id, $subset_items);
$sql_not_subset_items = $this->db->sql_in_set($this->column_item_id, $subset_items, true);
$sql_is_parent = $this->column_left_id . ' <= ' . (int) $bounding_item[$this->column_right_id] . '
AND ' . $this->column_right_id . ' >= ' . (int) $bounding_item[$this->column_right_id];
$sql_is_right = $this->column_left_id . ' > ' . (int) $bounding_item[$this->column_right_id];
$set_left_id = $this->db->sql_case($sql_is_right, $this->column_left_id . ' - ' . $diff, $this->column_left_id);
$set_right_id = $this->db->sql_case($sql_is_parent . ' OR ' . $sql_is_right, $this->column_right_id . ' - ' . $diff, $this->column_right_id);
if ($set_subset_zero)
{
$set_left_id = $this->db->sql_case($sql_subset_items, 0, $set_left_id);
$set_right_id = $this->db->sql_case($sql_subset_items, 0, $set_right_id);
}
$sql = 'UPDATE ' . $this->table_name . '
SET ' . (($set_subset_zero) ? $this->column_parent_id . ' = ' . $this->db->sql_case($sql_subset_items, 0, $this->column_parent_id) . ',' : '') . '
' . $this->column_left_id . ' = ' . $set_left_id . ',
' . $this->column_right_id . ' = ' . $set_right_id . '
' . ((!$set_subset_zero) ? ' WHERE ' . $sql_not_subset_items . ' ' . $this->get_sql_where('AND') : $this->get_sql_where('WHERE'));
$this->db->sql_query($sql);
if ($acquired_new_lock)
{
$this->lock->release();
}
}
/**
* Prepare adding a subset to the nested set
*
* @param array $subset_items Subset of items to add
* @param array $new_parent Item containing the right bound of the new parent
* @return int New right id of the parent item
*/
protected function prepare_adding_subset(array $subset_items, array $new_parent)
{
$diff = sizeof($subset_items) * 2;
$sql_not_subset_items = $this->db->sql_in_set($this->column_item_id, $subset_items, true);
$set_left_id = $this->db->sql_case($this->column_left_id . ' > ' . (int) $new_parent[$this->column_right_id], $this->column_left_id . ' + ' . $diff, $this->column_left_id);
$set_right_id = $this->db->sql_case($this->column_right_id . ' >= ' . (int) $new_parent[$this->column_right_id], $this->column_right_id . ' + ' . $diff, $this->column_right_id);
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->column_left_id . ' = ' . $set_left_id . ',
' . $this->column_right_id . ' = ' . $set_right_id . '
WHERE ' . $sql_not_subset_items . '
' . $this->get_sql_where('AND');
$this->db->sql_query($sql);
return $new_parent[$this->column_right_id] + $diff;
}
/**
* Resets values required for the nested set system
*
* @param array $item Original item data
* @return array Original item data + nested set defaults
*/
protected function reset_nestedset_values(array $item)
{
$item_data = array_merge($item, array(
$this->column_parent_id => 0,
$this->column_left_id => 0,
$this->column_right_id => 0,
$this->column_item_parents => '',
));
unset($item_data[$this->column_item_id]);
return $item_data;
}
/**
* Regenerate left/right ids from parent/child relationship
*
* This method regenerates the left/right ids for the tree based on
* the parent/child relations. This function executes three queries per
* item, so it should only be called, when the set has one of the following
* problems:
* - The set has a duplicated value inside the left/right id chain
* - The set has a missing value inside the left/right id chain
* - The set has items that do not have a left/right id set
*
* When regenerating the items, the items are sorted by parent id and their
* current left id, so the current child/parent relationships are kept
* and running the function on a working set will not change the order.
*
* @param int $new_id First left_id to be used (should start with 1)
* @param int $parent_id parent_id of the current set (default = 0)
* @param bool $reset_ids Should we reset all left_id/right_id on the first call?
* @return int $new_id The next left_id/right_id that should be used
*/
public function regenerate_left_right_ids($new_id, $parent_id = 0, $reset_ids = false)
{
if ($acquired_new_lock = $this->acquire_lock())
{
$this->db->sql_transaction('begin');
if (!$reset_ids)
{
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->column_item_parents . " = ''
" . $this->get_sql_where('WHERE');
$this->db->sql_query($sql);
}
}
if ($reset_ids)
{
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->db->sql_build_array('UPDATE', array(
$this->column_left_id => 0,
$this->column_right_id => 0,
$this->column_item_parents => '',
)) . '
' . $this->get_sql_where('WHERE');
$this->db->sql_query($sql);
}
$sql = 'SELECT *
FROM ' . $this->table_name . '
WHERE ' . $this->column_parent_id . ' = ' . (int) $parent_id . '
' . $this->get_sql_where('AND') . '
ORDER BY ' . $this->column_left_id . ', ' . $this->column_item_id . ' ASC';
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// First we update the left_id for this module
if ($row[$this->column_left_id] != $new_id)
{
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->db->sql_build_array('UPDATE', array($this->column_left_id => $new_id)) . '
WHERE ' . $this->column_item_id . ' = ' . (int) $row[$this->column_item_id];
$this->db->sql_query($sql);
}
$new_id++;
// Then we go through any children and update their left/right id's
$new_id = $this->regenerate_left_right_ids($new_id, $row[$this->column_item_id]);
// Then we come back and update the right_id for this module
if ($row[$this->column_right_id] != $new_id)
{
$sql = 'UPDATE ' . $this->table_name . '
SET ' . $this->db->sql_build_array('UPDATE', array($this->column_right_id => $new_id)) . '
WHERE ' . $this->column_item_id . ' = ' . (int) $row[$this->column_item_id];
$this->db->sql_query($sql);
}
$new_id++;
}
$this->db->sql_freeresult($result);
if ($acquired_new_lock)
{
$this->db->sql_transaction('commit');
$this->lock->release();
}
return $new_id;
}
}

View File

@ -0,0 +1,46 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
class phpbb_tree_nestedset_forum extends phpbb_tree_nestedset
{
/**
* Construct
*
* @param phpbb_db_driver $db Database connection
* @param phpbb_lock_db $lock Lock class used to lock the table when moving forums around
* @param string $table_name Table name
*/
public function __construct(phpbb_db_driver $db, phpbb_lock_db $lock, $table_name)
{
parent::__construct(
$db,
$lock,
$table_name,
'FORUM_NESTEDSET_',
'',
array(
'forum_id',
'forum_name',
'forum_type',
),
array(
'item_id' => 'forum_id',
'item_parents' => 'forum_parents',
)
);
}
}

View File

@ -32,13 +32,18 @@ class phpbb_lock_db_test extends phpbb_database_test_case
public function test_new_lock()
{
$this->assertFalse($this->lock->owns_lock());
$this->assertTrue($this->lock->acquire());
$this->assertTrue($this->lock->owns_lock());
$this->assertTrue(isset($this->config['test_lock']), 'Lock was created');
$lock2 = new phpbb_lock_db('test_lock', $this->config, $this->db);
$this->assertFalse($lock2->acquire());
$this->assertFalse($lock2->owns_lock());
$this->lock->release();
$this->assertFalse($this->lock->owns_lock());
$this->assertEquals('0', $this->config['test_lock'], 'Lock was released');
}
@ -50,31 +55,40 @@ class phpbb_lock_db_test extends phpbb_database_test_case
public function test_double_lock()
{
$this->assertFalse($this->lock->owns_lock());
$this->assertTrue($this->lock->acquire());
$this->assertTrue($this->lock->owns_lock());
$this->assertTrue(isset($this->config['test_lock']), 'Lock was created');
$value = $this->config['test_lock'];
$this->assertFalse($this->lock->acquire());
$this->assertTrue($this->lock->owns_lock());
$this->assertEquals($value, $this->config['test_lock'], 'Second lock failed');
$this->lock->release();
$this->assertFalse($this->lock->owns_lock());
$this->assertEquals('0', $this->config['test_lock'], 'Lock was released');
}
public function test_double_unlock()
{
$this->assertTrue($this->lock->acquire());
$this->assertTrue($this->lock->owns_lock());
$this->assertFalse(empty($this->config['test_lock']), 'First lock is acquired');
$this->lock->release();
$this->assertFalse($this->lock->owns_lock());
$this->assertEquals('0', $this->config['test_lock'], 'First lock is released');
$lock2 = new phpbb_lock_db('test_lock', $this->config, $this->db);
$this->assertTrue($lock2->acquire());
$this->assertTrue($lock2->owns_lock());
$this->assertFalse(empty($this->config['test_lock']), 'Second lock is acquired');
$this->lock->release();
$this->assertTrue($lock2->owns_lock());
$this->assertFalse(empty($this->config['test_lock']), 'Double release of first lock is ignored');
$lock2->release();

View File

@ -26,15 +26,21 @@ class phpbb_lock_flock_test extends phpbb_test_case
$lock = new phpbb_lock_flock($path);
$ok = $lock->acquire();
$this->assertTrue($ok);
$this->assertTrue($lock->owns_lock());
$lock->release();
$this->assertFalse($lock->owns_lock());
$ok = $lock->acquire();
$this->assertTrue($ok);
$this->assertTrue($lock->owns_lock());
$lock->release();
$this->assertFalse($lock->owns_lock());
$ok = $lock->acquire();
$this->assertTrue($ok);
$this->assertTrue($lock->owns_lock());
$lock->release();
$this->assertFalse($lock->owns_lock());
}
/* This hangs the process.
@ -77,15 +83,18 @@ class phpbb_lock_flock_test extends phpbb_test_case
$ok = $lock->acquire();
$delta = time() - $start;
$this->assertTrue($ok);
$this->assertTrue($lock->owns_lock());
$this->assertGreaterThan(0.5, $delta, 'First lock acquired too soon');
$lock->release();
$this->assertFalse($lock->owns_lock());
// acquire again, this should be instantaneous
$start = time();
$ok = $lock->acquire();
$delta = time() - $start;
$this->assertTrue($ok);
$this->assertTrue($lock->owns_lock());
$this->assertLessThan(0.1, $delta, 'Second lock not acquired instantaneously');
// reap the child
@ -99,8 +108,10 @@ class phpbb_lock_flock_test extends phpbb_test_case
$lock = new phpbb_lock_flock($path);
$ok = $lock->acquire();
$this->assertTrue($ok);
$this->assertTrue($lock->owns_lock());
sleep(2);
$lock->release();
$this->assertFalse($lock->owns_lock());
// and go away silently
pcntl_exec('/usr/bin/env', array('true'));

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_forums">
<column>forum_id</column>
<column>parent_id</column>
<column>left_id</column>
<column>right_id</column>
<column>forum_parents</column>
<column>forum_name</column>
<column>forum_desc</column>
<column>forum_rules</column>
</table>
</dataset>

View File

@ -0,0 +1,90 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
class phpbb_tests_tree_nestedset_forum_base extends phpbb_database_test_case
{
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/phpbb_forums.xml');
}
protected $forum_data = array(
// \__/
1 => array('forum_id' => 1, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
2 => array('forum_id' => 2, 'parent_id' => 1, 'user_id' => 0, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
3 => array('forum_id' => 3, 'parent_id' => 1, 'user_id' => 0, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
// \ /
// \/
4 => array('forum_id' => 4, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => 'a:0:{}'),
5 => array('forum_id' => 5, 'parent_id' => 4, 'user_id' => 0, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
6 => array('forum_id' => 6, 'parent_id' => 5, 'user_id' => 0, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
// \_ _/
// \/
7 => array('forum_id' => 7, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
8 => array('forum_id' => 8, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
9 => array('forum_id' => 9, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
10 => array('forum_id' => 10, 'parent_id' => 9, 'user_id' => 0, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
11 => array('forum_id' => 11, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'),
// Non-existent forums
0 => array('forum_id' => 0, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 0, 'right_id' => 0, 'forum_parents' => 'a:0:{}'),
200 => array('forum_id' => 200, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 0, 'right_id' => 0, 'forum_parents' => 'a:0:{}'),
);
protected $set,
$config,
$lock,
$db;
public function setUp()
{
parent::setUp();
$this->db = $this->new_dbal();
global $config;
$config = $this->config = new phpbb_config(array('nestedset_forum_lock' => 0));
set_config(null, null, null, $this->config);
$this->lock = new phpbb_lock_db('nestedset_forum_lock', $this->config, $this->db);
$this->set = new phpbb_tree_nestedset_forum($this->db, $this->lock, 'phpbb_forums');
$this->set_up_forums();
$sql = "UPDATE phpbb_forums
SET forum_parents = 'a:0:{}'";
$this->db->sql_query($sql);
}
protected function set_up_forums()
{
$this->create_forum('Parent with two flat children');
$this->create_forum('Flat child #1', 1);
$this->create_forum('Flat child #2', 1);
$this->create_forum('Parent with two nested children');
$this->create_forum('Nested child #1', 4);
$this->create_forum('Nested child #2', 5);
$this->create_forum('Parent with flat and nested children');
$this->create_forum('Mixed child #1', 7);
$this->create_forum('Mixed child #2', 7);
$this->create_forum('Nested child #1 of Mixed child #2', 9);
$this->create_forum('Mixed child #3', 7);
}
protected function create_forum($name, $parent_id = 0)
{
$forum = $this->set->insert(array('forum_name' => $name, 'forum_desc' => '', 'forum_rules' => ''));
$this->set->change_parent($forum['forum_id'], $parent_id);
}
}

View File

@ -0,0 +1,119 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/nestedset_forum_base.php';
class phpbb_tests_tree_nestedset_forum_get_data_test extends phpbb_tests_tree_nestedset_forum_base
{
public function get_path_and_subtree_data_data()
{
return array(
array(1, true, true, array(1, 2, 3)),
array(1, true, false, array(2, 3)),
array(1, false, true, array(3, 2, 1)),
array(1, false, false, array(3, 2)),
array(2, true, true, array(1, 2)),
array(2, true, false, array(1)),
array(2, false, true, array(2, 1)),
array(2, false, false, array(1)),
array(5, true, true, array(4, 5, 6)),
array(5, true, false, array(4, 6)),
array(5, false, true, array(6, 5, 4)),
array(5, false, false, array(6, 4)),
);
}
/**
* @dataProvider get_path_and_subtree_data_data
*/
public function test_get_path_and_subtree_data($forum_id, $order_asc, $include_item, $expected)
{
$this->assertEquals($expected, array_keys($this->set->get_path_and_subtree_data($forum_id, $order_asc, $include_item)));
}
public function get_path_data_data()
{
return array(
array(1, true, true, array(1)),
array(1, true, false, array()),
array(1, false, true, array(1)),
array(1, false, false, array()),
array(2, true, true, array(1, 2)),
array(2, true, false, array(1)),
array(2, false, true, array(2, 1)),
array(2, false, false, array(1)),
array(5, true, true, array(4, 5)),
array(5, true, false, array(4)),
array(5, false, true, array(5, 4)),
array(5, false, false, array(4)),
);
}
/**
* @dataProvider get_path_data_data
*/
public function test_get_path_data($forum_id, $order_asc, $include_item, $expected)
{
$this->assertEquals($expected, array_keys($this->set->get_path_data($forum_id, $order_asc, $include_item)));
}
public function get_subtree_data_data()
{
return array(
array(1, true, true, array(1, 2, 3)),
array(1, true, false, array(2, 3)),
array(1, false, true, array(3, 2, 1)),
array(1, false, false, array(3, 2)),
array(2, true, true, array(2)),
array(2, true, false, array()),
array(2, false, true, array(2)),
array(2, false, false, array()),
array(5, true, true, array(5, 6)),
array(5, true, false, array(6)),
array(5, false, true, array(6, 5)),
array(5, false, false, array(6)),
);
}
/**
* @dataProvider get_subtree_data_data
*/
public function test_get_subtree_data($forum_id, $order_asc, $include_item, $expected)
{
$this->assertEquals($expected, array_keys($this->set->get_subtree_data($forum_id, $order_asc, $include_item)));
}
public function get_path_basic_data_data()
{
return array(
array(1, '', array()),
array(1, serialize(array()), array()),
array(2, '', array(1)),
array(2, serialize(array(1 => array())), array(1)),
array(10, '', array(7, 9)),
array(10, serialize(array(7 => array(), 9 => array())), array(7, 9)),
);
}
/**
* @dataProvider get_path_basic_data_data
*/
public function test_get_path_basic_data($forum_id, $forum_parents, $expected)
{
$forum_data = $this->forum_data[$forum_id];
$forum_data['forum_parents'] = $forum_parents;
$this->assertEquals($expected, array_keys($this->set->get_path_basic_data($forum_data)));
}
}

View File

@ -0,0 +1,120 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/nestedset_forum_base.php';
class phpbb_tests_tree_nestedset_forum_add_remove_test extends phpbb_tests_tree_nestedset_forum_base
{
public function delete_data()
{
return array(
array(1, array(1, 2, 3), array(
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 16),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 8, 'right_id' => 9),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 13),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 11, 'right_id' => 12),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
)),
array(2, array(2), array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 4),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 5, 'right_id' => 10),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 6, 'right_id' => 9),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 7, 'right_id' => 8),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 20),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19),
)),
);
}
/**
* @dataProvider delete_data
*/
public function test_delete($forum_id, $expected_deleted, $expected)
{
$this->assertEquals($expected_deleted, $this->set->delete($forum_id));
$result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id
FROM phpbb_forums
ORDER BY left_id, forum_id ASC");
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
public function delete_throws_data()
{
return array(
array('Not an item', 0),
array('Item does not exist', 200),
);
}
/**
* @dataProvider delete_throws_data
*
* @expectedException OutOfBoundsException
* @expectedExceptionMessage FORUM_NESTEDSET_INVALID_ITEM
*/
public function test_delete_throws($explain, $forum_id)
{
$this->set->delete($forum_id);
}
public function insert_data()
{
return array(
array(array(
'forum_desc' => '',
'forum_rules' => '',
'forum_id' => 12,
'parent_id' => 0,
'left_id' => 23,
'right_id' => 24,
'forum_parents' => '',
), array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
array('forum_id' => 12, 'parent_id' => 0, 'left_id' => 23, 'right_id' => 24),
)),
);
}
/**
* @dataProvider insert_data
*/
public function test_insert($expected_data, $expected)
{
$this->assertEquals($expected_data, $this->set->insert(array(
'forum_desc' => '',
'forum_rules' => '',
)));
$result = $this->db->sql_query('SELECT forum_id, parent_id, left_id, right_id
FROM phpbb_forums
ORDER BY left_id, forum_id ASC');
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
}

View File

@ -0,0 +1,569 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/nestedset_forum_base.php';
class phpbb_tests_tree_nestedset_forum_move_test extends phpbb_tests_tree_nestedset_forum_base
{
public function move_data()
{
return array(
array('Move first item up',
1, 1, false, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
array('Move last item down',
7, -1, false, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
array('Move first item down',
1, -1, true, array(
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4),
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 8, 'right_id' => 9),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 10, 'right_id' => 11),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
array('Move second item up',
4, 1, true, array(
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4),
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 8, 'right_id' => 9),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 10, 'right_id' => 11),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
array('Move last item up',
7, 1, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 16),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 8, 'right_id' => 9),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 13),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 11, 'right_id' => 12),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 17, 'right_id' => 22),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 18, 'right_id' => 21),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20),
)),
array('Move last item up by 2',
7, 2, true, array(
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 10),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 4, 'right_id' => 7),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 5, 'right_id' => 6),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 8, 'right_id' => 9),
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 16),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 12, 'right_id' => 13),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 17, 'right_id' => 22),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 18, 'right_id' => 21),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20),
)),
array('Move last item up by 100',
7, 100, true, array(
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 10),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 4, 'right_id' => 7),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 5, 'right_id' => 6),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 8, 'right_id' => 9),
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 16),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 12, 'right_id' => 13),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 17, 'right_id' => 22),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 18, 'right_id' => 21),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20),
)),
);
}
/**
* @dataProvider move_data
*/
public function test_move($explain, $forum_id, $delta, $expected_moved, $expected)
{
$this->assertEquals($expected_moved, $this->set->move($forum_id, $delta));
$result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id
FROM phpbb_forums
ORDER BY left_id, forum_id ASC");
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
public function move_down_data()
{
return array(
array('Move last item down',
7, false, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
array('Move first item down',
1, true, array(
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4),
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 8, 'right_id' => 9),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 10, 'right_id' => 11),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
);
}
/**
* @dataProvider move_down_data
*/
public function test_move_down($explain, $forum_id, $expected_moved, $expected)
{
$this->assertEquals($expected_moved, $this->set->move_down($forum_id));
$result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id
FROM phpbb_forums
ORDER BY left_id, forum_id ASC");
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
public function move_up_data()
{
return array(
array('Move first item up',
1, false, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
array('Move second item up',
4, true, array(
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4),
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 8, 'right_id' => 9),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 10, 'right_id' => 11),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
);
}
/**
* @dataProvider move_up_data
*/
public function test_move_up($explain, $forum_id, $expected_moved, $expected)
{
$this->assertEquals($expected_moved, $this->set->move_up($forum_id));
$result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id
FROM phpbb_forums
ORDER BY left_id, forum_id ASC");
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
public function move_children_data()
{
return array(
array('Item has no children',
2, 1, false, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'),
)),
array('Move to same parent',
4, 4, false, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'),
)),
array('Move single child up',
5, 1, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 8, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 6, 'parent_id' => 1, 'left_id' => 6, 'right_id' => 7, 'forum_parents' => ''),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 12, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'),
)),
array('Move nested children up',
4, 1, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 1, 'left_id' => 6, 'right_id' => 9, 'forum_parents' => ''),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => ''),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 12, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'),
)),
array('Move single child down',
5, 7, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 9, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 6, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''),
)),
array('Move nested children down',
4, 7, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 13, 'right_id' => 14, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 17, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 21, 'forum_parents' => ''),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20, 'forum_parents' => ''),
)),
array('Move single child to parent 0',
5, 0, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 9, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 20, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 6, 'parent_id' => 0, 'left_id' => 21, 'right_id' => 22, 'forum_parents' => ''),
)),
array('Move nested children to parent 0',
4, 0, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 13, 'right_id' => 14, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 17, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 0, 'left_id' => 19, 'right_id' => 22, 'forum_parents' => ''),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''),
)),
);
}
/**
* @dataProvider move_children_data
*/
public function test_move_children($explain, $forum_id, $target_id, $expected_moved, $expected)
{
$this->assertEquals($expected_moved, $this->set->move_children($forum_id, $target_id));
$result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id, forum_parents
FROM phpbb_forums
ORDER BY left_id, forum_id ASC");
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
public function move_children_throws_item_data()
{
return array(
array('Item 0 does not exist', 0, 5),
array('Item does not exist', 200, 5),
);
}
/**
* @dataProvider move_children_throws_item_data
*
* @expectedException OutOfBoundsException
* @expectedExceptionMessage FORUM_NESTEDSET_INVALID_ITEM
*/
public function test_move_children_throws_item($explain, $forum_id, $target_id)
{
$this->set->move_children($forum_id, $target_id);
}
public function move_children_throws_parent_data()
{
return array(
array('New parent is child', 4, 5),
array('New parent is child 2', 7, 9),
array('New parent does not exist', 1, 200),
);
}
/**
* @dataProvider move_children_throws_parent_data
*
* @expectedException OutOfBoundsException
* @expectedExceptionMessage FORUM_NESTEDSET_INVALID_PARENT
*/
public function test_move_children_throws_parent($explain, $forum_id, $target_id)
{
$this->set->move_children($forum_id, $target_id);
}
public function change_parent_data()
{
return array(
array('Move single child up',
6, 1, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 8, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 6, 'parent_id' => 1, 'left_id' => 6, 'right_id' => 7, 'forum_parents' => ''),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 12, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'),
)),
array('Move nested children up',
5, 1, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 1, 'left_id' => 6, 'right_id' => 9, 'forum_parents' => ''),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => ''),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 12, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'),
)),
array('Move single child down',
6, 7, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 9, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 6, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''),
)),
array('Move nested children down',
5, 7, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 13, 'right_id' => 14, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 17, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 21, 'forum_parents' => ''),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20, 'forum_parents' => ''),
)),
array('Move single child to parent 0',
6, 0, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 9, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 20, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 6, 'parent_id' => 0, 'left_id' => 21, 'right_id' => 22, 'forum_parents' => ''),
)),
array('Move nested children to parent 0',
5, 0, true, array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 13, 'right_id' => 14, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 17, 'forum_parents' => 'a:0:{}'),
array('forum_id' => 5, 'parent_id' => 0, 'left_id' => 19, 'right_id' => 22, 'forum_parents' => ''),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''),
)),
);
}
/**
* @dataProvider change_parent_data
*/
public function test_change_parent($explain, $forum_id, $target_id, $expected_moved, $expected)
{
$this->assertEquals($expected_moved, $this->set->change_parent($forum_id, $target_id));
$result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id, forum_parents
FROM phpbb_forums
ORDER BY left_id, forum_id ASC");
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
public function change_parent_throws_item_data()
{
return array(
array('Item 0 does not exist', 0, 5),
array('Item does not exist', 200, 5),
);
}
/**
* @dataProvider change_parent_throws_item_data
*
* @expectedException OutOfBoundsException
* @expectedExceptionMessage FORUM_NESTEDSET_INVALID_ITEM
*/
public function test_change_parent_throws_item($explain, $forum_id, $target_id)
{
$this->set->change_parent($forum_id, $target_id);
}
public function change_parent_throws_parent_data()
{
return array(
array('New parent is child', 4, 5),
array('New parent is child 2', 7, 9),
array('New parent does not exist', 1, 200),
);
}
/**
* @dataProvider change_parent_throws_parent_data
*
* @expectedException OutOfBoundsException
* @expectedExceptionMessage FORUM_NESTEDSET_INVALID_PARENT
*/
public function test_change_parent_throws_parent($explain, $forum_id, $target_id)
{
$this->set->change_parent($forum_id, $target_id);
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/nestedset_forum_base.php';
class phpbb_tests_tree_nestedset_forum_regenerate_test extends phpbb_tests_tree_nestedset_forum_base
{
protected $fixed_set = array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => ''),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => ''),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => ''),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => ''),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => ''),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => ''),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => ''),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => ''),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => ''),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => ''),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''),
);
public function regenerate_left_right_ids_data()
{
return array(
array('UPDATE phpbb_forums
SET left_id = 0,
right_id = 0', false),
array('UPDATE phpbb_forums
SET left_id = 28,
right_id = 28
WHERE left_id > 12', false),
array('UPDATE phpbb_forums
SET left_id = left_id * 2,
right_id = right_id * 2', false),
array('UPDATE phpbb_forums
SET left_id = left_id * 2,
right_id = right_id * 2
WHERE left_id > 12', false),
array('UPDATE phpbb_forums
SET left_id = left_id - 4,
right_id = right_id * 4
WHERE left_id > 4', false),
array('UPDATE phpbb_forums
SET left_id = 0,
right_id = 0
WHERE left_id > 12', true),
);
}
/**
* @dataProvider regenerate_left_right_ids_data
*/
public function test_regenerate_left_right_ids($breaking_query, $reset_ids)
{
$result = $this->db->sql_query($breaking_query);
$this->assertEquals(23, $this->set->regenerate_left_right_ids(1, 0, $reset_ids));
$result = $this->db->sql_query('SELECT forum_id, parent_id, left_id, right_id, forum_parents
FROM phpbb_forums
ORDER BY left_id, forum_id ASC');
$this->assertEquals($this->fixed_set, $this->db->sql_fetchrowset($result));
}
}

View File

@ -0,0 +1,116 @@
<?php
/**
*
* @package tree
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/nestedset_forum_base.php';
class pphpbb_tests_tree_nestedset_forum_test extends phpbb_tests_tree_nestedset_forum_base
{
public function forum_constructor_data()
{
return array(
array(array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6),
array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3),
array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5),
array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12),
array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11),
array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10),
array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22),
array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15),
array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19),
array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18),
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21),
)),
);
}
/**
* @dataProvider forum_constructor_data
*/
public function test_forum_constructor($expected)
{
$result = $this->db->sql_query('SELECT forum_id, parent_id, left_id, right_id
FROM phpbb_forums
ORDER BY left_id, forum_id ASC');
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
public function get_sql_where_data()
{
return array(
array('SELECT forum_id
FROM phpbb_forums
%s
ORDER BY forum_id ASC',
'WHERE', '', array(
array('forum_id' => 1),
array('forum_id' => 2),
array('forum_id' => 3),
array('forum_id' => 4),
array('forum_id' => 5),
array('forum_id' => 6),
array('forum_id' => 7),
array('forum_id' => 8),
array('forum_id' => 9),
array('forum_id' => 10),
array('forum_id' => 11),
)),
array('SELECT f.forum_id
FROM phpbb_forums f
%s
ORDER BY f.forum_id ASC',
'WHERE', 'f.', array(
array('forum_id' => 1),
array('forum_id' => 2),
array('forum_id' => 3),
array('forum_id' => 4),
array('forum_id' => 5),
array('forum_id' => 6),
array('forum_id' => 7),
array('forum_id' => 8),
array('forum_id' => 9),
array('forum_id' => 10),
array('forum_id' => 11),
)),
array('SELECT forum_id
FROM phpbb_forums
WHERE forum_id < 4 %s
ORDER BY forum_id ASC',
'AND', '', array(
array('forum_id' => 1),
array('forum_id' => 2),
array('forum_id' => 3),
)),
array('SELECT f.forum_id
FROM phpbb_forums f
WHERE f.forum_id < 4 %s
ORDER BY f.forum_id ASC',
'AND', 'f.', array(
array('forum_id' => 1),
array('forum_id' => 2),
array('forum_id' => 3),
)),
);
}
/**
* @dataProvider get_sql_where_data
*/
public function test_get_sql_where($sql_query, $operator, $column_prefix, $expected)
{
$result = $this->db->sql_query(sprintf($sql_query, $this->set->get_sql_where($operator, $column_prefix)));
$this->assertEquals($expected, $this->db->sql_fetchrowset($result));
}
}