IDTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. class HTMLPurifier_AttrDef_HTML_IDTest extends HTMLPurifier_AttrDefHarness
  3. {
  4. function setUp() {
  5. parent::setUp();
  6. $id_accumulator = new HTMLPurifier_IDAccumulator();
  7. $this->context->register('IDAccumulator', $id_accumulator);
  8. $this->config->set('Attr.EnableID', true);
  9. $this->def = new HTMLPurifier_AttrDef_HTML_ID();
  10. }
  11. function test() {
  12. // valid ID names
  13. $this->assertDef('alpha');
  14. $this->assertDef('al_ha');
  15. $this->assertDef('a0-:.');
  16. $this->assertDef('a');
  17. // invalid ID names
  18. $this->assertDef('<asa', false);
  19. $this->assertDef('0123', false);
  20. $this->assertDef('.asa', false);
  21. // test duplicate detection
  22. $this->assertDef('once');
  23. $this->assertDef('once', false);
  24. // valid once whitespace stripped, but needs to be amended
  25. $this->assertDef(' whee ', 'whee');
  26. }
  27. function testPrefix() {
  28. $this->config->set('Attr.IDPrefix', 'user_');
  29. $this->assertDef('alpha', 'user_alpha');
  30. $this->assertDef('<asa', false);
  31. $this->assertDef('once', 'user_once');
  32. $this->assertDef('once', false);
  33. // if already prefixed, leave alone
  34. $this->assertDef('user_alas');
  35. $this->assertDef('user_user_alas'); // how to bypass
  36. }
  37. function testTwoPrefixes() {
  38. $this->config->set('Attr.IDPrefix', 'user_');
  39. $this->config->set('Attr.IDPrefixLocal', 'story95_');
  40. $this->assertDef('alpha', 'user_story95_alpha');
  41. $this->assertDef('<asa', false);
  42. $this->assertDef('once', 'user_story95_once');
  43. $this->assertDef('once', false);
  44. $this->assertDef('user_story95_alas');
  45. $this->assertDef('user_alas', 'user_story95_user_alas'); // !
  46. }
  47. function testLocalPrefixWithoutMainPrefix() {
  48. // no effect when IDPrefix isn't set
  49. $this->config->set('Attr.IDPrefix', '');
  50. $this->config->set('Attr.IDPrefixLocal', 'story95_');
  51. $this->expectError('%Attr.IDPrefixLocal cannot be used unless '.
  52. '%Attr.IDPrefix is set');
  53. $this->assertDef('amherst');
  54. }
  55. // reference functionality is disabled for now
  56. function disabled_testIDReference() {
  57. $this->def = new HTMLPurifier_AttrDef_HTML_ID(true);
  58. $this->assertDef('good_id');
  59. $this->assertDef('good_id'); // duplicates okay
  60. $this->assertDef('<b>', false);
  61. $this->def = new HTMLPurifier_AttrDef_HTML_ID();
  62. $this->assertDef('good_id');
  63. $this->assertDef('good_id', false); // duplicate now not okay
  64. $this->def = new HTMLPurifier_AttrDef_HTML_ID(true);
  65. $this->assertDef('good_id'); // reference still okay
  66. }
  67. function testRegexp() {
  68. $this->config->set('Attr.IDBlacklistRegexp', '/^g_/');
  69. $this->assertDef('good_id');
  70. $this->assertDef('g_bad_id', false);
  71. }
  72. }
  73. // vim: et sw=4 sts=4