Convert all to new configuration get/set format.
[htmlpurifier/darkodev.git] / tests / HTMLPurifier / HTMLDefinitionTest.php
blobe7f85011693496ed67c3bcede2a7c09140a787b0
1 <?php
3 class HTMLPurifier_HTMLDefinitionTest extends HTMLPurifier_Harness
6 function expectError($error = false, $message = '%s') {
7 // Because we're testing a definition, it's vital that the cache
8 // is turned off for tests that expect errors.
9 $this->config->set('Cache.DefinitionImpl', null);
10 parent::expectError($error);
13 function test_parseTinyMCEAllowedList() {
15 $def = new HTMLPurifier_HTMLDefinition();
17 // note: this is case-sensitive, but its config schema
18 // counterpart is not. This is generally a good thing for users,
19 // but it's a slight internal inconsistency
21 $this->assertEqual(
22 $def->parseTinyMCEAllowedList(''),
23 array(array(), array())
26 $this->assertEqual(
27 $def->parseTinyMCEAllowedList('a,b,c'),
28 array(array('a' => true, 'b' => true, 'c' => true), array())
31 $this->assertEqual(
32 $def->parseTinyMCEAllowedList('a[x|y|z]'),
33 array(array('a' => true), array('a.x' => true, 'a.y' => true, 'a.z' => true))
36 $this->assertEqual(
37 $def->parseTinyMCEAllowedList('*[id]'),
38 array(array(), array('*.id' => true))
41 $this->assertEqual(
42 $def->parseTinyMCEAllowedList('a[*]'),
43 array(array('a' => true), array('a.*' => true))
46 $this->assertEqual(
47 $def->parseTinyMCEAllowedList('span[style],strong,a[href|title]'),
48 array(array('span' => true, 'strong' => true, 'a' => true),
49 array('span.style' => true, 'a.href' => true, 'a.title' => true))
52 $this->assertEqual(
53 // alternate form:
54 $def->parseTinyMCEAllowedList(
55 'span[style]
56 strong
57 a[href|title]
58 '),
59 $val = array(array('span' => true, 'strong' => true, 'a' => true),
60 array('span.style' => true, 'a.href' => true, 'a.title' => true))
63 $this->assertEqual(
64 $def->parseTinyMCEAllowedList(' span [ style ], strong'."\n\t".'a[href | title]'),
65 $val
70 function test_Allowed() {
72 $config1 = HTMLPurifier_Config::create(array(
73 'HTML.AllowedElements' => array('b', 'i', 'p', 'a'),
74 'HTML.AllowedAttributes' => array('a@href', '*@id')
75 ));
77 $config2 = HTMLPurifier_Config::create(array(
78 'HTML.Allowed' => 'b,i,p,a[href],*[id]'
79 ));
81 $this->assertEqual($config1->getHTMLDefinition(), $config2->getHTMLDefinition());
85 function assertPurification_AllowedElements_p() {
86 $this->assertPurification('<p><b>Jelly</b></p>', '<p>Jelly</p>');
89 function test_AllowedElements() {
90 $this->config->set('HTML.AllowedElements', 'p');
91 $this->assertPurification_AllowedElements_p();
94 function test_AllowedElements_multiple() {
95 $this->config->set('HTML.AllowedElements', 'p,div');
96 $this->assertPurification('<div><p><b>Jelly</b></p></div>', '<div><p>Jelly</p></div>');
99 function test_AllowedElements_invalidElement() {
100 $this->config->set('HTML.AllowedElements', 'obviously_invalid,p');
101 $this->expectError(new PatternExpectation("/Element 'obviously_invalid' is not supported/"));
102 $this->assertPurification_AllowedElements_p();
105 function test_AllowedElements_invalidElement_xssAttempt() {
106 $this->config->set('HTML.AllowedElements', '<script>,p');
107 $this->expectError(new PatternExpectation("/Element '&lt;script&gt;' is not supported/"));
108 $this->assertPurification_AllowedElements_p();
111 function test_AllowedElements_multipleInvalidElements() {
112 $this->config->set('HTML.AllowedElements', 'dr-wiggles,dr-pepper,p');
113 $this->expectError(new PatternExpectation("/Element 'dr-wiggles' is not supported/"));
114 $this->expectError(new PatternExpectation("/Element 'dr-pepper' is not supported/"));
115 $this->assertPurification_AllowedElements_p();
118 function assertPurification_AllowedAttributes_global_style() {
119 $this->assertPurification(
120 '<p style="font-weight:bold;" class="foo">Jelly</p><br style="clear:both;" />',
121 '<p style="font-weight:bold;">Jelly</p><br style="clear:both;" />');
124 function test_AllowedAttributes_global_preferredSyntax() {
125 $this->config->set('HTML.AllowedAttributes', 'style');
126 $this->assertPurification_AllowedAttributes_global_style();
129 function test_AllowedAttributes_global_verboseSyntax() {
130 $this->config->set('HTML.AllowedAttributes', '*@style');
131 $this->assertPurification_AllowedAttributes_global_style();
134 function test_AllowedAttributes_global_discouragedSyntax() {
135 // Emit errors eventually
136 $this->config->set('HTML.AllowedAttributes', '*.style');
137 $this->assertPurification_AllowedAttributes_global_style();
140 function assertPurification_AllowedAttributes_local_p_style() {
141 $this->assertPurification(
142 '<p style="font-weight:bold;" class="foo">Jelly</p><br style="clear:both;" />',
143 '<p style="font-weight:bold;">Jelly</p><br />');
146 function test_AllowedAttributes_local_preferredSyntax() {
147 $this->config->set('HTML.AllowedAttributes', 'p@style');
148 $this->assertPurification_AllowedAttributes_local_p_style();
151 function test_AllowedAttributes_local_discouragedSyntax() {
152 $this->config->set('HTML.AllowedAttributes', 'p.style');
153 $this->assertPurification_AllowedAttributes_local_p_style();
156 function test_AllowedAttributes_multiple() {
157 $this->config->set('HTML.AllowedAttributes', 'p@style,br@class,title');
158 $this->assertPurification(
159 '<p style="font-weight:bold;" class="foo" title="foo">Jelly</p><br style="clear:both;" class="foo" title="foo" />',
160 '<p style="font-weight:bold;" title="foo">Jelly</p><br class="foo" title="foo" />'
164 function test_AllowedAttributes_local_invalidAttribute() {
165 $this->config->set('HTML.AllowedAttributes', array('p@style', 'p@<foo>'));
166 $this->expectError(new PatternExpectation("/Attribute '&lt;foo&gt;' in element 'p' not supported/"));
167 $this->assertPurification_AllowedAttributes_local_p_style();
170 function test_AllowedAttributes_global_invalidAttribute() {
171 $this->config->set('HTML.AllowedAttributes', array('style', '<foo>'));
172 $this->expectError(new PatternExpectation("/Global attribute '&lt;foo&gt;' is not supported in any elements/"));
173 $this->assertPurification_AllowedAttributes_global_style();
176 function test_AllowedAttributes_local_invalidAttributeDueToMissingElement() {
177 $this->config->set('HTML.AllowedAttributes', 'p.style,foo.style');
178 $this->expectError(new PatternExpectation("/Cannot allow attribute 'style' if element 'foo' is not allowed\/supported/"));
179 $this->assertPurification_AllowedAttributes_local_p_style();
182 function test_AllowedAttributes_duplicate() {
183 $this->config->set('HTML.AllowedAttributes', 'p.style,p@style');
184 $this->assertPurification_AllowedAttributes_local_p_style();
187 function test_AllowedAttributes_multipleErrors() {
188 $this->config->set('HTML.AllowedAttributes', 'p.style,foo.style,<foo>');
189 $this->expectError(new PatternExpectation("/Cannot allow attribute 'style' if element 'foo' is not allowed\/supported/"));
190 $this->expectError(new PatternExpectation("/Global attribute '&lt;foo&gt;' is not supported in any elements/"));
191 $this->assertPurification_AllowedAttributes_local_p_style();
194 function test_ForbiddenElements() {
195 $this->config->set('HTML.ForbiddenElements', 'b');
196 $this->assertPurification('<b>b</b><i>i</i>', 'b<i>i</i>');
199 function test_ForbiddenElements_invalidElement() {
200 $this->config->set('HTML.ForbiddenElements', 'obviously_incorrect');
201 // no error!
202 $this->assertPurification('<i>i</i>');
205 function assertPurification_ForbiddenAttributes_b_style() {
206 $this->assertPurification(
207 '<b style="float:left;">b</b><i style="float:left;">i</i>',
208 '<b>b</b><i style="float:left;">i</i>');
211 function test_ForbiddenAttributes() {
212 $this->config->set('HTML.ForbiddenAttributes', 'b@style');
213 $this->assertPurification_ForbiddenAttributes_b_style();
216 function test_ForbiddenAttributes_incorrectSyntax() {
217 $this->config->set('HTML.ForbiddenAttributes', 'b.style');
218 $this->expectError("Error with b.style: tag.attr syntax not supported for HTML.ForbiddenAttributes; use tag@attr instead");
219 $this->assertPurification('<b style="float:left;">Test</b>');
222 function test_ForbiddenAttributes_incorrectGlobalSyntax() {
223 $this->config->set('HTML.ForbiddenAttributes', '*.style');
224 $this->expectError("Error with *.style: *.attr syntax not supported for HTML.ForbiddenAttributes; use attr instead");
225 $this->assertPurification('<b style="float:left;">Test</b>');
228 function assertPurification_ForbiddenAttributes_style() {
229 $this->assertPurification(
230 '<b class="foo" style="float:left;">b</b><i style="float:left;">i</i>',
231 '<b class="foo">b</b><i>i</i>');
234 function test_ForbiddenAttributes_global() {
235 $this->config->set('HTML.ForbiddenAttributes', 'style');
236 $this->assertPurification_ForbiddenAttributes_style();
239 function test_ForbiddenAttributes_globalVerboseFormat() {
240 $this->config->set('HTML.ForbiddenAttributes', '*@style');
241 $this->assertPurification_ForbiddenAttributes_style();
244 function test_addAttribute() {
246 $config = HTMLPurifier_Config::create(array(
247 'HTML.DefinitionID' => 'HTMLPurifier_HTMLDefinitionTest->test_addAttribute'
249 $def = $config->getHTMLDefinition(true);
250 $def->addAttribute('span', 'custom', 'Enum#attribute');
252 $purifier = new HTMLPurifier($config);
253 $input = '<span custom="attribute">Custom!</span>';
254 $output = $purifier->purify($input);
255 $this->assertIdentical($input, $output);
259 function test_addAttribute_multiple() {
261 $config = HTMLPurifier_Config::create(array(
262 'HTML.DefinitionID' => 'HTMLPurifier_HTMLDefinitionTest->test_addAttribute_multiple'
264 $def = $config->getHTMLDefinition(true);
265 $def->addAttribute('span', 'custom', 'Enum#attribute');
266 $def->addAttribute('span', 'foo', 'Text');
268 $purifier = new HTMLPurifier($config);
269 $input = '<span custom="attribute" foo="asdf">Custom!</span>';
270 $output = $purifier->purify($input);
271 $this->assertIdentical($input, $output);
275 function test_addElement() {
277 $config = HTMLPurifier_Config::create(array(
278 'HTML.DefinitionID' => 'HTMLPurifier_HTMLDefinitionTest->test_addElement'
280 $def = $config->getHTMLDefinition(true);
281 $def->addElement('marquee', 'Inline', 'Inline', 'Common', array('width' => 'Length'));
283 $purifier = new HTMLPurifier($config);
284 $input = '<span><marquee width="50">Foobar</marquee></span>';
285 $output = $purifier->purify($input);
286 $this->assertIdentical($input, $output);
290 function test_injector() {
291 $this->config->set('HTML.DefinitionID', 'HTMLPurifier_HTMLDefinitionTest->test_injector');
293 generate_mock_once('HTMLPurifier_Injector');
294 $injector = new HTMLPurifier_InjectorMock();
295 $injector->name = 'MyInjector';
296 $injector->setReturnValue('checkNeeded', false);
298 $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
299 $module->info_injector[] = $injector;
301 $this->assertIdentical($this->config->getHTMLDefinition()->info_injector,
302 array(
303 'MyInjector' => $injector,
308 function test_injectorMissingNeeded() {
309 $this->config->set('HTML.DefinitionID', 'HTMLPurifier_HTMLDefinitionTest->test_injectorMissingNeeded');
311 generate_mock_once('HTMLPurifier_Injector');
312 $injector = new HTMLPurifier_InjectorMock();
313 $injector->name = 'MyInjector';
314 $injector->setReturnValue('checkNeeded', 'a');
316 $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
317 $module->info_injector[] = $injector;
319 $this->assertIdentical($this->config->getHTMLDefinition()->info_injector,
320 array()
324 function test_injectorIntegration() {
325 $this->config->set('HTML.DefinitionID', 'HTMLPurifier_HTMLDefinitionTest->test_injectorIntegration');
327 $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
328 $module->info_injector[] = 'Linkify';
330 $this->assertIdentical(
331 $this->config->getHTMLDefinition()->info_injector,
332 array('Linkify' => new HTMLPurifier_Injector_Linkify())
336 function test_injectorIntegrationFail() {
337 $this->config->set('HTML.DefinitionID', 'HTMLPurifier_HTMLDefinitionTest->test_injectorIntegrationFail');
339 $this->config->set('HTML.Allowed', 'p');
341 $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
342 $module->info_injector[] = 'Linkify';
344 $this->assertIdentical(
345 $this->config->getHTMLDefinition()->info_injector,
346 array()
352 // vim: et sw=4 sts=4