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.

112 line
3.4KB

  1. <?php
  2. /**
  3. * Special test-case for cases that can't be tested using
  4. * HTMLPurifier_ConfigSchema_ValidatorTestCase.
  5. */
  6. class HTMLPurifier_ConfigSchema_ValidatorTest extends UnitTestCase
  7. {
  8. public $validator, $interchange;
  9. public function setup()
  10. {
  11. $this->validator = new HTMLPurifier_ConfigSchema_Validator();
  12. $this->interchange = new HTMLPurifier_ConfigSchema_Interchange();
  13. }
  14. public function testDirectiveIntegrityViolation()
  15. {
  16. $d = $this->makeDirective('Ns.Dir');
  17. $d->id = new HTMLPurifier_ConfigSchema_Interchange_Id('Ns.Dir2');
  18. $this->expectValidationException("Integrity violation: key 'Ns.Dir' does not match internal id 'Ns.Dir2'");
  19. $this->validator->validate($this->interchange);
  20. }
  21. public function testDirectiveTypeNotEmpty()
  22. {
  23. $d = $this->makeDirective('Ns.Dir');
  24. $d->default = 0;
  25. $d->description = 'Description';
  26. $this->expectValidationException("Type in directive 'Ns.Dir' must not be empty");
  27. $this->validator->validate($this->interchange);
  28. }
  29. public function testDirectiveDefaultInvalid()
  30. {
  31. $d = $this->makeDirective('Ns.Dir');
  32. $d->default = 'asdf';
  33. $d->type = 'int';
  34. $d->description = 'Description';
  35. $this->expectValidationException("Default in directive 'Ns.Dir' had error: Expected type int, got string");
  36. $this->validator->validate($this->interchange);
  37. }
  38. public function testDirectiveIdIsString()
  39. {
  40. $d = $this->makeDirective(3);
  41. $d->default = 0;
  42. $d->type = 'int';
  43. $d->description = 'Description';
  44. $this->expectValidationException("Key in id '3' in directive '3' must be a string");
  45. $this->validator->validate($this->interchange);
  46. }
  47. public function testDirectiveTypeAllowsNullIsBool()
  48. {
  49. $d = $this->makeDirective('Ns.Dir');
  50. $d->default = 0;
  51. $d->type = 'int';
  52. $d->description = 'Description';
  53. $d->typeAllowsNull = 'yes';
  54. $this->expectValidationException("TypeAllowsNull in directive 'Ns.Dir' must be a boolean");
  55. $this->validator->validate($this->interchange);
  56. }
  57. public function testDirectiveValueAliasesIsArray()
  58. {
  59. $d = $this->makeDirective('Ns.Dir');
  60. $d->default = 'a';
  61. $d->type = 'string';
  62. $d->description = 'Description';
  63. $d->valueAliases = 2;
  64. $this->expectValidationException("ValueAliases in directive 'Ns.Dir' must be an array");
  65. $this->validator->validate($this->interchange);
  66. }
  67. public function testDirectiveAllowedIsLookup()
  68. {
  69. $d = $this->makeDirective('Ns.Dir');
  70. $d->default = 'foo';
  71. $d->type = 'string';
  72. $d->description = 'Description';
  73. $d->allowed = array('foo' => 1);
  74. $this->expectValidationException("Allowed in directive 'Ns.Dir' must be a lookup array");
  75. $this->validator->validate($this->interchange);
  76. }
  77. // helper functions
  78. protected function makeDirective($key)
  79. {
  80. $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
  81. $directive->id = new HTMLPurifier_ConfigSchema_Interchange_Id($key);
  82. $this->interchange->addDirective($directive);
  83. return $directive;
  84. }
  85. protected function expectValidationException($msg)
  86. {
  87. $this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg));
  88. }
  89. }
  90. // vim: et sw=4 sts=4