HTMLModuleManagerTest.php 4.0 KB

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