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 * Unit tests for the {@link filter_manager} class.
22 * @copyright 2015 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') ||
die();
30 * Unit tests for the {@link filter_manager} class.
32 * @copyright 2015 The Open University
33 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
35 class core_filter_manager_testcase
extends advanced_testcase
{
38 * Helper method to apply filters to some text and return the result.
39 * @param string $text the text to filter.
40 * @param array $skipfilters any filters not to apply, even if they are configured.
41 * @return string the filtered text.
43 protected function filter_text($text, $skipfilters) {
45 $filtermanager = filter_manager
::instance();
46 $filtermanager->setup_page_for_filters($PAGE, $PAGE->context
);
47 $filteroptions = array(
48 'originalformat' => FORMAT_HTML
,
51 return $filtermanager->filter_text($text, $PAGE->context
, $filteroptions, $skipfilters);
54 public function test_filter_normal() {
55 $this->resetAfterTest();
56 filter_set_global_state('emoticon', TEXTFILTER_ON
);
57 $this->assertRegExp('~^<p><img class="icon emoticon" alt="smile" title="smile" src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /></p>$~',
58 $this->filter_text('<p>:-)</p>', array()));
61 public function test_one_filter_disabled() {
62 $this->resetAfterTest();
63 filter_set_global_state('emoticon', TEXTFILTER_ON
);
64 $this->assertEquals('<p>:-)</p>',
65 $this->filter_text('<p>:-)</p>', array('emoticon')));
68 public function test_disabling_other_filter_does_not_break_it() {
69 $this->resetAfterTest();
70 filter_set_global_state('emoticon', TEXTFILTER_ON
);
71 $this->assertRegExp('~^<p><img class="icon emoticon" alt="smile" title="smile" src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /></p>$~',
72 $this->filter_text('<p>:-)</p>', array('urltolink')));
75 public function test_one_filter_of_two_disabled() {
76 $this->resetAfterTest();
77 filter_set_global_state('emoticon', TEXTFILTER_ON
);
78 filter_set_global_state('urltolink', TEXTFILTER_ON
);
79 $this->assertRegExp('~^<p><img class="icon emoticon" alt="smile" title="smile" src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /> http://google.com/</p>$~',
80 $this->filter_text('<p>:-) http://google.com/</p>', array('glossary', 'urltolink')));