3 use MediaWiki\Config\HashConfig
;
4 use MediaWiki\Config\ServiceOptions
;
5 use MediaWiki\MainConfigNames
;
6 use MediaWiki\Search\TitleMatcher
;
7 use MediaWiki\Title\Title
;
10 * @covers \MediaWiki\Search\TitleMatcher
13 class TitleMatcherTest
extends MediaWikiIntegrationTestCase
{
14 use LinkCacheTestTrait
;
16 public function setUp(): void
{
19 $this->overrideConfigValue( MainConfigNames
::UsePigLatinVariant
, false );
22 public static function nearMatchProvider() {
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'
29 'default behaviour exact' => [
30 'Near Match Test', 'en', 'Near Match Test', 'Near Match Test'
32 'default behaviour uppercased' => [
33 'NEAR MATCH TEST', 'en', 'near match test', 'NEAR MATCH TEST'
35 'default behaviour first capitalized' => [
36 'Near match test', 'en', 'near match test', 'Near match test'
38 'default behaviour capitalized' => [
39 'Near Match Test', 'en', 'near match test', 'Near Match Test'
41 'default behaviour lowercased' => [
42 'Near match test', 'en', 'NEAR MATCH TEST', 'Near match test'
44 'default behaviour hyphenated' => [
45 'Near-Match-Test', 'en', 'near-match-test', 'Near-Match-Test'
47 'default behaviour quoted' => [
48 'Near Match Test', 'en', '"Near Match Test"', 'Near Match Test'
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 // Special cases: files
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 // Special cases: users
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'
61 'user search use by IP' => [
62 'Special:Contributions/132.17.48.1', 'en', 'User:132.17.48.1', 'User:Superuser', true,
64 // Special cases: other content types
65 'mediawiki ok even if no page' => [
66 'MediaWiki:Add New Page', 'en', 'MediaWiki:Add New Page', 'MediaWiki:Add Old Page'
69 'File:Text', 'en', 'Media:Text', 'File:Text', true,
72 null, 'en', 'Media:Text', 'Media:Text', true,
78 * @param HashConfig $config
79 * @param string $langCode
81 * @return TitleMatcher
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()
98 * @dataProvider nearMatchProvider
99 * @covers \MediaWiki\Search\TitleMatcher::getNearMatchInternal
100 * @covers \MediaWiki\Search\TitleMatcher::getNearMatch
102 public function testNearMatch(
107 $enableSearchContributorsByIP = false
109 $this->overrideConfigValue( MainConfigNames
::LanguageCode
, 'en' );
110 $this->addGoodLinkObject( 42, Title
::newFromText( $titleText ) );
112 $config = new HashConfig( [
113 MainConfigNames
::EnableSearchContributorsByIP
=> $enableSearchContributorsByIP,
116 $matcher = $this->getTitleMatcher( $config, $langCode );
117 $title = $matcher->getNearMatch( $searchterm );
118 $this->assertEquals( $expected, $title === null ?
null : (string)$title );
121 public static function hooksProvider() {
123 'SearchGetNearMatchBefore' => [ 'SearchGetNearMatchBefore' ],
124 'SearchAfterNoDirectMatch' => [ 'SearchAfterNoDirectMatch' ],
125 'SearchGetNearMatch' => [ 'SearchGetNearMatch' ]
130 * @dataProvider hooksProvider
131 * @covers \MediaWiki\Search\TitleMatcher::getNearMatchInternal
132 * @covers \MediaWiki\Search\TitleMatcher::getNearMatch
134 public function testNearMatch_Hooks( $hook ) {
135 $config = new HashConfig( [
136 MainConfigNames
::EnableSearchContributorsByIP
=> false,
139 $this->setTemporaryHook( $hook, static function ( $term, &$title ) {
140 if ( $term === [ 'Hook' ] ||
$term === 'Hook' ) {
141 $title = Title
::makeTitle( NS_MAIN
, 'TitleFromHook' );
147 $matcher = $this->getTitleMatcher( $config, 'en' );
148 $title = $matcher->getNearMatch( 'Hook' );
149 $this->assertEquals( 'TitleFromHook', $title );
151 $this->assertNull( $matcher->getNearMatch( 'OtherHook' ) );
155 * @covers \MediaWiki\Search\TitleMatcher::getNearMatchResultSet
157 public function testGetNearMatchResultSet() {
158 $this->addGoodLinkObject( 42, Title
::makeTitle( NS_MAIN
, "Test Link" ) );
160 $config = new HashConfig( [
161 MainConfigNames
::EnableSearchContributorsByIP
=> false,
164 $matcher = $this->getTitleMatcher( $config, 'en' );
165 $result = $matcher->getNearMatchResultSet( 'Test Link' );
166 $this->assertSame( 1, $result->numRows() );
168 $result = $matcher->getNearMatchResultSet( 'Test Link Wrong' );
169 $this->assertSame( 0, $result->numRows() );
172 protected function tearDown(): void
{
173 Title
::clearCaches();