aboutsummaryrefslogtreecommitdiffstats
path: root/lib/htmlpurifier/tests/HTMLPurifier/ErrorCollectorTest.php
blob: 09becba53d41b14979a378000f5eb07da3570064 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php

/**
 * @warning HTML output is in flux, but eventually needs to be stabilized.
 */
class HTMLPurifier_ErrorCollectorTest extends HTMLPurifier_Harness
{

    protected $language, $generator, $line;
    protected $collector;

    public function setup() {
        generate_mock_once('HTMLPurifier_Language');
        generate_mock_once('HTMLPurifier_Generator');
        parent::setup();
        $this->language  = new HTMLPurifier_LanguageMock();
        $this->language->setReturnValue('getErrorName',  'Error',   array(E_ERROR));
        $this->language->setReturnValue('getErrorName',  'Warning', array(E_WARNING));
        $this->language->setReturnValue('getErrorName',  'Notice',  array(E_NOTICE));
        // this might prove to be troublesome if we need to set config
        $this->generator = new HTMLPurifier_Generator($this->config, $this->context);
        $this->line = false;
        $this->context->register('Locale', $this->language);
        $this->context->register('CurrentLine', $this->line);
        $this->context->register('Generator', $this->generator);
        $this->collector = new HTMLPurifier_ErrorCollector($this->context);
    }

    function test() {

        $language = $this->language;
        $language->setReturnValue('getMessage',    'Message 1',   array('message-1'));
        $language->setReturnValue('formatMessage', 'Message 2',   array('message-2', array(1 => 'param')));
        $language->setReturnValue('formatMessage', ' at line 23', array('ErrorCollector: At line', array('line' => 23)));
        $language->setReturnValue('formatMessage', ' at line 3',  array('ErrorCollector: At line', array('line' => 3)));

        $this->line = 23;
        $this->collector->send(E_ERROR, 'message-1');

        $this->line = 3;
        $this->collector->send(E_WARNING, 'message-2', 'param');

        $result = array(
            0 => array(23, E_ERROR, 'Message 1', array()),
            1 => array(3, E_WARNING, 'Message 2', array())
        );

        $this->assertIdentical($this->collector->getRaw(), $result);

        /*
        $formatted_result =
            '<ul><li><strong>Warning</strong>: Message 2 at line 3</li>'.
            '<li><strong>Error</strong>: Message 1 at line 23</li></ul>';

        $this->assertIdentical($this->collector->getHTMLFormatted($this->config), $formatted_result);
        */

    }

    function testNoErrors() {
        $this->language->setReturnValue('getMessage', 'No errors', array('ErrorCollector: No errors'));

        $formatted_result = '<p>No errors</p>';
        $this->assertIdentical(
            $this->collector->getHTMLFormatted($this->config),
            $formatted_result
        );
    }

    function testNoLineNumbers() {
        $this->language->setReturnValue('getMessage', 'Message 1', array('message-1'));
        $this->language->setReturnValue('getMessage', 'Message 2', array('message-2'));

        $this->collector->send(E_ERROR, 'message-1');
        $this->collector->send(E_ERROR, 'message-2');

        $result = array(
            0 => array(false, E_ERROR, 'Message 1', array()),
            1 => array(false, E_ERROR, 'Message 2', array())
        );
        $this->assertIdentical($this->collector->getRaw(), $result);

        /*
        $formatted_result =
            '<ul><li><strong>Error</strong>: Message 1</li>'.
            '<li><strong>Error</strong>: Message 2</li></ul>';
        $this->assertIdentical($this->collector->getHTMLFormatted($this->config), $formatted_result);
        */
    }

    function testContextSubstitutions() {

        $current_token = false;
        $this->context->register('CurrentToken', $current_token);

        // 0
        $current_token = new HTMLPurifier_Token_Start('a', array('href' => 'http://example.com'), 32);
        $this->language->setReturnValue('formatMessage', 'Token message',
          array('message-data-token', array('CurrentToken' => $current_token)));
        $this->collector->send(E_NOTICE, 'message-data-token');

        $current_attr  = 'href';
        $this->language->setReturnValue('formatMessage', '$CurrentAttr.Name => $CurrentAttr.Value',
          array('message-attr', array('CurrentToken' => $current_token)));

        // 1
        $this->collector->send(E_NOTICE, 'message-attr'); // test when context isn't available

        // 2
        $this->context->register('CurrentAttr', $current_attr);
        $this->collector->send(E_NOTICE, 'message-attr');

        $result = array(
            0 => array(32, E_NOTICE, 'Token message', array()),
            1 => array(32, E_NOTICE, '$CurrentAttr.Name => $CurrentAttr.Value', array()),
            2 => array(32, E_NOTICE, 'href => http://example.com', array())
        );
        $this->assertIdentical($this->collector->getRaw(), $result);

    }

    /*
    function testNestedErrors() {
        $this->language->setReturnValue('getMessage', 'Message 1',   array('message-1'));
        $this->language->setReturnValue('getMessage', 'Message 2',   array('message-2'));
        $this->language->setReturnValue('formatMessage', 'End Message', array('end-message', array(1 => 'param')));
        $this->language->setReturnValue('formatMessage', ' at line 4', array('ErrorCollector: At line', array('line' => 4)));

        $this->line = 4;
        $this->collector->start();
        $this->collector->send(E_WARNING, 'message-1');
        $this->collector->send(E_NOTICE,  'message-2');
        $this->collector->end(E_NOTICE, 'end-message', 'param');

        $expect = array(
            0 => array(4, E_NOTICE, 'End Message', array(
                0 => array(4, E_WARNING, 'Message 1', array()),
                1 => array(4, E_NOTICE,  'Message 2', array()),
            )),
        );
        $result = $this->collector->getRaw();
        $this->assertIdentical($result, $expect);

        $formatted_expect =
            '<ul><li><strong>Notice</strong>: End Message at line 4<ul>'.
                '<li><strong>Warning</strong>: Message 1 at line 4</li>'.
                '<li><strong>Notice</strong>: Message 2 at line 4</li></ul>'.
            '</li></ul>';
        $formatted_result = $this->collector->getHTMLFormatted($this->config);
        $this->assertIdentical($formatted_result, $formatted_expect);

    }
    */

}

// vim: et sw=4 sts=4