Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
namespace core;
18
 
19
use filter_manager;
20
 
21
/**
22
 * Unit tests for the {@link filter_manager} class.
23
 *
24
 * @package   core
25
 * @category  test
26
 * @copyright 2015 The Open University
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
28
 */
1441 ariadna 29
final class filter_manager_test extends \advanced_testcase {
1 efrain 30
 
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.
36
     */
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,
44
        );
45
        return $filtermanager->filter_text($text, $PAGE->context, $filteroptions, $skipfilters);
46
    }
47
 
11 efrain 48
    public function test_filter_normal(): void {
1 efrain 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()));
55
    }
56
 
11 efrain 57
    public function test_one_filter_disabled(): void {
1 efrain 58
        $this->resetAfterTest();
59
        filter_set_global_state('emoticon', TEXTFILTER_ON);
60
        $this->assertEquals('<p>:-)</p>',
61
                $this->filter_text('<p>:-)</p>', array('emoticon')));
62
    }
63
 
11 efrain 64
    public function test_disabling_other_filter_does_not_break_it(): void {
1 efrain 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')));
70
    }
71
 
11 efrain 72
    public function test_one_filter_of_two_disabled(): void {
1 efrain 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')));
79
    }
80
}