| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Block\DatabaseBlock; |
| 4 |
use MediaWiki\Context\RequestContext; |
| 5 |
use MediaWiki\HookContainer\HookContainer; |
| 6 |
use MediaWiki\Interwiki\InterwikiLookup; |
| 7 |
use MediaWiki\Linker\LinkTarget; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Output\OutputPage; |
| 10 |
use MediaWiki\Page\PageReferenceValue; |
| 11 |
use MediaWiki\Permissions\Authority; |
| 12 |
use MediaWiki\Request\FauxRequest; |
| 13 |
use MediaWiki\Tests\Unit\MockBlockTrait; |
| 14 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 15 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 16 |
use MediaWiki\Title\Title; |
| 17 |
use MediaWiki\Title\TitleValue; |
| 18 |
use MediaWiki\User\UserIdentity; |
| 19 |
use MediaWiki\User\UserIdentityLookup; |
| 20 |
use MediaWiki\User\UserIdentityValue; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class SkinTest extends MediaWikiIntegrationTestCase { |
| 27 |
use MockAuthorityTrait; |
| 28 |
use MockBlockTrait; |
| 29 |
use TempUserTestTrait; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function testGetSkinName() { |
| 35 |
$skin = new SkinFallback(); |
| 36 |
$this->assertEquals( 'fallback', $skin->getSkinName(), 'Default' ); |
| 37 |
$skin = new SkinFallback( 'testname' ); |
| 38 |
$this->assertEquals( 'testname', $skin->getSkinName(), 'Constructor argument' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function testGetDefaultModules() { |
| 42 |
$skin = $this->getMockBuilder( Skin::class ) |
| 43 |
->onlyMethods( [ 'outputPage' ] ) |
| 44 |
->getMock(); |
| 45 |
|
| 46 |
$modules = $skin->getDefaultModules(); |
| 47 |
$this->assertTrue( isset( $modules['core'] ), 'core key is set by default' ); |
| 48 |
$this->assertTrue( isset( $modules['styles'] ), 'style key is set by default' ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private function getMockOutputPage( $isSyndicated, $html ) { |
| 57 |
$mock = $this->createMock( OutputPage::class ); |
| 58 |
$mock->expects( $this->once() ) |
| 59 |
->method( 'isSyndicated' ) |
| 60 |
->willReturn( $isSyndicated ); |
| 61 |
$mock->method( 'getHTML' ) |
| 62 |
->willReturn( $html ); |
| 63 |
return $mock; |
| 64 |
} |
| 65 |
|
| 66 |
public static function provideGetDefaultModulesForOutput() { |
| 67 |
return [ |
| 68 |
[ |
| 69 |
false, |
| 70 |
'', |
| 71 |
[] |
| 72 |
], |
| 73 |
[ |
| 74 |
true, |
| 75 |
'', |
| 76 |
[ 'mediawiki.feedlink' ] |
| 77 |
], |
| 78 |
[ |
| 79 |
false, |
| 80 |
'FOO mw-ui-button BAR', |
| 81 |
[ 'mediawiki.ui.button' ] |
| 82 |
], |
| 83 |
[ |
| 84 |
true, |
| 85 |
'FOO mw-ui-button BAR', |
| 86 |
[ 'mediawiki.ui.button', 'mediawiki.feedlink' ] |
| 87 |
], |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function testGetDefaultModulesForContent( $isSyndicated, $html, array $expectedModuleStyles ) { |
| 95 |
$skin = new class extends Skin { |
| 96 |
public function outputPage() { |
| 97 |
} |
| 98 |
}; |
| 99 |
$fakeContext = new RequestContext(); |
| 100 |
$fakeContext->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) ); |
| 101 |
$fakeContext->setOutput( $this->getMockOutputPage( $isSyndicated, $html ) ); |
| 102 |
$skin->setContext( $fakeContext ); |
| 103 |
|
| 104 |
$modules = $skin->getDefaultModules(); |
| 105 |
|
| 106 |
$actualStylesModule = array_merge( ...array_values( $modules['styles'] ) ); |
| 107 |
foreach ( $expectedModuleStyles as $expected ) { |
| 108 |
$this->assertContains( $expected, $actualStylesModule ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
public function provideGetDefaultModulesForRights() { |
| 113 |
yield 'no rights' => [ |
| 114 |
$this->mockRegisteredNullAuthority(), |
| 115 |
false, |
| 116 |
]; |
| 117 |
yield 'has all rights' => [ |
| 118 |
$this->mockRegisteredUltimateAuthority(), |
| 119 |
true, |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function testGetDefaultModulesForRights( Authority $authority, bool $hasModule ) { |
| 127 |
$skin = new class extends Skin { |
| 128 |
public function outputPage() { |
| 129 |
} |
| 130 |
}; |
| 131 |
$fakeContext = new RequestContext(); |
| 132 |
$fakeContext->setAuthority( $authority ); |
| 133 |
$fakeContext->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) ); |
| 134 |
$skin->setContext( $fakeContext ); |
| 135 |
|
| 136 |
$defaultModules = $skin->getDefaultModules(); |
| 137 |
$this->assertArrayHasKey( 'watch', $defaultModules ); |
| 138 |
if ( $hasModule ) { |
| 139 |
$this->assertContains( 'mediawiki.page.watch.ajax', $defaultModules['watch'] ); |
| 140 |
} else { |
| 141 |
$this->assertNotContains( 'mediawiki.page.watch.ajax', $defaultModules['watch'] ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public function providGetPageClasses() { |
| 146 |
yield 'normal page has namespace' => [ |
| 147 |
new TitleValue( NS_MAIN, 'Test' ), |
| 148 |
$this->mockRegisteredUltimateAuthority(), |
| 149 |
[ 'ns-0' ], |
| 150 |
]; |
| 151 |
yield 'valid special page' => [ |
| 152 |
new TitleValue( NS_SPECIAL, 'Userlogin' ), |
| 153 |
$this->mockRegisteredUltimateAuthority(), |
| 154 |
[ 'mw-special-Userlogin' ], |
| 155 |
]; |
| 156 |
yield 'invalid special page' => [ |
| 157 |
new TitleValue( NS_SPECIAL, 'BLABLABLABLA_I_AM_INVALID' ), |
| 158 |
$this->mockRegisteredUltimateAuthority(), |
| 159 |
[ 'mw-invalidspecialpage' ], |
| 160 |
]; |
| 161 |
yield 'talk page' => [ |
| 162 |
new TitleValue( NS_TALK, 'Test' ), |
| 163 |
$this->mockRegisteredUltimateAuthority(), |
| 164 |
[ 'ns-talk' ], |
| 165 |
]; |
| 166 |
yield 'subject' => [ |
| 167 |
new TitleValue( NS_MAIN, 'Test' ), |
| 168 |
$this->mockRegisteredUltimateAuthority(), |
| 169 |
[ 'ns-subject' ], |
| 170 |
]; |
| 171 |
yield 'editable' => [ |
| 172 |
new TitleValue( NS_MAIN, 'Test' ), |
| 173 |
$this->mockRegisteredAuthorityWithPermissions( [ 'edit' ] ), |
| 174 |
[ 'mw-editable' ], |
| 175 |
]; |
| 176 |
yield 'not editable' => [ |
| 177 |
new TitleValue( NS_MAIN, 'Test' ), |
| 178 |
$this->mockRegisteredNullAuthority(), |
| 179 |
[], |
| 180 |
[ 'mw-editable' ], |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function testGetPageClasses( |
| 188 |
LinkTarget $title, |
| 189 |
Authority $authority, |
| 190 |
array $expectedClasses, |
| 191 |
array $unexpectedClasses = [] |
| 192 |
) { |
| 193 |
$skin = new class extends Skin { |
| 194 |
public function outputPage() { |
| 195 |
} |
| 196 |
}; |
| 197 |
$fakeContext = new RequestContext(); |
| 198 |
$fakeContext->setAuthority( $authority ); |
| 199 |
$skin->setContext( $fakeContext ); |
| 200 |
$classes = $skin->getPageClasses( Title::newFromLinkTarget( $title ) ); |
| 201 |
foreach ( $expectedClasses as $class ) { |
| 202 |
$this->assertStringContainsString( $class, $classes ); |
| 203 |
} |
| 204 |
foreach ( $unexpectedClasses as $class ) { |
| 205 |
$this->assertStringNotContainsString( $class, $classes ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function testIsResponsive( array $options, bool $expected ) { |
| 213 |
$skin = new class( $options ) extends Skin { |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function outputPage() { |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function getUser() { |
| 225 |
$user = TestUserRegistry::getImmutableTestUser( [] )->getUser(); |
| 226 |
\MediaWiki\MediaWikiServices::getInstance()->getUserOptionsManager()->setOption( |
| 227 |
$user, |
| 228 |
'skin-responsive', |
| 229 |
$this->options['userPreference'] |
| 230 |
); |
| 231 |
return $user; |
| 232 |
} |
| 233 |
}; |
| 234 |
|
| 235 |
$this->assertSame( $expected, $skin->isResponsive() ); |
| 236 |
} |
| 237 |
|
| 238 |
public static function provideSkinResponsiveOptions() { |
| 239 |
yield 'responsive not set' => [ |
| 240 |
[ 'name' => 'test', 'userPreference' => true ], |
| 241 |
false |
| 242 |
]; |
| 243 |
yield 'responsive false' => [ |
| 244 |
[ 'name' => 'test', 'responsive' => false, 'userPreference' => true ], |
| 245 |
false |
| 246 |
]; |
| 247 |
yield 'responsive true' => [ |
| 248 |
[ 'name' => 'test', 'responsive' => true, 'userPreference' => true ], |
| 249 |
true |
| 250 |
]; |
| 251 |
yield 'responsive true, user preference false' => [ |
| 252 |
[ 'name' => 'test', 'responsive' => true, 'userPreference' => false ], |
| 253 |
false |
| 254 |
]; |
| 255 |
yield 'responsive false, user preference false' => [ |
| 256 |
[ 'name' => 'test', 'responsive' => false, 'userPreference' => false ], |
| 257 |
false |
| 258 |
]; |
| 259 |
} |
| 260 |
|
| 261 |
public static function provideMakeLink() { |
| 262 |
return [ |
| 263 |
'Empty href with link class' => [ |
| 264 |
[ |
| 265 |
'text' => 'Test', |
| 266 |
'href' => '', |
| 267 |
'class' => [ |
| 268 |
'class1', |
| 269 |
'class2' |
| 270 |
] |
| 271 |
], |
| 272 |
[ 'link-class' => 'link-class' ], |
| 273 |
'<a href="" class="class1 class2 link-class">Test</a>', |
| 274 |
], |
| 275 |
'link with link-html' => [ |
| 276 |
[ |
| 277 |
'text' => '', |
| 278 |
'href' => '#go', |
| 279 |
'link-html' => '<i>label</i>' |
| 280 |
], |
| 281 |
[ 'text-wrapper' => [ 'tag' => 'span' ] ], |
| 282 |
'<a href="#go"><i>label</i> </a>', |
| 283 |
], |
| 284 |
'Basic text wrapper' => [ |
| 285 |
[ |
| 286 |
'text' => 'Test', |
| 287 |
], |
| 288 |
[ 'text-wrapper' => [ 'tag' => 'span' ] ], |
| 289 |
'<span>Test</span>' |
| 290 |
], |
| 291 |
'Text wrapper with tooltip ID in id attribute' => [ |
| 292 |
[ |
| 293 |
'text' => 'Test', |
| 294 |
'id' => 'ii' |
| 295 |
], |
| 296 |
[ 'text-wrapper' => [ 'tag' => 'span' ] ], |
| 297 |
'<span title="(tooltip-ii)">Test</span>' |
| 298 |
], |
| 299 |
'Text wrapper with tooltip ID in single-id' => [ |
| 300 |
[ |
| 301 |
'text' => 'Test', |
| 302 |
'id' => 'foo', |
| 303 |
'single-id' => 'ii' |
| 304 |
], |
| 305 |
[ 'text-wrapper' => [ 'tag' => 'span' ] ], |
| 306 |
'<span title="(tooltip-ii)">Test</span>' |
| 307 |
], |
| 308 |
'Multi-level text wrapper with tooltip' => [ |
| 309 |
[ |
| 310 |
'text' => 'Test', |
| 311 |
'id' => 'ii' |
| 312 |
], |
| 313 |
[ 'text-wrapper' => [ |
| 314 |
[ 'tag' => 'b' ], |
| 315 |
[ 'tag' => 'i' ] |
| 316 |
] ], |
| 317 |
'<b title="(tooltip-ii)"><i>Test</i></b>' |
| 318 |
], |
| 319 |
'Multi-level text wrapper with link' => [ |
| 320 |
[ |
| 321 |
'text' => 'Test', |
| 322 |
'id' => 'ii', |
| 323 |
'href' => '#', |
| 324 |
], |
| 325 |
[ 'text-wrapper' => [ |
| 326 |
[ 'tag' => 'b' ], |
| 327 |
[ 'tag' => 'i' ] |
| 328 |
] ], |
| 329 |
'<a id="ii" href="#" title="(tooltip-ii)(word-separator)(brackets: (accesskey-ii))" ' . |
| 330 |
'accesskey="(accesskey-ii)"><b><i>Test</i></b></a>' |
| 331 |
], |
| 332 |
'Specified HTML' => [ |
| 333 |
[ |
| 334 |
'html' => '<b>1</b>', |
| 335 |
], |
| 336 |
[], |
| 337 |
'<b>1</b>' |
| 338 |
], |
| 339 |
'Data attribute' => [ |
| 340 |
[ |
| 341 |
'text' => 'Test', |
| 342 |
'href' => '#', |
| 343 |
'data' => [ 'foo' => 'bar' ] |
| 344 |
], |
| 345 |
[], |
| 346 |
'<a href="#" data-foo="bar">Test</a>' |
| 347 |
], |
| 348 |
'tooltip only' => [ |
| 349 |
[ |
| 350 |
'text' => 'Save', |
| 351 |
'id' => 'save', |
| 352 |
'href' => '#', |
| 353 |
'tooltiponly' => true, |
| 354 |
], |
| 355 |
[], |
| 356 |
'<a id="save" href="#" title="(tooltip-save)">Save</a>' |
| 357 |
] |
| 358 |
]; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public function testMakeLinkLink( array $data, array $options, string $expected ) { |
| 368 |
$this->setUserLang( 'qqx' ); |
| 369 |
$skin = new class extends Skin { |
| 370 |
public function outputPage() { |
| 371 |
} |
| 372 |
}; |
| 373 |
|
| 374 |
$link = $skin->makeLink( |
| 375 |
'test', |
| 376 |
$data, |
| 377 |
$options |
| 378 |
); |
| 379 |
|
| 380 |
$this->assertHTMLEquals( |
| 381 |
$expected, |
| 382 |
$link |
| 383 |
); |
| 384 |
} |
| 385 |
|
| 386 |
public static function provideGetPersonalToolsForMakeListItem() { |
| 387 |
return [ |
| 388 |
[ |
| 389 |
[ |
| 390 |
'foo' => [ |
| 391 |
'class' => 'foo', |
| 392 |
'link-html' => '<i>text</i>', |
| 393 |
'text' => 'Hello', |
| 394 |
], |
| 395 |
], |
| 396 |
false, |
| 397 |
[ |
| 398 |
'foo' => [ |
| 399 |
'links' => [ |
| 400 |
[ |
| 401 |
'single-id' => 'pt-foo', |
| 402 |
'text' => 'Hello', |
| 403 |
'link-html' => '<i>text</i>', |
| 404 |
'class' => 'foo', |
| 405 |
] |
| 406 |
], |
| 407 |
'id' => 'pt-foo', |
| 408 |
] |
| 409 |
], |
| 410 |
], |
| 411 |
[ |
| 412 |
[ |
| 413 |
'foo' => [ |
| 414 |
'class' => 'foo', |
| 415 |
'link-html' => '<i>text</i>', |
| 416 |
'text' => 'Hello', |
| 417 |
], |
| 418 |
], |
| 419 |
true, |
| 420 |
[ |
| 421 |
'foo' => [ |
| 422 |
'links' => [ |
| 423 |
[ |
| 424 |
'single-id' => 'pt-foo', |
| 425 |
'text' => 'Hello', |
| 426 |
'link-html' => '<i>text</i>', |
| 427 |
] |
| 428 |
], |
| 429 |
'id' => 'pt-foo', |
| 430 |
'class' => 'foo', |
| 431 |
] |
| 432 |
], |
| 433 |
] |
| 434 |
]; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function testGetPersonalToolsForMakeListItem( array $urls, bool $applyClassesToListItems, array $expected ) { |
| 444 |
$skin = new class extends Skin { |
| 445 |
public function outputPage() { |
| 446 |
} |
| 447 |
}; |
| 448 |
|
| 449 |
$this->assertSame( |
| 450 |
$expected, |
| 451 |
$skin->getPersonalToolsForMakeListItem( |
| 452 |
$urls, |
| 453 |
$applyClassesToListItems |
| 454 |
) |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
public function testGetRelevantUser_get_set() { |
| 459 |
$skin = new class extends Skin { |
| 460 |
public function outputPage() { |
| 461 |
} |
| 462 |
}; |
| 463 |
$relevantUser = UserIdentityValue::newRegistered( 1, 'SomeUser' ); |
| 464 |
$skin->setRelevantUser( $relevantUser ); |
| 465 |
$this->assertSame( $relevantUser, $skin->getRelevantUser() ); |
| 466 |
|
| 467 |
$this->installMockBlockManager( |
| 468 |
[ |
| 469 |
'target' => $relevantUser, |
| 470 |
'hideName' => true, |
| 471 |
] |
| 472 |
); |
| 473 |
|
| 474 |
$ctx = RequestContext::getMain(); |
| 475 |
$ctx->setAuthority( $this->mockAnonNullAuthority() ); |
| 476 |
$skin->setContext( $ctx ); |
| 477 |
$this->assertNull( $skin->getRelevantUser() ); |
| 478 |
|
| 479 |
$ctx->setAuthority( $this->mockAnonUltimateAuthority() ); |
| 480 |
$skin->setContext( $ctx ); |
| 481 |
$skin->setRelevantUser( $relevantUser ); |
| 482 |
$skin->setRelevantUser( $relevantUser ); |
| 483 |
$this->assertSame( $relevantUser, $skin->getRelevantUser() ); |
| 484 |
} |
| 485 |
|
| 486 |
public static function provideGetRelevantUser_load_from_title() { |
| 487 |
yield 'Not user namespace' => [ |
| 488 |
'relevantPage' => PageReferenceValue::localReference( NS_MAIN, '123.123.123.123' ), |
| 489 |
'expectedUser' => null |
| 490 |
]; |
| 491 |
yield 'User namespace' => [ |
| 492 |
'relevantPage' => PageReferenceValue::localReference( NS_USER, '123.123.123.123' ), |
| 493 |
'expectedUser' => UserIdentityValue::newAnonymous( '123.123.123.123' ) |
| 494 |
]; |
| 495 |
yield 'User talk namespace' => [ |
| 496 |
'relevantPage' => PageReferenceValue::localReference( NS_USER_TALK, '123.123.123.123' ), |
| 497 |
'expectedUser' => UserIdentityValue::newAnonymous( '123.123.123.123' ) |
| 498 |
]; |
| 499 |
yield 'User page subpage' => [ |
| 500 |
'relevantPage' => PageReferenceValue::localReference( NS_USER, '123.123.123.123/bla' ), |
| 501 |
'expectedUser' => UserIdentityValue::newAnonymous( '123.123.123.123' ) |
| 502 |
]; |
| 503 |
yield 'Non-registered user with name' => [ |
| 504 |
'relevantPage' => PageReferenceValue::localReference( NS_USER, 'I_DO_NOT_EXIST' ), |
| 505 |
'expectedUser' => null |
| 506 |
]; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public function testGetRelevantUser_load_from_title( |
| 513 |
PageReferenceValue $relevantPage, |
| 514 |
?UserIdentity $expectedUser |
| 515 |
) { |
| 516 |
$skin = new class extends Skin { |
| 517 |
public function outputPage() { |
| 518 |
} |
| 519 |
}; |
| 520 |
$skin->setRelevantTitle( Title::newFromPageReference( $relevantPage ) ); |
| 521 |
$relevantUser = $skin->getRelevantUser(); |
| 522 |
if ( $expectedUser ) { |
| 523 |
$this->assertTrue( $expectedUser->equals( $relevantUser ) ); |
| 524 |
} else { |
| 525 |
$this->assertNull( $relevantUser ); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
public function testGetRelevantUser_load_existing() { |
| 530 |
$skin = new class extends Skin { |
| 531 |
public function outputPage() { |
| 532 |
} |
| 533 |
}; |
| 534 |
$user = new UserIdentityValue( 42, 'foo' ); |
| 535 |
$userIdentityLookup = $this->createMock( UserIdentityLookup::class ); |
| 536 |
$userIdentityLookup->method( 'getUserIdentityByName' ) |
| 537 |
->willReturnCallback( function ( $name ) use ( $user ) { |
| 538 |
if ( $name === $user->getName() ) { |
| 539 |
return $user; |
| 540 |
} |
| 541 |
return $this->createMock( UserIdentity::class ); |
| 542 |
} ); |
| 543 |
$this->setService( 'UserIdentityLookup', $userIdentityLookup ); |
| 544 |
$skin->setRelevantTitle( |
| 545 |
Title::makeTitle( NS_USER, $user->getName() ) |
| 546 |
); |
| 547 |
$this->assertTrue( $user->equals( $skin->getRelevantUser() ) ); |
| 548 |
$this->assertSame( $user->getId(), $skin->getRelevantUser()->getId() ); |
| 549 |
} |
| 550 |
|
| 551 |
public function testBuildSidebarCache() { |
| 552 |
|
| 553 |
$this->overrideConfigValues( [ |
| 554 |
MainConfigNames::UseDatabaseMessages => true, |
| 555 |
MainConfigNames::EnableSidebarCache => true, |
| 556 |
MainConfigNames::SidebarCacheExpiry => 3600, |
| 557 |
] ); |
| 558 |
|
| 559 |
$clock = 1301644800.0; |
| 560 |
$this->getServiceContainer()->getMainWANObjectCache()->setMockTime( $clock ); |
| 561 |
$id = 0; |
| 562 |
$this->setTemporaryHook( 'SkinBuildSidebar', |
| 563 |
static function ( Skin $skin, array &$bar ) use ( &$id, &$clock ) { |
| 564 |
$id++; |
| 565 |
$clock += 1.0; |
| 566 |
if ( $skin->getSkinName() === 'foo' ) { |
| 567 |
$bar['myhook'] = "foo $id"; |
| 568 |
} |
| 569 |
if ( $skin->getSkinName() === 'bar' ) { |
| 570 |
$bar['myhook'] = "bar $id"; |
| 571 |
} |
| 572 |
} |
| 573 |
); |
| 574 |
$context = RequestContext::newExtraneousContext( Title::makeTitle( NS_SPECIAL, 'Blankpage' ) ); |
| 575 |
$foo1 = new class( 'foo' ) extends Skin { |
| 576 |
public function outputPage() { |
| 577 |
} |
| 578 |
}; |
| 579 |
$foo2 = new class( 'foo' ) extends Skin { |
| 580 |
public function outputPage() { |
| 581 |
} |
| 582 |
}; |
| 583 |
$bar = new class( 'bar' ) extends Skin { |
| 584 |
public function outputPage() { |
| 585 |
} |
| 586 |
}; |
| 587 |
$foo1->setContext( $context ); |
| 588 |
$foo2->setContext( $context ); |
| 589 |
$bar->setContext( $context ); |
| 590 |
$this->assertArrayContains( [ 'myhook' => 'foo 1' ], $foo1->buildSidebar(), 'fresh' ); |
| 591 |
$clock += 0.01; |
| 592 |
$this->assertArrayContains( [ 'myhook' => 'foo 1' ], $foo2->buildSidebar(), 'cache hit' ); |
| 593 |
$this->assertArrayContains( [ 'myhook' => 'bar 2' ], $bar->buildSidebar(), 'cache miss' ); |
| 594 |
} |
| 595 |
|
| 596 |
public function testBuildSidebarWithUserAddedContent() { |
| 597 |
$this->overrideConfigValues( [ |
| 598 |
MainConfigNames::UseDatabaseMessages => true, |
| 599 |
MainConfigNames::EnableSidebarCache => false |
| 600 |
] ); |
| 601 |
$foo1 = new class( 'foo' ) extends Skin { |
| 602 |
public function outputPage() { |
| 603 |
} |
| 604 |
}; |
| 605 |
$this->editPage( 'MediaWiki:Sidebar', <<<EOS |
| 606 |
* navigation |
| 607 |
** mainpage|mainpage-description |
| 608 |
** recentchanges-url|recentchanges |
| 609 |
** randompage-url|randompage |
| 610 |
** helppage|help-mediawiki |
| 611 |
* SEARCH |
| 612 |
* TOOLBOX |
| 613 |
** A|B |
| 614 |
* LANGUAGES |
| 615 |
** C|D |
| 616 |
EOS ); |
| 617 |
|
| 618 |
$context = RequestContext::newExtraneousContext( Title::makeTitle( NS_MAIN, 'Main Page' ) ); |
| 619 |
$foo1->setContext( $context ); |
| 620 |
|
| 621 |
$this->assertArrayContains( [ [ 'id' => 'n-B', 'text' => 'B' ] ], $foo1->buildSidebar()['TOOLBOX'], 'Toolbox has user defined links' ); |
| 622 |
|
| 623 |
$hasUserDefinedLinks = false; |
| 624 |
$languageLinks = $foo1->buildSidebar()['LANGUAGES']; |
| 625 |
foreach ( $languageLinks as $languageLink ) { |
| 626 |
if ( $languageLink['id'] === 'n-D' ) { |
| 627 |
$hasUserDefinedLinks = true; |
| 628 |
break; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
$this->assertSame( false, $hasUserDefinedLinks, 'Languages does not support user defined links' ); |
| 633 |
} |
| 634 |
|
| 635 |
public function testIgnoreAutoblocksInUserLinks() { |
| 636 |
|
| 637 |
$this->clearHook( 'SidebarBeforeOutput' ); |
| 638 |
|
| 639 |
|
| 640 |
$this->overrideConfigValues( [ |
| 641 |
MainConfigNames::UploadNavigationUrl => false, |
| 642 |
MainConfigNames::EnableUploads => false, |
| 643 |
MainConfigNames::EnableSpecialMute => false, |
| 644 |
] ); |
| 645 |
|
| 646 |
$skin = new class extends Skin { |
| 647 |
public function outputPage() { |
| 648 |
} |
| 649 |
}; |
| 650 |
$context = RequestContext::newExtraneousContext( Title::makeTitle( NS_SPECIAL, 'Contributions/127.0.0.1' ) ); |
| 651 |
$context->setUser( $this->getTestSysop()->getUser() ); |
| 652 |
$skin->setContext( $context ); |
| 653 |
$anonymousUser = $this->getServiceContainer()->getUserFactory() |
| 654 |
->newFromUserIdentity( UserIdentityValue::newAnonymous( '127.0.0.1' ) ); |
| 655 |
$skin->setRelevantUser( $anonymousUser ); |
| 656 |
|
| 657 |
|
| 658 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 659 |
$this->assertNull( $blockStore->newFromTarget( '127.0.0.1' ) ); |
| 660 |
$user = $this->getTestUser()->getUser(); |
| 661 |
RequestContext::getMain()->getRequest()->setIP( '127.0.0.1' ); |
| 662 |
$this->editPage( |
| 663 |
'Test page', 'Test Content', 'test', NS_MAIN, $user |
| 664 |
); |
| 665 |
$block = new DatabaseBlock( [ |
| 666 |
'address' => $user->getName(), |
| 667 |
'by' => $user, |
| 668 |
'reason' => 'test', |
| 669 |
'timestamp' => wfTimestampNow(), |
| 670 |
'expiry' => 'infinity', |
| 671 |
'enableAutoblock' => true, |
| 672 |
] ); |
| 673 |
$blockStore->insertBlock( $block ); |
| 674 |
$this->assertNotNull( $blockStore->newFromTarget( '127.0.0.1' ), 'Autoblock exists' ); |
| 675 |
|
| 676 |
|
| 677 |
$result = array_keys( $skin->buildSidebar()['TOOLBOX'] ); |
| 678 |
$expected = [ 'contributions', 'log', 'blockip' ]; |
| 679 |
$this->assertArrayEquals( |
| 680 |
$expected, |
| 681 |
array_intersect( $expected, $result ) |
| 682 |
); |
| 683 |
} |
| 684 |
|
| 685 |
public function testBuildSidebarForContributionsPageOfTemporaryAccount() { |
| 686 |
|
| 687 |
$this->clearHook( 'SidebarBeforeOutput' ); |
| 688 |
|
| 689 |
$this->overrideConfigValues( [ |
| 690 |
MainConfigNames::UploadNavigationUrl => false, |
| 691 |
MainConfigNames::EnableUploads => false, |
| 692 |
MainConfigNames::EnableSpecialMute => true, |
| 693 |
] ); |
| 694 |
$foo1 = new class( 'foo' ) extends Skin { |
| 695 |
public function outputPage() { |
| 696 |
} |
| 697 |
}; |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
$this->enableAutoCreateTempUser(); |
| 702 |
$tempUser = $this->getServiceContainer()->getTempUserCreator() |
| 703 |
->create( null, new FauxRequest() )->getUser(); |
| 704 |
$context = RequestContext::newExtraneousContext( |
| 705 |
Title::makeTitle( NS_SPECIAL, 'Contributions/' . $tempUser->getName() ) |
| 706 |
); |
| 707 |
$context->setUser( $this->getTestSysop()->getUser() ); |
| 708 |
$foo1->setContext( $context ); |
| 709 |
$foo1->setRelevantUser( $tempUser ); |
| 710 |
$foo1->getOutput()->setArticleRelated( false ); |
| 711 |
|
| 712 |
|
| 713 |
$this->assertArrayEquals( |
| 714 |
[ 'contributions', 'log', 'blockip', 'mute', 'print', 'specialpages' ], |
| 715 |
array_keys( $foo1->buildSidebar()['TOOLBOX'] ) |
| 716 |
); |
| 717 |
} |
| 718 |
|
| 719 |
public function testGetLanguagesHidden() { |
| 720 |
$this->overrideConfigValues( [ |
| 721 |
MainConfigNames::HideInterlanguageLinks => true, |
| 722 |
] ); |
| 723 |
$skin = new class extends Skin { |
| 724 |
public function outputPage() { |
| 725 |
} |
| 726 |
}; |
| 727 |
$this->assertSame( [], $skin->getLanguages() ); |
| 728 |
} |
| 729 |
|
| 730 |
public function testGetLanguages() { |
| 731 |
$this->overrideConfigValues( [ |
| 732 |
MainConfigNames::HideInterlanguageLinks => false, |
| 733 |
MainConfigNames::InterlanguageLinkCodeMap => [ 'talk' => 'fr' ], |
| 734 |
MainConfigNames::LanguageCode => 'qqx', |
| 735 |
] ); |
| 736 |
|
| 737 |
$mockOutputPage = $this->createMock( OutputPage::class ); |
| 738 |
$mockOutputPage->method( 'getLanguageLinks' ) |
| 739 |
|
| 740 |
|
| 741 |
->willReturn( [ 'en:Foo', 'talk:Page' ] ); |
| 742 |
|
| 743 |
$fakeContext = new RequestContext(); |
| 744 |
$fakeContext->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) ); |
| 745 |
$fakeContext->setOutput( $mockOutputPage ); |
| 746 |
$fakeContext->setLanguage( 'en' ); |
| 747 |
|
| 748 |
$hookContainer = $this->createMock( HookContainer::class ); |
| 749 |
$this->setService( 'HookContainer', $hookContainer ); |
| 750 |
|
| 751 |
$mockIwLookup = $this->createMock( InterwikiLookup::class ); |
| 752 |
$mockIwLookup->method( 'isValidInterwiki' )->willReturn( true ); |
| 753 |
$mockIwLookup->method( 'fetch' )->willReturnCallback( static function ( string $prefix ) { |
| 754 |
return new Interwiki( |
| 755 |
$prefix, |
| 756 |
"https://$prefix.example.com/$1" |
| 757 |
); |
| 758 |
} ); |
| 759 |
$this->setService( 'InterwikiLookup', $mockIwLookup ); |
| 760 |
|
| 761 |
$skin = new class extends Skin { |
| 762 |
public function outputPage() { |
| 763 |
} |
| 764 |
}; |
| 765 |
$skin->setContext( $fakeContext ); |
| 766 |
|
| 767 |
$this->assertSame( [ |
| 768 |
[ |
| 769 |
'href' => 'https://en.example.com/Foo', |
| 770 |
'text' => 'English', |
| 771 |
'title' => 'Foo – English', |
| 772 |
'class' => 'interlanguage-link interwiki-en', |
| 773 |
'link-class' => 'interlanguage-link-target', |
| 774 |
'lang' => 'en', |
| 775 |
'hreflang' => 'en', |
| 776 |
'data-title' => 'Foo', |
| 777 |
'data-language-autonym' => 'English', |
| 778 |
'data-language-local-name' => 'English', |
| 779 |
], |
| 780 |
[ |
| 781 |
'href' => 'https://talk.example.com/Page', |
| 782 |
'text' => 'Français', |
| 783 |
'title' => 'Page – français', |
| 784 |
'class' => 'interlanguage-link interwiki-talk', |
| 785 |
'link-class' => 'interlanguage-link-target', |
| 786 |
'lang' => 'fr', |
| 787 |
'hreflang' => 'fr', |
| 788 |
'data-title' => 'Page', |
| 789 |
'data-language-autonym' => 'Français', |
| 790 |
'data-language-local-name' => 'français', |
| 791 |
], |
| 792 |
], $skin->getLanguages() ); |
| 793 |
} |
| 794 |
} |