Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Config/SiteConfigTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Config;
4
5 use PHPUnit\Framework\MockObject\MockObject;
6 use Wikimedia\Bcp47Code\Bcp47CodeValue;
7 use Wikimedia\Parsoid\Config\SiteConfig;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @covers \Wikimedia\Parsoid\Config\SiteConfig
12 */
13 class SiteConfigTest extends \PHPUnit\Framework\TestCase {
14
15 /**
16 * @param array $methods Subset of methods to mock.
17 *
18 * @return MockObject|SiteConfig
19 */
20 private function getSiteConfig( array $methods = [] ) {
21 return $this->getMockBuilder( SiteConfig::class )
22 ->onlyMethods( $methods )
23 ->getMockForAbstractClass();
24 }
25
26 public function testNamespaceIsTalk() {
27 $siteConfig = $this->getSiteConfig();
28 $this->assertFalse( $siteConfig->namespaceIsTalk( -2 ) );
29 $this->assertFalse( $siteConfig->namespaceIsTalk( -1 ) );
30 $this->assertFalse( $siteConfig->namespaceIsTalk( 0 ) );
31 $this->assertTrue( $siteConfig->namespaceIsTalk( 1 ) );
32 $this->assertFalse( $siteConfig->namespaceIsTalk( 2 ) );
33 $this->assertTrue( $siteConfig->namespaceIsTalk( 3 ) );
34 }
35
36 public function testUcfirst() {
37 $siteConfig = $this->getSiteConfig( [ 'langBcp47' ] );
38 $siteConfig->method( 'langBcp47' )->willReturn( new Bcp47CodeValue( 'en' ) );
39
40 $this->assertSame( 'Foo', $siteConfig->ucfirst( 'Foo' ) );
41 $this->assertSame( 'Foo', $siteConfig->ucfirst( 'foo' ) );
42 $this->assertSame( 'Ááá', $siteConfig->ucfirst( 'ááá' ) );
43 $this->assertSame( 'Iii', $siteConfig->ucfirst( 'iii' ) );
44
45 foreach ( [ 'tr', 'kaa', 'kk', 'az' ] as $lang ) {
46 $siteConfig = $this->getSiteConfig( [ 'langBcp47' ] );
47 $siteConfig->method( 'langBcp47' )->willReturn( new Bcp47CodeValue( $lang ) );
48 $this->assertSame( 'İii', $siteConfig->ucfirst( 'iii' ), "Special logic for $lang" );
49 }
50 }
51
52 /**
53 * @dataProvider provideMakeExtResourceURL
54 */
55 public function testMakeExtResourceURL( array $match, string $href, string $content, string $expect ) {
56 $siteConfig = $this->getSiteConfig();
57 $this->assertSame( $expect, $siteConfig->makeExtResourceURL( $match, $href, $content ) );
58 }
59
60 public function provideMakeExtResourceURL(): array {
61 return [
62 [
63 [ 'ISBN', '9780615720302' ], './Special:Booksources/9780615720302', 'ISBN 978-0615720302',
64 'ISBN 978-0615720302'
65 ],
66 [
67 [ 'ISBN', '0596519796' ], './Special:Booksources/0596519796', "ISBN\u{00A0}0-596-51979-6",
68 "ISBN\u{00A0}0-596-51979-6"
69 ],
70 [
71 [ 'ISBN', '221212466X' ], './Special:Booksources/221212466X', 'ISBN 2-212-12466-x',
72 'ISBN 2-212-12466-x'
73 ],
74 [
75 [ 'ISBN', '9780615720302' ], './Special:Booksources/9780615720302', 'Working With MediaWiki',
76 '[[Special:Booksources/9780615720302|Working With MediaWiki]]'
77 ],
78 [
79 [ 'RFC', '2324' ], 'https://datatracker.ietf.org/doc/html/rfc2324', 'RFC 2324', 'RFC 2324'
80 ],
81 [
82 [ 'RFC', '2324' ], 'https://datatracker.ietf.org/doc/html/rfc2324', 'HTCPCP',
83 '[https://datatracker.ietf.org/doc/html/rfc2324 HTCPCP]'
84 ],
85 ];
86 }
87
88 public function testMakeExtResourceURL_invalid() {
89 $siteConfig = $this->getSiteConfig();
90 $this->expectException( \InvalidArgumentException::class );
91 $this->expectExceptionMessage( "Invalid match type 'Bogus'" );
92 $siteConfig->makeExtResourceURL( [ 'Bogus', '0' ], './Bogus', 'Bogus 0' );
93 }
94
95 /**
96 * @dataProvider provideInterwikiMatcher
97 */
98 public function testInterwikiMatcher( $href, $expect, $batchSize = null ) {
99 $siteConfig = $this->getSiteConfig( [ 'interwikiMap' ] );
100 if ( $batchSize !== null ) {
101 TestingAccessWrapper::newFromObject( $siteConfig )->iwMatcherBatchSize = $batchSize;
102 }
103 $siteConfig->expects( $this->once() )->method( 'interwikiMap' )->willReturn( [
104 'w' => [
105 'prefix' => 'w',
106 'local' => true,
107 'localinterwiki' => true,
108 'url' => 'https://en.wikipedia.org/wiki/$1',
109 ],
110 'en' => [
111 'prefix' => 'en',
112 'local' => true,
113 'language' => true,
114 'localinterwiki' => true,
115 'url' => 'https://en.wikipedia.org/wiki/$1',
116 ],
117 'de' => [
118 'prefix' => 'de',
119 'local' => true,
120 'language' => true,
121 'url' => 'https://de.wikipedia.org/wiki/$1',
122 ],
123 'iarchive' => [
124 'prefix' => 'iarchive',
125 'url' => 'https://archive.org/details/$1',
126 'protorel' => true,
127 ],
128 'example' => [
129 'prefix' => 'example',
130 'url' => '//example.org/$1',
131 ],
132 '2' => [
133 'prefix' => '2',
134 'url' => 'https://two.org/$1',
135 ],
136 ] );
137
138 $this->assertSame( $expect, $siteConfig->interwikiMatcher( $href ) );
139 // Again, to test caching
140 $this->assertSame( $expect, $siteConfig->interwikiMatcher( $href ) );
141 }
142
143 /**
144 * @dataProvider provideInterwikiMatcher
145 */
146 public function testInterwikiMatcher_smallBatches( $href, $expect ) {
147 $this->testInterwikiMatcher( $href, $expect, 1 );
148 }
149
150 public function provideInterwikiMatcher() {
151 return [
152 [ 'https://de.wikipedia.org/wiki/Foobar', [ ':de', 'Foobar' ] ],
153 [ 'https://en.wikipedia.org/wiki/Foobar', [ ':en', 'Foobar' ] ], // not 'w'
154 [ './w:Foobar', [ 'w', 'Foobar' ] ],
155 [ 'de%3AFoobar', [ ':de', 'Foobar' ] ],
156 [ 'https://two.org/Two', [ '2', 'Two' ] ],
157
158 // Protocol-relative handling
159 [ 'https://archive.org/details/301works', [ 'iarchive', '301works' ] ],
160 [ 'http://archive.org/details/301works', [ 'iarchive', '301works' ] ],
161 [ '//archive.org/details/301works', [ 'iarchive', '301works' ] ],
162 [ 'https://example.org/foobar', [ 'example', 'foobar' ] ],
163 [ 'http://example.org/foobar', [ 'example', 'foobar' ] ],
164 [ '//example.org/foobar', [ 'example', 'foobar' ] ],
165 [ 'http://en.wikipedia.org/wiki/Foobar', null ],
166 [ '//en.wikipedia.org/wiki/Foobar', null ],
167 ];
168 }
169
170 public function testInterwikiMatcher_tooManyPatterns() {
171 $map = [];
172 for ( $i = 0; $i < 100; $i++ ) {
173 $map["x$i"] = [
174 'prefix' => "x$i",
175 'url' => "https://example.org/$i/$1",
176 ];
177 }
178 $map["l"] = [
179 'prefix' => 'l',
180 'language' => true,
181 'url' => 'https://example.org/42/$1'
182 ];
183
184 $siteConfig = $this->getSiteConfig( [ 'interwikiMap' ] );
185 $siteConfig->method( 'interwikiMap' )->willReturn( $map );
186
187 $this->assertSame(
188 [ 'x76', 'Foobar' ],
189 $siteConfig->interwikiMatcher( 'https://example.org/76/Foobar' )
190 );
191 $this->assertSame(
192 [ ':l', 'Foobar' ],
193 $siteConfig->interwikiMatcher( 'https://example.org/42/Foobar' )
194 );
195 $this->assertSame(
196 null,
197 $siteConfig->interwikiMatcher( 'https://example.org/9999999/Foobar' )
198 );
199 }
200
201 /**
202 * @dataProvider provideProtocolMethods
203 */
204 public function testProtocolMethods( $link, $expectHas, $expectFind ) {
205 $siteConfig = $this->getSiteConfig( [ 'getProtocols' ] );
206 $siteConfig->method( 'getProtocols' )->willReturn( [ 'https' ] );
207
208 $this->assertSame( $expectHas, $siteConfig->hasValidProtocol( $link ) );
209 $this->assertSame( $expectFind, $siteConfig->findValidProtocol( $link ) );
210 }
211
212 public function provideProtocolMethods() {
213 return [
214 [ 'http://wikipedia.org', false, false ],
215 [ 'https://wikipedia.org', true, true ],
216 [ 'ftp://ftp.something.net', false, false ],
217 [ 'something http://wikipedia.org', false, false ],
218 [ 'something https://wikipedia.org', false, true ],
219 [ 'something ftp://ftp.something.net', false, false ],
220 [ 'http://wikipedia.org https://wikipedia.org', false, true ],
221 [ 'https://wikipedia.org http://wikipedia.org', true, true ],
222 ];
223 }
224
225 public function mockSpecialPageAlias( $specialPage ) {
226 if ( $specialPage === 'Booksources' ) {
227 return [ 'Booksources', 'BookSources' ]; // Mock value
228 } else {
229 throw new \BadMethodCallException( 'Not implemented' );
230 }
231 }
232
233 /**
234 * @dataProvider provideGetResourceURLPatternMatcher
235 */
236 public function testGetResourceURLPatternMatcher( $input, $res ) {
237 // Alternatively, we can construct a MockSiteConfig object and use it since
238 // it delegates all the work of the method under test to the base class.
239 // But, this technique is probably more resilient in case MockSiteConfig changes.
240 $siteConfig = $this->getSiteConfig( [ 'getSpecialNSAliases', 'getSpecialPageAliases' ] );
241 $siteConfig->method( 'getSpecialNSAliases' )->willReturn( [ 'Special', 'special' ] );
242 $siteConfig->method( 'getSpecialPageAliases' )->
243 will( $this->returnCallBack( [ $this, 'mockSpecialPageAlias' ] ) );
244
245 $matcher = $siteConfig->getExtResourceURLPatternMatcher();
246 $this->assertSame( $res, $matcher( $input ) );
247 }
248
249 public function provideGetResourceURLPatternMatcher() {
250 $isbnTests = [
251 [ "Special:BookSources/1234567890X", [ 'ISBN', '1234567890X' ] ],
252 [ "Special:Booksources/1234567890X", [ 'ISBN', '1234567890X' ] ],
253 [ "special:BookSources/1234567890X", [ 'ISBN', '1234567890X' ] ],
254 [ "special:Booksources/1234567890X", [ 'ISBN', '1234567890X' ] ],
255 [ "Special:BookSources/1234567890x", [ 'ISBN', '1234567890x' ] ],
256 [ "Special:BookSources/1234567890", [ 'ISBN', '1234567890' ] ],
257 [ "../Special:BookSources/1234567890", [ 'ISBN', '1234567890' ] ],
258 [ "../../Special:BookSources/1234567890", [ 'ISBN', '1234567890' ] ],
259 [ "SPECIAL:BOOKSOURCES/1234567890", [ 'ISBN', '1234567890' ] ], // see the ?i flag
260 [ "Special:BookSources/1234567890Y", false ],
261 [ "special:boksources/1234567890", false ],
262 [ "Notspecial:Booksources/1234567890", false ],
263 ];
264 $pmidTests = [
265 [ "//www.ncbi.nlm.nih.gov/pubmed/covid19?dopt=Abstract", [ 'PMID', 'covid19' ] ],
266 [ "https://www.ncbi.nlm.nih.gov/pubmed/covid19?dopt=Abstract", [ 'PMID', 'covid19' ] ],
267 [ "http://www.ncbi.nlm.nih.gov/pubmed/covid19?dopt=Abstract", [ 'PMID', 'covid19' ] ],
268 [ "http://www.ncbi.nlm.nih.gov/pubmed/covid19", false ],
269 // FIXME T257629: Strange that our code treats "foobar://" as "foobar:" + "//"
270 [ "foobar://www.ncbi.nlm.nih.gov/pubmed/covid19?dopt=Abstract", [ 'PMID', 'covid19' ] ],
271 [ "http://www.ncbi.nlm.nih.gov/pubmed/covid19?dopt=Abstract&something_more", false ],
272 ];
273 $rfcTests = [
274 [ "//datatracker.ietf.org/doc/html/rfc1234", [ 'RFC', '1234' ] ],
275 [ "https://datatracker.ietf.org/doc/html/rfc1234", [ 'RFC', '1234' ] ],
276 [ "http://datatracker.ietf.org/doc/html/rfc1234", [ 'RFC', '1234' ] ],
277 // FIXME T257629: Strange that our code accepts RFCs with "_" and doesn't have more validity checking
278 // but, magic links are on the way out anyway.
279 [ "http://datatracker.ietf.org/doc/html/rfc_1234", [ 'RFC', '_1234' ] ],
280 // FIXME T257629: Strange that our code treats "foobar://" as "foobar:" + "//"
281 [ "foobar://datatracker.ietf.org/doc/html/rfc1234", [ 'RFC', '1234' ] ],
282 [ "http://datatracker.ietf.org/doc/html/RFC1234", false ],
283 [ "http://tools.ietf.org/json/rfc1234", false ],
284 ];
285
286 return array_merge( $isbnTests, $pmidTests, $rfcTests );
287 }
288
289 private function setupMagicWordTestConfig(): SiteConfig {
290 $mws = [
291 "img_lossy" => [ true, "lossy=$1" ],
292 "numberofwikis" => [ false, "numberofwikis" ], // variable
293 "lcfirst" => [ false, "LCFIRST:" ], // is a no-hash function hook
294 "expr" => [ false, "expr" ], // function hook with valid hashed version
295 "noglobal" => [ true, "__NOGLOBAL__" ],
296 "defaultsort" => [ true, "DEFAULTSORT:", "DEFAULTSORTKEY:", "DEFAULTCATEGORYSORT:" ],
297 ];
298 // computed based on magicword array above
299 $functionSyns = [
300 "0" => [ "#expr" => "expr", "lcfirst" => "lcfirst" ],
301 ];
302 $vars = [ "numberofwikis" ];
303 $siteConfig = $this->getSiteConfig( [
304 'getMagicWords', 'getFunctionSynonyms', 'getVariableIDs'
305 ] );
306 $siteConfig->method( 'getMagicWords' )->willReturn( $mws );
307 $siteConfig->method( 'getFunctionSynonyms' )->willReturn( $functionSyns );
308 $siteConfig->method( 'getVariableIDs' )->willReturn( $vars );
309
310 return $siteConfig;
311 }
312
313 public function testMwAliases() {
314 // FIXME: Given that Parsoid proxies {{..}} wikitext to core for expansion,
315 // some of these tests don't mean a while lot right now. There are known
316 // bugs in SiteConfig right now. (T257629)
317 $siteConfig = $this->setupMagicWordTestConfig();
318 $aliases = [
319 // FIXME: Should the magic word code be de-duping the aliases array?
320 "img_lossy" => [ "lossy=$1" ],
321 "numberofwikis" => [ "numberofwikis", "numberofwikis" ],
322 "lcfirst" => [ "LCFIRST:", "lcfirst:" ],
323 "expr" => [ "expr", "expr" ],
324 "noglobal" => [ "__NOGLOBAL__" ],
325 "defaultsort" => [ "DEFAULTSORT:", "DEFAULTSORTKEY:", "DEFAULTCATEGORYSORT:" ],
326 ];
327 $this->assertSame( $aliases, $siteConfig->mwAliases() );
328 }
329
330 /**
331 * @dataProvider provideGetMagicWordForFunctionHooks
332 */
333 public function testGetMagicWordForFunctionHooks( $input, $res ) {
334 $siteConfig = $this->setupMagicWordTestConfig();
335 $this->assertSame( $res, $siteConfig->getMagicWordForFunctionHook( $input ) );
336 }
337
338 public function provideGetMagicWordForFunctionHooks() {
339 return [
340 [ "LCFIRST", "lcfirst" ],
341 [ "lcfirst", "lcfirst" ],
342 [ "#expr", "expr" ],
343 [ "#EXPR", "expr" ],
344 [ "expr", null ],
345 [ "expr:", null ],
346 [ "#expr:", null ],
347 [ "lcfirst:", null ],
348 [ "#lcfirst", null ],
349 ];
350 }
351
352 /**
353 * @dataProvider provideGetMagicWordForVariable
354 */
355 public function testGetMagicWordForVariable( $input, $res ) {
356 $siteConfig = $this->setupMagicWordTestConfig();
357 $this->assertSame( $res, $siteConfig->getMagicWordForVariable( $input ) );
358 }
359
360 public function provideGetMagicWordForVariable() {
361 return [
362 [ "numberofwikis", "numberofwikis" ],
363 [ "NUMBEROFWIKIS", null ],
364 [ "numberofadmins", null ],
365 ];
366 }
367
368 /**
369 * @dataProvider provideLinkTrailRegex
370 */
371 public function testLinkTrailRegex( $input, $res ) {
372 $siteConfig = $this->getSiteConfig( [ 'linkTrail' ] );
373 $siteConfig->method( 'linkTrail' )->willReturn( $input );
374 $this->assertSame( $res, $siteConfig->linkTrailRegex() );
375 }
376
377 public function provideLinkTrailRegex() {
378 return [
379 [ '/^([a-z]+)(.*)$/sD', '/^([a-z]+)/sD' ], // enwiki
380 [ '/^()(.*)$/sD', null ] // zhwiki
381 ];
382 }
383 }