| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\SpecialPage; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\Context\RequestContext; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\MainConfigSchema; |
| 9 |
use MediaWiki\Output\OutputPage; |
| 10 |
use MediaWiki\Page\PageReferenceValue; |
| 11 |
use MediaWiki\Request\FauxRequest; |
| 12 |
use MediaWiki\SpecialPage\SpecialPage; |
| 13 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 14 |
use MediaWiki\Specials\SpecialAllPages; |
| 15 |
use MediaWiki\Title\Title; |
| 16 |
use MediaWikiIntegrationTestCase; |
| 17 |
use RuntimeException; |
| 18 |
use Wikimedia\ScopedCallback; |
| 19 |
use Wikimedia\TestingAccessWrapper; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class SpecialPageFactoryTest extends MediaWikiIntegrationTestCase { |
| 43 |
private function getFactory() { |
| 44 |
return $this->getServiceContainer()->getSpecialPageFactory(); |
| 45 |
} |
| 46 |
|
| 47 |
public function testHookNotCalledTwice() { |
| 48 |
$count = 0; |
| 49 |
$this->setTemporaryHook( |
| 50 |
'SpecialPage_initList', |
| 51 |
static function () use ( &$count ) { |
| 52 |
$count++; |
| 53 |
} |
| 54 |
); |
| 55 |
$spf = $this->getServiceContainer()->getSpecialPageFactory(); |
| 56 |
$spf->getNames(); |
| 57 |
$spf->getNames(); |
| 58 |
$this->assertSame( 1, $count ); |
| 59 |
} |
| 60 |
|
| 61 |
public function newSpecialAllPages() { |
| 62 |
return new SpecialAllPages(); |
| 63 |
} |
| 64 |
|
| 65 |
public function specialPageProvider() { |
| 66 |
$specialPageTestHelper = new SpecialPageTestHelper(); |
| 67 |
|
| 68 |
return [ |
| 69 |
'class name' => [ 'SpecialAllPages', false ], |
| 70 |
'closure' => [ static function () { |
| 71 |
return new SpecialAllPages(); |
| 72 |
}, false ], |
| 73 |
'function' => [ [ $this, 'newSpecialAllPages' ], false ], |
| 74 |
'callback string' => [ SpecialPageTestHelper::class . '::newSpecialAllPages', false ], |
| 75 |
'callback with object' => [ |
| 76 |
[ $specialPageTestHelper, 'newSpecialAllPages' ], |
| 77 |
false |
| 78 |
], |
| 79 |
'callback array' => [ |
| 80 |
[ SpecialPageTestHelper::class, 'newSpecialAllPages' ], |
| 81 |
false |
| 82 |
], |
| 83 |
'object factory spec' => [ |
| 84 |
[ 'class' => SpecialAllPages::class ], |
| 85 |
false |
| 86 |
] |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function testGetPage( $spec, $shouldReuseInstance ) { |
| 95 |
$this->overrideConfigValue( |
| 96 |
MainConfigNames::SpecialPages, |
| 97 |
[ 'testdummy' => $spec ] + MainConfigSchema::getDefaultValue( MainConfigNames::SpecialPages ) |
| 98 |
); |
| 99 |
|
| 100 |
$factory = $this->getFactory(); |
| 101 |
$page = $factory->getPage( 'testdummy' ); |
| 102 |
$this->assertInstanceOf( SpecialPage::class, $page ); |
| 103 |
|
| 104 |
$page2 = $factory->getPage( 'testdummy' ); |
| 105 |
$this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testGetNames() { |
| 112 |
$this->overrideConfigValue( |
| 113 |
MainConfigNames::SpecialPages, |
| 114 |
[ 'testdummy' => SpecialAllPages::class ] + |
| 115 |
MainConfigSchema::getDefaultValue( MainConfigNames::SpecialPages ) |
| 116 |
); |
| 117 |
|
| 118 |
$names = $this->getFactory()->getNames(); |
| 119 |
$this->assertIsArray( $names ); |
| 120 |
$this->assertContains( 'testdummy', $names ); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function testResolveAlias() { |
| 127 |
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'de' ); |
| 128 |
|
| 129 |
[ $name, $param ] = $this->getFactory()->resolveAlias( 'Spezialseiten/Foo' ); |
| 130 |
$this->assertEquals( 'Specialpages', $name ); |
| 131 |
$this->assertEquals( 'Foo', $param ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function testGetLocalNameFor() { |
| 138 |
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'de' ); |
| 139 |
|
| 140 |
$name = $this->getFactory()->getLocalNameFor( 'Specialpages', 'Foo' ); |
| 141 |
$this->assertEquals( 'Spezialseiten/Foo', $name ); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function testGetTitleForAlias() { |
| 148 |
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'de' ); |
| 149 |
|
| 150 |
$title = $this->getFactory()->getTitleForAlias( 'Specialpages/Foo' ); |
| 151 |
$this->assertEquals( 'Spezialseiten/Foo', $title->getText() ); |
| 152 |
$this->assertEquals( NS_SPECIAL, $title->getNamespace() ); |
| 153 |
} |
| 154 |
|
| 155 |
public static function provideExecutePath() { |
| 156 |
yield [ 'BlankPage', 'intentionallyblankpage' ]; |
| 157 |
|
| 158 |
$path = new PageReferenceValue( NS_SPECIAL, 'BlankPage', PageReferenceValue::LOCAL ); |
| 159 |
yield [ $path, 'intentionallyblankpage' ]; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function testExecutePath( $path, $expected ) { |
| 167 |
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'qqx' ); |
| 168 |
|
| 169 |
$context = new RequestContext(); |
| 170 |
$context->setRequest( new FauxRequest() ); |
| 171 |
|
| 172 |
$output = new OutputPage( $context ); |
| 173 |
$context->setOutput( $output ); |
| 174 |
|
| 175 |
$this->getFactory()->executePath( $path, $context ); |
| 176 |
$this->assertStringContainsString( $expected, $output->getHTML() ); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function testConflictResolution( |
| 183 |
$test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings |
| 184 |
) { |
| 185 |
$lang = clone $this->getServiceContainer()->getContentLanguage(); |
| 186 |
$wrappedLang = TestingAccessWrapper::newFromObject( $lang ); |
| 187 |
$wrappedLang->mExtendedSpecialPageAliases = $aliasesList; |
| 188 |
$this->overrideConfigValues( [ |
| 189 |
MainConfigNames::DevelopmentWarnings => true, |
| 190 |
MainConfigNames::SpecialPages => |
| 191 |
array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) ) |
| 192 |
] ); |
| 193 |
$this->setContentLang( $lang ); |
| 194 |
|
| 195 |
|
| 196 |
$warnings = []; |
| 197 |
set_error_handler( static function ( $errno, $errstr ) use ( &$warnings ) { |
| 198 |
if ( preg_match( '/First alias \'[^\']*\' for .*/', $errstr ) || |
| 199 |
preg_match( '/Did not find a usable alias for special page .*/', $errstr ) |
| 200 |
) { |
| 201 |
$warnings[] = $errstr; |
| 202 |
return true; |
| 203 |
} |
| 204 |
return false; |
| 205 |
} ); |
| 206 |
$reset = new ScopedCallback( 'restore_error_handler' ); |
| 207 |
|
| 208 |
[ $name, ] = $this->getFactory()->resolveAlias( $alias ); |
| 209 |
$this->assertEquals( $expectedName, $name, "$test: Alias to name" ); |
| 210 |
$result = $this->getFactory()->getLocalNameFor( $name ); |
| 211 |
$this->assertEquals( $expectedAlias, $result, "$test: Alias to name to alias" ); |
| 212 |
|
| 213 |
$gotWarnings = count( $warnings ); |
| 214 |
if ( $gotWarnings !== $expectWarnings ) { |
| 215 |
$this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" . |
| 216 |
implode( "\n", $warnings ) |
| 217 |
); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function testConflictResolutionReversed( |
| 225 |
$test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings |
| 226 |
) { |
| 227 |
|
| 228 |
$aliasesList = array_reverse( $aliasesList ); |
| 229 |
$this->testConflictResolution( |
| 230 |
$test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
public static function provideTestConflictResolution() { |
| 235 |
return [ |
| 236 |
[ |
| 237 |
'Canonical name wins', |
| 238 |
[ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ], |
| 239 |
'Foo', |
| 240 |
'Foo', |
| 241 |
'Foo', |
| 242 |
1, |
| 243 |
], |
| 244 |
|
| 245 |
[ |
| 246 |
'Doesn\'t redirect to a different special page\'s canonical name', |
| 247 |
[ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ], |
| 248 |
'Baz', |
| 249 |
'Baz', |
| 250 |
'BazPage', |
| 251 |
1, |
| 252 |
], |
| 253 |
|
| 254 |
[ |
| 255 |
'Canonical name wins even if not aliased', |
| 256 |
[ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ], |
| 257 |
'Foo', |
| 258 |
'Foo', |
| 259 |
'FooPage', |
| 260 |
1, |
| 261 |
], |
| 262 |
|
| 263 |
[ |
| 264 |
'Doesn\'t redirect to a different special page\'s canonical name even if not aliased', |
| 265 |
[ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ], |
| 266 |
'Baz', |
| 267 |
'Baz', |
| 268 |
'BazPage', |
| 269 |
1, |
| 270 |
], |
| 271 |
|
| 272 |
[ |
| 273 |
'First local name beats non-first', |
| 274 |
[ 'First' => [ 'Foo' ], 'NonFirst' => [ 'Bar', 'Foo' ] ], |
| 275 |
'Foo', |
| 276 |
'First', |
| 277 |
'Foo', |
| 278 |
0, |
| 279 |
], |
| 280 |
|
| 281 |
[ |
| 282 |
'Doesn\'t redirect to a different special page\'s first alias', |
| 283 |
[ |
| 284 |
'Foo' => [ 'Foo' ], |
| 285 |
'First' => [ 'Bar' ], |
| 286 |
'Baz' => [ 'Foo', 'Bar', 'BazPage', 'Baz2' ] |
| 287 |
], |
| 288 |
'Baz', |
| 289 |
'Baz', |
| 290 |
'BazPage', |
| 291 |
1, |
| 292 |
], |
| 293 |
|
| 294 |
[ |
| 295 |
'Doesn\'t redirect wrong even if all aliases conflict', |
| 296 |
[ |
| 297 |
'Foo' => [ 'Foo' ], |
| 298 |
'First' => [ 'Bar' ], |
| 299 |
'Baz' => [ 'Foo', 'Bar' ] |
| 300 |
], |
| 301 |
'Baz', |
| 302 |
'Baz', |
| 303 |
'Baz', |
| 304 |
2, |
| 305 |
], |
| 306 |
|
| 307 |
]; |
| 308 |
} |
| 309 |
|
| 310 |
public function testGetAliasListRecursion() { |
| 311 |
$called = false; |
| 312 |
$this->setTemporaryHook( |
| 313 |
'SpecialPage_initList', |
| 314 |
function () use ( &$called ) { |
| 315 |
$this->getServiceContainer() |
| 316 |
->getSpecialPageFactory() |
| 317 |
->getLocalNameFor( 'Specialpages' ); |
| 318 |
$called = true; |
| 319 |
} |
| 320 |
); |
| 321 |
$this->getFactory()->getLocalNameFor( 'Specialpages' ); |
| 322 |
$this->assertTrue( $called, 'Recursive call succeeded' ); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function testSpecialPageCreationThatRequiresService() { |
| 329 |
$type = null; |
| 330 |
|
| 331 |
$this->overrideConfigValue( MainConfigNames::SpecialPages, |
| 332 |
[ 'TestPage' => [ |
| 333 |
'factory' => static function ( $spf ) use ( &$type ) { |
| 334 |
$type = get_class( $spf ); |
| 335 |
|
| 336 |
return new class() extends SpecialPage { |
| 337 |
|
| 338 |
}; |
| 339 |
}, |
| 340 |
'services' => [ |
| 341 |
'SpecialPageFactory' |
| 342 |
] |
| 343 |
] ] |
| 344 |
); |
| 345 |
|
| 346 |
$this->getFactory()->getPage( 'TestPage' ); |
| 347 |
|
| 348 |
$this->assertEquals( SpecialPageFactory::class, $type ); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public function testSpecialPageCapturePathExceptions() { |
| 355 |
$expectedException = new RuntimeException( 'Uh-oh!' ); |
| 356 |
$this->overrideConfigValue( MainConfigNames::SpecialPages, [ |
| 357 |
'ExceptionPage' => [ |
| 358 |
'factory' => static function () use ( $expectedException ) { |
| 359 |
return new class( $expectedException ) extends SpecialPage { |
| 360 |
private Exception $expectedException; |
| 361 |
|
| 362 |
public function __construct( $expectedException ) { |
| 363 |
parent::__construct(); |
| 364 |
$this->expectedException = $expectedException; |
| 365 |
} |
| 366 |
|
| 367 |
public function execute( $par ) { |
| 368 |
throw $this->expectedException; |
| 369 |
} |
| 370 |
|
| 371 |
public function isIncludable() { |
| 372 |
return true; |
| 373 |
} |
| 374 |
}; |
| 375 |
}, |
| 376 |
] |
| 377 |
] ); |
| 378 |
|
| 379 |
$factory = $this->getFactory(); |
| 380 |
$factory->getPage( 'ExceptionPage' ); |
| 381 |
|
| 382 |
$this->expectExceptionObject( $expectedException ); |
| 383 |
$factory->capturePath( |
| 384 |
Title::makeTitle( NS_SPECIAL, 'ExceptionPage' ), |
| 385 |
RequestContext::getMain() |
| 386 |
); |
| 387 |
} |
| 388 |
} |