From 6b8ae1c1931b33c461f6361ce1027f0e0e3a017e Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Thu, 31 Mar 2022 15:49:20 +0100 Subject: [PATCH] MDL-74390 filter admin: log filter order changes in config_log --- lib/filterlib.php | 14 ++++++++++++++ lib/tests/filterlib_test.php | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/filterlib.php b/lib/filterlib.php index b3f4162b8b7..ec80be2d720 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -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; @@ -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. diff --git a/lib/tests/filterlib_test.php b/lib/tests/filterlib_test.php index a896ca7b541..21df7ed847a 100644 --- a/lib/tests/filterlib_test.php +++ b/lib/tests/filterlib_test.php @@ -131,6 +131,8 @@ class core_filterlib_testcase extends advanced_testcase { } public function test_update_reorder_down() { + global $DB; + $this->resetAfterTest(); $this->remove_all_filters_from_config(); // Remove all filters. // Setup fixture. @@ -141,9 +143,19 @@ class core_filterlib_testcase extends advanced_testcase { 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. @@ -155,6 +167,14 @@ class core_filterlib_testcase extends advanced_testcase { 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() {