aboutsummaryrefslogtreecommitdiffstats
path: root/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorTest.php
blob: 9cbf36e2da12d5954de5846e112039995b197932 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

/**
 * Special test-case for cases that can't be tested using
 * HTMLPurifier_ConfigSchema_ValidatorTestCase.
 */
class HTMLPurifier_ConfigSchema_ValidatorTest extends UnitTestCase
{
    public $validator, $interchange;

    public function setup() {
        $this->validator = new HTMLPurifier_ConfigSchema_Validator();
        $this->interchange = new HTMLPurifier_ConfigSchema_Interchange();
    }

    function testDirectiveIntegrityViolation() {
        $d = $this->makeDirective('Ns.Dir');
        $d->id = new HTMLPurifier_ConfigSchema_Interchange_Id('Ns.Dir2');
        $this->expectValidationException("Integrity violation: key 'Ns.Dir' does not match internal id 'Ns.Dir2'");
        $this->validator->validate($this->interchange);
    }

    function testDirectiveTypeNotEmpty() {
        $d = $this->makeDirective('Ns.Dir');
        $d->default = 0;
        $d->description = 'Description';

        $this->expectValidationException("Type in directive 'Ns.Dir' must not be empty");
        $this->validator->validate($this->interchange);
    }

    function testDirectiveDefaultInvalid() {
        $d = $this->makeDirective('Ns.Dir');
        $d->default = 'asdf';
        $d->type = 'int';
        $d->description = 'Description';

        $this->expectValidationException("Default in directive 'Ns.Dir' had error: Expected type int, got string");
        $this->validator->validate($this->interchange);
    }

    function testDirectiveIdIsString() {
        $d = $this->makeDirective(3);
        $d->default = 0;
        $d->type = 'int';
        $d->description = 'Description';

        $this->expectValidationException("Key in id '3' in directive '3' must be a string");
        $this->validator->validate($this->interchange);
    }

    function testDirectiveTypeAllowsNullIsBool() {
        $d = $this->makeDirective('Ns.Dir');
        $d->default = 0;
        $d->type = 'int';
        $d->description = 'Description';
        $d->typeAllowsNull = 'yes';

        $this->expectValidationException("TypeAllowsNull in directive 'Ns.Dir' must be a boolean");
        $this->validator->validate($this->interchange);
    }

    function testDirectiveValueAliasesIsArray() {
        $d = $this->makeDirective('Ns.Dir');
        $d->default = 'a';
        $d->type = 'string';
        $d->description = 'Description';
        $d->valueAliases = 2;

        $this->expectValidationException("ValueAliases in directive 'Ns.Dir' must be an array");
        $this->validator->validate($this->interchange);
    }

    function testDirectiveAllowedIsLookup() {
        $d = $this->makeDirective('Ns.Dir');
        $d->default = 'foo';
        $d->type = 'string';
        $d->description = 'Description';
        $d->allowed = array('foo' => 1);

        $this->expectValidationException("Allowed in directive 'Ns.Dir' must be a lookup array");
        $this->validator->validate($this->interchange);
    }

    // helper functions


    protected function makeDirective($key) {
        $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
        $directive->id = new HTMLPurifier_ConfigSchema_Interchange_Id($key);
        $this->interchange->addDirective($directive);
        return $directive;
    }

    protected function expectValidationException($msg) {
        $this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg));
    }

}

// vim: et sw=4 sts=4