ValidatorTest.php 3.3 KB

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