|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package framework |
|
4
|
|
|
* @subpackage tests |
|
5
|
|
|
*/ |
|
6
|
|
|
class HTMLEditorSanitiserTest extends FunctionalTest { |
|
7
|
|
|
|
|
8
|
|
|
public function testSanitisation() { |
|
9
|
|
|
$tests = array( |
|
10
|
|
|
array( |
|
11
|
|
|
'p,strong', |
|
12
|
|
|
'<p>Leave Alone</p><div>Strip parent<strong>But keep children</strong> in order</div>', |
|
13
|
|
|
'<p>Leave Alone</p>Strip parent<strong>But keep children</strong> in order', |
|
14
|
|
|
'Non-whitelisted elements are stripped, but children are kept' |
|
15
|
|
|
), |
|
16
|
|
|
array( |
|
17
|
|
|
'p,strong', |
|
18
|
|
|
'<div>A <strong>B <div>Nested elements are still filtered</div> C</strong> D</div>', |
|
19
|
|
|
'A <strong>B Nested elements are still filtered C</strong> D', |
|
20
|
|
|
'Non-whitelisted elements are stripped even when children of non-whitelisted elements' |
|
21
|
|
|
), |
|
22
|
|
|
array( |
|
23
|
|
|
'p', |
|
24
|
|
|
'<p>Keep</p><script>Strip <strong>including children</strong></script>', |
|
25
|
|
|
'<p>Keep</p>', |
|
26
|
|
|
'Non-whitelisted script elements are totally stripped, including any children' |
|
27
|
|
|
), |
|
28
|
|
|
array( |
|
29
|
|
|
'p[id]', |
|
30
|
|
|
'<p id="keep" bad="strip">Test</p>', |
|
31
|
|
|
'<p id="keep">Test</p>', |
|
32
|
|
|
'Non-whitelisted attributes are stripped' |
|
33
|
|
|
), |
|
34
|
|
|
array( |
|
35
|
|
|
'p[default1=default1|default2=default2|force1:force1|force2:force2]', |
|
36
|
|
|
'<p default1="specific1" force1="specific1">Test</p>', |
|
37
|
|
|
'<p default1="specific1" force1="force1" default2="default2" force2="force2">Test</p>', |
|
38
|
|
|
'Default attributes are set when not present in input, forced attributes are always set' |
|
39
|
|
|
) |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
$config = HTMLEditorConfig::get('htmleditorsanitisertest'); |
|
43
|
|
|
|
|
44
|
|
|
foreach($tests as $test) { |
|
45
|
|
|
list($validElements, $input, $output, $desc) = $test; |
|
46
|
|
|
|
|
47
|
|
|
$config->setOptions(array('valid_elements' => $validElements)); |
|
48
|
|
|
$sanitiser = new HtmlEditorSanitiser($config); |
|
49
|
|
|
|
|
50
|
|
|
$htmlValue = Injector::inst()->create('HTMLValue', $input); |
|
51
|
|
|
$sanitiser->sanitise($htmlValue); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertEquals($output, $htmlValue->getContent(), $desc); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|