Debian Codegraph @

tests/phpunit/unit/includes/GlobalFunctions/WfEscapeWikiTextTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * @group GlobalFunctions
5 * @covers ::wfEscapeWikiText
6 */
7 class WfEscapeWikiTextTest extends MediaWikiUnitTestCase {
8 /**
9 * @dataProvider provideEscape
10 */
11 public function testEscape( $input, $expected ) {
12 // save global
13 global $wgEnableMagicLinks;
14 $old = $wgEnableMagicLinks;
15 $wgEnableMagicLinks = [];
16
17 try {
18 $actual = wfEscapeWikiText( $input );
19 // Sanity check that the output can be decoded back to the input
20 // input as well.
21 $decoded = html_entity_decode( $actual, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5 );
22 $this->assertEquals( $decoded, (string)$input );
23 // And that the output was what we expected
24 $this->assertEquals( $expected, $actual );
25 } finally {
26 // restore global
27 $wgEnableMagicLinks = $old;
28 }
29 }
30
31 public function provideEscape() {
32 return [
33 'null' => [
34 null,
35 '',
36 ],
37 'false' => [
38 false,
39 '',
40 ],
41 'empty string' => [
42 '',
43 '',
44 ],
45 'no escapes' => [
46 'a',
47 'a',
48 ],
49 'braces and brackets' => [
50 '[[WikiLink]] {{Template}} <html>',
51 '&#91;&#91;WikiLink&#93;&#93; &#123;&#123;Template&#125;&#125; &#60;html&#62;',
52 ],
53 'quotes' => [
54 '"\'',
55 '&#34;&#39;',
56 ],
57 'tokens' => [
58 '{| |- |+ !! ~~~~~ __FOO__',
59 '&#123;&#124; &#124;- &#124;+ &#33;! ~~&#126;~~ _&#95;FOO_&#95;',
60 ],
61 'start of line' => [
62 "* foo\n! bar\n# bat\n:baz\n pre\n----",
63 "&#42; foo\n&#33; bar\n&#35; bat\n&#58;baz\n&#32;pre\n&#45;---",
64 ],
65 'paragraph separators' => [
66 "a\n\n\n\nb",
67 "a\n&#10;\n&#10;b",
68 ],
69 'language converter' => [
70 '-{ foo ; bar }-',
71 '&#45;&#123; foo &#59; bar &#125;-',
72 ],
73 'left-side context: |+' => [
74 '+ foo + bar',
75 '&#43; foo + bar',
76 ],
77 'left-side context: |-' => [
78 '- foo - bar',
79 '&#45; foo - bar',
80 ],
81 'left-side context: __FOO__' => [
82 '_FOO__',
83 '&#95;FOO_&#95;',
84 ],
85 'left-side context: ~~~' => [
86 '~~ long string here',
87 '&#126;~ long string here',
88 ],
89 'left-side context: newlines' => [
90 "\n\n\nFoo",
91 "&#10;\n&#10;Foo",
92 ],
93 'right-side context: ~~~' => [
94 'long string here ~~',
95 'long string here ~&#126;',
96 ],
97 'right-side context: __FOO__' => [
98 '__FOO_',
99 '&#95;&#95;FOO&#95;',
100 ],
101 'right-side context: newlines' => [
102 "foo\n\n\n",
103 "foo\n&#10;&#10;",
104 ],
105 // A single character input needs to be protected against both
106 // left-side context and right-side context.
107 'both-side context: +' => [ // | + + (left side)
108 '+',
109 '&#43;',
110 ],
111 'both-side context: -' => [ // | + - (left side)
112 '-',
113 '&#45;',
114 ],
115 'both-side context: _' => [ // _ + _FOO as well as __FOO_ + _
116 '_',
117 '&#95;',
118 ],
119 'both-side context: ~' => [ // ~ + ~~ as well as ~~ + ~
120 '~',
121 '&#126;',
122 ],
123 'both-side context: \\n' => [ // \n + \n
124 "\n",
125 '&#10;',
126 ],
127 'both-side context: \\t' => [ // \n + \t + \n becomes paragraph break
128 "\t",
129 '&#9;',
130 ],
131 ];
132 }
133 }