From: tjhunt Date: Mon, 13 Apr 2009 06:53:33 +0000 (+0000) Subject: filters: MDL-7336 functions for g/setting local filter_active overrides X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=56881fdc936b5c2532a84955366f9fda5ef64543;p=moodle.git filters: MDL-7336 functions for g/setting local filter_active overrides --- diff --git a/lib/filterlib.php b/lib/filterlib.php index 368908fdae..3e1fc91511 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -5,6 +5,7 @@ * The states a filter can be in, stored in the filter_active table. */ define('TEXTFILTER_ON', 1); +define('TEXTFILTER_INHERIT', 0); define('TEXTFILTER_OFF', -1); define('TEXTFILTER_DISABLED', -9999); @@ -161,7 +162,7 @@ function filter_get_all_installed() { /** * Set the global activated state for a text filter. - * @param string$filter The filter name, for example 'filter/tex' or 'mod/glossary'. + * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'. * @param integer $state One of the values TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED. * @param integer $sortorder (optional) a position in the sortorder to place this filter. * If not given defaults to: @@ -174,8 +175,8 @@ function filter_set_global_state($filter, $state, $sortorder = false) { // Check requested state is valid. if (!in_array($state, array(TEXTFILTER_ON, TEXTFILTER_OFF, TEXTFILTER_DISABLED))) { - throw new coding_exception("Illegal option '$state' passed to filter_set_global_state. ' . - 'Must be one of TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED."); + throw new coding_exception("Illegal option '$state' passed to filter_set_global_state. " . + "Must be one of TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED."); } // Check sortorder is valid. @@ -242,10 +243,53 @@ function filter_set_global_state($filter, $state, $sortorder = false) { } } +/** + * Set the local activated state for a text filter. + * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'. + * @param integer $contextid The id of the context to get the local config for. + * @param integer $state One of the values TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_INHERIT. + */ +function filter_set_local_state($filter, $contextid, $state) { + global $DB; + + // Check requested state is valid. + if (!in_array($state, array(TEXTFILTER_ON, TEXTFILTER_OFF, TEXTFILTER_INHERIT))) { + throw new coding_exception("Illegal option '$state' passed to filter_set_local_state. " . + "Must be one of TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_INHERIT."); + } + + if ($contextid == get_context_instance(CONTEXT_SYSTEM)->id) { + throw new coding_exception('You cannot use filter_set_local_state ' . + 'with $contextid equal to the system context id.'); + } + + if ($state == TEXTFILTER_INHERIT) { + $DB->delete_records('filter_active', array('filter' => $filter, 'contextid' => $contextid)); + return; + } + + $rec = $DB->get_record('filter_active', array('filter' => $filter, 'contextid' => $contextid)); + $insert = false; + if (empty($rec)) { + $insert = true; + $rec = new stdClass; + $rec->filter = $filter; + $rec->contextid = $contextid; + } + + $rec->active = $state; + + if ($insert) { + $DB->insert_record('filter_active', $rec); + } else { + $DB->update_record('filter_active', $rec); + } +} + /** * Set a particular local config variable for a filter in a context. * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'. - * @param integer $contextid The ID of the context to get the local config for. + * @param integer $contextid The id of the context to get the local config for. * @param string $name the setting name. * @param string $value the corresponding value. */ diff --git a/lib/simpletest/testfilterconfig.php b/lib/simpletest/testfilterconfig.php index b3c4fc8629..5603c317ef 100644 --- a/lib/simpletest/testfilterconfig.php +++ b/lib/simpletest/testfilterconfig.php @@ -38,9 +38,9 @@ if (!defined('MOODLE_INTERNAL')) { require_once($CFG->libdir . '/filterlib.php'); /** - * Test functions that use just the filter_active table. + * Test functions that affect filter_active table with contextid = $syscontextid. */ -class filter_active_test extends UnitTestCaseUsingDatabase { +class filter_active_global_test extends UnitTestCaseUsingDatabase { private $syscontextid; public function setUp() { @@ -219,6 +219,78 @@ class filter_active_test extends UnitTestCaseUsingDatabase { } } +/** + * Test functions that affect filter_active table with contextid = $syscontextid. + */ +class filter_active_local_test extends UnitTestCaseUsingDatabase { + public function setUp() { + // Create the table we need and switch to test DB. + $this->create_test_table('filter_active', 'lib'); + $this->switch_to_test_db(); + } + + private function assert_only_one_local_setting($filter, $contextid, $state) { + $recs = $this->testdb->get_records('filter_active'); + $this->assertEqual(1, count($recs), 'More than one record returned %s.'); + $rec = reset($recs); + $expectedrec = new stdClass; + $expectedrec->filter = $filter; + $expectedrec->contextid = $contextid; + $expectedrec->active = $state; + $this->assert(new CheckSpecifiedFieldsExpectation($expectedrec), $rec); + } + + private function assert_no_local_setting() { + $this->assertEqual(0, $this->testdb->count_records('filter_active')); + } + + public function test_local_on() { + // Exercise SUT. + filter_set_local_state('filter/name', 123, TEXTFILTER_ON); + // Validate. + $this->assert_only_one_local_setting('filter/name', 123, TEXTFILTER_ON); + } + + public function test_local_off() { + // Exercise SUT. + filter_set_local_state('filter/name', 123, TEXTFILTER_OFF); + // Validate. + $this->assert_only_one_local_setting('filter/name', 123, TEXTFILTER_OFF); + } + + public function test_local_inherit() { + global $DB; + // Exercise SUT. + filter_set_local_state('filter/name', 123, TEXTFILTER_INHERIT); + // Validate. + $this->assert_no_local_setting(); + } + + public function test_local_invalid_state_throws_exception() { + // Set expectation. + $this->expectException(); + // Exercise SUT. + filter_set_local_state('filter/name', 123, -9999); + } + + public function test_throws_exception_when_setting_global() { + // Set expectation. + $this->expectException(); + // Exercise SUT. + filter_set_local_state('filter/name', get_context_instance(CONTEXT_SYSTEM)->id, TEXTFILTER_INHERIT); + } + + public function test_local_inherit_deletes_existing() { + global $DB; + // Setup fixture. + filter_set_local_state('filter/name', 123, TEXTFILTER_INHERIT); + // Exercise SUT. + filter_set_local_state('filter/name', 123, TEXTFILTER_INHERIT); + // Validate. + $this->assert_no_local_setting(); + } +} + /** * Test functions that use just the filter_config table. */