選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ErrorCollectorTest.php 6.2KB

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