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