1<?php
2
3class html_hilight_test extends DokuWikiTest {
4
5    function testHighlightOneWord() {
6        $html = 'Foo bar Foo';
7        $this->assertMatchesRegularExpression(
8            '/Foo <span.*>bar<\/span> Foo/',
9            html_hilight($html,'bar')
10        );
11    }
12
13    function testHighlightTwoWords() {
14        $html = 'Foo bar Foo php Foo';
15        $this->assertMatchesRegularExpression(
16            '/Foo <span.*>bar<\/span> Foo <span.*>php<\/span> Foo/',
17            html_hilight($html,array('bar','php'))
18        );
19    }
20
21    function testHighlightTwoWordsHtml() {
22        $html = 'Foo <b>bar</b> <i>Foo</i> php Foo';
23        $this->assertMatchesRegularExpression(
24            '/Foo <b><span.*>bar<\/span><\/b> <i>Foo<\/i> <span.*>php<\/span> Foo/',
25            html_hilight($html,array('bar','php'))
26        );
27    }
28
29    function testNoMatchHtml() {
30        $html = 'Foo <font>font</font> Bar';
31        $this->assertMatchesRegularExpression(
32            '/Foo <font><span.*>font<\/span><\/font> Bar/',
33            html_hilight($html,'font')
34        );
35    }
36
37    function testWildcardRight() {
38        $html = 'foo bar foobar barfoo foobarfoo foo';
39        $this->assertMatchesRegularExpression(
40            '/foo <span.*>bar<\/span> foobar <span.*>bar<\/span>foo foobarfoo foo/',
41            html_hilight($html,'bar*')
42        );
43    }
44
45    function testWildcardLeft() {
46        $html = 'foo bar foobar barfoo foobarfoo foo';
47        $this->assertMatchesRegularExpression(
48            '/foo <span.*>bar<\/span> foo<span.*>bar<\/span> barfoo foobarfoo foo/',
49            html_hilight($html,'*bar')
50        );
51    }
52
53    function testWildcardBoth() {
54        $html = 'foo bar foobar barfoo foobarfoo foo';
55        $this->assertMatchesRegularExpression(
56            '/foo <span.*>bar<\/span> foo<span.*>bar<\/span> <span.*>bar<\/span>foo foo<span.*>bar<\/span>foo foo/',
57            html_hilight($html,'*bar*')
58        );
59    }
60
61    function testNoHighlight() {
62        $html = 'Foo bar Foo';
63        $this->assertMatchesRegularExpression(
64            '/Foo bar Foo/',
65            html_hilight($html,'php')
66        );
67    }
68
69    function testMatchAttribute() {
70        $html = 'Foo <b class="x">bar</b> Foo';
71        $this->assertMatchesRegularExpression(
72            '/Foo <b class="x">bar<\/b> Foo/',
73            html_hilight($html,'class="x"')
74        );
75    }
76
77    function testMatchAttributeWord() {
78        $html = 'Foo <b class="x">bar</b> Foo';
79        $this->assertEquals(
80            'Foo <b class="x">bar</b> Foo',
81            html_hilight($html,'class="x">bar')
82        );
83    }
84
85    function testRegexInjection() {
86        $html = 'Foo bar Foo';
87        $this->assertMatchesRegularExpression(
88            '/Foo bar Foo/',
89            html_hilight($html,'*')
90        );
91    }
92
93    function testRegexInjectionSlash() {
94        $html = 'Foo bar Foo';
95        $this->assertMatchesRegularExpression(
96            '/Foo bar Foo/',
97            html_hilight($html,'x/')
98        );
99    }
100
101    function testMB() {
102        $html = 'foo ДокуВики bar';
103        $this->assertMatchesRegularExpression(
104            '/foo <span.*>ДокуВики<\/span> bar/',
105            html_hilight($html,'ДокуВики')
106        );
107    }
108
109    function testMBright() {
110        $html = 'foo ДокуВики bar';
111        $this->assertMatchesRegularExpression(
112            '/foo <span.*>Доку<\/span>Вики bar/',
113            html_hilight($html,'Доку*')
114        );
115    }
116
117    function testMBleft() {
118        $html = 'foo ДокуВики bar';
119        $this->assertMatchesRegularExpression(
120            '/foo Доку<span.*>Вики<\/span> bar/',
121            html_hilight($html,'*Вики')
122        );
123    }
124
125    function testMBboth() {
126        $html = 'foo ДокуВики bar';
127        $this->assertMatchesRegularExpression(
128            '/foo До<span.*>куВи<\/span>ки bar/',
129            html_hilight($html,'*куВи*')
130        );
131    }
132}
133