SearchUpdateTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. class MockSearch extends SearchEngine {
  3. public static $id;
  4. public static $title;
  5. public static $text;
  6. public function __construct( $db ) {
  7. }
  8. public function update( $id, $title, $text ) {
  9. self::$id = $id;
  10. self::$title = $title;
  11. self::$text = $text;
  12. }
  13. }
  14. /**
  15. * @group Search
  16. */
  17. class SearchUpdateTest extends MediaWikiTestCase {
  18. /**
  19. * @var SearchUpdate
  20. */
  21. private $su;
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->setMwGlobals( 'wgSearchType', 'MockSearch' );
  25. $this->su = new SearchUpdate( 0, "" );
  26. }
  27. public function updateText( $text ) {
  28. return trim( $this->su->updateText( $text ) );
  29. }
  30. /**
  31. * @covers SearchUpdate::updateText
  32. */
  33. public function testUpdateText() {
  34. $this->assertEquals(
  35. 'test',
  36. $this->updateText( '<div>TeSt</div>' ),
  37. 'HTML stripped, text lowercased'
  38. );
  39. $this->assertEquals(
  40. 'foo bar boz quux',
  41. $this->updateText( <<<EOT
  42. <table style="color:red; font-size:100px">
  43. <tr class="scary"><td><div>foo</div></td><tr>bar</td></tr>
  44. <tr><td>boz</td><tr>quux</td></tr>
  45. </table>
  46. EOT
  47. ), 'Stripping HTML tables' );
  48. $this->assertEquals(
  49. 'a b',
  50. $this->updateText( 'a > b' ),
  51. 'Handle unclosed tags'
  52. );
  53. $text = str_pad( "foo <barbarbar \n", 10000, 'x' );
  54. $this->assertNotEquals(
  55. '',
  56. $this->updateText( $text ),
  57. 'T20609'
  58. );
  59. }
  60. /**
  61. * @covers SearchUpdate::updateText
  62. * Test T34712
  63. * Test if unicode quotes in article links make its search index empty
  64. */
  65. public function testUnicodeLinkSearchIndexError() {
  66. $text = "text „http://example.com“ text";
  67. $result = $this->updateText( $text );
  68. $processed = preg_replace( '/Q/u', 'Q', $result );
  69. $this->assertTrue(
  70. $processed != '',
  71. 'Link surrounded by unicode quotes should not fail UTF-8 validation'
  72. );
  73. }
  74. }