Slight adjustments to demo: reset to default time limit and send out a header indicat...
[htmlpurifier.git] / tests / HTMLPurifier / GeneratorTest.php
blobd567c236eed6e8706ac3a1bb9c958314c1bc0c0b
1 <?php
3 require_once 'HTMLPurifier/Generator.php';
4 require_once 'HTMLPurifier/EntityLookup.php';
6 class HTMLPurifier_GeneratorTest extends UnitTestCase
9 var $gen;
10 var $_entity_lookup;
12 function HTMLPurifier_GeneratorTest() {
13 $this->UnitTestCase();
14 $this->gen = new HTMLPurifier_Generator();
15 $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
18 function test_generateFromToken() {
20 $inputs = array();
21 $expect = array();
22 $config = array();
24 $inputs[0] = new HTMLPurifier_Token_Text('Foobar.<>');
25 $expect[0] = 'Foobar.&lt;&gt;';
27 $inputs[1] = new HTMLPurifier_Token_Start('a',
28 array('href' => 'dyn?a=foo&b=bar')
30 $expect[1] = '<a href="dyn?a=foo&amp;b=bar">';
32 $inputs[2] = new HTMLPurifier_Token_End('b');
33 $expect[2] = '</b>';
35 $inputs[3] = new HTMLPurifier_Token_Empty('br',
36 array('style' => 'font-family:"Courier New";')
38 $expect[3] = '<br style="font-family:&quot;Courier New&quot;;" />';
40 $inputs[4] = new HTMLPurifier_Token_Start('asdf');
41 $expect[4] = '<asdf>';
43 $inputs[5] = new HTMLPurifier_Token_Empty('br');
44 $expect[5] = '<br />';
46 // test fault tolerance
47 $inputs[6] = null;
48 $expect[6] = '';
50 // don't convert non-special characters
51 $theta_char = $this->_entity_lookup->table['theta'];
52 $inputs[7] = new HTMLPurifier_Token_Text($theta_char);
53 $expect[7] = $theta_char;
55 $default_config = HTMLPurifier_Config::createDefault();
56 foreach ($inputs as $i => $input) {
57 if (!isset($config[$i])) $config[$i] = $default_config;
58 $result = $this->gen->generateFromToken($input, $config[$i]);
59 $this->assertEqual($result, $expect[$i]);
60 paintIf($result, $result != $expect[$i]);
65 function test_generateAttributes() {
67 $inputs = array();
68 $expect = array();
69 $config = array();
71 $inputs[0] = array();
72 $expect[0] = '';
74 $inputs[1] = array('href' => 'dyn?a=foo&b=bar');
75 $expect[1] = 'href="dyn?a=foo&amp;b=bar"';
77 $inputs[2] = array('style' => 'font-family:"Courier New";');
78 $expect[2] = 'style="font-family:&quot;Courier New&quot;;"';
80 $inputs[3] = array('src' => 'picture.jpg', 'alt' => 'Short & interesting');
81 $expect[3] = 'src="picture.jpg" alt="Short &amp; interesting"';
83 // don't escape nonspecial characters
84 $theta_char = $this->_entity_lookup->table['theta'];
85 $inputs[4] = array('title' => 'Theta is ' . $theta_char);
86 $expect[4] = 'title="Theta is ' . $theta_char . '"';
88 $default_config = HTMLPurifier_Config::createDefault();
89 foreach ($inputs as $i => $input) {
90 if (!isset($config[$i])) $config[$i] = $default_config;
91 $result = $this->gen->generateAttributes($input, $config[$i]);
92 $this->assertEqual($result, $expect[$i]);
93 paintIf($result, $result != $expect[$i]);
98 function test_generateFromTokens() {
100 $inputs = array();
101 $expect = array();
102 $config = array();
104 $inputs[0] = array(
105 new HTMLPurifier_Token_Start('b'),
106 new HTMLPurifier_Token_Text('Foobar!'),
107 new HTMLPurifier_Token_End('b')
109 $expect[0] = '<b>Foobar!</b>';
111 $inputs[1] = array();
112 $expect[1] = '';
114 $default_config = HTMLPurifier_Config::createDefault();
115 foreach ($inputs as $i => $input) {
116 if (!isset($config[$i])) $config[$i] = $default_config;
117 $result = $this->gen->generateFromTokens($input, $config[$i]);
118 $this->assertEqual($expect[$i], $result);
119 paintIf($result, $result != $expect[$i]);