PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / tests / HTMLPurifier / ErrorCollectorTest.php
blob20cec81f594d3725287362ad257422e356d7e31b
1 <?php
3 /**
4 * @warning HTML output is in flux, but eventually needs to be stabilized.
5 */
6 class HTMLPurifier_ErrorCollectorTest extends HTMLPurifier_Harness
9 protected $language, $generator, $line;
10 protected $collector;
12 public function setup()
14 generate_mock_once('HTMLPurifier_Language');
15 generate_mock_once('HTMLPurifier_Generator');
16 parent::setup();
17 $this->language = new HTMLPurifier_LanguageMock();
18 $this->language->setReturnValue('getErrorName', 'Error', array(E_ERROR));
19 $this->language->setReturnValue('getErrorName', 'Warning', array(E_WARNING));
20 $this->language->setReturnValue('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);
23 $this->line = false;
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->setReturnValue('getMessage', 'Message 1', array('message-1'));
34 $language->setReturnValue('formatMessage', 'Message 2', array('message-2', array(1 => 'param')));
35 $language->setReturnValue('formatMessage', ' at line 23', array('ErrorCollector: At line', array('line' => 23)));
36 $language->setReturnValue('formatMessage', ' at line 3', array('ErrorCollector: At line', array('line' => 3)));
38 $this->line = 23;
39 $this->collector->send(E_ERROR, 'message-1');
41 $this->line = 3;
42 $this->collector->send(E_WARNING, 'message-2', 'param');
44 $result = array(
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);
52 $formatted_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->setReturnValue('getMessage', 'No errors', array('ErrorCollector: No errors'));
65 $formatted_result = '<p>No errors</p>';
66 $this->assertIdentical(
67 $this->collector->getHTMLFormatted($this->config),
68 $formatted_result
72 public function testNoLineNumbers()
74 $this->language->setReturnValue('getMessage', 'Message 1', array('message-1'));
75 $this->language->setReturnValue('getMessage', 'Message 2', array('message-2'));
77 $this->collector->send(E_ERROR, 'message-1');
78 $this->collector->send(E_ERROR, 'message-2');
80 $result = array(
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);
87 $formatted_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);
99 // 0
100 $current_token = new HTMLPurifier_Token_Start('a', array('href' => 'http://example.com'), 32);
101 $this->language->setReturnValue('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->setReturnValue('formatMessage', '$CurrentAttr.Name => $CurrentAttr.Value',
107 array('message-attr', array('CurrentToken' => $current_token)));
109 // 1
110 $this->collector->send(E_NOTICE, 'message-attr'); // test when context isn't available
112 // 2
113 $this->context->register('CurrentAttr', $current_attr);
114 $this->collector->send(E_NOTICE, 'message-attr');
116 $result = array(
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->setReturnValue('getMessage', 'Message 1', array('message-1'));
129 $this->language->setReturnValue('getMessage', 'Message 2', array('message-2'));
130 $this->language->setReturnValue('formatMessage', 'End Message', array('end-message', array(1 => 'param')));
131 $this->language->setReturnValue('formatMessage', ' at line 4', array('ErrorCollector: At line', array('line' => 4)));
133 $this->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');
139 $expect = array(
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);
148 $formatted_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>'.
152 '</li></ul>';
153 $formatted_result = $this->collector->getHTMLFormatted($this->config);
154 $this->assertIdentical($formatted_result, $formatted_expect);
161 // vim: et sw=4 sts=4