Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / search / TitleMatcherTest.php
blobb7cc9b3aec7f563f687a42a68a9efd63518a4743
1 <?php
3 use MediaWiki\Config\HashConfig;
4 use MediaWiki\Config\ServiceOptions;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Search\TitleMatcher;
7 use MediaWiki\Title\Title;
9 /**
10 * @covers \MediaWiki\Search\TitleMatcher
11 * @group Database
13 class TitleMatcherTest extends MediaWikiIntegrationTestCase {
14 use LinkCacheTestTrait;
16 public function setUp(): void {
17 parent::setUp();
19 $this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, false );
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'
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'
68 'Media ok' => [
69 'File:Text', 'en', 'Media:Text', 'File:Text', true,
71 'Media not ok' => [
72 null, 'en', 'Media:Text', 'Media:Text', true,
77 /**
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()
97 /**
98 * @dataProvider nearMatchProvider
99 * @covers \MediaWiki\Search\TitleMatcher::getNearMatchInternal
100 * @covers \MediaWiki\Search\TitleMatcher::getNearMatch
102 public function testNearMatch(
103 $expected,
104 $langCode,
105 $searchterm,
106 $titleText,
107 $enableSearchContributorsByIP = false
109 $this->overrideConfigValue( MainConfigNames::LanguageCode, 'en' );
110 $this->addGoodLinkObject( 42, Title::newFromText( $titleText ) );
112 $config = new HashConfig( [
113 MainConfigNames::EnableSearchContributorsByIP => $enableSearchContributorsByIP,
114 ] );
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() {
122 return [
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,
137 ] );
139 $this->setTemporaryHook( $hook, static function ( $term, &$title ) {
140 if ( $term === [ 'Hook' ] || $term === 'Hook' ) {
141 $title = Title::makeTitle( NS_MAIN, 'TitleFromHook' );
142 return false;
144 return null;
145 } );
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,
162 ] );
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();
174 parent::tearDown();