]> git.mjollnir.org Git - moodle.git/commitdiff
filters: MDL-7336 functions for g/setting local filter_active overrides
authortjhunt <tjhunt>
Mon, 13 Apr 2009 06:53:33 +0000 (06:53 +0000)
committertjhunt <tjhunt>
Mon, 13 Apr 2009 06:53:33 +0000 (06:53 +0000)
lib/filterlib.php
lib/simpletest/testfilterconfig.php

index 368908fdaefad66a0b8088b0e15bcd91ce1f5d8d..3e1fc915111428d454f6030d4eeec8e2551d3233 100644 (file)
@@ -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.
  */
index b3c4fc8629f680283e0441efac8c066a58f69604..5603c317ef769fff18b27be6d6cf7a40f37a6324 100644 (file)
@@ -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.
  */