ErrorCollectorTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @warning HTML output is in flux, but eventually needs to be stabilized.
  4. */
  5. class HTMLPurifier_ErrorCollectorTest extends HTMLPurifier_Harness
  6. {
  7. protected $language, $generator, $line;
  8. protected $collector;
  9. public function setup() {
  10. generate_mock_once('HTMLPurifier_Language');
  11. generate_mock_once('HTMLPurifier_Generator');
  12. parent::setup();
  13. $this->language = new HTMLPurifier_LanguageMock();
  14. $this->language->setReturnValue('getErrorName', 'Error', array(E_ERROR));
  15. $this->language->setReturnValue('getErrorName', 'Warning', array(E_WARNING));
  16. $this->language->setReturnValue('getErrorName', 'Notice', array(E_NOTICE));
  17. // this might prove to be troublesome if we need to set config
  18. $this->generator = new HTMLPurifier_Generator($this->config, $this->context);
  19. $this->line = false;
  20. $this->context->register('Locale', $this->language);
  21. $this->context->register('CurrentLine', $this->line);
  22. $this->context->register('Generator', $this->generator);
  23. $this->collector = new HTMLPurifier_ErrorCollector($this->context);
  24. }
  25. function test() {
  26. $language = $this->language;
  27. $language->setReturnValue('getMessage', 'Message 1', array('message-1'));
  28. $language->setReturnValue('formatMessage', 'Message 2', array('message-2', array(1 => 'param')));
  29. $language->setReturnValue('formatMessage', ' at line 23', array('ErrorCollector: At line', array('line' => 23)));
  30. $language->setReturnValue('formatMessage', ' at line 3', array('ErrorCollector: At line', array('line' => 3)));
  31. $this->line = 23;
  32. $this->collector->send(E_ERROR, 'message-1');
  33. $this->line = 3;
  34. $this->collector->send(E_WARNING, 'message-2', 'param');
  35. $result = array(
  36. 0 => array(23, E_ERROR, 'Message 1', array()),
  37. 1 => array(3, E_WARNING, 'Message 2', array())
  38. );
  39. $this->assertIdentical($this->collector->getRaw(), $result);
  40. /*
  41. $formatted_result =
  42. '<ul><li><strong>Warning</strong>: Message 2 at line 3</li>'.
  43. '<li><strong>Error</strong>: Message 1 at line 23</li></ul>';
  44. $this->assertIdentical($this->collector->getHTMLFormatted($this->config), $formatted_result);
  45. */
  46. }
  47. function testNoErrors() {
  48. $this->language->setReturnValue('getMessage', 'No errors', array('ErrorCollector: No errors'));
  49. $formatted_result = '<p>No errors</p>';
  50. $this->assertIdentical(
  51. $this->collector->getHTMLFormatted($this->config),
  52. $formatted_result
  53. );
  54. }
  55. function testNoLineNumbers() {
  56. $this->language->setReturnValue('getMessage', 'Message 1', array('message-1'));
  57. $this->language->setReturnValue('getMessage', 'Message 2', array('message-2'));
  58. $this->collector->send(E_ERROR, 'message-1');
  59. $this->collector->send(E_ERROR, 'message-2');
  60. $result = array(
  61. 0 => array(false, E_ERROR, 'Message 1', array()),
  62. 1 => array(false, E_ERROR, 'Message 2', array())
  63. );
  64. $this->assertIdentical($this->collector->getRaw(), $result);
  65. /*
  66. $formatted_result =
  67. '<ul><li><strong>Error</strong>: Message 1</li>'.
  68. '<li><strong>Error</strong>: Message 2</li></ul>';
  69. $this->assertIdentical($this->collector->getHTMLFormatted($this->config), $formatted_result);
  70. */
  71. }
  72. function testContextSubstitutions() {
  73. $current_token = false;
  74. $this->context->register('CurrentToken', $current_token);
  75. // 0
  76. $current_token = new HTMLPurifier_Token_Start('a', array('href' => 'http://example.com'), 32);
  77. $this->language->setReturnValue('formatMessage', 'Token message',
  78. array('message-data-token', array('CurrentToken' => $current_token)));
  79. $this->collector->send(E_NOTICE, 'message-data-token');
  80. $current_attr = 'href';
  81. $this->language->setReturnValue('formatMessage', '$CurrentAttr.Name => $CurrentAttr.Value',
  82. array('message-attr', array('CurrentToken' => $current_token)));
  83. // 1
  84. $this->collector->send(E_NOTICE, 'message-attr'); // test when context isn't available
  85. // 2
  86. $this->context->register('CurrentAttr', $current_attr);
  87. $this->collector->send(E_NOTICE, 'message-attr');
  88. $result = array(
  89. 0 => array(32, E_NOTICE, 'Token message', array()),
  90. 1 => array(32, E_NOTICE, '$CurrentAttr.Name => $CurrentAttr.Value', array()),
  91. 2 => array(32, E_NOTICE, 'href => http://example.com', array())
  92. );
  93. $this->assertIdentical($this->collector->getRaw(), $result);
  94. }
  95. /*
  96. function testNestedErrors() {
  97. $this->language->setReturnValue('getMessage', 'Message 1', array('message-1'));
  98. $this->language->setReturnValue('getMessage', 'Message 2', array('message-2'));
  99. $this->language->setReturnValue('formatMessage', 'End Message', array('end-message', array(1 => 'param')));
  100. $this->language->setReturnValue('formatMessage', ' at line 4', array('ErrorCollector: At line', array('line' => 4)));
  101. $this->line = 4;
  102. $this->collector->start();
  103. $this->collector->send(E_WARNING, 'message-1');
  104. $this->collector->send(E_NOTICE, 'message-2');
  105. $this->collector->end(E_NOTICE, 'end-message', 'param');
  106. $expect = array(
  107. 0 => array(4, E_NOTICE, 'End Message', array(
  108. 0 => array(4, E_WARNING, 'Message 1', array()),
  109. 1 => array(4, E_NOTICE, 'Message 2', array()),
  110. )),
  111. );
  112. $result = $this->collector->getRaw();
  113. $this->assertIdentical($result, $expect);
  114. $formatted_expect =
  115. '<ul><li><strong>Notice</strong>: End Message at line 4<ul>'.
  116. '<li><strong>Warning</strong>: Message 1 at line 4</li>'.
  117. '<li><strong>Notice</strong>: Message 2 at line 4</li></ul>'.
  118. '</li></ul>';
  119. $formatted_result = $this->collector->getHTMLFormatted($this->config);
  120. $this->assertIdentical($formatted_result, $formatted_expect);
  121. }
  122. */
  123. }
  124. // vim: et sw=4 sts=4