AttrValidator_ErrorsTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. class HTMLPurifier_AttrValidator_ErrorsTest extends HTMLPurifier_ErrorsHarness
  3. {
  4. /**
  5. * @type HTMLPurifier_Language
  6. */
  7. public $language;
  8. public function setup()
  9. {
  10. parent::setup();
  11. $config = HTMLPurifier_Config::createDefault();
  12. $this->language = HTMLPurifier_LanguageFactory::instance()->create($config, $this->context);
  13. $this->context->register('Locale', $this->language);
  14. $this->collector = new HTMLPurifier_ErrorCollector($this->context);
  15. $gen = new HTMLPurifier_Generator($config, $this->context);
  16. $this->context->register('Generator', $gen);
  17. }
  18. protected function invoke($input)
  19. {
  20. $validator = new HTMLPurifier_AttrValidator();
  21. $validator->validateToken($input, $this->config, $this->context);
  22. }
  23. public function testAttributesTransformedGlobalPre()
  24. {
  25. $def = $this->config->getHTMLDefinition(true);
  26. generate_mock_once('HTMLPurifier_AttrTransform');
  27. $transform = new HTMLPurifier_AttrTransformMock();
  28. $input = array('original' => 'value');
  29. $output = array('class' => 'value'); // must be valid
  30. $transform->returns('transform', $output, array($input, new AnythingExpectation(), new AnythingExpectation()));
  31. $def->info_attr_transform_pre[] = $transform;
  32. $token = new HTMLPurifier_Token_Start('span', $input, 1);
  33. $this->invoke($token);
  34. $result = $this->collector->getRaw();
  35. $expect = array(
  36. array(1, E_NOTICE, 'Attributes on <span> transformed from original to class', array()),
  37. );
  38. $this->assertIdentical($result, $expect);
  39. }
  40. public function testAttributesTransformedLocalPre()
  41. {
  42. $this->config->set('HTML.TidyLevel', 'heavy');
  43. $input = array('align' => 'right');
  44. $output = array('style' => 'text-align:right;');
  45. $token = new HTMLPurifier_Token_Start('p', $input, 1);
  46. $this->invoke($token);
  47. $result = $this->collector->getRaw();
  48. $expect = array(
  49. array(1, E_NOTICE, 'Attributes on <p> transformed from align to style', array()),
  50. );
  51. $this->assertIdentical($result, $expect);
  52. }
  53. // too lazy to check for global post and global pre
  54. public function testAttributeRemoved()
  55. {
  56. $token = new HTMLPurifier_Token_Start('p', array('foobar' => 'right'), 1);
  57. $this->invoke($token);
  58. $result = $this->collector->getRaw();
  59. $expect = array(
  60. array(1, E_ERROR, 'foobar attribute on <p> removed', array()),
  61. );
  62. $this->assertIdentical($result, $expect);
  63. }
  64. }
  65. // vim: et sw=4 sts=4