[3.0.0] Upgraded test scripts and other goodies. Also removed some PHP4 cruft.
[htmlpurifier/darkodev.git] / tests / HTMLPurifierTest.php
blob5462b67f7a13ccad521b35748cd6a22016569978
1 <?php
3 require_once 'HTMLPurifier.php';
5 // integration test
7 class HTMLPurifierTest extends HTMLPurifier_Harness
9 protected $purifier;
11 function setUp() {
12 $this->purifier = new HTMLPurifier();
15 function assertPurification($input, $expect = null, $config = array()) {
16 if ($expect === null) $expect = $input;
17 $result = $this->purifier->purify($input, $config);
18 $this->assertIdentical($expect, $result);
21 function testNull() {
22 $this->assertPurification("Null byte\0", "Null byte");
25 function testStrict() {
26 $config = HTMLPurifier_Config::createDefault();
27 $config->set('HTML', 'Strict', true);
28 $this->purifier = new HTMLPurifier( $config ); // verbose syntax
30 $this->assertPurification(
31 '<u>Illegal underline</u>',
32 '<span style="text-decoration:underline;">Illegal underline</span>'
35 $this->assertPurification(
36 '<blockquote>Illegal contents</blockquote>',
37 '<blockquote><p>Illegal contents</p></blockquote>'
42 function testDifferentAllowedElements() {
44 $this->purifier = new HTMLPurifier(array(
45 'HTML.AllowedElements' => array('b', 'i', 'p', 'a'),
46 'HTML.AllowedAttributes' => array('a.href', '*.id')
47 ));
49 $this->assertPurification(
50 '<p>Par.</p><p>Para<a href="http://google.com/">gr</a>aph</p>Text<b>Bol<i>d</i></b>'
53 $this->assertPurification(
54 '<span>Not allowed</span><a class="mef" id="foobar">Foobar</a>',
55 'Not allowed<a>Foobar</a>' // no ID!!!
60 function testDisableURI() {
62 $this->purifier = new HTMLPurifier( array('Attr.DisableURI' => true) );
64 $this->assertPurification(
65 '<img src="foobar"/>',
71 function test_purifyArray() {
73 $this->purifier = new HTMLPurifier();
75 $this->assertIdentical(
76 $this->purifier->purifyArray(
77 array('Good', '<b>Sketchy', 'foo' => '<script>bad</script>')
79 array('Good', '<b>Sketchy</b>', 'foo' => '')
82 $this->assertIsA($this->purifier->context, 'array');
86 function testEnableAttrID() {
88 $this->purifier = new HTMLPurifier();
90 $this->assertPurification(
91 '<span id="moon">foobar</span>',
92 '<span>foobar</span>'
95 $this->purifier = new HTMLPurifier(array('HTML.EnableAttrID' => true));
96 $this->assertPurification('<span id="moon">foobar</span>');
97 $this->assertPurification('<img id="folly" src="folly.png" alt="Omigosh!" />');
101 function testScript() {
102 $this->purifier = new HTMLPurifier(array('HTML.Trusted' => true));
103 $ideal = '<script type="text/javascript"><!--//--><![CDATA[//><!--
104 alert("<This is compatible with XHTML>");
105 //--><!]]></script>';
107 $this->assertPurification($ideal);
109 $this->assertPurification(
110 '<script type="text/javascript"><![CDATA[
111 alert("<This is compatible with XHTML>");
112 ]]></script>',
113 $ideal
116 $this->assertPurification(
117 '<script type="text/javascript">alert("<This is compatible with XHTML>");</script>',
118 $ideal
121 $this->assertPurification(
122 '<script type="text/javascript"><!--
123 alert("<This is compatible with XHTML>");
124 //--></script>',
125 $ideal
128 $this->assertPurification(
129 '<script type="text/javascript"><![CDATA[
130 alert("<This is compatible with XHTML>");
131 //]]></script>',
132 $ideal
136 function testGetInstance() {
137 $purifier =& HTMLPurifier::getInstance();
138 $purifier2 =& HTMLPurifier::getInstance();
139 $this->assertReference($purifier, $purifier2);
142 function testMakeAbsolute() {
143 $this->assertPurification(
144 '<a href="foo.txt">Foobar</a>',
145 '<a href="http://example.com/bar/foo.txt">Foobar</a>',
146 array(
147 'URI.Base' => 'http://example.com/bar/baz.php',
148 'URI.MakeAbsolute' => true