| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Config\Api; |
| 4 |
|
| 5 |
use Wikimedia\Bcp47Code\Bcp47CodeValue; |
| 6 |
use Wikimedia\Parsoid\Config\Api\SiteConfig; |
| 7 |
|
| 8 |
/** |
| 9 |
* @covers \Wikimedia\Parsoid\Config\Api\SiteConfig |
| 10 |
*/ |
| 11 |
class SiteConfigTest extends \PHPUnit\Framework\TestCase { |
| 12 |
|
| 13 |
private static $siteConfig; |
| 14 |
|
| 15 |
protected function getSiteConfig(): SiteConfig { |
| 16 |
if ( self::$siteConfig === null ) { |
| 17 |
$helper = new TestApiHelper( $this, 'siteinfo' ); |
| 18 |
self::$siteConfig = new SiteConfig( $helper, [] ); |
| 19 |
} |
| 20 |
|
| 21 |
return self::$siteConfig; |
| 22 |
} |
| 23 |
|
| 24 |
public function testAllowedExternalImagePrefixes() { |
| 25 |
$this->assertSame( |
| 26 |
[], |
| 27 |
$this->getSiteConfig()->allowedExternalImagePrefixes() |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
public function testBaseURI() { |
| 32 |
$this->assertSame( |
| 33 |
'//en.wikipedia.org/wiki/', |
| 34 |
$this->getSiteConfig()->baseURI() |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
public function testRelativeLinkPrefix() { |
| 39 |
$this->assertSame( |
| 40 |
'./', |
| 41 |
$this->getSiteConfig()->relativeLinkPrefix() |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** @dataProvider bswPagePropProvider */ |
| 46 |
public function testBswPagePropRegexp( string $pageProp, bool $caseSensitive ) { |
| 47 |
$re = $this->getSiteConfig()->bswPagePropRegexp(); |
| 48 |
$this->assertSame( 1, preg_match( $re, "mw:PageProp/$pageProp" ) ); |
| 49 |
$this->assertSame( 1, preg_match( $re, "foo mw:PageProp/$pageProp bar" ) ); |
| 50 |
// Check case sensitivity |
| 51 |
$lower = strtolower( $pageProp ); |
| 52 |
$expected = $lower === $pageProp ? 1 : ( $caseSensitive ? 0 : 1 ); |
| 53 |
$this->assertSame( $expected, preg_match( $re, "mw:PageProp/$lower" ) ); |
| 54 |
$this->assertSame( $expected, preg_match( $re, " mw:PageProp/$lower " ) ); |
| 55 |
|
| 56 |
$upper = strtoupper( $pageProp ); |
| 57 |
$expected = ( $upper === $pageProp ) ? 1 : ( $caseSensitive ? 0 : 1 ); |
| 58 |
$this->assertSame( $expected, preg_match( $re, "mw:PageProp/$upper" ) ); |
| 59 |
$this->assertSame( $expected, preg_match( $re, " mw:PageProp/$upper " ) ); |
| 60 |
} |
| 61 |
|
| 62 |
public function bswPagePropProvider() { |
| 63 |
return [ |
| 64 |
// Case sensitive |
| 65 |
[ 'NOGLOBAL', true ], |
| 66 |
[ 'DISAMBIG', true ], |
| 67 |
[ 'NEWSECTIONLINK', true ], |
| 68 |
[ 'NONEWSECTIONLINK', true ], |
| 69 |
[ 'HIDDENCAT', true ], |
| 70 |
[ 'EXPECTUNUSEDCATEGORY', true ], |
| 71 |
[ 'INDEX', true ], |
| 72 |
[ 'NOINDEX', true ], |
| 73 |
[ 'STATICREDIRECT', true ], |
| 74 |
|
| 75 |
// Case insensitive |
| 76 |
[ 'NoTOC', false ], |
| 77 |
[ 'NoGallery', false ], |
| 78 |
[ 'ForceTOC', false ], |
| 79 |
[ 'ToC', false ], |
| 80 |
[ 'NoEditSection', false ], |
| 81 |
[ 'NoTitleConvert', false ], |
| 82 |
[ 'NoTC', false ], |
| 83 |
[ 'NoContentConvert', false ], |
| 84 |
[ 'NoCC', false ], |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
public function testCanonicalNamespaceId() { |
| 89 |
$this->assertSame( 5, $this->getSiteConfig()->canonicalNamespaceId( 'Project talk' ) ); |
| 90 |
$this->assertNull( $this->getSiteConfig()->canonicalNamespaceId( 'Wikipedia talk' ) ); |
| 91 |
} |
| 92 |
|
| 93 |
public function testNamespaceId() { |
| 94 |
$this->assertSame( 0, $this->getSiteConfig()->namespaceId( '' ) ); |
| 95 |
$this->assertSame( 5, $this->getSiteConfig()->namespaceId( 'Wikipedia talk' ) ); |
| 96 |
$this->assertSame( 5, $this->getSiteConfig()->namespaceId( 'WiKiPeDiA_TaLk' ) ); |
| 97 |
$this->assertNull( $this->getSiteConfig()->namespaceId( 'Foobar' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
public function testNamespaceName() { |
| 101 |
$this->assertSame( '', $this->getSiteConfig()->namespaceName( 0 ) ); |
| 102 |
$this->assertSame( 'Wikipedia talk', $this->getSiteConfig()->namespaceName( 5 ) ); |
| 103 |
$this->assertNull( $this->getSiteConfig()->namespaceName( 500 ) ); |
| 104 |
} |
| 105 |
|
| 106 |
public function testNamespaceHasSubpages() { |
| 107 |
$this->assertSame( false, $this->getSiteConfig()->namespaceHasSubpages( 0 ) ); |
| 108 |
$this->assertTrue( $this->getSiteConfig()->namespaceHasSubpages( 1 ) ); |
| 109 |
} |
| 110 |
|
| 111 |
public function testNamespaceCase() { |
| 112 |
$this->assertSame( 'first-letter', $this->getSiteConfig()->namespaceCase( 0 ) ); |
| 113 |
$this->assertSame( 'first-letter', $this->getSiteConfig()->namespaceCase( 1 ) ); |
| 114 |
} |
| 115 |
|
| 116 |
public function testSpecialPageLocalName() { |
| 117 |
$this->assertSame( |
| 118 |
'RecentChanges', $this->getSiteConfig()->specialPageLocalName( 'recentchanges' ) |
| 119 |
); |
| 120 |
$this->assertSame( |
| 121 |
'RecentChangesLinked', $this->getSiteConfig()->specialPageLocalName( 'RelatedChanges' ) |
| 122 |
); |
| 123 |
$this->assertSame( |
| 124 |
null, $this->getSiteConfig()->specialPageLocalName( 'FooBar' ) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
public function testNamespaceIsTalk() { |
| 129 |
$this->assertSame( false, $this->getSiteConfig()->namespaceIsTalk( -1 ) ); |
| 130 |
$this->assertSame( false, $this->getSiteConfig()->namespaceIsTalk( 0 ) ); |
| 131 |
$this->assertTrue( $this->getSiteConfig()->namespaceIsTalk( 1 ) ); |
| 132 |
} |
| 133 |
|
| 134 |
public function testInterwikiMagic() { |
| 135 |
$this->assertTrue( |
| 136 |
$this->getSiteConfig()->interwikiMagic() |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
public function testInterwikiMap() { |
| 141 |
$ret = $this->getSiteConfig()->interwikiMap(); |
| 142 |
$this->assertIsArray( $ret ); |
| 143 |
$this->assertSame( |
| 144 |
[ |
| 145 |
'prefix' => 'zh-cn', |
| 146 |
'local' => true, |
| 147 |
'language' => true, |
| 148 |
'url' => 'https://zh.wikipedia.org/wiki/$1', |
| 149 |
], |
| 150 |
$ret['zh-cn'] |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
public function testIwp() { |
| 155 |
$this->assertSame( |
| 156 |
'enwiki', |
| 157 |
$this->getSiteConfig()->iwp() |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
public function testLegalTitleChars() { |
| 162 |
$this->assertSame( |
| 163 |
' %!"$&\'()*,\-.\/0-9:;=?@A-Z\\\\^_`a-z~\x80-\xFF+', |
| 164 |
$this->getSiteConfig()->legalTitleChars() |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
public function testLinkPrefixRegex() { |
| 169 |
$this->assertSame( |
| 170 |
null, |
| 171 |
$this->getSiteConfig()->linkPrefixRegex() |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
public function testLinkTrailRegex() { |
| 176 |
$this->assertSame( |
| 177 |
'/^([a-z]+)/sD', |
| 178 |
$this->getSiteConfig()->linkTrailRegex() |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
public function testLangBcp47() { |
| 183 |
$this->assertEqualsIgnoringCase( |
| 184 |
'en', |
| 185 |
$this->getSiteConfig()->langBcp47()->toBcp47Code() |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
public function testMainpage() { |
| 190 |
$this->assertSame( |
| 191 |
'Main Page', |
| 192 |
$this->getSiteConfig()->mainpage() |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
public function testGetMWConfigValue() { |
| 197 |
$this->assertSame( |
| 198 |
true, |
| 199 |
$this->getSiteConfig()->getMWConfigValue( 'CiteResponsiveReferences' ) |
| 200 |
); |
| 201 |
$this->assertSame( |
| 202 |
10, |
| 203 |
$this->getSiteConfig()->getMWConfigValue( 'CiteResponsiveReferencesThreshold' ) |
| 204 |
); |
| 205 |
$this->assertSame( |
| 206 |
null, |
| 207 |
$this->getSiteConfig()->getMWConfigValue( 'CiteUnknownConfig' ) |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
public function testRtl() { |
| 212 |
$this->assertSame( |
| 213 |
false, |
| 214 |
$this->getSiteConfig()->rtl() |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
public function testLangConverterEnabledBcp47() { |
| 219 |
$this->assertTrue( $this->getSiteConfig()->langConverterEnabledBcp47( new Bcp47CodeValue( 'zh' ) ) ); |
| 220 |
$this->assertFalse( $this->getSiteConfig()->langConverterEnabledBcp47( new Bcp47CodeValue( 'de' ) ) ); |
| 221 |
} |
| 222 |
|
| 223 |
public function testScript() { |
| 224 |
$this->assertSame( |
| 225 |
'/w/index.php', |
| 226 |
$this->getSiteConfig()->script() |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
public function testScriptpath() { |
| 231 |
$this->assertSame( |
| 232 |
'/w', |
| 233 |
$this->getSiteConfig()->scriptpath() |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
public function testServer() { |
| 238 |
$this->assertSame( |
| 239 |
'//en.wikipedia.org', |
| 240 |
$this->getSiteConfig()->server() |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
public function testSolTransparentWikitextRegexp() { |
| 245 |
$this->assertSame( |
| 246 |
// phpcs:ignore Generic.Files.LineLength.TooLong |
| 247 |
'@^[ \t\n\r\0\x0b]*(?:(?:(?i:\#REDIRECT))[ \t\n\r\x0c]*(?::[ \t\n\r\x0c]*)?\[\[[^\]]+\]\])?(?:\[\[Category\:[^\]]*?\]\]|__(?:(?:NOGLOBAL|DISAMBIG|EXPECTUNUSEDCATEGORY|HIDDENCAT|INDEX|NEWSECTIONLINK|NOINDEX|NONEWSECTIONLINK|STATICREDIRECT)|(?i:FORCETOC|NOCONTENTCONVERT|NOCC|NOEDITSECTION|NOGALLERY|NOTITLECONVERT|NOTC|NOTOC|TOC))__|<!--(?>[\s\S]*?-->)|[ \t\n\r\0\x0b])*$@', |
| 248 |
$this->getSiteConfig()->solTransparentWikitextRegexp() |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
public function testSolTransparentWikitextNoWsRegexp() { |
| 253 |
$this->assertSame( |
| 254 |
// phpcs:ignore Generic.Files.LineLength.TooLong |
| 255 |
'@((?:(?:(?i:\#REDIRECT))[ \t\n\r\x0c]*(?::[ \t\n\r\x0c]*)?\[\[[^\]]+\]\])?(?:\[\[Category\:[^\]]*?\]\]|__(?:(?:NOGLOBAL|DISAMBIG|EXPECTUNUSEDCATEGORY|HIDDENCAT|INDEX|NEWSECTIONLINK|NOINDEX|NONEWSECTIONLINK|STATICREDIRECT)|(?i:FORCETOC|NOCONTENTCONVERT|NOCC|NOEDITSECTION|NOGALLERY|NOTITLECONVERT|NOTC|NOTOC|TOC))__|<!--(?>[\s\S]*?-->))*)@', |
| 256 |
$this->getSiteConfig()->solTransparentWikitextNoWsRegexp() |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
public function testTimezoneOffset() { |
| 261 |
$this->assertSame( |
| 262 |
0, |
| 263 |
$this->getSiteConfig()->timezoneOffset() |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
public function testVariantsFor() { |
| 268 |
$ret = $this->getSiteConfig()->variantsFor( new Bcp47CodeValue( 'zh-hant-tw' ) ); |
| 269 |
$this->assertIsArray( $ret ); |
| 270 |
$this->assertEquals( |
| 271 |
[ |
| 272 |
'base' => new Bcp47CodeValue( 'zh' ), |
| 273 |
'fallbacks' => [ |
| 274 |
new Bcp47CodeValue( 'zh-Hant' ), |
| 275 |
new Bcp47CodeValue( 'zh-Hant-HK' ), |
| 276 |
new Bcp47CodeValue( 'zh-Hant-MO' ), |
| 277 |
], |
| 278 |
], |
| 279 |
$ret |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
public function testWidthOption() { |
| 284 |
$this->assertSame( |
| 285 |
220, |
| 286 |
$this->getSiteConfig()->widthOption() |
| 287 |
); |
| 288 |
} |
| 289 |
|
| 290 |
public function testGetMagicWordForBehaviorSwitch() { |
| 291 |
$this->assertSame( |
| 292 |
'disambiguation', |
| 293 |
$this->getSiteConfig()->getMagicWordForBehaviorSwitch( '__DISAMBIG__' ) |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
public function testMwAliases() { |
| 298 |
$ret = $this->getSiteConfig()->mwAliases(); |
| 299 |
$this->assertIsArray( $ret ); |
| 300 |
$this->assertSame( |
| 301 |
[ |
| 302 |
'DEFAULTSORT:', |
| 303 |
'DEFAULTSORTKEY:', |
| 304 |
'DEFAULTCATEGORYSORT:', |
| 305 |
], |
| 306 |
$ret['defaultsort'] ?? null |
| 307 |
); |
| 308 |
} |
| 309 |
|
| 310 |
public function testGetMagicWordForMediaOption() { |
| 311 |
$this->assertSame( |
| 312 |
'img_width', |
| 313 |
$this->getSiteConfig()->getMagicWordForMediaOption( '$1px' ) |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
public function testIsBehaviorSwitch() { |
| 318 |
$this->assertTrue( $this->getSiteConfig()->isBehaviorSwitch( '__TOC__' ) ); |
| 319 |
$this->assertSame( false, $this->getSiteConfig()->isBehaviorSwitch( 'img_width' ) ); |
| 320 |
} |
| 321 |
|
| 322 |
public function testGetMagicWordMatcher() { |
| 323 |
$this->assertSame( |
| 324 |
'/^(?:(?:SUBJECTPAGENAME|ARTICLEPAGENAME))$/D', |
| 325 |
$this->getSiteConfig()->getMagicWordMatcher( 'subjectpagename' ) |
| 326 |
); |
| 327 |
$this->assertSame( |
| 328 |
'/^(?!)$/', |
| 329 |
$this->getSiteConfig()->getMagicWordMatcher( 'doesnotexist' ) |
| 330 |
); |
| 331 |
} |
| 332 |
|
| 333 |
public function testGetMagicPatternMatcher() { |
| 334 |
$matcher = $this->getSiteConfig()->getParameterizedAliasMatcher( |
| 335 |
[ 'img_manualthumb', 'img_lossy', 'img_width', 'img_link' ] ); |
| 336 |
|
| 337 |
// Basic tests |
| 338 |
$this->assertSame( [ 'k' => 'img_width', 'v' => '123' ], $matcher( '123px' ) ); |
| 339 |
$this->assertSame( [ 'k' => 'img_lossy', 'v' => '123' ], $matcher( 'lossy=123' ) ); |
| 340 |
$this->assertSame( [ 'k' => 'img_link', 'v' => 'http://example.com' ], |
| 341 |
$matcher( 'link=http://example.com' ) ); |
| 342 |
|
| 343 |
// Test alias handling |
| 344 |
$this->assertSame( [ 'k' => 'img_manualthumb', 'v' => 'Foo.jpg' ], |
| 345 |
$matcher( 'thumbnail=Foo.jpg' ) ); // primary alias for img_manualthumb |
| 346 |
$this->assertSame( [ 'k' => 'img_manualthumb', 'v' => 'Foo.jpg' ], |
| 347 |
$matcher( 'thumb=Foo.jpg' ) ); // secondary alias for img_manualthumb |
| 348 |
|
| 349 |
// Tests partial matches of just the key without a value. |
| 350 |
// WikiLinkHandler use this method in this fashion. |
| 351 |
$this->assertSame( [ 'k' => 'img_link', 'v' => '' ], $matcher( 'link=' ) ); |
| 352 |
|
| 353 |
// img_page isn't in the list of image options above |
| 354 |
$this->assertNull( $matcher( 'page=123' ) ); |
| 355 |
// enlace is link in Spanish, but this alias isn't present in siteconfig data |
| 356 |
$this->assertNull( $matcher( 'enlace=123' ) ); |
| 357 |
} |
| 358 |
|
| 359 |
public function testIsExtensionTag() { |
| 360 |
$this->assertTrue( $this->getSiteConfig()->isExtensionTag( 'pre' ) ); |
| 361 |
$this->assertFalse( $this->getSiteConfig()->isExtensionTag( 'bogus' ) ); |
| 362 |
} |
| 363 |
|
| 364 |
public function testGetExtensionTagNameMap() { |
| 365 |
$this->assertSame( |
| 366 |
[ |
| 367 |
'pre' => true, |
| 368 |
'nowiki' => true, |
| 369 |
'gallery' => true, |
| 370 |
'indicator' => true, |
| 371 |
'langconvert' => true, |
| 372 |
'timeline' => true, |
| 373 |
'hiero' => true, |
| 374 |
'inputbox' => true, |
| 375 |
'imagemap' => true, |
| 376 |
'source' => true, |
| 377 |
'syntaxhighlight' => true, |
| 378 |
'poem' => true, |
| 379 |
'categorytree' => true, |
| 380 |
'score' => true, |
| 381 |
'templatestyles' => true, |
| 382 |
'templatedata' => true, |
| 383 |
'math' => true, |
| 384 |
'ce' => true, |
| 385 |
'chem' => true, |
| 386 |
'graph' => true, |
| 387 |
'maplink' => true, |
| 388 |
'mapframe' => true, |
| 389 |
'charinsert' => true, |
| 390 |
'ref' => true, |
| 391 |
'references' => true, |
| 392 |
'section' => true, |
| 393 |
'labeledsectiontransclusion' => true, |
| 394 |
'labeledsectiontransclusion/begin' => true, |
| 395 |
'labeledsectiontransclusion/end' => true |
| 396 |
], |
| 397 |
array_fill_keys( array_keys( $this->getSiteConfig()->getExtensionTagNameMap() ), true ) |
| 398 |
); |
| 399 |
} |
| 400 |
|
| 401 |
public function testGetMaxTemplateDepth() { |
| 402 |
$this->assertSame( 40, $this->getSiteConfig()->getMaxTemplateDepth() ); |
| 403 |
} |
| 404 |
|
| 405 |
public function testGetExtResourceURLPatternMatcher() { |
| 406 |
$matcher = $this->getSiteConfig()->getExtResourceURLPatternMatcher(); |
| 407 |
$this->assertIsCallable( $matcher ); |
| 408 |
$this->assertSame( |
| 409 |
[ 'ISBN', '12345' ], |
| 410 |
$matcher( 'Special:Booksources/12345' ) |
| 411 |
); |
| 412 |
} |
| 413 |
|
| 414 |
public function testHasValidProtocol() { |
| 415 |
$this->assertSame( |
| 416 |
false, |
| 417 |
$this->getSiteConfig()->hasValidProtocol( 'foo bar http://www.example.com/xyz baz' ) |
| 418 |
); |
| 419 |
$this->assertSame( |
| 420 |
true, |
| 421 |
$this->getSiteConfig()->hasValidProtocol( 'http://www.example.com/xyz baz' ) |
| 422 |
); |
| 423 |
} |
| 424 |
|
| 425 |
public function testFindValidProtocol() { |
| 426 |
$this->assertSame( |
| 427 |
true, |
| 428 |
$this->getSiteConfig()->findValidProtocol( 'foo bar http://www.example.com/xyz baz' ) |
| 429 |
); |
| 430 |
} |
| 431 |
|
| 432 |
} |