summaryrefslogtreecommitdiff
path: root/_test/cases/inc/html_hilight.test.php
blob: cc5579c7104d685ca47bb923a363b864df6a167d (plain)
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
<?php

require_once DOKU_INC.'inc/html.php';

if (!extension_loaded('runkit')) {
    SimpleTestOptions::ignore('html_hilight_test');
    trigger_error('Skipping html_hilight_test - http://www.php.net/runkit required');
}

function html_hilight_test_unslash($string,$char="'"){
  $str= str_replace('\\'.$char,$char,$string);
  return $str;
}

class html_hilight_test extends UnitTestCase{

  function setup() {
    if ( function_exists('unslash') ) {
        runkit_function_rename('unslash','html_hilight_test_unslash_real');
    }
    runkit_function_rename('html_hilight_test_unslash','unslash');
  }

  function teardown() {
    runkit_function_rename('unslash','html_hilight_test_unslash');
    if ( function_exists('html_hilight_test_unslash_real') ) {
        runkit_function_rename('html_hilight_test_unslash_real','unslash');
    }
  }

  function testHighlightOneWord() {
    $html = 'Foo bar Foo';
    $this->assertPattern(
      '/Foo <span.*>bar<\/span> Foo/',
      html_hilight($html,'bar')
      );
  }

  function testHighlightTwoWords() {
    $html = 'Foo bar Foo php Foo';
    $this->assertPattern(
      '/Foo <span.*>bar<\/span> Foo <span.*>php<\/span> Foo/',
      html_hilight($html,array('bar','php'))
      );
  }

  function testHighlightTwoWordsHtml() {
    $html = 'Foo <b>bar</b> <i>Foo</i> php Foo';
    $this->assertPattern(
      '/Foo <b><span.*>bar<\/span><\/b> <i>Foo<\/i> <span.*>php<\/span> Foo/',
      html_hilight($html,array('bar','php'))
      );
  }

  function testNoHighlight() {
    $html = 'Foo bar Foo';
    $this->assertPattern(
      '/Foo bar Foo/',
      html_hilight($html,'php')
      );
  }

  function testHighlightPHP() {
    $html = 'Foo $_GET[\'bar\'] Foo';
    $this->assertEqual(
      'Foo <span class="search_hit">$_GET[\'bar\']</span> Foo',
      html_hilight($html,'$_GET[\'bar\']')
      );
  }

  function testMatchAttribute() {
    $html = 'Foo <b class="x">bar</b> Foo';
    $this->assertPattern(
      '/Foo <b class="x">bar<\/b> Foo/',
      html_hilight($html,'class="x"')
      );
  }

  function testMatchAttributeWord() {
    $html = 'Foo <b class="x">bar</b> Foo';
    $this->assertEqual(
      'Foo <b class="x">bar</b> Foo',
      html_hilight($html,'class="x">bar')
      );
  }

  function testRegexInjection() {
    $html = 'Foo bar Foo';
    $this->assertPattern(
      '/Foo bar Foo/',
      html_hilight($html,'*')
      );
  }

  function testRegexInjectionSlash() {
    $html = 'Foo bar Foo';
    $this->assertPattern(
      '/Foo bar Foo/',
      html_hilight($html,'x/')
      );
  }

}