SECURITY: blacklist CSS var()
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / StripStateTest.php
1 <?php
2
3 /**
4 * @covers StripState
5 */
6 class StripStateTest extends MediaWikiTestCase {
7 public function setUp() {
8 parent::setUp();
9 $this->setContentLang( 'qqx' );
10 }
11
12 private function getMarker() {
13 static $i;
14 return Parser::MARKER_PREFIX . '-blah-' . sprintf( '%08X', $i++ ) . Parser::MARKER_SUFFIX;
15 }
16
17 private static function getWarning( $message, $max = '' ) {
18 return "<span class=\"error\">($message: $max)</span>";
19 }
20
21 public function testAddNoWiki() {
22 $ss = new StripState;
23 $marker = $this->getMarker();
24 $ss->addNoWiki( $marker, '<>' );
25 $text = "x{$marker}y";
26 $text = $ss->unstripGeneral( $text );
27 $text = str_replace( '<', '', $text );
28 $text = $ss->unstripNoWiki( $text );
29 $this->assertSame( 'x<>y', $text );
30 }
31
32 public function testAddGeneral() {
33 $ss = new StripState;
34 $marker = $this->getMarker();
35 $ss->addGeneral( $marker, '<>' );
36 $text = "x{$marker}y";
37 $text = $ss->unstripNoWiki( $text );
38 $text = str_replace( '<', '', $text );
39 $text = $ss->unstripGeneral( $text );
40 $this->assertSame( 'x<>y', $text );
41 }
42
43 public function testUnstripBoth() {
44 $ss = new StripState;
45 $mk1 = $this->getMarker();
46 $mk2 = $this->getMarker();
47 $ss->addNoWiki( $mk1, '<1>' );
48 $ss->addGeneral( $mk2, '<2>' );
49 $text = "x{$mk1}{$mk2}y";
50 $text = str_replace( '<', '', $text );
51 $text = $ss->unstripBoth( $text );
52 $this->assertSame( 'x<1><2>y', $text );
53 }
54
55 public static function provideUnstripRecursive() {
56 return [
57 [ 0, 'text' ],
58 [ 1, '=text=' ],
59 [ 2, '==text==' ],
60 [ 3, '==' . self::getWarning( 'unstrip-depth-warning', 2 ) . '==' ],
61 ];
62 }
63
64 /** @dataProvider provideUnstripRecursive */
65 public function testUnstripRecursive( $depth, $expected ) {
66 $ss = new StripState( null, [ 'depthLimit' => 2 ] );
67 $text = 'text';
68 for ( $i = 0; $i < $depth; $i++ ) {
69 $mk = $this->getMarker();
70 $ss->addNoWiki( $mk, "={$text}=" );
71 $text = $mk;
72 }
73 $text = $ss->unstripNoWiki( $text );
74 $this->assertSame( $expected, $text );
75 }
76
77 public function testUnstripLoop() {
78 $ss = new StripState( null, [ 'depthLimit' => 2 ] );
79 $mk = $this->getMarker();
80 $ss->addNoWiki( $mk, $mk );
81 $text = $ss->unstripNoWiki( $mk );
82 $this->assertSame( self::getWarning( 'parser-unstrip-loop-warning' ), $text );
83 }
84
85 public static function provideUnstripSize() {
86 return [
87 [ 0, 'x' ],
88 [ 1, 'xx' ],
89 [ 2, str_repeat( self::getWarning( 'unstrip-size-warning', 5 ), 2 ) ]
90 ];
91 }
92
93 /** @dataProvider provideUnstripSize */
94 public function testUnstripSize( $depth, $expected ) {
95 $ss = new StripState( null, [ 'sizeLimit' => 5 ] );
96 $text = 'x';
97 for ( $i = 0; $i < $depth; $i++ ) {
98 $mk = $this->getMarker();
99 $ss->addNoWiki( $mk, $text );
100 $text = "$mk$mk";
101 }
102 $text = $ss->unstripNoWiki( $text );
103 $this->assertSame( $expected, $text );
104 }
105
106 public function provideGetLimitReport() {
107 for ( $i = 1; $i < 4; $i++ ) {
108 yield [ $i ];
109 }
110 }
111
112 /** @dataProvider provideGetLimitReport */
113 public function testGetLimitReport( $depth ) {
114 $sizeLimit = 100000;
115 $ss = new StripState( null, [ 'depthLimit' => 5, 'sizeLimit' => $sizeLimit ] );
116 $text = 'x';
117 for ( $i = 0; $i < $depth; $i++ ) {
118 $mk = $this->getMarker();
119 $ss->addNoWiki( $mk, $text );
120 $text = "$mk$mk";
121 }
122 $text = $ss->unstripNoWiki( $text );
123 $report = $ss->getLimitReport();
124 $messages = [];
125 foreach ( $report as list( $msg, $params ) ) {
126 $messages[$msg] = $params;
127 }
128 $this->assertSame( [ $depth - 1, 5 ], $messages['limitreport-unstrip-depth'] );
129 $this->assertSame(
130 [
131 strlen( $this->getMarker() ) * 2 * ( pow( 2, $depth ) - 2 ) + pow( 2, $depth ),
132 $sizeLimit
133 ],
134 $messages['limitreport-unstrip-size' ] );
135 }
136 }