Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / tests / filterlib_test.php
blobc3aab08cb91dd4a07959f60cfd8f92f72050f1a3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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
22 * @category phpunit
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->libdir . '/filterlib.php');
32 /**
33 * Test filters.
35 class core_filterlib_testcase extends advanced_testcase {
36 private $syscontext;
37 private $childcontext;
38 private $childcontext2;
39 private $catcontext;
40 private $coursecontext;
41 private $course;
42 private $activity1context;
43 private $activity2context;
45 protected function setUp() {
46 global $DB;
47 parent::setUp();
49 $this->resetAfterTest();
50 $DB->delete_records('filter_active', array());
51 $DB->delete_records('filter_config', array());
54 private function assert_only_one_filter_globally($filter, $state) {
55 global $DB;
56 $recs = $DB->get_records('filter_active');
57 $this->assertCount(1, $recs);
58 $rec = reset($recs);
59 unset($rec->id);
60 $expectedrec = new stdClass();
61 $expectedrec->filter = $filter;
62 $expectedrec->contextid = context_system::instance()->id;
63 $expectedrec->active = $state;
64 $expectedrec->sortorder = 1;
65 $this->assertEquals($expectedrec, $rec);
68 private function assert_global_sort_order($filters) {
69 global $DB;
71 $sortedfilters = $DB->get_records_menu('filter_active',
72 array('contextid' => context_system::instance()->id), 'sortorder', 'sortorder,filter');
73 $testarray = array();
74 $index = 1;
75 foreach ($filters as $filter) {
76 $testarray[$index++] = $filter;
78 $this->assertEquals($testarray, $sortedfilters);
81 public function test_set_filter_globally_on() {
82 // Setup fixture.
83 // Exercise SUT.
84 filter_set_global_state('name', TEXTFILTER_ON);
85 // Validate.
86 $this->assert_only_one_filter_globally('name', TEXTFILTER_ON);
89 public function test_set_filter_globally_off() {
90 // Setup fixture.
91 // Exercise SUT.
92 filter_set_global_state('name', TEXTFILTER_OFF);
93 // Validate.
94 $this->assert_only_one_filter_globally('name', TEXTFILTER_OFF);
97 public function test_set_filter_globally_disabled() {
98 // Setup fixture.
99 // Exercise SUT.
100 filter_set_global_state('name', TEXTFILTER_DISABLED);
101 // Validate.
102 $this->assert_only_one_filter_globally('name', TEXTFILTER_DISABLED);
106 * @expectedException coding_exception
108 public function test_global_config_exception_on_invalid_state() {
109 filter_set_global_state('name', 0);
112 public function test_auto_sort_order() {
113 // Setup fixture.
114 // Exercise SUT.
115 filter_set_global_state('one', TEXTFILTER_DISABLED);
116 filter_set_global_state('two', TEXTFILTER_DISABLED);
117 // Validate.
118 $this->assert_global_sort_order(array('one', 'two'));
121 public function test_auto_sort_order_enabled() {
122 // Setup fixture.
123 // Exercise SUT.
124 filter_set_global_state('one', TEXTFILTER_ON);
125 filter_set_global_state('two', TEXTFILTER_OFF);
126 // Validate.
127 $this->assert_global_sort_order(array('one', 'two'));
130 public function test_update_existing_dont_duplicate() {
131 // Setup fixture.
132 // Exercise SUT.
133 filter_set_global_state('name', TEXTFILTER_ON);
134 filter_set_global_state('name', TEXTFILTER_OFF);
135 // Validate.
136 $this->assert_only_one_filter_globally('name', TEXTFILTER_OFF);
139 public function test_update_reorder_down() {
140 // Setup fixture.
141 filter_set_global_state('one', TEXTFILTER_ON);
142 filter_set_global_state('two', TEXTFILTER_ON);
143 filter_set_global_state('three', TEXTFILTER_ON);
144 // Exercise SUT.
145 filter_set_global_state('two', TEXTFILTER_ON, -1);
146 // Validate.
147 $this->assert_global_sort_order(array('two', 'one', 'three'));
150 public function test_update_reorder_up() {
151 // Setup fixture.
152 filter_set_global_state('one', TEXTFILTER_ON);
153 filter_set_global_state('two', TEXTFILTER_ON);
154 filter_set_global_state('three', TEXTFILTER_ON);
155 filter_set_global_state('four', TEXTFILTER_ON);
156 // Exercise SUT.
157 filter_set_global_state('two', TEXTFILTER_ON, 1);
158 // Validate.
159 $this->assert_global_sort_order(array('one', 'three', 'two', 'four'));
162 public function test_auto_sort_order_change_to_enabled() {
163 // Setup fixture.
164 filter_set_global_state('one', TEXTFILTER_ON);
165 filter_set_global_state('two', TEXTFILTER_DISABLED);
166 filter_set_global_state('three', TEXTFILTER_DISABLED);
167 // Exercise SUT.
168 filter_set_global_state('three', TEXTFILTER_ON);
169 // Validate.
170 $this->assert_global_sort_order(array('one', 'three', 'two'));
173 public function test_auto_sort_order_change_to_disabled() {
174 // Setup fixture.
175 filter_set_global_state('one', TEXTFILTER_ON);
176 filter_set_global_state('two', TEXTFILTER_ON);
177 filter_set_global_state('three', TEXTFILTER_DISABLED);
178 // Exercise SUT.
179 filter_set_global_state('one', TEXTFILTER_DISABLED);
180 // Validate.
181 $this->assert_global_sort_order(array('two', 'one', 'three'));
184 public function test_filter_get_global_states() {
185 // Setup fixture.
186 filter_set_global_state('one', TEXTFILTER_ON);
187 filter_set_global_state('two', TEXTFILTER_OFF);
188 filter_set_global_state('three', TEXTFILTER_DISABLED);
189 // Exercise SUT.
190 $filters = filter_get_global_states();
191 // Validate.
192 $this->assertEquals(array(
193 'one' => (object) array('filter' => 'one', 'active' => TEXTFILTER_ON, 'sortorder' => 1),
194 'two' => (object) array('filter' => 'two', 'active' => TEXTFILTER_OFF, 'sortorder' => 2),
195 'three' => (object) array('filter' => 'three', 'active' => TEXTFILTER_DISABLED, 'sortorder' => 3)
196 ), $filters);
199 private function assert_only_one_local_setting($filter, $contextid, $state) {
200 global $DB;
201 $recs = $DB->get_records('filter_active');
202 $this->assertEquals(1, count($recs), 'More than one record returned %s.');
203 $rec = reset($recs);
204 unset($rec->id);
205 unset($rec->sortorder);
206 $expectedrec = new stdClass();
207 $expectedrec->filter = $filter;
208 $expectedrec->contextid = $contextid;
209 $expectedrec->active = $state;
210 $this->assertEquals($expectedrec, $rec);
213 private function assert_no_local_setting() {
214 global $DB;
215 $this->assertEquals(0, $DB->count_records('filter_active'));
218 public function test_local_on() {
219 // Exercise SUT.
220 filter_set_local_state('name', 123, TEXTFILTER_ON);
221 // Validate.
222 $this->assert_only_one_local_setting('name', 123, TEXTFILTER_ON);
225 public function test_local_off() {
226 // Exercise SUT.
227 filter_set_local_state('name', 123, TEXTFILTER_OFF);
228 // Validate.
229 $this->assert_only_one_local_setting('name', 123, TEXTFILTER_OFF);
232 public function test_local_inherit() {
233 // Exercise SUT.
234 filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
235 // Validate.
236 $this->assert_no_local_setting();
240 * @expectedException coding_exception
242 public function test_local_invalid_state_throws_exception() {
243 // Exercise SUT.
244 filter_set_local_state('name', 123, -9999);
248 * @expectedException coding_exception
250 public function test_throws_exception_when_setting_global() {
251 // Exercise SUT.
252 filter_set_local_state('name', context_system::instance()->id, TEXTFILTER_INHERIT);
255 public function test_local_inherit_deletes_existing() {
256 // Setup fixture.
257 filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
258 // Exercise SUT.
259 filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
260 // Validate.
261 $this->assert_no_local_setting();
264 private function assert_only_one_config($filter, $context, $name, $value) {
265 global $DB;
266 $recs = $DB->get_records('filter_config');
267 $this->assertEquals(1, count($recs), 'More than one record returned %s.');
268 $rec = reset($recs);
269 unset($rec->id);
270 $expectedrec = new stdClass();
271 $expectedrec->filter = $filter;
272 $expectedrec->contextid = $context;
273 $expectedrec->name = $name;
274 $expectedrec->value = $value;
275 $this->assertEquals($expectedrec, $rec);
278 public function test_set_new_config() {
279 // Exercise SUT.
280 filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
281 // Validate.
282 $this->assert_only_one_config('name', 123, 'settingname', 'An arbitrary value');
285 public function test_update_existing_config() {
286 // Setup fixture.
287 filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
288 // Exercise SUT.
289 filter_set_local_config('name', 123, 'settingname', 'A changed value');
290 // Validate.
291 $this->assert_only_one_config('name', 123, 'settingname', 'A changed value');
294 public function test_filter_get_local_config() {
295 // Setup fixture.
296 filter_set_local_config('name', 123, 'setting1', 'An arbitrary value');
297 filter_set_local_config('name', 123, 'setting2', 'Another arbitrary value');
298 filter_set_local_config('name', 122, 'settingname', 'Value from another context');
299 filter_set_local_config('other', 123, 'settingname', 'Someone else\'s value');
300 // Exercise SUT.
301 $config = filter_get_local_config('name', 123);
302 // Validate.
303 $this->assertEquals(array('setting1' => 'An arbitrary value', 'setting2' => 'Another arbitrary value'), $config);
306 protected function setup_available_in_context_tests() {
307 $course = $this->getDataGenerator()->create_course(array('category'=>1));
309 $this->childcontext = context_coursecat::instance(1);
310 $this->childcontext2 = context_course::instance($course->id);
311 $this->syscontext = context_system::instance();
314 private function assert_filter_list($expectedfilters, $filters) {
315 $this->setup_available_in_context_tests();
316 $this->assertEquals($expectedfilters, array_keys($filters), '', 0, 10, true);
319 public function test_globally_on_is_returned() {
320 $this->setup_available_in_context_tests();
321 // Setup fixture.
322 filter_set_global_state('name', TEXTFILTER_ON);
323 // Exercise SUT.
324 $filters = filter_get_active_in_context($this->syscontext);
325 // Validate.
326 $this->assert_filter_list(array('name'), $filters);
327 // Check no config returned correctly.
328 $this->assertEquals(array(), $filters['name']);
331 public function test_globally_off_not_returned() {
332 $this->setup_available_in_context_tests();
333 // Setup fixture.
334 filter_set_global_state('name', TEXTFILTER_OFF);
335 // Exercise SUT.
336 $filters = filter_get_active_in_context($this->childcontext2);
337 // Validate.
338 $this->assert_filter_list(array(), $filters);
341 public function test_globally_off_overridden() {
342 $this->setup_available_in_context_tests();
343 // Setup fixture.
344 filter_set_global_state('name', TEXTFILTER_OFF);
345 filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
346 // Exercise SUT.
347 $filters = filter_get_active_in_context($this->childcontext2);
348 // Validate.
349 $this->assert_filter_list(array('name'), $filters);
352 public function test_globally_on_overridden() {
353 $this->setup_available_in_context_tests();
354 // Setup fixture.
355 filter_set_global_state('name', TEXTFILTER_ON);
356 filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
357 // Exercise SUT.
358 $filters = filter_get_active_in_context($this->childcontext2);
359 // Validate.
360 $this->assert_filter_list(array(), $filters);
363 public function test_globally_disabled_not_overridden() {
364 $this->setup_available_in_context_tests();
365 // Setup fixture.
366 filter_set_global_state('name', TEXTFILTER_DISABLED);
367 filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
368 // Exercise SUT.
369 $filters = filter_get_active_in_context($this->syscontext);
370 // Validate.
371 $this->assert_filter_list(array(), $filters);
374 public function test_single_config_returned() {
375 $this->setup_available_in_context_tests();
376 // Setup fixture.
377 filter_set_global_state('name', TEXTFILTER_ON);
378 filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
379 // Exercise SUT.
380 $filters = filter_get_active_in_context($this->childcontext);
381 // Validate.
382 $this->assertEquals(array('settingname' => 'A value'), $filters['name']);
385 public function test_multi_config_returned() {
386 $this->setup_available_in_context_tests();
387 // Setup fixture.
388 filter_set_global_state('name', TEXTFILTER_ON);
389 filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
390 filter_set_local_config('name', $this->childcontext->id, 'anothersettingname', 'Another value');
391 // Exercise SUT.
392 $filters = filter_get_active_in_context($this->childcontext);
393 // Validate.
394 $this->assertEquals(array('settingname' => 'A value', 'anothersettingname' => 'Another value'), $filters['name']);
397 public function test_config_from_other_context_not_returned() {
398 $this->setup_available_in_context_tests();
399 // Setup fixture.
400 filter_set_global_state('name', TEXTFILTER_ON);
401 filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
402 filter_set_local_config('name', $this->childcontext2->id, 'anothersettingname', 'Another value');
403 // Exercise SUT.
404 $filters = filter_get_active_in_context($this->childcontext2);
405 // Validate.
406 $this->assertEquals(array('anothersettingname' => 'Another value'), $filters['name']);
409 public function test_config_from_other_filter_not_returned() {
410 $this->setup_available_in_context_tests();
411 // Setup fixture.
412 filter_set_global_state('name', TEXTFILTER_ON);
413 filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
414 filter_set_local_config('other', $this->childcontext->id, 'anothersettingname', 'Another value');
415 // Exercise SUT.
416 $filters = filter_get_active_in_context($this->childcontext);
417 // Validate.
418 $this->assertEquals(array('settingname' => 'A value'), $filters['name']);
421 protected function assert_one_available_filter($filter, $localstate, $inheritedstate, $filters) {
422 $this->setup_available_in_context_tests();
423 $this->assertEquals(1, count($filters), 'More than one record returned %s.');
424 $rec = $filters[$filter];
425 unset($rec->id);
426 $expectedrec = new stdClass();
427 $expectedrec->filter = $filter;
428 $expectedrec->localstate = $localstate;
429 $expectedrec->inheritedstate = $inheritedstate;
430 $this->assertEquals($expectedrec, $rec);
433 public function test_available_in_context_localoverride() {
434 $this->setup_available_in_context_tests();
435 // Setup fixture.
436 filter_set_global_state('name', TEXTFILTER_ON);
437 filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
438 // Exercise SUT.
439 $filters = filter_get_available_in_context($this->childcontext);
440 // Validate.
441 $this->assert_one_available_filter('name', TEXTFILTER_OFF, TEXTFILTER_ON, $filters);
444 public function test_available_in_context_nolocaloverride() {
445 $this->setup_available_in_context_tests();
446 // Setup fixture.
447 filter_set_global_state('name', TEXTFILTER_ON);
448 filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
449 // Exercise SUT.
450 $filters = filter_get_available_in_context($this->childcontext2);
451 // Validate.
452 $this->assert_one_available_filter('name', TEXTFILTER_INHERIT, TEXTFILTER_OFF, $filters);
455 public function test_available_in_context_disabled_not_returned() {
456 $this->setup_available_in_context_tests();
457 // Setup fixture.
458 filter_set_global_state('name', TEXTFILTER_DISABLED);
459 filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
460 // Exercise SUT.
461 $filters = filter_get_available_in_context($this->childcontext);
462 // Validate.
463 $this->assertEquals(array(), $filters);
467 * @expectedException coding_exception
469 public function test_available_in_context_exception_with_syscontext() {
470 $this->setup_available_in_context_tests();
471 // Exercise SUT.
472 filter_get_available_in_context($this->syscontext);
475 protected function setup_preload_activities_test() {
476 $this->syscontext = context_system::instance();
477 $this->catcontext = context_coursecat::instance(1);
478 $this->course = $this->getDataGenerator()->create_course(array('category'=>1));
479 $this->coursecontext = context_course::instance($this->course->id);
480 $page1 = $this->getDataGenerator()->create_module('page', array('course'=>$this->course->id));
481 $this->activity1context = context_module::instance($page1->cmid);
482 $page2 = $this->getDataGenerator()->create_module('page', array('course'=>$this->course->id));
483 $this->activity2context = context_module::instance($page2->cmid);
486 private function assert_matches($modinfo) {
487 global $FILTERLIB_PRIVATE, $DB;
489 // Use preload cache...
490 $FILTERLIB_PRIVATE = new stdClass();
491 filter_preload_activities($modinfo);
493 // Get data and check no queries are made.
494 $before = $DB->perf_get_reads();
495 $plfilters1 = filter_get_active_in_context($this->activity1context);
496 $plfilters2 = filter_get_active_in_context($this->activity2context);
497 $after = $DB->perf_get_reads();
498 $this->assertEquals($before, $after);
500 // Repeat without cache and check it makes queries now.
501 $FILTERLIB_PRIVATE = new stdClass;
502 $before = $DB->perf_get_reads();
503 $filters1 = filter_get_active_in_context($this->activity1context);
504 $filters2 = filter_get_active_in_context($this->activity2context);
505 $after = $DB->perf_get_reads();
506 $this->assertTrue($after > $before);
508 // Check they match.
509 $this->assertEquals($plfilters1, $filters1);
510 $this->assertEquals($plfilters2, $filters2);
513 public function test_preload() {
514 $this->setup_preload_activities_test();
515 // Get course and modinfo.
516 $modinfo = new course_modinfo($this->course, 2);
518 // Note: All the tests in this function check that the result from the
519 // preloaded cache is the same as the result from calling the standard
520 // function without preloading.
522 // Initially, check with no filters enabled.
523 $this->assert_matches($modinfo);
525 // Enable filter globally, check.
526 filter_set_global_state('name', TEXTFILTER_ON);
527 $this->assert_matches($modinfo);
529 // Disable for activity 2.
530 filter_set_local_state('name', $this->activity2context->id, TEXTFILTER_OFF);
531 $this->assert_matches($modinfo);
533 // Disable at category.
534 filter_set_local_state('name', $this->catcontext->id, TEXTFILTER_OFF);
535 $this->assert_matches($modinfo);
537 // Enable for activity 1.
538 filter_set_local_state('name', $this->activity1context->id, TEXTFILTER_ON);
539 $this->assert_matches($modinfo);
541 // Disable globally.
542 filter_set_global_state('name', TEXTFILTER_DISABLED);
543 $this->assert_matches($modinfo);
545 // Add another 2 filters.
546 filter_set_global_state('frog', TEXTFILTER_ON);
547 filter_set_global_state('zombie', TEXTFILTER_ON);
548 $this->assert_matches($modinfo);
550 // Disable random one of these in each context.
551 filter_set_local_state('zombie', $this->activity1context->id, TEXTFILTER_OFF);
552 filter_set_local_state('frog', $this->activity2context->id, TEXTFILTER_OFF);
553 $this->assert_matches($modinfo);
555 // Now do some filter options.
556 filter_set_local_config('name', $this->activity1context->id, 'a', 'x');
557 filter_set_local_config('zombie', $this->activity1context->id, 'a', 'y');
558 filter_set_local_config('frog', $this->activity1context->id, 'a', 'z');
559 // These last two don't do anything as they are not at final level but I
560 // thought it would be good to have that verified in test.
561 filter_set_local_config('frog', $this->coursecontext->id, 'q', 'x');
562 filter_set_local_config('frog', $this->catcontext->id, 'q', 'z');
563 $this->assert_matches($modinfo);
566 public function test_filter_delete_all_for_filter() {
567 global $DB;
569 // Setup fixture.
570 filter_set_global_state('name', TEXTFILTER_ON);
571 filter_set_global_state('other', TEXTFILTER_ON);
572 filter_set_local_config('name', context_system::instance()->id, 'settingname', 'A value');
573 filter_set_local_config('other', context_system::instance()->id, 'settingname', 'Other value');
574 set_config('configname', 'A config value', 'filter_name');
575 set_config('configname', 'Other config value', 'filter_other');
576 // Exercise SUT.
577 filter_delete_all_for_filter('name');
578 // Validate.
579 $this->assertEquals(1, $DB->count_records('filter_active'));
580 $this->assertTrue($DB->record_exists('filter_active', array('filter' => 'other')));
581 $this->assertEquals(1, $DB->count_records('filter_config'));
582 $this->assertTrue($DB->record_exists('filter_config', array('filter' => 'other')));
583 $expectedconfig = new stdClass;
584 $expectedconfig->configname = 'Other config value';
585 $this->assertEquals($expectedconfig, get_config('filter_other'));
586 $this->assertEquals(get_config('filter_name'), new stdClass());
589 public function test_filter_delete_all_for_context() {
590 global $DB;
592 // Setup fixture.
593 filter_set_global_state('name', TEXTFILTER_ON);
594 filter_set_local_state('name', 123, TEXTFILTER_OFF);
595 filter_set_local_config('name', 123, 'settingname', 'A value');
596 filter_set_local_config('other', 123, 'settingname', 'Other value');
597 filter_set_local_config('other', 122, 'settingname', 'Other value');
598 // Exercise SUT.
599 filter_delete_all_for_context(123);
600 // Validate.
601 $this->assertEquals(1, $DB->count_records('filter_active'));
602 $this->assertTrue($DB->record_exists('filter_active', array('contextid' => context_system::instance()->id)));
603 $this->assertEquals(1, $DB->count_records('filter_config'));
604 $this->assertTrue($DB->record_exists('filter_config', array('filter' => 'other')));
607 public function test_set() {
608 global $CFG;
610 $this->assertFileExists("$CFG->dirroot/filter/emailprotect"); // Any standard filter.
611 $this->assertFileExists("$CFG->dirroot/filter/tidy"); // Any standard filter.
612 $this->assertFileNotExists("$CFG->dirroot/filter/grgrggr"); // Any non-existent filter
614 // Setup fixture.
615 set_config('filterall', 0);
616 set_config('stringfilters', '');
617 // Exercise SUT.
618 filter_set_applies_to_strings('tidy', true);
619 // Validate.
620 $this->assertEquals('tidy', $CFG->stringfilters);
621 $this->assertEquals(1, $CFG->filterall);
623 filter_set_applies_to_strings('grgrggr', true);
624 $this->assertEquals('tidy', $CFG->stringfilters);
625 $this->assertEquals(1, $CFG->filterall);
627 filter_set_applies_to_strings('emailprotect', true);
628 $this->assertEquals('tidy,emailprotect', $CFG->stringfilters);
629 $this->assertEquals(1, $CFG->filterall);
632 public function test_unset_to_empty() {
633 global $CFG;
635 $this->assertFileExists("$CFG->dirroot/filter/tidy"); // Any standard filter.
637 // Setup fixture.
638 set_config('filterall', 1);
639 set_config('stringfilters', 'tidy');
640 // Exercise SUT.
641 filter_set_applies_to_strings('tidy', false);
642 // Validate.
643 $this->assertEquals('', $CFG->stringfilters);
644 $this->assertEquals('', $CFG->filterall);
647 public function test_unset_multi() {
648 global $CFG;
650 $this->assertFileExists("$CFG->dirroot/filter/emailprotect"); // Any standard filter.
651 $this->assertFileExists("$CFG->dirroot/filter/tidy"); // Any standard filter.
652 $this->assertFileExists("$CFG->dirroot/filter/multilang"); // Any standard filter.
654 // Setup fixture.
655 set_config('filterall', 1);
656 set_config('stringfilters', 'emailprotect,tidy,multilang');
657 // Exercise SUT.
658 filter_set_applies_to_strings('tidy', false);
659 // Validate.
660 $this->assertEquals('emailprotect,multilang', $CFG->stringfilters);
661 $this->assertEquals(1, $CFG->filterall);
664 public function test_filter_manager_instance() {
666 set_config('perfdebug', 7);
667 filter_manager::reset_caches();
668 $filterman = filter_manager::instance();
669 $this->assertInstanceOf('filter_manager', $filterman);
670 $this->assertNotInstanceOf('performance_measuring_filter_manager', $filterman);
672 set_config('perfdebug', 15);
673 filter_manager::reset_caches();
674 $filterman = filter_manager::instance();
675 $this->assertInstanceOf('filter_manager', $filterman);
676 $this->assertInstanceOf('performance_measuring_filter_manager', $filterman);
679 public function test_filter_get_globally_enabled_default() {
680 $enabledfilters = filter_get_globally_enabled();
681 $this->assertArrayNotHasKey('glossary', $enabledfilters);
684 public function test_filter_get_globally_enabled_after_change() {
685 filter_set_global_state('glossary', TEXTFILTER_ON);
686 $enabledfilters = filter_get_globally_enabled();
687 $this->assertArrayHasKey('glossary', $enabledfilters);
690 public function test_filter_get_globally_enabled_filters_with_config() {
691 $this->setup_available_in_context_tests();
693 // Set few filters.
694 filter_set_global_state('one', TEXTFILTER_ON);
695 filter_set_global_state('three', TEXTFILTER_OFF, -1);
696 filter_set_global_state('two', TEXTFILTER_DISABLED);
698 // Set global config.
699 filter_set_local_config('one', $this->syscontext->id, 'test1a', 'In root');
700 filter_set_local_config('one', $this->syscontext->id, 'test1b', 'In root');
701 filter_set_local_config('two', $this->syscontext->id, 'test2a', 'In root');
702 filter_set_local_config('two', $this->syscontext->id, 'test2b', 'In root');
704 // Set child config.
705 filter_set_local_config('one', $this->childcontext->id, 'test1a', 'In child');
706 filter_set_local_config('one', $this->childcontext->id, 'test1b', 'In child');
707 filter_set_local_config('two', $this->childcontext->id, 'test2a', 'In child');
708 filter_set_local_config('two', $this->childcontext->id, 'test2b', 'In child');
709 filter_set_local_config('three', $this->childcontext->id, 'test3a', 'In child');
710 filter_set_local_config('three', $this->childcontext->id, 'test3b', 'In child');
712 // Check.
713 $actual = filter_get_globally_enabled_filters_with_config();
714 $this->assertCount(2, $actual);
715 $this->assertEquals(['three', 'one'], array_keys($actual)); // Checks sortorder.
716 $this->assertArrayHasKey('one', $actual);
717 $this->assertArrayNotHasKey('two', $actual);
718 $this->assertArrayHasKey('three', $actual);
719 $this->assertEquals(['test1a' => 'In root', 'test1b' => 'In root'], $actual['one']);
720 $this->assertEquals([], $actual['three']);