6 class StripStateTest
extends MediaWikiTestCase
{
7 public function setUp() {
9 $this->setContentLang( 'qqx' );
12 private function getMarker() {
14 return Parser
::MARKER_PREFIX
. '-blah-' . sprintf( '%08X', $i++
) . Parser
::MARKER_SUFFIX
;
17 private static function getWarning( $message, $max = '' ) {
18 return "<span class=\"error\">($message: $max)</span>";
21 public function testAddNoWiki() {
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 );
32 public function testAddGeneral() {
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 );
43 public function testUnstripBoth() {
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 );
55 public static function provideUnstripRecursive() {
60 [ 3, '==' . self
::getWarning( 'unstrip-depth-warning', 2 ) . '==' ],
64 /** @dataProvider provideUnstripRecursive */
65 public function testUnstripRecursive( $depth, $expected ) {
66 $ss = new StripState( null, [ 'depthLimit' => 2 ] );
68 for ( $i = 0; $i < $depth; $i++
) {
69 $mk = $this->getMarker();
70 $ss->addNoWiki( $mk, "={$text}=" );
73 $text = $ss->unstripNoWiki( $text );
74 $this->assertSame( $expected, $text );
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 );
85 public static function provideUnstripSize() {
89 [ 2, str_repeat( self
::getWarning( 'unstrip-size-warning', 5 ), 2 ) ]
93 /** @dataProvider provideUnstripSize */
94 public function testUnstripSize( $depth, $expected ) {
95 $ss = new StripState( null, [ 'sizeLimit' => 5 ] );
97 for ( $i = 0; $i < $depth; $i++
) {
98 $mk = $this->getMarker();
99 $ss->addNoWiki( $mk, $text );
102 $text = $ss->unstripNoWiki( $text );
103 $this->assertSame( $expected, $text );
106 public function provideGetLimitReport() {
107 for ( $i = 1; $i < 4; $i++
) {
112 /** @dataProvider provideGetLimitReport */
113 public function testGetLimitReport( $depth ) {
115 $ss = new StripState( null, [ 'depthLimit' => 5, 'sizeLimit' => $sizeLimit ] );
117 for ( $i = 0; $i < $depth; $i++
) {
118 $mk = $this->getMarker();
119 $ss->addNoWiki( $mk, $text );
122 $text = $ss->unstripNoWiki( $text );
123 $report = $ss->getLimitReport();
125 foreach ( $report as list( $msg, $params ) ) {
126 $messages[$msg] = $params;
128 $this->assertSame( [ $depth - 1, 5 ], $messages['limitreport-unstrip-depth'] );
131 strlen( $this->getMarker() ) * 2 * ( pow( 2, $depth ) - 2 ) +
pow( 2, $depth ),
134 $messages['limitreport-unstrip-size' ] );