~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/tests/htmlpurifier_test.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
class core_htmlpurifier_testcase extends basic_testcase {
38
38
 
39
39
    /**
40
 
     * Verify _blank target is allowed
41
 
     * @return void
 
40
     * Verify _blank target is allowed.
42
41
     */
43
42
    public function test_allow_blank_target() {
44
43
        $text = '<a href="http://moodle.org" target="_blank">Some link</a>';
50
49
    }
51
50
 
52
51
    /**
53
 
     * Verify our nolink tag accepted
54
 
     * @return void
 
52
     * Verify our nolink tag accepted.
55
53
     */
56
54
    public function test_nolink() {
57
 
        // we can not use format text because nolink changes result
 
55
        // We can not use format text because nolink changes result.
58
56
        $text = '<nolink><div>no filters</div></nolink>';
59
57
        $result = purify_html($text, array());
60
58
        $this->assertSame($text, $result);
65
63
    }
66
64
 
67
65
    /**
68
 
     * Verify our tex tag accepted
69
 
     * @return void
 
66
     * Verify our tex tag accepted.
70
67
     */
71
68
    public function test_tex() {
72
69
        $text = '<tex>a+b=c</tex>';
75
72
    }
76
73
 
77
74
    /**
78
 
     * Verify our algebra tag accepted
79
 
     * @return void
 
75
     * Verify our algebra tag accepted.
80
76
     */
81
77
    public function test_algebra() {
82
78
        $text = '<algebra>a+b=c</algebra>';
85
81
    }
86
82
 
87
83
    /**
88
 
     * Verify our hacky multilang works
89
 
     * @return void
 
84
     * Verify our hacky multilang works.
90
85
     */
91
86
    public function test_multilang() {
92
87
        $text = '<lang lang="en">hmmm</lang><lang lang="anything">hm</lang>';
101
96
        $result = purify_html($text, array());
102
97
        $this->assertNotSame($text, $result);
103
98
 
104
 
        // keep standard lang tags
 
99
        // Keep standard lang tags.
105
100
 
106
101
        $text = '<span lang="de_DU" class="multilang">asas</span>';
107
102
        $result = purify_html($text, array());
114
109
 
115
110
    /**
116
111
     * Tests the 'allowid' option for format_text.
117
 
     * @return void
118
112
     */
119
113
    public function test_format_text_allowid() {
120
 
        // Start off by not allowing ids (default)
 
114
        // Start off by not allowing ids (default).
121
115
        $options = array(
122
116
            'nocache' => true
123
117
        );
124
118
        $result = format_text('<div id="example">Frog</div>', FORMAT_HTML, $options);
125
119
        $this->assertSame('<div>Frog</div>', $result);
126
120
 
127
 
        // Now allow ids
 
121
        // Now allow ids.
128
122
        $options['allowid'] = true;
129
123
        $result = format_text('<div id="example">Frog</div>', FORMAT_HTML, $options);
130
124
        $this->assertSame('<div id="example">Frog</div>', $result);
131
125
    }
132
126
 
 
127
    public function test_allowobjectembed() {
 
128
        global $CFG;
 
129
 
 
130
        $this->assertSame('0', $CFG->allowobjectembed);
 
131
 
 
132
        $text = '<object width="425" height="350">
 
133
<param name="movie" value="http://www.youtube.com/v/AyPzM5WK8ys" />
 
134
<param name="wmode" value="transparent" />
 
135
<embed src="http://www.youtube.com/v/AyPzM5WK8ys" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350" />
 
136
</object>hmmm';
 
137
        $result = purify_html($text, array());
 
138
        $this->assertSame('hmmm', trim($result));
 
139
 
 
140
        $CFG->allowobjectembed = '1';
 
141
 
 
142
        $expected = '<object width="425" height="350" data="http://www.youtube.com/v/AyPzM5WK8ys" type="application/x-shockwave-flash">
 
143
<param name="allowScriptAccess" value="never" />
 
144
<param name="allowNetworking" value="internal" />
 
145
<param name="movie" value="http://www.youtube.com/v/AyPzM5WK8ys" />
 
146
<param name="wmode" value="transparent" />
 
147
<embed src="http://www.youtube.com/v/AyPzM5WK8ys" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350" allowscriptaccess="never" allownetworking="internal" />
 
148
</object>hmmm';
 
149
        $result = purify_html($text, array());
 
150
        $this->assertSame(str_replace("\n", '', $expected), str_replace("\n", '', $result));
 
151
 
 
152
        $CFG->allowobjectembed = '0';
 
153
 
 
154
        $result = purify_html($text, array());
 
155
        $this->assertSame('hmmm', trim($result));
 
156
    }
 
157
 
133
158
    /**
134
159
     * Test if linebreaks kept unchanged.
135
 
     * @return void
136
160
     */
137
161
    public function test_line_breaking() {
138
162
        $text = "\n\raa\rsss\nsss\r";
141
165
 
142
166
    /**
143
167
     * Test fixing of strict problems.
144
 
     * @return void
145
168
     */
146
169
    public function test_tidy() {
147
170
        $text = "<p>xx";
155
178
    }
156
179
 
157
180
    /**
158
 
     * Test nesting - this used to cause problems in earlier versions
159
 
     * @return void
 
181
     * Test nesting - this used to cause problems in earlier versions.
160
182
     */
161
183
    public function test_nested_lists() {
162
184
        $text = "<ul><li>One<ul><li>Two</li></ul></li><li>Three</li></ul>";
165
187
 
166
188
    /**
167
189
     * Test that XSS protection works, complete smoke tests are in htmlpurifier itself.
168
 
     * @return void
169
190
     */
170
191
    public function test_cleaning_nastiness() {
171
192
        $text = "x<SCRIPT>alert('XSS')</SCRIPT>x";
192
213
 
193
214
    /**
194
215
     * Test internal function used for clean_text() speedup.
195
 
     * @return void
196
216
     */
197
217
    public function test_is_purify_html_necessary() {
198
 
        // first our shortcuts
 
218
        // First our shortcuts.
199
219
        $text = "";
200
220
        $this->assertFalse(is_purify_html_necessary($text));
201
221
        $this->assertSame($text, purify_html($text));
216
236
        $this->assertFalse(is_purify_html_necessary($text));
217
237
        $this->assertSame($text, purify_html($text));
218
238
 
219
 
        // now failures
 
239
        // Now failures.
220
240
        $text = "&nbsp;";
221
241
        $this->assertTrue(is_purify_html_necessary($text));
222
242
 
249
269
    }
250
270
 
251
271
    public function test_allowed_schemes() {
252
 
        // first standard schemes
 
272
        // First standard schemas.
253
273
        $text = '<a href="http://www.example.com/course/view.php?id=5">link</a>';
254
274
        $this->assertSame($text, purify_html($text));
255
275
 
268
288
        $text = '<a href="mailto:user@example.com">link</a>';
269
289
        $this->assertSame($text, purify_html($text));
270
290
 
271
 
        // extra schemes allowed in moodle
 
291
        // Extra schemes allowed in moodle.
272
292
        $text = '<a href="irc://irc.example.com/3213?pass">link</a>';
273
293
        $this->assertSame($text, purify_html($text));
274
294
 
284
304
        $text = '<a href="mms://www.example.com/movie.mms">link</a>';
285
305
        $this->assertSame($text, purify_html($text));
286
306
 
287
 
        // now some borked or dangerous schemes
 
307
        // Now some borked or dangerous schemes.
288
308
        $text = '<a href="javascript://www.example.com">link</a>';
289
309
        $this->assertSame('<a>link</a>', purify_html($text));
290
310
 
292
312
        $this->assertSame('<a>link</a>', purify_html($text));
293
313
    }
294
314
}
295