MDL-80203 courseformat: Fix some typos and PHPDoc
[moodle.git] / lib / tests / filter_manager_test.php
blob061f23280271b862ab221cc595e1b67cb02aa09f
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 namespace core;
19 use filter_manager;
21 /**
22 * Unit tests for the {@link filter_manager} class.
24 * @package core
25 * @category test
26 * @copyright 2015 The Open University
27 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
29 class filter_manager_test extends \advanced_testcase {
31 /**
32 * Helper method to apply filters to some text and return the result.
33 * @param string $text the text to filter.
34 * @param array $skipfilters any filters not to apply, even if they are configured.
35 * @return string the filtered text.
37 protected function filter_text($text, $skipfilters) {
38 global $PAGE;
39 $filtermanager = filter_manager::instance();
40 $filtermanager->setup_page_for_filters($PAGE, $PAGE->context);
41 $filteroptions = array(
42 'originalformat' => FORMAT_HTML,
43 'noclean' => false,
45 return $filtermanager->filter_text($text, $PAGE->context, $filteroptions, $skipfilters);
48 public function test_filter_normal() {
49 $this->resetAfterTest();
50 filter_set_global_state('emoticon', TEXTFILTER_ON);
51 $this->assertMatchesRegularExpression(
52 '~^<p><img class="icon emoticon" alt="smile" title="smile" ' .
53 'src="https://www.example.com/moodle/theme/image.php/boost/core/1/s/smiley" /></p>$~',
54 $this->filter_text('<p>:-)</p>', array()));
57 public function test_one_filter_disabled() {
58 $this->resetAfterTest();
59 filter_set_global_state('emoticon', TEXTFILTER_ON);
60 $this->assertEquals('<p>:-)</p>',
61 $this->filter_text('<p>:-)</p>', array('emoticon')));
64 public function test_disabling_other_filter_does_not_break_it() {
65 $this->resetAfterTest();
66 filter_set_global_state('emoticon', TEXTFILTER_ON);
67 $this->assertMatchesRegularExpression('~^<p><img class="icon emoticon" alt="smile" ' .
68 'title="smile" src="https://www.example.com/moodle/theme/image.php/boost/core/1/s/smiley" /></p>$~',
69 $this->filter_text('<p>:-)</p>', array('urltolink')));
72 public function test_one_filter_of_two_disabled() {
73 $this->resetAfterTest();
74 filter_set_global_state('emoticon', TEXTFILTER_ON);
75 filter_set_global_state('urltolink', TEXTFILTER_ON);
76 $this->assertMatchesRegularExpression('~^<p><img class="icon emoticon" alt="smile" title="smile" ' .
77 'src="https://www.example.com/moodle/theme/image.php/boost/core/1/s/smiley" /> http://google.com/</p>$~',
78 $this->filter_text('<p>:-) http://google.com/</p>', array('glossary', 'urltolink')));