4 * @warning HTML output is in flux, but eventually needs to be stabilized.
6 class HTMLPurifier_ErrorCollectorTest
extends HTMLPurifier_Harness
9 protected $language, $generator, $line;
12 public function setup()
14 generate_mock_once('HTMLPurifier_Language');
15 generate_mock_once('HTMLPurifier_Generator');
17 $this->language
= new HTMLPurifier_LanguageMock();
18 $this->language
->returns('getErrorName', 'Error', array(E_ERROR
));
19 $this->language
->returns('getErrorName', 'Warning', array(E_WARNING
));
20 $this->language
->returns('getErrorName', 'Notice', array(E_NOTICE
));
21 // this might prove to be troublesome if we need to set config
22 $this->generator
= new HTMLPurifier_Generator($this->config
, $this->context
);
24 $this->context
->register('Locale', $this->language
);
25 $this->context
->register('CurrentLine', $this->line
);
26 $this->context
->register('Generator', $this->generator
);
27 $this->collector
= new HTMLPurifier_ErrorCollector($this->context
);
30 public function test()
32 $language = $this->language
;
33 $language->returns('getMessage', 'Message 1', array('message-1'));
34 $language->returns('formatMessage', 'Message 2', array('message-2', array(1 => 'param')));
35 $language->returns('formatMessage', ' at line 23', array('ErrorCollector: At line', array('line' => 23)));
36 $language->returns('formatMessage', ' at line 3', array('ErrorCollector: At line', array('line' => 3)));
39 $this->collector
->send(E_ERROR
, 'message-1');
42 $this->collector
->send(E_WARNING
, 'message-2', 'param');
45 0 => array(23, E_ERROR
, 'Message 1', array()),
46 1 => array(3, E_WARNING
, 'Message 2', array())
49 $this->assertIdentical($this->collector
->getRaw(), $result);
53 '<ul><li><strong>Warning</strong>: Message 2 at line 3</li>'.
54 '<li><strong>Error</strong>: Message 1 at line 23</li></ul>';
56 $this->assertIdentical($this->collector->getHTMLFormatted($this->config), $formatted_result);
61 public function testNoErrors()
63 $this->language
->returns('getMessage', 'No errors', array('ErrorCollector: No errors'));
65 $formatted_result = '<p>No errors</p>';
66 $this->assertIdentical(
67 $this->collector
->getHTMLFormatted($this->config
),
72 public function testNoLineNumbers()
74 $this->language
->returns('getMessage', 'Message 1', array('message-1'));
75 $this->language
->returns('getMessage', 'Message 2', array('message-2'));
77 $this->collector
->send(E_ERROR
, 'message-1');
78 $this->collector
->send(E_ERROR
, 'message-2');
81 0 => array(false, E_ERROR
, 'Message 1', array()),
82 1 => array(false, E_ERROR
, 'Message 2', array())
84 $this->assertIdentical($this->collector
->getRaw(), $result);
88 '<ul><li><strong>Error</strong>: Message 1</li>'.
89 '<li><strong>Error</strong>: Message 2</li></ul>';
90 $this->assertIdentical($this->collector->getHTMLFormatted($this->config), $formatted_result);
94 public function testContextSubstitutions()
96 $current_token = false;
97 $this->context
->register('CurrentToken', $current_token);
100 $current_token = new HTMLPurifier_Token_Start('a', array('href' => 'http://example.com'), 32);
101 $this->language
->returns('formatMessage', 'Token message',
102 array('message-data-token', array('CurrentToken' => $current_token)));
103 $this->collector
->send(E_NOTICE
, 'message-data-token');
105 $current_attr = 'href';
106 $this->language
->returns('formatMessage', '$CurrentAttr.Name => $CurrentAttr.Value',
107 array('message-attr', array('CurrentToken' => $current_token)));
110 $this->collector
->send(E_NOTICE
, 'message-attr'); // test when context isn't available
113 $this->context
->register('CurrentAttr', $current_attr);
114 $this->collector
->send(E_NOTICE
, 'message-attr');
117 0 => array(32, E_NOTICE
, 'Token message', array()),
118 1 => array(32, E_NOTICE
, '$CurrentAttr.Name => $CurrentAttr.Value', array()),
119 2 => array(32, E_NOTICE
, 'href => http://example.com', array())
121 $this->assertIdentical($this->collector
->getRaw(), $result);
126 public function testNestedErrors()
128 $this->language->returns('getMessage', 'Message 1', array('message-1'));
129 $this->language->returns('getMessage', 'Message 2', array('message-2'));
130 $this->language->returns('formatMessage', 'End Message', array('end-message', array(1 => 'param')));
131 $this->language->returns('formatMessage', ' at line 4', array('ErrorCollector: At line', array('line' => 4)));
134 $this->collector->start();
135 $this->collector->send(E_WARNING, 'message-1');
136 $this->collector->send(E_NOTICE, 'message-2');
137 $this->collector->end(E_NOTICE, 'end-message', 'param');
140 0 => array(4, E_NOTICE, 'End Message', array(
141 0 => array(4, E_WARNING, 'Message 1', array()),
142 1 => array(4, E_NOTICE, 'Message 2', array()),
145 $result = $this->collector->getRaw();
146 $this->assertIdentical($result, $expect);
149 '<ul><li><strong>Notice</strong>: End Message at line 4<ul>'.
150 '<li><strong>Warning</strong>: Message 1 at line 4</li>'.
151 '<li><strong>Notice</strong>: Message 2 at line 4</li></ul>'.
153 $formatted_result = $this->collector->getHTMLFormatted($this->config);
154 $this->assertIdentical($formatted_result, $formatted_expect);
161 // vim: et sw=4 sts=4