StripStateTest.php 3.6 KB

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