* 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);
/**
* 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:
// 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.
}
}
+/**
+ * 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.
*/
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() {
}
}
+/**
+ * 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.
*/