Merge branch 'MDL-72965-master' of https://github.com/peterRd/moodle
[moodle.git] / lib / tests / filter_manager_test.php
blob200fe139f133023616478d92bfb102ae3f0cf078
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 * Unit tests for the {@link filter_manager} class.
20 * @package core
21 * @category test
22 * @copyright 2015 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') || die();
29 /**
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 {
37 /**
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) {
44 global $PAGE;
45 $filtermanager = filter_manager::instance();
46 $filtermanager->setup_page_for_filters($PAGE, $PAGE->context);
47 $filteroptions = array(
48 'originalformat' => FORMAT_HTML,
49 'noclean' => false,
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->assertMatchesRegularExpression(
58 '~^<p><img class="icon emoticon" alt="smile" title="smile" ' .
59 'src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /></p>$~',
60 $this->filter_text('<p>:-)</p>', array()));
63 public function test_one_filter_disabled() {
64 $this->resetAfterTest();
65 filter_set_global_state('emoticon', TEXTFILTER_ON);
66 $this->assertEquals('<p>:-)</p>',
67 $this->filter_text('<p>:-)</p>', array('emoticon')));
70 public function test_disabling_other_filter_does_not_break_it() {
71 $this->resetAfterTest();
72 filter_set_global_state('emoticon', TEXTFILTER_ON);
73 $this->assertMatchesRegularExpression('~^<p><img class="icon emoticon" alt="smile" ' .
74 'title="smile" src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /></p>$~',
75 $this->filter_text('<p>:-)</p>', array('urltolink')));
78 public function test_one_filter_of_two_disabled() {
79 $this->resetAfterTest();
80 filter_set_global_state('emoticon', TEXTFILTER_ON);
81 filter_set_global_state('urltolink', TEXTFILTER_ON);
82 $this->assertMatchesRegularExpression('~^<p><img class="icon emoticon" alt="smile" title="smile" ' .
83 'src="https://www.example.com/moodle/theme/image.php/_s/boost/core/1/s/smiley" /> http://google.com/</p>$~',
84 $this->filter_text('<p>:-) http://google.com/</p>', array('glossary', 'urltolink')));