| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use MediaWiki\Parser\Parser; |
| 6 |
use MediaWiki\Parser\StripState; |
| 7 |
use MediaWikiIntegrationTestCase; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class StripStateTest extends MediaWikiIntegrationTestCase { |
| 13 |
protected function setUp(): void { |
| 14 |
parent::setUp(); |
| 15 |
$this->setContentLang( 'qqx' ); |
| 16 |
} |
| 17 |
|
| 18 |
private function getMarker() { |
| 19 |
static $i; |
| 20 |
return Parser::MARKER_PREFIX . '-blah-' . sprintf( '%08X', $i++ ) . Parser::MARKER_SUFFIX; |
| 21 |
} |
| 22 |
|
| 23 |
private static function getWarning( $message, $max = '' ) { |
| 24 |
return "<span class=\"error\">($message: $max)</span>"; |
| 25 |
} |
| 26 |
|
| 27 |
public function testAddNoWiki() { |
| 28 |
$ss = new StripState; |
| 29 |
$marker = $this->getMarker(); |
| 30 |
$ss->addNoWiki( $marker, '<>' ); |
| 31 |
$text = "x{$marker}y"; |
| 32 |
$text = $ss->unstripGeneral( $text ); |
| 33 |
$text = str_replace( '<', '', $text ); |
| 34 |
$text = $ss->unstripNoWiki( $text ); |
| 35 |
$this->assertSame( 'x<>y', $text ); |
| 36 |
} |
| 37 |
|
| 38 |
public function testAddGeneral() { |
| 39 |
$ss = new StripState; |
| 40 |
$marker = $this->getMarker(); |
| 41 |
$ss->addGeneral( $marker, '<>' ); |
| 42 |
$text = "x{$marker}y"; |
| 43 |
$text = $ss->unstripNoWiki( $text ); |
| 44 |
$text = str_replace( '<', '', $text ); |
| 45 |
$text = $ss->unstripGeneral( $text ); |
| 46 |
$this->assertSame( 'x<>y', $text ); |
| 47 |
} |
| 48 |
|
| 49 |
public function testUnstripBoth() { |
| 50 |
$ss = new StripState; |
| 51 |
$mk1 = $this->getMarker(); |
| 52 |
$mk2 = $this->getMarker(); |
| 53 |
$ss->addNoWiki( $mk1, '<1>' ); |
| 54 |
$ss->addGeneral( $mk2, '<2>' ); |
| 55 |
$text = "x{$mk1}{$mk2}y"; |
| 56 |
$text = str_replace( '<', '', $text ); |
| 57 |
$text = $ss->unstripBoth( $text ); |
| 58 |
$this->assertSame( 'x<1><2>y', $text ); |
| 59 |
} |
| 60 |
|
| 61 |
public static function provideUnstripRecursive() { |
| 62 |
return [ |
| 63 |
[ 0, 'text' ], |
| 64 |
[ 1, '=text=' ], |
| 65 |
[ 2, '==text==' ], |
| 66 |
[ 3, '==' . self::getWarning( 'unstrip-depth-warning', 2 ) . '==' ], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public function testUnstripRecursive( $depth, $expected ) { |
| 72 |
$ss = new StripState( null, [ 'depthLimit' => 2 ] ); |
| 73 |
$text = 'text'; |
| 74 |
for ( $i = 0; $i < $depth; $i++ ) { |
| 75 |
$mk = $this->getMarker(); |
| 76 |
$ss->addNoWiki( $mk, "={$text}=" ); |
| 77 |
$text = $mk; |
| 78 |
} |
| 79 |
$text = $ss->unstripNoWiki( $text ); |
| 80 |
$this->assertSame( $expected, $text ); |
| 81 |
} |
| 82 |
|
| 83 |
public function testUnstripLoop() { |
| 84 |
$ss = new StripState( null, [ 'depthLimit' => 2 ] ); |
| 85 |
$mk = $this->getMarker(); |
| 86 |
$ss->addNoWiki( $mk, $mk ); |
| 87 |
$text = $ss->unstripNoWiki( $mk ); |
| 88 |
$this->assertSame( self::getWarning( 'parser-unstrip-loop-warning' ), $text ); |
| 89 |
} |
| 90 |
|
| 91 |
public static function provideUnstripSize() { |
| 92 |
return [ |
| 93 |
[ 0, 'x' ], |
| 94 |
[ 1, 'xx' ], |
| 95 |
[ 2, str_repeat( self::getWarning( 'unstrip-size-warning', 5 ), 2 ) ] |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
public function testUnstripSize( $depth, $expected ) { |
| 101 |
$ss = new StripState( null, [ 'sizeLimit' => 5 ] ); |
| 102 |
$text = 'x'; |
| 103 |
for ( $i = 0; $i < $depth; $i++ ) { |
| 104 |
$mk = $this->getMarker(); |
| 105 |
$ss->addNoWiki( $mk, $text ); |
| 106 |
$text = "$mk$mk"; |
| 107 |
} |
| 108 |
$text = $ss->unstripNoWiki( $text ); |
| 109 |
$this->assertSame( $expected, $text ); |
| 110 |
} |
| 111 |
|
| 112 |
public static function provideGetLimitReport() { |
| 113 |
for ( $i = 1; $i < 4; $i++ ) { |
| 114 |
yield [ $i ]; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public function testGetLimitReport( $depth ) { |
| 120 |
$sizeLimit = 100000; |
| 121 |
$ss = new StripState( null, [ 'depthLimit' => 5, 'sizeLimit' => $sizeLimit ] ); |
| 122 |
$text = 'x'; |
| 123 |
for ( $i = 0; $i < $depth; $i++ ) { |
| 124 |
$mk = $this->getMarker(); |
| 125 |
$ss->addNoWiki( $mk, $text ); |
| 126 |
$text = "$mk$mk"; |
| 127 |
} |
| 128 |
$text = $ss->unstripNoWiki( $text ); |
| 129 |
$report = $ss->getLimitReport(); |
| 130 |
$messages = []; |
| 131 |
foreach ( $report as [ $msg, $params ] ) { |
| 132 |
$messages[$msg] = $params; |
| 133 |
} |
| 134 |
$this->assertSame( [ $depth - 1, 5 ], $messages['limitreport-unstrip-depth'] ); |
| 135 |
$this->assertSame( |
| 136 |
[ |
| 137 |
strlen( $this->getMarker() ) * 2 * ( pow( 2, $depth ) - 2 ) + pow( 2, $depth ), |
| 138 |
$sizeLimit |
| 139 |
], |
| 140 |
$messages['limitreport-unstrip-size' ] ); |
| 141 |
} |
| 142 |
|
| 143 |
public function testReplaceNowikis() { |
| 144 |
$ss = new StripState(); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
$s1 = "[[Foo]]"; |
| 150 |
$nowikiS1 = "<nowiki>$s1</nowiki>"; |
| 151 |
$m1 = $this->getMarker(); |
| 152 |
$ss->addNoWiki( $m1, $nowikiS1 ); |
| 153 |
|
| 154 |
$s2 = "[[Foo]]"; |
| 155 |
$nowikiS2 = "<nowiki>$s2</nowiki>"; |
| 156 |
$m2 = $this->getMarker(); |
| 157 |
$ss->addNoWiki( $m2, $nowikiS2 ); |
| 158 |
|
| 159 |
$s3 = ""; |
| 160 |
$nowikiS3 = "<nowiki />"; |
| 161 |
$m3 = $this->getMarker(); |
| 162 |
$ss->addNoWiki( $m3, $nowikiS3 ); |
| 163 |
|
| 164 |
$text = "$s1; $s2; $s3"; |
| 165 |
$strippedText = "$m1; $m2; $m3"; |
| 166 |
$unstrippedText = "$nowikiS1; $nowikiS2; $nowikiS3"; |
| 167 |
|
| 168 |
$this->assertSame( $ss->unstripGeneral( $strippedText ), $strippedText ); |
| 169 |
$this->assertSame( $ss->unstripNoWiki( $strippedText ), $unstrippedText ); |
| 170 |
|
| 171 |
$out1 = $ss->replaceNoWikis( $strippedText, static function ( $s ) { |
| 172 |
return $s; |
| 173 |
} ); |
| 174 |
$this->assertSame( $out1, $unstrippedText ); |
| 175 |
|
| 176 |
|
| 177 |
$out2 = $ss->replaceNoWikis( $strippedText, static function ( $s ) { |
| 178 |
return preg_replace( "#</?nowiki[^>]*>#", '', $s ); |
| 179 |
} ); |
| 180 |
$this->assertSame( $out2, $text ); |
| 181 |
} |
| 182 |
} |