File: WfEscapeWikiTextTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (133 lines) | stat: -rw-r--r-- 2,976 bytes parent folder | download
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
<?php

/**
 * @group GlobalFunctions
 * @covers ::wfEscapeWikiText
 */
class WfEscapeWikiTextTest extends MediaWikiUnitTestCase {
	/**
	 * @dataProvider provideEscape
	 */
	public function testEscape( $input, $expected ) {
		// save global
		global $wgEnableMagicLinks;
		$old = $wgEnableMagicLinks;
		$wgEnableMagicLinks = [];

		try {
			$actual = wfEscapeWikiText( $input );
			// Sanity check that the output can be decoded back to the input
			// input as well.
			$decoded = html_entity_decode( $actual, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5 );
			$this->assertEquals( $decoded, (string)$input );
			// And that the output was what we expected
			$this->assertEquals( $expected, $actual );
		} finally {
			// restore global
			$wgEnableMagicLinks = $old;
		}
	}

	public function provideEscape() {
		return [
			'null' => [
				null,
				'',
			],
			'false' => [
				false,
				'',
			],
			'empty string' => [
				'',
				'',
			],
			'no escapes' => [
				'a',
				'a',
			],
			'braces and brackets' => [
				'[[WikiLink]] {{Template}} <html>',
				'&#91;&#91;WikiLink&#93;&#93; &#123;&#123;Template&#125;&#125; &#60;html&#62;',
			],
			'quotes' => [
				'"\'',
				'&#34;&#39;',
			],
			'tokens' => [
				'{| |- |+ !! ~~~~~ __FOO__',
				'&#123;&#124; &#124;- &#124;+ &#33;! ~~&#126;~~ _&#95;FOO_&#95;',
			],
			'start of line' => [
				"* foo\n! bar\n# bat\n:baz\n pre\n----",
				"&#42; foo\n&#33; bar\n&#35; bat\n&#58;baz\n&#32;pre\n&#45;---",
			],
			'paragraph separators' => [
				"a\n\n\n\nb",
				"a\n&#10;\n&#10;b",
			],
			'language converter' => [
				'-{ foo ; bar }-',
				'&#45;&#123; foo &#59; bar &#125;-',
			],
			'left-side context: |+' => [
				'+ foo + bar',
				'&#43; foo + bar',
			],
			'left-side context: |-' => [
				'- foo - bar',
				'&#45; foo - bar',
			],
			'left-side context: __FOO__' => [
				'_FOO__',
				'&#95;FOO_&#95;',
			],
			'left-side context: ~~~' => [
				'~~ long string here',
				'&#126;~ long string here',
			],
			'left-side context: newlines' => [
				"\n\n\nFoo",
				"&#10;\n&#10;Foo",
			],
			'right-side context: ~~~' => [
				'long string here ~~',
				'long string here ~&#126;',
			],
			'right-side context: __FOO__' => [
				'__FOO_',
				'&#95;&#95;FOO&#95;',
			],
			'right-side context: newlines' => [
				"foo\n\n\n",
				"foo\n&#10;&#10;",
			],
			// A single character input needs to be protected against both
			// left-side context and right-side context.
			'both-side context: +' => [ // | + + (left side)
				'+',
				'&#43;',
			],
			'both-side context: -' => [ // | + - (left side)
				'-',
				'&#45;',
			],
			'both-side context: _' => [ // _ + _FOO as well as __FOO_ + _
				'_',
				'&#95;',
			],
			'both-side context: ~' => [ // ~ + ~~ as well as ~~ + ~
				'~',
				'&#126;',
			],
			'both-side context: \\n' => [ // \n + \n
				"\n",
				'&#10;',
			],
			'both-side context: \\t' => [ // \n + \t + \n becomes paragraph break
				"\t",
				'&#9;',
			],
		];
	}
}