2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Tests for the parts of ../filterlib.php that involve loading the configuration
19 * from, and saving the configuration to, the database.
21 * @package core_filter
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->libdir
. '/filterlib.php');
33 * Test functions that affect filter_active table with contextid = $syscontextid.
35 class filter_active_global_testcase
extends advanced_testcase
{
37 protected function setUp() {
40 $DB->delete_records('filter_active', array());
41 $this->resetAfterTest(false);
44 private function assert_only_one_filter_globally($filter, $state) {
46 $recs = $DB->get_records('filter_active');
47 $this->assertCount(1, $recs);
50 $expectedrec = new stdClass();
51 $expectedrec->filter
= $filter;
52 $expectedrec->contextid
= context_system
::instance()->id
;
53 $expectedrec->active
= $state;
54 $expectedrec->sortorder
= 1;
55 $this->assertEquals($expectedrec, $rec);
58 private function assert_global_sort_order($filters) {
61 $sortedfilters = $DB->get_records_menu('filter_active',
62 array('contextid' => context_system
::instance()->id
), 'sortorder', 'sortorder,filter');
65 foreach($filters as $filter) {
66 $testarray[$index++
] = $filter;
68 $this->assertEquals($testarray, $sortedfilters);
71 public function test_set_filter_globally_on() {
74 filter_set_global_state('name', TEXTFILTER_ON
);
76 $this->assert_only_one_filter_globally('name', TEXTFILTER_ON
);
79 public function test_set_filter_globally_off() {
82 filter_set_global_state('name', TEXTFILTER_OFF
);
84 $this->assert_only_one_filter_globally('name', TEXTFILTER_OFF
);
87 public function test_set_filter_globally_disabled() {
90 filter_set_global_state('name', TEXTFILTER_DISABLED
);
92 $this->assert_only_one_filter_globally('name', TEXTFILTER_DISABLED
);
96 * @expectedException coding_exception
99 public function test_global_config_exception_on_invalid_state() {
100 filter_set_global_state('name', 0);
103 public function test_auto_sort_order() {
106 filter_set_global_state('one', TEXTFILTER_DISABLED
);
107 filter_set_global_state('two', TEXTFILTER_DISABLED
);
109 $this->assert_global_sort_order(array('one', 'two'));
112 public function test_auto_sort_order_enabled() {
115 filter_set_global_state('one', TEXTFILTER_ON
);
116 filter_set_global_state('two', TEXTFILTER_OFF
);
118 $this->assert_global_sort_order(array('one', 'two'));
121 public function test_update_existing_dont_duplicate() {
124 filter_set_global_state('name', TEXTFILTER_ON
);
125 filter_set_global_state('name', TEXTFILTER_OFF
);
127 $this->assert_only_one_filter_globally('name', TEXTFILTER_OFF
);
130 public function test_update_reorder_down() {
132 filter_set_global_state('one', TEXTFILTER_ON
);
133 filter_set_global_state('two', TEXTFILTER_ON
);
134 filter_set_global_state('three', TEXTFILTER_ON
);
136 filter_set_global_state('two', TEXTFILTER_ON
, -1);
138 $this->assert_global_sort_order(array('two', 'one', 'three'));
141 public function test_update_reorder_up() {
143 filter_set_global_state('one', TEXTFILTER_ON
);
144 filter_set_global_state('two', TEXTFILTER_ON
);
145 filter_set_global_state('three', TEXTFILTER_ON
);
146 filter_set_global_state('four', TEXTFILTER_ON
);
148 filter_set_global_state('two', TEXTFILTER_ON
, 1);
150 $this->assert_global_sort_order(array('one', 'three', 'two', 'four'));
153 public function test_auto_sort_order_change_to_enabled() {
155 filter_set_global_state('one', TEXTFILTER_ON
);
156 filter_set_global_state('two', TEXTFILTER_DISABLED
);
157 filter_set_global_state('three', TEXTFILTER_DISABLED
);
159 filter_set_global_state('three', TEXTFILTER_ON
);
161 $this->assert_global_sort_order(array('one', 'three', 'two'));
164 public function test_auto_sort_order_change_to_disabled() {
166 filter_set_global_state('one', TEXTFILTER_ON
);
167 filter_set_global_state('two', TEXTFILTER_ON
);
168 filter_set_global_state('three', TEXTFILTER_DISABLED
);
170 filter_set_global_state('one', TEXTFILTER_DISABLED
);
172 $this->assert_global_sort_order(array('two', 'one', 'three'));
175 public function test_filter_get_global_states() {
177 filter_set_global_state('one', TEXTFILTER_ON
);
178 filter_set_global_state('two', TEXTFILTER_OFF
);
179 filter_set_global_state('three', TEXTFILTER_DISABLED
);
181 $filters = filter_get_global_states();
183 $this->assertEquals(array(
184 'one' => (object) array('filter' => 'one', 'active' => TEXTFILTER_ON
, 'sortorder' => 1),
185 'two' => (object) array('filter' => 'two', 'active' => TEXTFILTER_OFF
, 'sortorder' => 2),
186 'three' => (object) array('filter' => 'three', 'active' => TEXTFILTER_DISABLED
, 'sortorder' => 3)
193 * Test functions that affect filter_active table with contextid = $syscontextid.
195 class filter_active_local_testcase
extends advanced_testcase
{
197 protected function setUp() {
200 $DB->delete_records('filter_active', array());
201 $this->resetAfterTest(false);
204 private function assert_only_one_local_setting($filter, $contextid, $state) {
206 $recs = $DB->get_records('filter_active');
207 $this->assertEquals(1, count($recs), 'More than one record returned %s.');
210 unset($rec->sortorder
);
211 $expectedrec = new stdClass();
212 $expectedrec->filter
= $filter;
213 $expectedrec->contextid
= $contextid;
214 $expectedrec->active
= $state;
215 $this->assertEquals($expectedrec, $rec);
218 private function assert_no_local_setting() {
220 $this->assertEquals(0, $DB->count_records('filter_active'));
223 public function test_local_on() {
225 filter_set_local_state('name', 123, TEXTFILTER_ON
);
227 $this->assert_only_one_local_setting('name', 123, TEXTFILTER_ON
);
230 public function test_local_off() {
232 filter_set_local_state('name', 123, TEXTFILTER_OFF
);
234 $this->assert_only_one_local_setting('name', 123, TEXTFILTER_OFF
);
237 public function test_local_inherit() {
239 filter_set_local_state('name', 123, TEXTFILTER_INHERIT
);
241 $this->assert_no_local_setting();
245 * @expectedException coding_exception
248 public function test_local_invalid_state_throws_exception() {
250 filter_set_local_state('name', 123, -9999);
254 * @expectedException coding_exception
257 public function test_throws_exception_when_setting_global() {
259 filter_set_local_state('name', context_system
::instance()->id
, TEXTFILTER_INHERIT
);
262 public function test_local_inherit_deletes_existing() {
264 filter_set_local_state('name', 123, TEXTFILTER_INHERIT
);
266 filter_set_local_state('name', 123, TEXTFILTER_INHERIT
);
268 $this->assert_no_local_setting();
274 * Test functions that use just the filter_config table.
276 class filter_config_testcase
extends advanced_testcase
{
278 protected function setUp() {
281 $DB->delete_records('filter_config', array());
282 $this->resetAfterTest(false);
285 private function assert_only_one_config($filter, $context, $name, $value) {
287 $recs = $DB->get_records('filter_config');
288 $this->assertEquals(1, count($recs), 'More than one record returned %s.');
291 $expectedrec = new stdClass();
292 $expectedrec->filter
= $filter;
293 $expectedrec->contextid
= $context;
294 $expectedrec->name
= $name;
295 $expectedrec->value
= $value;
296 $this->assertEquals($expectedrec, $rec);
299 public function test_set_new_config() {
301 filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
303 $this->assert_only_one_config('name', 123, 'settingname', 'An arbitrary value');
306 public function test_update_existing_config() {
308 filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
310 filter_set_local_config('name', 123, 'settingname', 'A changed value');
312 $this->assert_only_one_config('name', 123, 'settingname', 'A changed value');
315 public function test_filter_get_local_config() {
317 filter_set_local_config('name', 123, 'setting1', 'An arbitrary value');
318 filter_set_local_config('name', 123, 'setting2', 'Another arbitrary value');
319 filter_set_local_config('name', 122, 'settingname', 'Value from another context');
320 filter_set_local_config('other', 123, 'settingname', 'Someone else\'s value');
322 $config = filter_get_local_config('name', 123);
324 $this->assertEquals(array('setting1' => 'An arbitrary value', 'setting2' => 'Another arbitrary value'), $config);
329 class filter_get_active_available_in_context_testcase
extends advanced_testcase
{
330 private static $syscontext;
331 private static $childcontext;
332 private static $childcontext2;
334 public static function setUpBeforeClass() {
335 parent
::setUpBeforeClass();
337 $course = self
::getDataGenerator()->create_course(array('category'=>1));
339 self
::$childcontext = context_coursecat
::instance(1);
340 self
::$childcontext2 = context_course
::instance($course->id
);
341 self
::$syscontext = context_system
::instance();
344 protected function setUp() {
348 $DB->delete_records('filter_active', array());
349 $DB->delete_records('filter_config', array());
350 $this->resetAfterTest(false);
353 private function assert_filter_list($expectedfilters, $filters) {
354 $this->assertEquals($expectedfilters, array_keys($filters), '', 0, 10, true);
357 public function test_globally_on_is_returned() {
359 filter_set_global_state('name', TEXTFILTER_ON
);
361 $filters = filter_get_active_in_context(self
::$syscontext);
363 $this->assert_filter_list(array('name'), $filters);
364 // Check no config returned correctly.
365 $this->assertEquals(array(), $filters['name']);
368 public function test_globally_off_not_returned() {
370 filter_set_global_state('name', TEXTFILTER_OFF
);
372 $filters = filter_get_active_in_context(self
::$childcontext2);
374 $this->assert_filter_list(array(), $filters);
377 public function test_globally_off_overridden() {
379 filter_set_global_state('name', TEXTFILTER_OFF
);
380 filter_set_local_state('name', self
::$childcontext->id
, TEXTFILTER_ON
);
382 $filters = filter_get_active_in_context(self
::$childcontext2);
384 $this->assert_filter_list(array('name'), $filters);
387 public function test_globally_on_overridden() {
389 filter_set_global_state('name', TEXTFILTER_ON
);
390 filter_set_local_state('name', self
::$childcontext->id
, TEXTFILTER_OFF
);
392 $filters = filter_get_active_in_context(self
::$childcontext2);
394 $this->assert_filter_list(array(), $filters);
397 public function test_globally_disabled_not_overridden() {
399 filter_set_global_state('name', TEXTFILTER_DISABLED
);
400 filter_set_local_state('name', self
::$childcontext->id
, TEXTFILTER_ON
);
402 $filters = filter_get_active_in_context(self
::$syscontext);
404 $this->assert_filter_list(array(), $filters);
407 public function test_single_config_returned() {
409 filter_set_global_state('name', TEXTFILTER_ON
);
410 filter_set_local_config('name', self
::$childcontext->id
, 'settingname', 'A value');
412 $filters = filter_get_active_in_context(self
::$childcontext);
414 $this->assertEquals(array('settingname' => 'A value'), $filters['name']);
417 public function test_multi_config_returned() {
419 filter_set_global_state('name', TEXTFILTER_ON
);
420 filter_set_local_config('name', self
::$childcontext->id
, 'settingname', 'A value');
421 filter_set_local_config('name', self
::$childcontext->id
, 'anothersettingname', 'Another value');
423 $filters = filter_get_active_in_context(self
::$childcontext);
425 $this->assertEquals(array('settingname' => 'A value', 'anothersettingname' => 'Another value'), $filters['name']);
428 public function test_config_from_other_context_not_returned() {
430 filter_set_global_state('name', TEXTFILTER_ON
);
431 filter_set_local_config('name', self
::$childcontext->id
, 'settingname', 'A value');
432 filter_set_local_config('name', self
::$childcontext2->id
, 'anothersettingname', 'Another value');
434 $filters = filter_get_active_in_context(self
::$childcontext2);
436 $this->assertEquals(array('anothersettingname' => 'Another value'), $filters['name']);
439 public function test_config_from_other_filter_not_returned() {
441 filter_set_global_state('name', TEXTFILTER_ON
);
442 filter_set_local_config('name', self
::$childcontext->id
, 'settingname', 'A value');
443 filter_set_local_config('other', self
::$childcontext->id
, 'anothersettingname', 'Another value');
445 $filters = filter_get_active_in_context(self
::$childcontext);
447 $this->assertEquals(array('settingname' => 'A value'), $filters['name']);
450 protected function assert_one_available_filter($filter, $localstate, $inheritedstate, $filters) {
451 $this->assertEquals(1, count($filters), 'More than one record returned %s.');
452 $rec = $filters[$filter];
454 $expectedrec = new stdClass();
455 $expectedrec->filter
= $filter;
456 $expectedrec->localstate
= $localstate;
457 $expectedrec->inheritedstate
= $inheritedstate;
458 $this->assertEquals($expectedrec, $rec);
461 public function test_available_in_context_localoverride() {
463 filter_set_global_state('name', TEXTFILTER_ON
);
464 filter_set_local_state('name', self
::$childcontext->id
, TEXTFILTER_OFF
);
466 $filters = filter_get_available_in_context(self
::$childcontext);
468 $this->assert_one_available_filter('name', TEXTFILTER_OFF
, TEXTFILTER_ON
, $filters);
471 public function test_available_in_context_nolocaloverride() {
473 filter_set_global_state('name', TEXTFILTER_ON
);
474 filter_set_local_state('name', self
::$childcontext->id
, TEXTFILTER_OFF
);
476 $filters = filter_get_available_in_context(self
::$childcontext2);
478 $this->assert_one_available_filter('name', TEXTFILTER_INHERIT
, TEXTFILTER_OFF
, $filters);
481 public function test_available_in_context_disabled_not_returned() {
483 filter_set_global_state('name', TEXTFILTER_DISABLED
);
484 filter_set_local_state('name', self
::$childcontext->id
, TEXTFILTER_ON
);
486 $filters = filter_get_available_in_context(self
::$childcontext);
488 $this->assertEquals(array(), $filters);
492 * @expectedException coding_exception
495 public function test_available_in_context_exception_with_syscontext() {
497 filter_get_available_in_context(self
::$syscontext);
502 class filter_preload_activities_testcase
extends advanced_testcase
{
503 private static $syscontext;
504 private static $catcontext;
505 private static $coursecontext;
506 private static $course;
507 private static $activity1context;
508 private static $activity2context;
510 public static function setUpBeforeClass() {
511 parent
::setUpBeforeClass();
513 self
::$syscontext = context_system
::instance();
514 self
::$catcontext = context_coursecat
::instance(1);
515 self
::$course = self
::getDataGenerator()->create_course(array('category'=>1));
516 self
::$coursecontext = context_course
::instance(self
::$course->id
);
517 $page1 = self
::getDataGenerator()->create_module('page', array('course'=>self
::$course->id
));
518 self
::$activity1context = context_module
::instance($page1->cmid
);
519 $page2 = self
::getDataGenerator()->create_module('page', array('course'=>self
::$course->id
));
520 self
::$activity2context = context_module
::instance($page2->cmid
);
523 protected function setUp() {
527 $DB->delete_records('filter_active', array());
528 $DB->delete_records('filter_config', array());
529 $this->resetAfterTest(false);
532 private function assert_matches($modinfo) {
533 global $FILTERLIB_PRIVATE, $DB;
535 // Use preload cache...
536 $FILTERLIB_PRIVATE = new stdClass();
537 filter_preload_activities($modinfo);
539 // Get data and check no queries are made
540 $before = $DB->perf_get_reads();
541 $plfilters1 = filter_get_active_in_context(self
::$activity1context);
542 $plfilters2 = filter_get_active_in_context(self
::$activity2context);
543 $after = $DB->perf_get_reads();
544 $this->assertEquals($before, $after);
546 // Repeat without cache and check it makes queries now
547 $FILTERLIB_PRIVATE = new stdClass
;
548 $before = $DB->perf_get_reads();
549 $filters1 = filter_get_active_in_context(self
::$activity1context);
550 $filters2 = filter_get_active_in_context(self
::$activity2context);
551 $after = $DB->perf_get_reads();
552 $this->assertTrue($after > $before);
555 $this->assertEquals($plfilters1, $filters1);
556 $this->assertEquals($plfilters2, $filters2);
559 public function test_preload() {
560 // Get course and modinfo
561 $modinfo = new course_modinfo(self
::$course, 2);
563 // Note: All the tests in this function check that the result from the
564 // preloaded cache is the same as the result from calling the standard
565 // function without preloading.
567 // Initially, check with no filters enabled
568 $this->assert_matches($modinfo);
570 // Enable filter globally, check
571 filter_set_global_state('name', TEXTFILTER_ON
);
572 $this->assert_matches($modinfo);
574 // Disable for activity 2
575 filter_set_local_state('name', self
::$activity2context->id
, TEXTFILTER_OFF
);
576 $this->assert_matches($modinfo);
578 // Disable at category
579 filter_set_local_state('name', self
::$catcontext->id
, TEXTFILTER_OFF
);
580 $this->assert_matches($modinfo);
582 // Enable for activity 1
583 filter_set_local_state('name', self
::$activity1context->id
, TEXTFILTER_ON
);
584 $this->assert_matches($modinfo);
587 filter_set_global_state('name', TEXTFILTER_DISABLED
);
588 $this->assert_matches($modinfo);
590 // Add another 2 filters
591 filter_set_global_state('frog', TEXTFILTER_ON
);
592 filter_set_global_state('zombie', TEXTFILTER_ON
);
593 $this->assert_matches($modinfo);
595 // Disable random one of these in each context
596 filter_set_local_state('zombie', self
::$activity1context->id
, TEXTFILTER_OFF
);
597 filter_set_local_state('frog', self
::$activity2context->id
, TEXTFILTER_OFF
);
598 $this->assert_matches($modinfo);
600 // Now do some filter options
601 filter_set_local_config('name', self
::$activity1context->id
, 'a', 'x');
602 filter_set_local_config('zombie', self
::$activity1context->id
, 'a', 'y');
603 filter_set_local_config('frog', self
::$activity1context->id
, 'a', 'z');
604 // These last two don't do anything as they are not at final level but I
605 // thought it would be good to have that verified in test
606 filter_set_local_config('frog', self
::$coursecontext->id
, 'q', 'x');
607 filter_set_local_config('frog', self
::$catcontext->id
, 'q', 'z');
608 $this->assert_matches($modinfo);
613 class filter_delete_config_testcase
extends advanced_testcase
{
614 protected function setUp() {
618 $DB->delete_records('filter_active', array());
619 $DB->delete_records('filter_config', array());
620 $this->resetAfterTest(false);
623 public function test_filter_delete_all_for_filter() {
627 filter_set_global_state('name', TEXTFILTER_ON
);
628 filter_set_global_state('other', TEXTFILTER_ON
);
629 filter_set_local_config('name', context_system
::instance()->id
, 'settingname', 'A value');
630 filter_set_local_config('other', context_system
::instance()->id
, 'settingname', 'Other value');
631 set_config('configname', 'A config value', 'filter_name');
632 set_config('configname', 'Other config value', 'filter_other');
634 filter_delete_all_for_filter('name');
636 $this->assertEquals(1, $DB->count_records('filter_active'));
637 $this->assertTrue($DB->record_exists('filter_active', array('filter' => 'other')));
638 $this->assertEquals(1, $DB->count_records('filter_config'));
639 $this->assertTrue($DB->record_exists('filter_config', array('filter' => 'other')));
640 $expectedconfig = new stdClass
;
641 $expectedconfig->configname
= 'Other config value';
642 $this->assertEquals($expectedconfig, get_config('filter_other'));
643 $this->assertEquals(get_config('filter_name'), new stdClass());
646 public function test_filter_delete_all_for_context() {
650 filter_set_global_state('name', TEXTFILTER_ON
);
651 filter_set_local_state('name', 123, TEXTFILTER_OFF
);
652 filter_set_local_config('name', 123, 'settingname', 'A value');
653 filter_set_local_config('other', 123, 'settingname', 'Other value');
654 filter_set_local_config('other', 122, 'settingname', 'Other value');
656 filter_delete_all_for_context(123);
658 $this->assertEquals(1, $DB->count_records('filter_active'));
659 $this->assertTrue($DB->record_exists('filter_active', array('contextid' => context_system
::instance()->id
)));
660 $this->assertEquals(1, $DB->count_records('filter_config'));
661 $this->assertTrue($DB->record_exists('filter_config', array('filter' => 'other')));
665 class filter_filter_set_applies_to_strings
extends advanced_testcase
{
666 protected $origcfgstringfilters;
667 protected $origcfgfilterall;
669 protected function setUp() {
673 $DB->delete_records('filter_active', array());
674 $DB->delete_records('filter_config', array());
675 $this->resetAfterTest(false);
677 // Store original $CFG;
678 $this->origcfgstringfilters
= $CFG->stringfilters
;
679 $this->origcfgfilterall
= $CFG->filterall
;
682 protected function tearDown() {
684 $CFG->stringfilters
= $this->origcfgstringfilters
;
685 $CFG->filterall
= $this->origcfgfilterall
;
690 public function test_set() {
694 $CFG->stringfilters
= '';
696 filter_set_applies_to_strings('name', true);
698 $this->assertEquals('name', $CFG->stringfilters
);
699 $this->assertEquals(1, $CFG->filterall
);
702 public function test_unset_to_empty() {
706 $CFG->stringfilters
= 'name';
708 filter_set_applies_to_strings('name', false);
710 $this->assertEquals('', $CFG->stringfilters
);
711 $this->assertEquals('', $CFG->filterall
);
714 public function test_unset_multi() {
718 $CFG->stringfilters
= 'name,other';
720 filter_set_applies_to_strings('name', false);
722 $this->assertEquals('other', $CFG->stringfilters
);
723 $this->assertEquals(1, $CFG->filterall
);