MDL-41623 ensure all rss links are valid urls.
[moodle.git] / lib / tests / filter_test.php
blob2d35b9dea1bb05e339b1c8bb692e9fd73e8289ae
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 functions that affect filter_active table with contextid = $syscontextid.
35 class filter_active_global_testcase extends advanced_testcase {
37 protected function setUp() {
38 global $DB;
39 parent::setUp();
40 $DB->delete_records('filter_active', array());
41 $this->resetAfterTest(false);
44 private function assert_only_one_filter_globally($filter, $state) {
45 global $DB;
46 $recs = $DB->get_records('filter_active');
47 $this->assertCount(1, $recs);
48 $rec = reset($recs);
49 unset($rec->id);
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) {
59 global $DB;
61 $sortedfilters = $DB->get_records_menu('filter_active',
62 array('contextid' => context_system::instance()->id), 'sortorder', 'sortorder,filter');
63 $testarray = array();
64 $index = 1;
65 foreach($filters as $filter) {
66 $testarray[$index++] = $filter;
68 $this->assertEquals($testarray, $sortedfilters);
71 public function test_set_filter_globally_on() {
72 // Setup fixture.
73 // Exercise SUT.
74 filter_set_global_state('name', TEXTFILTER_ON);
75 // Validate.
76 $this->assert_only_one_filter_globally('name', TEXTFILTER_ON);
79 public function test_set_filter_globally_off() {
80 // Setup fixture.
81 // Exercise SUT.
82 filter_set_global_state('name', TEXTFILTER_OFF);
83 // Validate.
84 $this->assert_only_one_filter_globally('name', TEXTFILTER_OFF);
87 public function test_set_filter_globally_disabled() {
88 // Setup fixture.
89 // Exercise SUT.
90 filter_set_global_state('name', TEXTFILTER_DISABLED);
91 // Validate.
92 $this->assert_only_one_filter_globally('name', TEXTFILTER_DISABLED);
95 /**
96 * @expectedException coding_exception
97 * @return void
99 public function test_global_config_exception_on_invalid_state() {
100 filter_set_global_state('name', 0);
103 public function test_auto_sort_order() {
104 // Setup fixture.
105 // Exercise SUT.
106 filter_set_global_state('one', TEXTFILTER_DISABLED);
107 filter_set_global_state('two', TEXTFILTER_DISABLED);
108 // Validate.
109 $this->assert_global_sort_order(array('one', 'two'));
112 public function test_auto_sort_order_enabled() {
113 // Setup fixture.
114 // Exercise SUT.
115 filter_set_global_state('one', TEXTFILTER_ON);
116 filter_set_global_state('two', TEXTFILTER_OFF);
117 // Validate.
118 $this->assert_global_sort_order(array('one', 'two'));
121 public function test_update_existing_dont_duplicate() {
122 // Setup fixture.
123 // Exercise SUT.
124 filter_set_global_state('name', TEXTFILTER_ON);
125 filter_set_global_state('name', TEXTFILTER_OFF);
126 // Validate.
127 $this->assert_only_one_filter_globally('name', TEXTFILTER_OFF);
130 public function test_update_reorder_down() {
131 // Setup fixture.
132 filter_set_global_state('one', TEXTFILTER_ON);
133 filter_set_global_state('two', TEXTFILTER_ON);
134 filter_set_global_state('three', TEXTFILTER_ON);
135 // Exercise SUT.
136 filter_set_global_state('two', TEXTFILTER_ON, -1);
137 // Validate.
138 $this->assert_global_sort_order(array('two', 'one', 'three'));
141 public function test_update_reorder_up() {
142 // Setup fixture.
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);
147 // Exercise SUT.
148 filter_set_global_state('two', TEXTFILTER_ON, 1);
149 // Validate.
150 $this->assert_global_sort_order(array('one', 'three', 'two', 'four'));
153 public function test_auto_sort_order_change_to_enabled() {
154 // Setup fixture.
155 filter_set_global_state('one', TEXTFILTER_ON);
156 filter_set_global_state('two', TEXTFILTER_DISABLED);
157 filter_set_global_state('three', TEXTFILTER_DISABLED);
158 // Exercise SUT.
159 filter_set_global_state('three', TEXTFILTER_ON);
160 // Validate.
161 $this->assert_global_sort_order(array('one', 'three', 'two'));
164 public function test_auto_sort_order_change_to_disabled() {
165 // Setup fixture.
166 filter_set_global_state('one', TEXTFILTER_ON);
167 filter_set_global_state('two', TEXTFILTER_ON);
168 filter_set_global_state('three', TEXTFILTER_DISABLED);
169 // Exercise SUT.
170 filter_set_global_state('one', TEXTFILTER_DISABLED);
171 // Validate.
172 $this->assert_global_sort_order(array('two', 'one', 'three'));
175 public function test_filter_get_global_states() {
176 // Setup fixture.
177 filter_set_global_state('one', TEXTFILTER_ON);
178 filter_set_global_state('two', TEXTFILTER_OFF);
179 filter_set_global_state('three', TEXTFILTER_DISABLED);
180 // Exercise SUT.
181 $filters = filter_get_global_states();
182 // Validate.
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)
187 ), $filters);
193 * Test functions that affect filter_active table with contextid = $syscontextid.
195 class filter_active_local_testcase extends advanced_testcase {
197 protected function setUp() {
198 global $DB;
199 parent::setUp();
200 $DB->delete_records('filter_active', array());
201 $this->resetAfterTest(false);
204 private function assert_only_one_local_setting($filter, $contextid, $state) {
205 global $DB;
206 $recs = $DB->get_records('filter_active');
207 $this->assertEquals(1, count($recs), 'More than one record returned %s.');
208 $rec = reset($recs);
209 unset($rec->id);
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() {
219 global $DB;
220 $this->assertEquals(0, $DB->count_records('filter_active'));
223 public function test_local_on() {
224 // Exercise SUT.
225 filter_set_local_state('name', 123, TEXTFILTER_ON);
226 // Validate.
227 $this->assert_only_one_local_setting('name', 123, TEXTFILTER_ON);
230 public function test_local_off() {
231 // Exercise SUT.
232 filter_set_local_state('name', 123, TEXTFILTER_OFF);
233 // Validate.
234 $this->assert_only_one_local_setting('name', 123, TEXTFILTER_OFF);
237 public function test_local_inherit() {
238 // Exercise SUT.
239 filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
240 // Validate.
241 $this->assert_no_local_setting();
245 * @expectedException coding_exception
246 * @return void
248 public function test_local_invalid_state_throws_exception() {
249 // Exercise SUT.
250 filter_set_local_state('name', 123, -9999);
254 * @expectedException coding_exception
255 * @return void
257 public function test_throws_exception_when_setting_global() {
258 // Exercise SUT.
259 filter_set_local_state('name', context_system::instance()->id, TEXTFILTER_INHERIT);
262 public function test_local_inherit_deletes_existing() {
263 // Setup fixture.
264 filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
265 // Exercise SUT.
266 filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
267 // Validate.
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() {
279 global $DB;
280 parent::setUp();
281 $DB->delete_records('filter_config', array());
282 $this->resetAfterTest(false);
285 private function assert_only_one_config($filter, $context, $name, $value) {
286 global $DB;
287 $recs = $DB->get_records('filter_config');
288 $this->assertEquals(1, count($recs), 'More than one record returned %s.');
289 $rec = reset($recs);
290 unset($rec->id);
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() {
300 // Exercise SUT.
301 filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
302 // Validate.
303 $this->assert_only_one_config('name', 123, 'settingname', 'An arbitrary value');
306 public function test_update_existing_config() {
307 // Setup fixture.
308 filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
309 // Exercise SUT.
310 filter_set_local_config('name', 123, 'settingname', 'A changed value');
311 // Validate.
312 $this->assert_only_one_config('name', 123, 'settingname', 'A changed value');
315 public function test_filter_get_local_config() {
316 // Setup fixture.
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');
321 // Exercise SUT.
322 $config = filter_get_local_config('name', 123);
323 // Validate.
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() {
345 global $DB;
346 parent::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() {
358 // Setup fixture.
359 filter_set_global_state('name', TEXTFILTER_ON);
360 // Exercise SUT.
361 $filters = filter_get_active_in_context(self::$syscontext);
362 // Validate.
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() {
369 // Setup fixture.
370 filter_set_global_state('name', TEXTFILTER_OFF);
371 // Exercise SUT.
372 $filters = filter_get_active_in_context(self::$childcontext2);
373 // Validate.
374 $this->assert_filter_list(array(), $filters);
377 public function test_globally_off_overridden() {
378 // Setup fixture.
379 filter_set_global_state('name', TEXTFILTER_OFF);
380 filter_set_local_state('name', self::$childcontext->id, TEXTFILTER_ON);
381 // Exercise SUT.
382 $filters = filter_get_active_in_context(self::$childcontext2);
383 // Validate.
384 $this->assert_filter_list(array('name'), $filters);
387 public function test_globally_on_overridden() {
388 // Setup fixture.
389 filter_set_global_state('name', TEXTFILTER_ON);
390 filter_set_local_state('name', self::$childcontext->id, TEXTFILTER_OFF);
391 // Exercise SUT.
392 $filters = filter_get_active_in_context(self::$childcontext2);
393 // Validate.
394 $this->assert_filter_list(array(), $filters);
397 public function test_globally_disabled_not_overridden() {
398 // Setup fixture.
399 filter_set_global_state('name', TEXTFILTER_DISABLED);
400 filter_set_local_state('name', self::$childcontext->id, TEXTFILTER_ON);
401 // Exercise SUT.
402 $filters = filter_get_active_in_context(self::$syscontext);
403 // Validate.
404 $this->assert_filter_list(array(), $filters);
407 public function test_single_config_returned() {
408 // Setup fixture.
409 filter_set_global_state('name', TEXTFILTER_ON);
410 filter_set_local_config('name', self::$childcontext->id, 'settingname', 'A value');
411 // Exercise SUT.
412 $filters = filter_get_active_in_context(self::$childcontext);
413 // Validate.
414 $this->assertEquals(array('settingname' => 'A value'), $filters['name']);
417 public function test_multi_config_returned() {
418 // Setup fixture.
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');
422 // Exercise SUT.
423 $filters = filter_get_active_in_context(self::$childcontext);
424 // Validate.
425 $this->assertEquals(array('settingname' => 'A value', 'anothersettingname' => 'Another value'), $filters['name']);
428 public function test_config_from_other_context_not_returned() {
429 // Setup fixture.
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');
433 // Exercise SUT.
434 $filters = filter_get_active_in_context(self::$childcontext2);
435 // Validate.
436 $this->assertEquals(array('anothersettingname' => 'Another value'), $filters['name']);
439 public function test_config_from_other_filter_not_returned() {
440 // Setup fixture.
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');
444 // Exercise SUT.
445 $filters = filter_get_active_in_context(self::$childcontext);
446 // Validate.
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];
453 unset($rec->id);
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() {
462 // Setup fixture.
463 filter_set_global_state('name', TEXTFILTER_ON);
464 filter_set_local_state('name', self::$childcontext->id, TEXTFILTER_OFF);
465 // Exercise SUT.
466 $filters = filter_get_available_in_context(self::$childcontext);
467 // Validate.
468 $this->assert_one_available_filter('name', TEXTFILTER_OFF, TEXTFILTER_ON, $filters);
471 public function test_available_in_context_nolocaloverride() {
472 // Setup fixture.
473 filter_set_global_state('name', TEXTFILTER_ON);
474 filter_set_local_state('name', self::$childcontext->id, TEXTFILTER_OFF);
475 // Exercise SUT.
476 $filters = filter_get_available_in_context(self::$childcontext2);
477 // Validate.
478 $this->assert_one_available_filter('name', TEXTFILTER_INHERIT, TEXTFILTER_OFF, $filters);
481 public function test_available_in_context_disabled_not_returned() {
482 // Setup fixture.
483 filter_set_global_state('name', TEXTFILTER_DISABLED);
484 filter_set_local_state('name', self::$childcontext->id, TEXTFILTER_ON);
485 // Exercise SUT.
486 $filters = filter_get_available_in_context(self::$childcontext);
487 // Validate.
488 $this->assertEquals(array(), $filters);
492 * @expectedException coding_exception
493 * @return void
495 public function test_available_in_context_exception_with_syscontext() {
496 // Exercise SUT.
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() {
524 global $DB;
525 parent::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);
554 // Check they match
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);
586 // Disable globally
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() {
615 global $DB;
616 parent::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() {
624 global $DB;
626 // Setup fixture.
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');
633 // Exercise SUT.
634 filter_delete_all_for_filter('name');
635 // Validate.
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() {
647 global $DB;
649 // Setup fixture.
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');
655 // Exercise SUT.
656 filter_delete_all_for_context(123);
657 // Validate.
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() {
670 global $DB, $CFG;
671 parent::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() {
683 global $CFG;
684 $CFG->stringfilters = $this->origcfgstringfilters;
685 $CFG->filterall = $this->origcfgfilterall;
687 parent::tearDown();
690 public function test_set() {
691 global $CFG;
692 // Setup fixture.
693 $CFG->filterall = 0;
694 $CFG->stringfilters = '';
695 // Exercise SUT.
696 filter_set_applies_to_strings('name', true);
697 // Validate.
698 $this->assertEquals('name', $CFG->stringfilters);
699 $this->assertEquals(1, $CFG->filterall);
702 public function test_unset_to_empty() {
703 global $CFG;
704 // Setup fixture.
705 $CFG->filterall = 1;
706 $CFG->stringfilters = 'name';
707 // Exercise SUT.
708 filter_set_applies_to_strings('name', false);
709 // Validate.
710 $this->assertEquals('', $CFG->stringfilters);
711 $this->assertEquals('', $CFG->filterall);
714 public function test_unset_multi() {
715 global $CFG;
716 // Setup fixture.
717 $CFG->filterall = 1;
718 $CFG->stringfilters = 'name,other';
719 // Exercise SUT.
720 filter_set_applies_to_strings('name', false);
721 // Validate.
722 $this->assertEquals('other', $CFG->stringfilters);
723 $this->assertEquals(1, $CFG->filterall);