mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
filters: MDL-7336 functions for getting and setting filter_config
This commit is contained in:
parent
c07e6d8da5
commit
e1a9622fad
2 changed files with 313 additions and 3 deletions
|
@ -161,9 +161,9 @@ function filter_get_all_installed() {
|
|||
|
||||
/**
|
||||
* Set the global activated state for a text filter.
|
||||
* @param $filter The filter name, for example 'filter/tex' or 'mod/glossary'.
|
||||
* @param $state One of the values TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.
|
||||
* @param $sortorder (optional) a position in the sortorder to place this filter.
|
||||
* @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:
|
||||
* No change in order if we are updating an exsiting record, and not changing to or from TEXTFILTER_DISABLED.
|
||||
* Just after the last currently active filter for a change to TEXTFILTER_ON or TEXTFILTER_OFF
|
||||
|
@ -242,6 +242,48 @@ function filter_set_global_state($filter, $state, $sortorder = false) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 string $name the setting name.
|
||||
* @param string $value the corresponding value.
|
||||
*/
|
||||
function filter_set_local_config($filter, $contextid, $name, $value) {
|
||||
global $DB;
|
||||
$rec = $DB->get_record('filter_config', array('filter' => $filter, 'contextid' => $contextid, 'name' => $name));
|
||||
$insert = false;
|
||||
if (empty($rec)) {
|
||||
$insert = true;
|
||||
$rec = new stdClass;
|
||||
$rec->filter = $filter;
|
||||
$rec->contextid = $contextid;
|
||||
$rec->name = $name;
|
||||
}
|
||||
|
||||
$rec->value = $value;
|
||||
|
||||
if ($insert) {
|
||||
$DB->insert_record('filter_config', $rec);
|
||||
} else {
|
||||
$DB->update_record('filter_config', $rec);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get local config variables for a filter in a context. Normally (when your
|
||||
* filter is running) you don't need to call this, becuase the config is fetched
|
||||
* for you automatically. You only need this, for example, when you are getting
|
||||
* the config so you can show the user an editing from.
|
||||
* @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.
|
||||
* @return array of name => value pairs.
|
||||
*/
|
||||
function filter_get_local_config($filter, $contextid) {
|
||||
global $DB;
|
||||
return $DB->get_records_menu('filter_config', array('filter' => $filter, 'contextid' => $contextid), '', 'name,value');
|
||||
}
|
||||
|
||||
/**
|
||||
* Process phrases intelligently found within a HTML text (such as adding links)
|
||||
*
|
||||
|
|
268
lib/simpletest/testfilterconfig.php
Normal file
268
lib/simpletest/testfilterconfig.php
Normal file
|
@ -0,0 +1,268 @@
|
|||
<?php // $Id$
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// NOTICE OF COPYRIGHT //
|
||||
// //
|
||||
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
||||
// http://moodle.org //
|
||||
// //
|
||||
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
|
||||
// //
|
||||
// This program is free software; you can redistribute it and/or modify //
|
||||
// it under the terms of the GNU General Public License as published by //
|
||||
// the Free Software Foundation; either version 2 of the License, or //
|
||||
// (at your option) any later version. //
|
||||
// //
|
||||
// This program is distributed in the hope that it will be useful, //
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
||||
// GNU General Public License for more details: //
|
||||
// //
|
||||
// http://www.gnu.org/copyleft/gpl.html //
|
||||
// //
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Tests for the parts of ../filterlib.php that involve loading the configuration
|
||||
* from, and saving the configuration to, the database.
|
||||
*
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package moodlecore
|
||||
*/
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
||||
}
|
||||
|
||||
require_once($CFG->libdir . '/filterlib.php');
|
||||
|
||||
/**
|
||||
* Test functions that use just the filter_active table.
|
||||
*/
|
||||
class filter_active_test extends UnitTestCaseUsingDatabase {
|
||||
private $syscontextid;
|
||||
|
||||
public function setUp() {
|
||||
// Make sure accesslib has cached a sensible system context object
|
||||
// before we switch to the test DB.
|
||||
$this->syscontextid = get_context_instance(CONTEXT_SYSTEM)->id;
|
||||
|
||||
// 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_filter_globally($filter, $state) {
|
||||
$recs = $this->testdb->get_records('filter_active');
|
||||
$this->assertEqual(1, count($recs), 'More than one record returned %s.');
|
||||
$rec = reset($recs);
|
||||
$this->assertEqual($rec->filter, $filter);
|
||||
$this->assertEqual($rec->contextid, $this->syscontextid);
|
||||
$this->assertEqual($rec->active, $state);
|
||||
$this->assertEqual($rec->sortorder, 1);
|
||||
}
|
||||
|
||||
private function assert_global_sort_order($filters) {
|
||||
$sortedfilters = $this->testdb->get_records_menu('filter_active',
|
||||
array('contextid' => $this->syscontextid), 'sortorder', 'sortorder,filter');
|
||||
$testarray = array();
|
||||
$index = 1;
|
||||
foreach($filters as $filter) {
|
||||
$testarray[$index++] = $filter;
|
||||
}
|
||||
$this->assertEqual($testarray, $sortedfilters);
|
||||
}
|
||||
|
||||
public function test_set_filter_globally_on() {
|
||||
// Setup fixture.
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/name', TEXTFILTER_ON, 1);
|
||||
// Validate.
|
||||
$this->assert_only_one_filter_globally('filter/name', TEXTFILTER_ON);
|
||||
}
|
||||
|
||||
public function test_set_filter_globally_off() {
|
||||
// Setup fixture.
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/name', TEXTFILTER_OFF, 1);
|
||||
// Validate.
|
||||
$this->assert_only_one_filter_globally('filter/name', TEXTFILTER_OFF);
|
||||
}
|
||||
|
||||
public function test_set_filter_globally_disabled() {
|
||||
// Setup fixture.
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/name', TEXTFILTER_DISABLED, 1);
|
||||
// Validate.
|
||||
$this->assert_only_one_filter_globally('filter/name', TEXTFILTER_DISABLED);
|
||||
}
|
||||
|
||||
public function test_global_config_exception_on_invalid_state() {
|
||||
// Setup fixture.
|
||||
// Set expectation.
|
||||
$this->expectException();
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/name', 0, 1);
|
||||
}
|
||||
|
||||
public function test_set_no_sortorder_clash() {
|
||||
// Setup fixture.
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/one', TEXTFILTER_DISABLED, 1);
|
||||
filter_set_global_state('filter/two', TEXTFILTER_DISABLED, 1);
|
||||
// Validate - should have pushed other filters down.
|
||||
$this->assert_global_sort_order(array('filter/two', 'filter/one'));
|
||||
}
|
||||
|
||||
public function test_auto_sort_order_disabled() {
|
||||
// Setup fixture.
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/one', TEXTFILTER_DISABLED);
|
||||
filter_set_global_state('filter/two', TEXTFILTER_DISABLED);
|
||||
// Validate.
|
||||
$this->assert_global_sort_order(array('filter/one', 'filter/two'));
|
||||
}
|
||||
|
||||
public function test_auto_sort_order_enabled() {
|
||||
// Setup fixture.
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/one', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/two', TEXTFILTER_OFF);
|
||||
// Validate.
|
||||
$this->assert_global_sort_order(array('filter/one', 'filter/two'));
|
||||
}
|
||||
|
||||
public function test_auto_sort_order_mixed() {
|
||||
// Setup fixture.
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/0', TEXTFILTER_DISABLED);
|
||||
filter_set_global_state('filter/1', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/2', TEXTFILTER_DISABLED);
|
||||
filter_set_global_state('filter/3', TEXTFILTER_OFF);
|
||||
// Validate.
|
||||
$this->assert_global_sort_order(array('filter/1', 'filter/3', 'filter/0', 'filter/2'));
|
||||
}
|
||||
|
||||
public function test_update_existing_dont_duplicate() {
|
||||
// Setup fixture.
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/name', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/name', TEXTFILTER_OFF);
|
||||
// Validate.
|
||||
$this->assert_only_one_filter_globally('filter/name', TEXTFILTER_OFF);
|
||||
}
|
||||
|
||||
public function test_sort_order_not_too_low() {
|
||||
// Setup fixture.
|
||||
filter_set_global_state('filter/1', TEXTFILTER_ON);
|
||||
// Set expectation.
|
||||
$this->expectException();
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/2', TEXTFILTER_ON, 0);
|
||||
}
|
||||
|
||||
public function test_sort_order_not_too_high() {
|
||||
// Setup fixture.
|
||||
filter_set_global_state('filter/1', TEXTFILTER_ON);
|
||||
// Set expectation.
|
||||
$this->expectException();
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/2', TEXTFILTER_ON, 3);
|
||||
}
|
||||
|
||||
public function test_update_reorder_down() {
|
||||
// Setup fixture.
|
||||
filter_set_global_state('filter/1', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/2', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/3', TEXTFILTER_ON);
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/2', TEXTFILTER_ON, 1);
|
||||
// Validate.
|
||||
$this->assert_global_sort_order(array('filter/2', 'filter/1', 'filter/3'));
|
||||
}
|
||||
|
||||
public function test_update_reorder_up() {
|
||||
// Setup fixture.
|
||||
filter_set_global_state('filter/1', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/2', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/3', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/4', TEXTFILTER_ON);
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/2', TEXTFILTER_ON, 3);
|
||||
// Validate.
|
||||
$this->assert_global_sort_order(array('filter/1', 'filter/3', 'filter/2', 'filter/4'));
|
||||
}
|
||||
|
||||
public function test_auto_sort_order_change_to_enabled() {
|
||||
// Setup fixture.
|
||||
filter_set_global_state('filter/1', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/2', TEXTFILTER_DISABLED);
|
||||
filter_set_global_state('filter/3', TEXTFILTER_DISABLED);
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/3', TEXTFILTER_ON);
|
||||
// Validate.
|
||||
$this->assert_global_sort_order(array('filter/1', 'filter/3', 'filter/2'));
|
||||
}
|
||||
|
||||
public function test_auto_sort_order_change_to_disabled() {
|
||||
// Setup fixture.
|
||||
filter_set_global_state('filter/1', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/2', TEXTFILTER_ON);
|
||||
filter_set_global_state('filter/3', TEXTFILTER_DISABLED);
|
||||
// Exercise SUT.
|
||||
filter_set_global_state('filter/1', TEXTFILTER_DISABLED);
|
||||
// Validate.
|
||||
$this->assert_global_sort_order(array('filter/2', 'filter/3', 'filter/1'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test functions that use just the filter_config table.
|
||||
*/
|
||||
class filter_config_test extends UnitTestCaseUsingDatabase {
|
||||
public function setUp() {
|
||||
// Create the table we need and switch to test DB.
|
||||
$this->create_test_table('filter_config', 'lib');
|
||||
$this->switch_to_test_db();
|
||||
}
|
||||
|
||||
private function assert_only_one_config($filter, $context, $name, $value) {
|
||||
$recs = $this->testdb->get_records('filter_config');
|
||||
$this->assertEqual(1, count($recs), 'More than one record returned %s.');
|
||||
$rec = reset($recs);
|
||||
$this->assertEqual($rec->filter, $filter);
|
||||
$this->assertEqual($rec->contextid, $context);
|
||||
$this->assertEqual($rec->name, $name);
|
||||
$this->assertEqual($rec->value, $value);
|
||||
}
|
||||
|
||||
public function test_set_new_config() {
|
||||
// Exercise SUT.
|
||||
filter_set_local_config('filter/name', 123, 'settingname', 'An arbitrary value');
|
||||
// Validate.
|
||||
$this->assert_only_one_config('filter/name', 123, 'settingname', 'An arbitrary value');
|
||||
}
|
||||
|
||||
public function test_update_existing_config() {
|
||||
// Setup fixture.
|
||||
filter_set_local_config('filter/name', 123, 'settingname', 'An arbitrary value');
|
||||
// Exercise SUT.
|
||||
filter_set_local_config('filter/name', 123, 'settingname', 'A changed value');
|
||||
// Validate.
|
||||
$this->assert_only_one_config('filter/name', 123, 'settingname', 'A changed value');
|
||||
}
|
||||
|
||||
public function test_filter_get_local_config() {
|
||||
// Setup fixture.
|
||||
filter_set_local_config('filter/name', 123, 'setting1', 'An arbitrary value');
|
||||
filter_set_local_config('filter/name', 123, 'setting2', 'Another arbitrary value');
|
||||
filter_set_local_config('filter/name', 122, 'settingname', 'Value from another context');
|
||||
filter_set_local_config('filter/other', 123, 'settingname', 'Someone else\'s value');
|
||||
// Exercise SUT.
|
||||
$config = filter_get_local_config('filter/name', 123);
|
||||
// Validate.
|
||||
$this->assertEqual(array('setting1' => 'An arbitrary value', 'setting2' => 'Another arbitrary value'), $config);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue