Skip to content

Commit

Permalink
Merge branch 'MDL-74390_311' of https://github.com/timhunt/moodle int…
Browse files Browse the repository at this point in the history
…o MOODLE_311_STABLE
  • Loading branch information
junpataleta committed Apr 1, 2022
2 parents 660b5b0 + 6b8ae1c commit 5562361
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/filterlib.php
Expand Up @@ -664,6 +664,13 @@ function filter_set_global_state($filtername, $state, $move = 0) {

// Move only active.
if ($move != 0 and isset($on[$filter->filter])) {
// Capture the old order for logging.
$oldorder = implode(', ', array_map(
function($f) {
return $f->filter;
}, $on));

// Work out the new order.
$i = 1;
foreach ($on as $f) {
$f->newsortorder = $i;
Expand All @@ -686,6 +693,13 @@ function filter_set_global_state($filtername, $state, $move = 0) {
}

core_collator::asort_objects_by_property($on, 'newsortorder', core_collator::SORT_NUMERIC);

// Log in config_log.
$neworder = implode(', ', array_map(
function($f) {
return $f->filter;
}, $on));
add_to_config_log('order', $oldorder, $neworder, 'core_filter');
}

// Inactive are sorted by filter name.
Expand Down
20 changes: 20 additions & 0 deletions lib/tests/filterlib_test.php
Expand Up @@ -131,6 +131,8 @@ public function test_update_existing_dont_duplicate() {
}

public function test_update_reorder_down() {
global $DB;

$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
Expand All @@ -141,9 +143,19 @@ public function test_update_reorder_down() {
filter_set_global_state('two', TEXTFILTER_ON, -1);
// Validate.
$this->assert_global_sort_order(array('two', 'one', 'three'));

// Check this was logged in config log.
$logs = $DB->get_records('config_log', null, 'id DESC', '*', 0, 1);
$log = reset($logs);
$this->assertEquals('core_filter', $log->plugin);
$this->assertEquals('order', $log->name);
$this->assertEquals('two, one, three', $log->value);
$this->assertEquals('one, two, three', $log->oldvalue);
}

public function test_update_reorder_up() {
global $DB;

$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
Expand All @@ -155,6 +167,14 @@ public function test_update_reorder_up() {
filter_set_global_state('two', TEXTFILTER_ON, 1);
// Validate.
$this->assert_global_sort_order(array('one', 'three', 'two', 'four'));

// Check this was logged in config log.
$logs = $DB->get_records('config_log', null, 'id DESC', '*', 0, 1);
$log = reset($logs);
$this->assertEquals('core_filter', $log->plugin);
$this->assertEquals('order', $log->name);
$this->assertEquals('one, three, two, four', $log->value);
$this->assertEquals('one, two, three, four', $log->oldvalue);
}

public function test_auto_sort_order_change_to_enabled() {
Expand Down

0 comments on commit 5562361

Please sign in to comment.