MDL-52036 tablelib: Fix the behaviour of resetting table preferences
The problem with the previous implementation was that the table's $this->prefs can contain valid non-empty value in its default state - the default column to sort by. On resetting, we must not throw away these default prefs. This patch simplifies the TABLE_VAR_RESET interpretation. If such an HTTP parameter is passed via the request, the table simply behaves as if there were no previously stored preferences (does not matter if coming from the current session, or from the persistent cross-session storage). The logic that decides on whether or not the reset widget should be displayed is put into a new method can_be_reset() with unit tests attached. Finally, the previously private method render_reset_button() is now protected and the reset widget is given a new semantic CSS class.
This commit is contained in:
@@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/tablelib.php');
|
||||
require_once($CFG->libdir . '/tests/fixtures/testable_flexible_table.php');
|
||||
|
||||
/**
|
||||
* Test some of tablelib.
|
||||
@@ -498,4 +499,110 @@ class core_tablelib_testcase extends basic_testcase {
|
||||
|
||||
$this->assertEquals($table5, $table6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for preparing tables instances in {@link self::test_can_be_reset()}.
|
||||
*
|
||||
* @param string $tableid
|
||||
* @return testable_flexible_table
|
||||
*/
|
||||
protected function prepare_table_for_reset_test($tableid) {
|
||||
global $SESSION;
|
||||
|
||||
unset($SESSION->flextable[$tableid]);
|
||||
|
||||
$data = $this->generate_data(25, 3);
|
||||
$columns = array('column0', 'column1', 'column2');
|
||||
$headers = $this->generate_headers(3);
|
||||
|
||||
$table = new testable_flexible_table($tableid);
|
||||
$table->define_baseurl('/invalid.php');
|
||||
$table->define_columns($columns);
|
||||
$table->define_headers($headers);
|
||||
$table->collapsible(true);
|
||||
$table->is_persistent(false);
|
||||
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function test_can_be_reset() {
|
||||
|
||||
// Table in its default state (as if seen for the first time), nothing to reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$table->setup();
|
||||
$this->assertFalse($table->can_be_reset());
|
||||
|
||||
// Table in its default state with default sorting defined, nothing to reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$table->sortable(true, 'column1', SORT_DESC);
|
||||
$table->setup();
|
||||
$this->assertFalse($table->can_be_reset());
|
||||
|
||||
// Table explicitly sorted by the default column (reverses the order), can be reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$table->sortable(true, 'column1', SORT_DESC);
|
||||
$_GET['tsort'] = 'column1';
|
||||
$table->setup();
|
||||
unset($_GET['tsort']);
|
||||
$this->assertTrue($table->can_be_reset());
|
||||
|
||||
// Table explicitly sorted twice by the default column (puts back to default order), nothing to reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$table->sortable(true, 'column1', SORT_DESC);
|
||||
$_GET['tsort'] = 'column1';
|
||||
$table->setup();
|
||||
$table->setup(); // Set up again to simulate the second page request.
|
||||
unset($_GET['tsort']);
|
||||
$this->assertFalse($table->can_be_reset());
|
||||
|
||||
// Table sorted by other than default column, can be reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$table->sortable(true, 'column1', SORT_DESC);
|
||||
$_GET['tsort'] = 'column2';
|
||||
$table->setup();
|
||||
unset($_GET['tsort']);
|
||||
$this->assertTrue($table->can_be_reset());
|
||||
|
||||
// Table sorted by the default column after another sorting previously selected.
|
||||
// This leads to different ORDER BY than just having a single sort defined, can be reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$table->sortable(true, 'column1', SORT_DESC);
|
||||
$_GET['tsort'] = 'column0';
|
||||
$table->setup();
|
||||
$_GET['tsort'] = 'column1';
|
||||
$table->setup();
|
||||
unset($_GET['tsort']);
|
||||
$this->assertTrue($table->can_be_reset());
|
||||
|
||||
// Table having some column collapsed, can be reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$_GET['thide'] = 'column2';
|
||||
$table->setup();
|
||||
unset($_GET['thide']);
|
||||
$this->assertTrue($table->can_be_reset());
|
||||
|
||||
// Table having some column explicitly expanded, nothing to reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$_GET['tshow'] = 'column2';
|
||||
$table->setup();
|
||||
unset($_GET['tshow']);
|
||||
$this->assertFalse($table->can_be_reset());
|
||||
|
||||
// Table after expanding a collapsed column, nothing to reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$_GET['thide'] = 'column0';
|
||||
$table->setup();
|
||||
$_GET['tshow'] = 'column0';
|
||||
$table->setup();
|
||||
unset($_GET['thide']);
|
||||
unset($_GET['tshow']);
|
||||
$this->assertFalse($table->can_be_reset());
|
||||
|
||||
// Table with some name filtering enabled, can be reset.
|
||||
$table = $this->prepare_table_for_reset_test(uniqid('tablelib_test_'));
|
||||
$_GET['tifirst'] = 'A';
|
||||
$table->setup();
|
||||
unset($_GET['tifirst']);
|
||||
$this->assertTrue($table->can_be_reset());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user