You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.0KB

  1. <?php
  2. class HTMLPurifier_HTMLModuleManagerTest extends HTMLPurifier_Harness
  3. {
  4. protected function createManager()
  5. {
  6. $manager = new HTMLPurifier_HTMLModuleManager();
  7. $this->config->set('HTML.CustomDoctype', 'Blank');
  8. $manager->doctypes->register('Blank');
  9. $attrdef_nmtokens = new HTMLPurifier_AttrDef_HTML_Nmtokens();
  10. generate_mock_once('HTMLPurifier_AttrDef');
  11. $attrdef = new HTMLPurifier_AttrDefMock();
  12. $attrdef->setReturnValue('make', $attrdef_nmtokens);
  13. $manager->attrTypes->set('NMTOKENS', $attrdef);
  14. return $manager;
  15. }
  16. public function test_addModule()
  17. {
  18. $manager = $this->createManager();
  19. // ...but we add user modules
  20. $common_module = new HTMLPurifier_HTMLModule();
  21. $common_module->name = 'Common';
  22. $common_module->attr_collections['Common'] = array('class' => 'NMTOKENS');
  23. $common_module->content_sets['Flow'] = 'Block | Inline';
  24. $manager->addModule($common_module);
  25. $structural_module = new HTMLPurifier_HTMLModule();
  26. $structural_module->name = 'Structural';
  27. $structural_module->addElement('p', 'Block', 'Inline', 'Common');
  28. $manager->addModule($structural_module);
  29. $formatting_module = new HTMLPurifier_HTMLModule();
  30. $formatting_module->name = 'Formatting';
  31. $formatting_module->addElement('em', 'Inline', 'Inline', 'Common');
  32. $manager->addModule($formatting_module);
  33. $unsafe_module = new HTMLPurifier_HTMLModule();
  34. $unsafe_module->name = 'Unsafe';
  35. $unsafe_module->safe = false;
  36. $unsafe_module->addElement('div', 'Block', 'Flow');
  37. $manager->addModule($unsafe_module);
  38. $config = HTMLPurifier_Config::createDefault();
  39. $config->set('HTML.Trusted', false);
  40. $config->set('HTML.CustomDoctype', 'Blank');
  41. $manager->setup($config);
  42. $attrdef_nmtokens = new HTMLPurifier_AttrDef_HTML_Nmtokens();
  43. $p = new HTMLPurifier_ElementDef();
  44. $p->attr['class'] = $attrdef_nmtokens;
  45. $p->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
  46. $p->content_model = 'em | #PCDATA';
  47. $p->content_model_type = 'optional';
  48. $p->descendants_are_inline = true;
  49. $em = new HTMLPurifier_ElementDef();
  50. $em->attr['class'] = $attrdef_nmtokens;
  51. $em->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
  52. $em->content_model = 'em | #PCDATA';
  53. $em->content_model_type = 'optional';
  54. $em->descendants_are_inline = true;
  55. $this->assertEqual(
  56. array('p' => $p, 'em' => $em),
  57. $manager->getElements()
  58. );
  59. // test trusted parameter override
  60. $div = new HTMLPurifier_ElementDef();
  61. $div->child = new HTMLPurifier_ChildDef_Optional(array('p', 'div', 'em', '#PCDATA'));
  62. $div->content_model = 'p | div | em | #PCDATA';
  63. $div->content_model_type = 'optional';
  64. $div->descendants_are_inline = false;
  65. $this->assertEqual($div, $manager->getElement('div', true));
  66. }
  67. public function testAllowedModules()
  68. {
  69. $manager = new HTMLPurifier_HTMLModuleManager();
  70. $manager->doctypes->register(
  71. 'Fantasy Inventory 1.0', true,
  72. array('Weapons', 'Magic')
  73. );
  74. // register these modules so it doesn't blow up
  75. $weapons_module = new HTMLPurifier_HTMLModule();
  76. $weapons_module->name = 'Weapons';
  77. $manager->registerModule($weapons_module);
  78. $magic_module = new HTMLPurifier_HTMLModule();
  79. $magic_module->name = 'Magic';
  80. $manager->registerModule($magic_module);
  81. $config = HTMLPurifier_Config::create(array(
  82. 'HTML.CustomDoctype' => 'Fantasy Inventory 1.0',
  83. 'HTML.AllowedModules' => 'Weapons'
  84. ));
  85. $manager->setup($config);
  86. $this->assertTrue( isset($manager->modules['Weapons']));
  87. $this->assertFalse(isset($manager->modules['Magic']));
  88. }
  89. }
  90. // vim: et sw=4 sts=4