| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Config\HashConfig; |
| 4 |
use MediaWiki\Config\ServiceOptions; |
| 5 |
use MediaWiki\MainConfigNames; |
| 6 |
use MediaWiki\Search\TitleMatcher; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class TitleMatcherTest extends MediaWikiIntegrationTestCase { |
| 14 |
use LinkCacheTestTrait; |
| 15 |
|
| 16 |
public function setUp(): void { |
| 17 |
parent::setUp(); |
| 18 |
|
| 19 |
$this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, false ); |
| 20 |
} |
| 21 |
|
| 22 |
public static function nearMatchProvider() { |
| 23 |
return [ |
| 24 |
'empty request returns nothing' => [ null, 'en', '', 'Near Match Test' ], |
| 25 |
'with a hash returns nothing' => [ null, 'en', '#near match test', 'Near Match Test' ], |
| 26 |
'wrong seach string returns nothing' => [ |
| 27 |
null, 'en', ':', 'Near Match Test' |
| 28 |
], |
| 29 |
'default behaviour exact' => [ |
| 30 |
'Near Match Test', 'en', 'Near Match Test', 'Near Match Test' |
| 31 |
], |
| 32 |
'default behaviour uppercased' => [ |
| 33 |
'NEAR MATCH TEST', 'en', 'near match test', 'NEAR MATCH TEST' |
| 34 |
], |
| 35 |
'default behaviour first capitalized' => [ |
| 36 |
'Near match test', 'en', 'near match test', 'Near match test' |
| 37 |
], |
| 38 |
'default behaviour capitalized' => [ |
| 39 |
'Near Match Test', 'en', 'near match test', 'Near Match Test' |
| 40 |
], |
| 41 |
'default behaviour lowercased' => [ |
| 42 |
'Near match test', 'en', 'NEAR MATCH TEST', 'Near match test' |
| 43 |
], |
| 44 |
'default behaviour hyphenated' => [ |
| 45 |
'Near-Match-Test', 'en', 'near-match-test', 'Near-Match-Test' |
| 46 |
], |
| 47 |
'default behaviour quoted' => [ |
| 48 |
'Near Match Test', 'en', '"Near Match Test"', 'Near Match Test' |
| 49 |
], |
| 50 |
'check language with variants direct' => [ 'Near', 'tg', 'near', 'Near' ], |
| 51 |
'check language with variants converted' => [ 'Near', 'tg', 'неар', 'Near' ], |
| 52 |
'no matching' => [ null, 'en', 'near match test', 'Far Match Test' ], |
| 53 |
|
| 54 |
'file ok' => [ 'File:Example.svg', 'en', 'File:Example.svg', 'File:Example.svg' ], |
| 55 |
'file not ok' => [ null, 'en', 'File:Example_s.svg', 'File:Example.svg' ], |
| 56 |
|
| 57 |
'user ok' => [ 'User:Superuser', 'en', 'User:Superuser', 'User:Superuser' ], |
| 58 |
'user ok even if no user' => [ |
| 59 |
'User:SuperuserNew', 'en', 'User:SuperuserNew', 'User:Superuser' |
| 60 |
], |
| 61 |
'user search use by IP' => [ |
| 62 |
'Special:Contributions/132.17.48.1', 'en', 'User:132.17.48.1', 'User:Superuser', true, |
| 63 |
], |
| 64 |
|
| 65 |
'mediawiki ok even if no page' => [ |
| 66 |
'MediaWiki:Add New Page', 'en', 'MediaWiki:Add New Page', 'MediaWiki:Add Old Page' |
| 67 |
], |
| 68 |
'Media ok' => [ |
| 69 |
'File:Text', 'en', 'Media:Text', 'File:Text', true, |
| 70 |
], |
| 71 |
'Media not ok' => [ |
| 72 |
null, 'en', 'Media:Text', 'Media:Text', true, |
| 73 |
], |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private function getTitleMatcher( HashConfig $config, $langCode ): TitleMatcher { |
| 84 |
$services = $this->getServiceContainer(); |
| 85 |
return new TitleMatcher( |
| 86 |
new ServiceOptions( TitleMatcher::CONSTRUCTOR_OPTIONS, $config ), |
| 87 |
$services->getLanguageFactory()->getLanguage( $langCode ), |
| 88 |
$services->getLanguageConverterFactory(), |
| 89 |
$services->getHookContainer(), |
| 90 |
$services->getWikiPageFactory(), |
| 91 |
$services->getUserNameUtils(), |
| 92 |
$services->getRepoGroup(), |
| 93 |
$services->getTitleFactory() |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function testNearMatch( |
| 103 |
$expected, |
| 104 |
$langCode, |
| 105 |
$searchterm, |
| 106 |
$titleText, |
| 107 |
$enableSearchContributorsByIP = false |
| 108 |
) { |
| 109 |
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'en' ); |
| 110 |
$this->addGoodLinkObject( 42, Title::newFromText( $titleText ) ); |
| 111 |
|
| 112 |
$config = new HashConfig( [ |
| 113 |
MainConfigNames::EnableSearchContributorsByIP => $enableSearchContributorsByIP, |
| 114 |
] ); |
| 115 |
|
| 116 |
$matcher = $this->getTitleMatcher( $config, $langCode ); |
| 117 |
$title = $matcher->getNearMatch( $searchterm ); |
| 118 |
$this->assertEquals( $expected, $title === null ? null : (string)$title ); |
| 119 |
} |
| 120 |
|
| 121 |
public static function hooksProvider() { |
| 122 |
return [ |
| 123 |
'SearchGetNearMatchBefore' => [ 'SearchGetNearMatchBefore' ], |
| 124 |
'SearchAfterNoDirectMatch' => [ 'SearchAfterNoDirectMatch' ], |
| 125 |
'SearchGetNearMatch' => [ 'SearchGetNearMatch' ] |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function testNearMatch_Hooks( $hook ) { |
| 135 |
$config = new HashConfig( [ |
| 136 |
MainConfigNames::EnableSearchContributorsByIP => false, |
| 137 |
] ); |
| 138 |
|
| 139 |
$this->setTemporaryHook( $hook, static function ( $term, &$title ) { |
| 140 |
if ( $term === [ 'Hook' ] || $term === 'Hook' ) { |
| 141 |
$title = Title::makeTitle( NS_MAIN, 'TitleFromHook' ); |
| 142 |
return false; |
| 143 |
} |
| 144 |
return null; |
| 145 |
} ); |
| 146 |
|
| 147 |
$matcher = $this->getTitleMatcher( $config, 'en' ); |
| 148 |
$title = $matcher->getNearMatch( 'Hook' ); |
| 149 |
$this->assertEquals( 'TitleFromHook', $title ); |
| 150 |
|
| 151 |
$this->assertNull( $matcher->getNearMatch( 'OtherHook' ) ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function testGetNearMatchResultSet() { |
| 158 |
$this->addGoodLinkObject( 42, Title::makeTitle( NS_MAIN, "Test Link" ) ); |
| 159 |
|
| 160 |
$config = new HashConfig( [ |
| 161 |
MainConfigNames::EnableSearchContributorsByIP => false, |
| 162 |
] ); |
| 163 |
|
| 164 |
$matcher = $this->getTitleMatcher( $config, 'en' ); |
| 165 |
$result = $matcher->getNearMatchResultSet( 'Test Link' ); |
| 166 |
$this->assertSame( 1, $result->numRows() ); |
| 167 |
|
| 168 |
$result = $matcher->getNearMatchResultSet( 'Test Link Wrong' ); |
| 169 |
$this->assertSame( 0, $result->numRows() ); |
| 170 |
} |
| 171 |
} |