MDL-25826 add more HTMLPurifier integration tests
[moodle.git] / lib / simpletest / testpurifier.php
CommitLineData
a32b6847
PS
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/**
18 * Unit tests for the HTMLPurifier integration
19 *
20 * @package core
21 * @subpackage simpletest
22 * @copyright 2011 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29class purifier_test extends UnitTestCase {
30
31 public static $includecoverage = array('lib/htmlpurifier/HTMLPurifier.php');
32
33 private $enablehtmlpurifier = null;
34 private $cachetext = null;
35
36 function setUp() {
37 global $CFG;
38 $this->enablehtmlpurifier = $CFG->enablehtmlpurifier;
39 $CFG->enablehtmlpurifier = 1;
40
41 $this->cachetext = $CFG->cachetext;
42 $CFG->cachetext = 0;
43 }
44
45 function tearDown() {
46 global $CFG;
47 $CFG->enablehtmlpurifier = $this->enablehtmlpurifier;
48 $CFG->cachetext = $this->cachetext;
49 }
50
51 /**
52 * Verify _blank target is allowed
53 * @return void
54 */
55 public function test_allow_blank_target() {
56 $text = '<a href="http://moodle.org" target="_blank">Some link</a>';
57 $result = format_text($text, FORMAT_HTML);
58 $this->assertIdentical($text, $result);
59
60 $result = format_text('<a href="http://moodle.org" target="some">Some link</a>', FORMAT_HTML);
61 $this->assertIdentical('<a href="http://moodle.org">Some link</a>', $result);
62 }
63
64 /**
65 * Verify our nolink tag accepted
66 * @return void
67 */
68 public function test_nolink() {
69 // we can not use format text because nolink changes result
70 $text = '<nolink><div>no filters</div></nolink>';
71 $result = purify_html($text, array());
72 $this->assertIdentical($text, $result);
73 }
74
75 /**
76 * Verify our tex tag accepted
77 * @return void
78 */
79 public function test_tex() {
80 $text = '<tex>a+b=c</tex>';
81 $result = purify_html($text, array());
82 $this->assertIdentical($text, $result);
83 }
84
85 /**
86 * Verify our algebra tag accepted
87 * @return void
88 */
89 public function test_algebra() {
90 $text = '<algebra>a+b=c</algebra>';
91 $result = purify_html($text, array());
92 $this->assertIdentical($text, $result);
93 }
94
95 /**
96 * Verify our hacky multilang works
97 * @return void
98 */
99 public function test_multilang() {
100 $text = '<lang lang="en">hmmm</lang><lang lang="anything">hm</lang>';
101 $result = purify_html($text, array());
102 $this->assertIdentical($text, $result);
103
104 $text = '<span lang="en" class="multilang">hmmm</span><span lang="anything" class="multilang">hm</span>';
105 $result = purify_html($text, array());
106 $this->assertIdentical($text, $result);
107
108 $text = '<span lang="en">hmmm</span>';
109 $result = purify_html($text, array());
110 $this->assertNotIdentical($text, $result);
111 }
112
113 /**
114 * Tests the 'allowid' option for format_text.
115 */
116 public function test_format_text_allowid() {
117 // Start off by not allowing ids (default)
118 $options = array(
119 'nocache' => true
120 );
121 $result = format_text('<div id="example">Frog</div>', FORMAT_HTML, $options);
122 $this->assertIdentical('<div>Frog</div>', $result);
123
124 // Now allow ids
125 $options['allowid'] = true;
126 $result = format_text('<div id="example">Frog</div>', FORMAT_HTML, $options);
127 $this->assertIdentical('<div id="example">Frog</div>', $result);
128 }
129
130
131 //TODO: add XSS smoke tests here
132}
133