[ticket/216] Methods for last module order and handle_after_move()

The method for getting the last module order will return the last module
order in a column. handle_after_move() will take care of return an error
to the user or redirecting if the move was successful.

B3P-216
This commit is contained in:
Marc Alexander
2014-02-11 22:05:55 +01:00
parent 48ae4e63fb
commit feb2f52d4d
2 changed files with 118 additions and 11 deletions

View File

@@ -847,6 +847,47 @@ class portal_module
return $module_data;
}
/**
* Handle output after moving module
*
* @param bool $success Whether moving module was successful
* @param bool $is_row Whether the module move was inside a row
* @return void
*/
public function handle_after_move($success = true, $is_row = false)
{
if (!$success)
{
trigger_error($this->user->lang['UNABLE_TO_MOVE' . (($is_row) ? '_ROW' : '')] . adm_back_link($this->u_action));
}
$this->cache->destroy('portal_modules');
redirect($this->u_action); // redirect in order to get rid of excessive URL parameters
}
/**
* Get the module order to the last module in the column
*
* @param int $module_column Module column to check
* @return int Module order of the last module in the column
*/
public function get_last_module_order($module_column)
{
$modules = obtain_portal_modules();
$last_order = 1;
foreach ($modules as $cur_module)
{
if ($cur_module['module_column'] != $module_column)
{
continue;
}
$last_order = max($last_order, $cur_module['module_order']);
}
return $last_order;
}
/**
* Move module up one row
*
@@ -873,13 +914,8 @@ class portal_module
$this->db->sql_query($sql);
}
}
else
{
trigger_error($this->user->lang['UNABLE_TO_MOVE_ROW'] . adm_back_link($this->u_action));
}
$this->cache->destroy('portal_modules');
redirect($this->u_action); // redirect in order to get rid of excessive URL parameters
$this->handle_after_move($updated, true);
}
/**
@@ -887,11 +923,11 @@ class portal_module
*
* @param int $module_id ID of the module that should be moved
*/
protected function move_module_down($module_id)
public function move_module_down($module_id)
{
$module_data = $this->get_move_module_data($module_id);
if ($module_data !== false)
if ($module_data !== false && $this->get_last_module_order($module_data['module_column']) != $module_data['module_order'])
{
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order - 1

View File

@@ -33,14 +33,25 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
new \board3\portal\modules\clock(),
new \board3\portal\modules\birthday_list(new \phpbb\config\config(array()), $template, $this->db, $user),
));
$cache = $this->getMock('\phpbb\cache\cache', array('destroy', 'sql_exists'));
$cache = $this->getMock('\phpbb\cache\cache', array('destroy', 'sql_exists', 'get', 'put'));
$cache->expects($this->any())
->method('destroy')
->with($this->equalTo('portal_modules'));
$cache->expects($this->any())
->method('get')
->with($this->anything())
->will($this->returnValue(false));
$cache->expects($this->any())
->method('sql_exists')
->with($this->anything());
$cache->expects($this->any())
->method('put')
->with($this->anything());
$db = $this->db;
$user->set(array(
'UNABLE_TO_MOVE' => 'UNABLE_TO_MOVE',
'UNABLE_TO_MOVE_ROW' => 'UNABLE_TO_MOVE_ROW',
));
$this->portal_module = new \board3\portal\acp\portal_module();
}
@@ -59,17 +70,77 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
), $module_data);
}
public function data_get_last_module_order()
{
return array(
array(1, 1),
array(2, 2),
array(2, 4),
);
}
/**
* @dataProvider data_get_last_module_order
*/
public function test_get_last_module_order($expected, $column)
{
$this->assertEquals($expected, $this->portal_module->get_last_module_order($column));
}
public function test_move_module_up()
{
self::$redirected = false;
$this->portal_module->move_module_up(2);
$this->assertTrue(self::$redirected);
$this->setExpectedTriggerError(E_USER_NOTICE);
$this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE_ROW');
self::$redirected = false;
$this->portal_module->move_module_up(2);
$this->assertFalse(self::$redirected);
}
public function test_move_module_down()
{
self::$redirected = false;
$this->portal_module->move_module_down(1);
$this->assertTrue(self::$redirected);
$this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE_ROW');
self::$redirected = false;
$this->portal_module->move_module_down(1);
$this->assertFalse(self::$redirected);
}
public function data_handle_after_move()
{
return array(
array(true, false, false),
array(false, false, 'UNABLE_TO_MOVE'),
array(false, true, 'UNABLE_TO_MOVE_ROW'),
);
}
/**
* @dataProvider data_handle_after_move
*/
public function test_handle_after_move($success, $is_row, $error)
{
if ($error)
{
$this->setExpectedTriggerError(E_USER_NOTICE, $error);
}
else
{
self::$redirected = false;
}
$this->portal_module->handle_after_move($success, $is_row);
if (!$error)
{
$this->assertTrue(self::$redirected);
}
}
}
function redirect($url)