From 1bd09db94763f99eb45c5c40509b8c5fef3c4f31 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Mon, 13 Apr 2009 06:53:53 +0000 Subject: [PATCH] filters: MDL-7336 implement get_active_filters --- lib/filterlib.php | 47 ++++++++++ lib/simpletest/testfilterconfig.php | 136 ++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) diff --git a/lib/filterlib.php b/lib/filterlib.php index 3e1fc91511..19aef380a3 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -328,6 +328,53 @@ function filter_get_local_config($filter, $contextid) { return $DB->get_records_menu('filter_config', array('filter' => $filter, 'contextid' => $contextid), '', 'name,value'); } +/** + * Get the list of active filters, in the order that they should be used + * for a particular context, along with any local configuration variables. + * + * @param object $context a context + * + * @return array an array where the keys are the filter names, for example + * 'filter/tex' or 'mod/glossary' and the values are any local + * configuration for that filter, as an array of name => value pairs + * from the filter_config table. In a lot of cases, this will be an + * empty array. So, an example return value for this function might be + * array('filter/tex' => array(), 'mod/glossary' => array('glossaryid', 123)) + */ +function get_active_filters($context) { + global $DB; + $contextids = str_replace('/', ',', trim($context->path, '/')); + + // The following SQL is tricky. It is explained on + // http://docs.moodle.org/en/Development:Filter_enable/disable_by_context + $rs = $DB->get_recordset_sql( + "SELECT active.filter, fc.name, fc.value + FROM (SELECT f.filter + FROM {filter_active} f + JOIN {context} ctx ON f.contextid = ctx.id + WHERE ctx.id IN ($contextids) + GROUP BY filter + HAVING MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth) + ORDER BY MAX(f.sortorder)) active + LEFT JOIN {filter_config} fc ON fc.filter = active.filter + WHERE fc.contextid = $context->id OR fc.contextid IS NULL"); + + // Masssage the data into the specified format to return. + $filters = array(); + foreach ($rs as $row) { + if (!isset($filters[$row->filter])) { + $filters[$row->filter] = array(); + } + if (!is_null($row->name)) { + $filters[$row->filter][$row->name] = $row->value; + } + } + + $rs->close(); + + return $filters; +} + /** * Process phrases intelligently found within a HTML text (such as adding links) * diff --git a/lib/simpletest/testfilterconfig.php b/lib/simpletest/testfilterconfig.php index 5603c317ef..1237fbb397 100644 --- a/lib/simpletest/testfilterconfig.php +++ b/lib/simpletest/testfilterconfig.php @@ -341,4 +341,140 @@ class filter_config_test extends UnitTestCaseUsingDatabase { $this->assertEqual(array('setting1' => 'An arbitrary value', 'setting2' => 'Another arbitrary value'), $config); } } + +class get_active_filters_test extends UnitTestCaseUsingDatabase { + private $syscontext; + private $childcontext; + private $childcontext2; + + public function setUp() { + // Make sure accesslib has cached a sensible system context object + // before we switch to the test DB. + $this->syscontext = get_context_instance(CONTEXT_SYSTEM); + + // Create the table we need and switch to test DB. + $this->create_test_tables(array('filter_active', 'filter_config', 'context'), 'lib'); + $this->switch_to_test_db(); + + // Set up systcontext in the test database. + $this->testdb->insert_record('context', $this->syscontext); + $this->syscontext->id = 1; + + // Set up a child context. + $this->childcontext = new stdClass; + $this->childcontext->contextlevel = CONTEXT_COURSECAT; + $this->childcontext->instanceid = 1; + $this->childcontext->depth = 2; + $this->childcontext->path = '/1/2'; + $this->testdb->insert_record('context', $this->childcontext); + $this->childcontext->id = 2; + + // Set up a grandchild context. + $this->childcontext2 = new stdClass; + $this->childcontext2->contextlevel = CONTEXT_COURSE; + $this->childcontext2->instanceid = 2; + $this->childcontext2->depth = 3; + $this->childcontext2->path = '/1/2/3'; + $this->testdb->insert_record('context', $this->childcontext2); + $this->childcontext2->id = 3; + } + + private function assert_filter_list($expectedfilters, $filters) { + $this->assert(new ArraysHaveSameValuesExpectation($expectedfilters), array_keys($filters)); + } + + public function test_globally_on_is_returned() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_ON); + // Exercise SUT. + $filters = get_active_filters($this->syscontext); + // Validate. + $this->assert_filter_list(array('filter/name'), $filters); + // Check no config returned correctly. + $this->assertEqual(array(), $filters['filter/name']); + } + + public function test_globally_off_not_returned() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_OFF); + // Exercise SUT. + $filters = get_active_filters($this->childcontext2); + // Validate. + $this->assert_filter_list(array(), $filters); + } + + public function test_globally_off_overridden() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_OFF); + filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_ON); + // Exercise SUT. + $filters = get_active_filters($this->childcontext2); + // Validate. + $this->assert_filter_list(array('filter/name'), $filters); + } + + public function test_globally_on_overridden() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_ON); + filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_OFF); + // Exercise SUT. + $filters = get_active_filters($this->childcontext2); + // Validate. + $this->assert_filter_list(array(), $filters); + } + + public function test_globally_disabled_not_overridden() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_DISABLED); + filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_ON); + // Exercise SUT. + $filters = get_active_filters($this->syscontext); + // Validate. + $this->assert_filter_list(array(), $filters); + } + + public function test_single_config_returned() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_ON); + filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value'); + // Exercise SUT. + $filters = get_active_filters($this->childcontext); + // Validate. + $this->assertEqual(array('settingname' => 'A value'), $filters['filter/name']); + } + + public function test_multi_config_returned() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_ON); + filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value'); + filter_set_local_config('filter/name', $this->childcontext->id, 'anothersettingname', 'Another value'); + // Exercise SUT. + $filters = get_active_filters($this->childcontext); + // Validate. + $this->assertEqual(array('settingname' => 'A value', 'anothersettingname' => 'Another value'), $filters['filter/name']); + } + + public function test_config_from_other_context_not_returned() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_ON); + filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value'); + filter_set_local_config('filter/name', $this->childcontext2->id, 'anothersettingname', 'Another value'); + // Exercise SUT. + $filters = get_active_filters($this->childcontext2); + // Validate. + $this->assertEqual(array('anothersettingname' => 'Another value'), $filters['filter/name']); + } + + public function test_config_from_other_filter_not_returned() { + // Setup fixture. + filter_set_global_state('filter/name', TEXTFILTER_ON); + filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value'); + filter_set_local_config('filter/other', $this->childcontext->id, 'anothersettingname', 'Another value'); + // Exercise SUT. + $filters = get_active_filters($this->childcontext); + // Validate. + $this->assertEqual(array('settingname' => 'A value'), $filters['filter/name']); + } +} + ?> -- 2.39.5