2007-06-18 03:05:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/ErrorCollector.php';
|
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
class HTMLPurifier_ErrorCollectorTest extends HTMLPurifier_Harness
|
2007-06-18 03:05:18 +00:00
|
|
|
{
|
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
function setup() {
|
|
|
|
generate_mock_once('HTMLPurifier_Language');
|
2007-06-26 02:49:21 +00:00
|
|
|
generate_mock_once('HTMLPurifier_Generator');
|
2007-06-25 00:48:26 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:05:18 +00:00
|
|
|
function test() {
|
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
$language = new HTMLPurifier_LanguageMock();
|
|
|
|
$language->setReturnValue('getErrorName', 'Error', array(E_ERROR));
|
|
|
|
$language->setReturnValue('getErrorName', 'Warning', array(E_WARNING));
|
|
|
|
$language->setReturnValue('getMessage', 'Message 1', array('message-1'));
|
|
|
|
$language->setReturnValue('formatMessage', 'Message 2', array('message-2', array(1 => 'param')));
|
|
|
|
$language->setReturnValue('formatMessage', ' at line 23', array('ErrorCollector: At line', array('line' => 23)));
|
|
|
|
$language->setReturnValue('formatMessage', ' at line 3', array('ErrorCollector: At line', array('line' => 3)));
|
|
|
|
|
2007-06-25 01:08:57 +00:00
|
|
|
$line = false;
|
|
|
|
|
|
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
$context->register('Locale', $language);
|
|
|
|
$context->register('CurrentLine', $line);
|
|
|
|
|
2007-06-26 19:33:37 +00:00
|
|
|
$generator = new HTMLPurifier_Generator();
|
2007-06-26 02:49:21 +00:00
|
|
|
$context->register('Generator', $generator);
|
|
|
|
|
2007-06-25 01:08:57 +00:00
|
|
|
$collector = new HTMLPurifier_ErrorCollector($context);
|
|
|
|
|
|
|
|
$line = 23;
|
|
|
|
$collector->send(E_ERROR, 'message-1');
|
|
|
|
|
|
|
|
$line = 3;
|
|
|
|
$collector->send(E_WARNING, 'message-2', 'param');
|
2007-06-18 03:05:18 +00:00
|
|
|
|
|
|
|
$result = array(
|
2007-06-25 00:48:26 +00:00
|
|
|
0 => array(23, E_ERROR, 'Message 1'),
|
|
|
|
1 => array(3, E_WARNING, 'Message 2')
|
2007-06-18 03:05:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertIdentical($collector->getRaw(), $result);
|
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
$formatted_result =
|
2007-06-27 02:03:15 +00:00
|
|
|
'<ul><li><strong>Warning</strong>: Message 2 at line 3</li>'.
|
|
|
|
'<li><strong>Error</strong>: Message 1 at line 23</li></ul>';
|
2007-06-18 03:05:18 +00:00
|
|
|
|
|
|
|
$config = HTMLPurifier_Config::create(array('Core.MaintainLineNumbers' => true));
|
2007-06-24 23:20:35 +00:00
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
$this->assertIdentical($collector->getHTMLFormatted($config), $formatted_result);
|
2007-06-24 23:20:35 +00:00
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testNoErrors() {
|
|
|
|
$language = new HTMLPurifier_LanguageMock();
|
|
|
|
$language->setReturnValue('getMessage', 'No errors', array('ErrorCollector: No errors'));
|
2007-06-25 01:08:57 +00:00
|
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
$context->register('Locale', $language);
|
|
|
|
|
2007-06-26 19:33:37 +00:00
|
|
|
$generator = new HTMLPurifier_Generator();
|
2007-06-26 02:49:21 +00:00
|
|
|
$context->register('Generator', $generator);
|
|
|
|
|
2007-06-25 01:08:57 +00:00
|
|
|
$collector = new HTMLPurifier_ErrorCollector($context);
|
2007-06-25 00:48:26 +00:00
|
|
|
$formatted_result = '<p>No errors</p>';
|
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$this->assertIdentical($collector->getHTMLFormatted($config), $formatted_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testNoLineNumbers() {
|
2007-06-24 23:20:35 +00:00
|
|
|
$language = new HTMLPurifier_LanguageMock();
|
2007-06-25 00:48:26 +00:00
|
|
|
$language->setReturnValue('getMessage', 'Message 1', array('message-1'));
|
|
|
|
$language->setReturnValue('getMessage', 'Message 2', array('message-2'));
|
2007-06-24 23:20:35 +00:00
|
|
|
$language->setReturnValue('getErrorName', 'Error', array(E_ERROR));
|
2007-06-25 01:08:57 +00:00
|
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
$context->register('Locale', $language);
|
|
|
|
|
2007-06-26 19:33:37 +00:00
|
|
|
$generator = new HTMLPurifier_Generator();
|
2007-06-26 02:49:21 +00:00
|
|
|
$context->register('Generator', $generator);
|
|
|
|
|
2007-06-25 01:08:57 +00:00
|
|
|
$collector = new HTMLPurifier_ErrorCollector($context);
|
|
|
|
$collector->send(E_ERROR, 'message-1');
|
|
|
|
$collector->send(E_ERROR, 'message-2');
|
2007-06-24 23:20:35 +00:00
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
$result = array(
|
2007-06-25 01:08:57 +00:00
|
|
|
0 => array(null, E_ERROR, 'Message 1'),
|
|
|
|
1 => array(null, E_ERROR, 'Message 2')
|
2007-06-25 00:48:26 +00:00
|
|
|
);
|
|
|
|
$this->assertIdentical($collector->getRaw(), $result);
|
2007-06-18 03:05:18 +00:00
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
$formatted_result =
|
2007-06-27 02:03:15 +00:00
|
|
|
'<ul><li><strong>Error</strong>: Message 1</li>'.
|
|
|
|
'<li><strong>Error</strong>: Message 2</li></ul>';
|
2007-06-25 00:48:26 +00:00
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$this->assertIdentical($collector->getHTMLFormatted($config), $formatted_result);
|
2007-06-18 03:05:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 02:49:21 +00:00
|
|
|
function testContextSubstitutions() {
|
|
|
|
|
|
|
|
$language = new HTMLPurifier_LanguageMock();
|
2007-06-26 19:33:37 +00:00
|
|
|
$context = new HTMLPurifier_Context();
|
2007-06-26 02:49:21 +00:00
|
|
|
$context->register('Locale', $language);
|
|
|
|
|
2007-06-26 19:33:37 +00:00
|
|
|
$generator = new HTMLPurifier_Generator();
|
2007-06-26 02:49:21 +00:00
|
|
|
$context->register('Generator', $generator);
|
|
|
|
|
2007-06-26 19:33:37 +00:00
|
|
|
$current_token = false;
|
|
|
|
$context->register('CurrentToken', $current_token);
|
|
|
|
|
2007-06-26 02:49:21 +00:00
|
|
|
$collector = new HTMLPurifier_ErrorCollector($context);
|
|
|
|
|
2007-06-26 19:33:37 +00:00
|
|
|
// 0
|
|
|
|
$current_token = new HTMLPurifier_Token_Start('a', array('href' => 'http://example.com'), 32);
|
|
|
|
$language->setReturnValue('formatMessage', 'Token message',
|
|
|
|
array('message-data-token', array('CurrentToken' => $current_token)));
|
|
|
|
$collector->send(E_NOTICE, 'message-data-token');
|
|
|
|
|
|
|
|
$current_attr = 'href';
|
|
|
|
$language->setReturnValue('formatMessage', '$CurrentAttr.Name => $CurrentAttr.Value',
|
|
|
|
array('message-attr', array('CurrentToken' => $current_token)));
|
|
|
|
|
|
|
|
// 1
|
2007-06-26 02:49:21 +00:00
|
|
|
$collector->send(E_NOTICE, 'message-attr'); // test when context isn't available
|
|
|
|
|
2007-06-26 19:33:37 +00:00
|
|
|
// 2
|
2007-06-26 02:49:21 +00:00
|
|
|
$context->register('CurrentAttr', $current_attr);
|
|
|
|
$collector->send(E_NOTICE, 'message-attr');
|
|
|
|
|
|
|
|
$result = array(
|
2007-06-26 19:33:37 +00:00
|
|
|
0 => array(32, E_NOTICE, 'Token message'),
|
2007-06-26 02:49:21 +00:00
|
|
|
1 => array(32, E_NOTICE, '$CurrentAttr.Name => $CurrentAttr.Value'),
|
|
|
|
2 => array(32, E_NOTICE, 'href => http://example.com')
|
|
|
|
);
|
|
|
|
$this->assertIdentical($collector->getRaw(), $result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:05:18 +00:00
|
|
|
}
|
|
|
|
|