<?php
+use MediaWiki\Interwiki\InterwikiLookup;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices;
-use Wikimedia\TestingAccessWrapper;
/**
* @group Database
$this->setContentLang( 'en' );
}
- protected function tearDown() {
- // For testNewMainPage
- MessageCache::destroyInstance();
- parent::tearDown();
- }
-
/**
* @covers Title::legalChars
*/
];
}
- /**
- * Auth-less test of Title::isValidMoveOperation
- *
- * @param string $source
- * @param string $target
- * @param array|string|bool $expected Required error
- * @dataProvider provideTestIsValidMoveOperation
- * @covers Title::isValidMoveOperation
- */
- public function testIsValidMoveOperation( $source, $target, $expected ) {
- $this->hideDeprecated( 'Title::isValidMoveOperation' );
-
- $this->setMwGlobals( 'wgContentHandlerUseDB', false );
- $title = Title::newFromText( $source );
- $nt = Title::newFromText( $target );
- $errors = $title->isValidMoveOperation( $nt, false );
- if ( $expected === true ) {
- $this->assertTrue( $errors );
- } else {
- $errors = $this->flattenErrorsArray( $errors );
- foreach ( (array)$expected as $error ) {
- $this->assertContains( $error, $errors );
- }
- }
- }
-
- public static function provideTestIsValidMoveOperation() {
- return [
- // for Title::isValidMoveOperation
- [ 'Some page', '', 'badtitletext' ],
- [ 'Test', 'Test', 'selfmove' ],
- [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
- [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
- [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
- [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
- [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
- ];
- }
-
/**
* Auth-less test of Title::userCan
*
];
}
+ public function provideSubpage() {
+ // NOTE: avoid constructing Title objects in the provider, since it may access the database.
+ return [
+ [ 'Foo', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
+ [ 'Foo#bar', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
+ [ 'User:Foo', 'x', new TitleValue( NS_USER, 'Foo/x' ) ],
+ [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN, 'User:Foo/x', '', 'wiki' ) ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideSubpage
+ * @covers Title::getSubpage
+ */
+ public function testSubpage( $title, $sub, LinkTarget $expected ) {
+ $interwikiLookup = $this->getMock( InterwikiLookup::class );
+ $interwikiLookup->expects( $this->any() )
+ ->method( 'isValidInterwiki' )
+ ->willReturnCallback(
+ function ( $prefix ) {
+ return $prefix == 'wiki';
+ }
+ );
+
+ $this->setService( 'InterwikiLookup', $interwikiLookup );
+
+ $title = Title::newFromText( $title );
+ $expected = Title::newFromLinkTarget( $expected );
+ $actual = $title->getSubpage( $sub );
+
+ // NOTE: convert to string for comparison
+ $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
+ $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
+ }
+
public static function provideNewFromTitleValue() {
return [
[ new TitleValue( NS_MAIN, 'Foo' ) ],
// Tell Title it doesn't know whether it exists
$title->mArticleID = -1;
- // Tell the link cache it doesn't exists when it really does
+ // Tell the link cache it doesn't exist when it really does
$linkCache->clearLink( $title );
$linkCache->addBadLinkObj( $title );
$title->getOtherPage();
}
+ /**
+ * @dataProvider provideIsMovable
+ * @covers Title::isMovable
+ *
+ * @param string|Title $title
+ * @param bool $expected
+ * @param callable|null $hookCallback For TitleIsMovable
+ */
+ public function testIsMovable( $title, $expected, $hookCallback = null ) {
+ if ( $hookCallback ) {
+ $this->setTemporaryHook( 'TitleIsMovable', $hookCallback );
+ }
+ if ( is_string( $title ) ) {
+ $title = Title::newFromText( $title );
+ }
+
+ $this->assertSame( $expected, $title->isMovable() );
+ }
+
+ public static function provideIsMovable() {
+ return [
+ 'Simple title' => [ 'Foo', true ],
+ // @todo Should these next two really be true?
+ 'Empty name' => [ Title::makeTitle( NS_MAIN, '' ), true ],
+ 'Invalid name' => [ Title::makeTitle( NS_MAIN, '<' ), true ],
+ 'Interwiki' => [ Title::makeTitle( NS_MAIN, 'Test', '', 'otherwiki' ), false ],
+ 'Special page' => [ 'Special:FooBar', false ],
+ 'Aborted by hook' => [ 'Hooked in place', false,
+ function ( Title $title, &$result ) {
+ $result = false;
+ }
+ ],
+ ];
+ }
+
public function provideCreateFragmentTitle() {
return [
[ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
* @covers Title::newMainPage
*/
public function testNewMainPage() {
- $msgCache = TestingAccessWrapper::newFromClass( MessageCache::class );
- $msgCache->instance = $this->createMock( MessageCache::class );
- $msgCache->instance->method( 'get' )->willReturn( 'Foresheet' );
- $msgCache->instance->method( 'transform' )->willReturn( 'Foresheet' );
+ $mock = $this->createMock( MessageCache::class );
+ $mock->method( 'get' )->willReturn( 'Foresheet' );
+ $mock->method( 'transform' )->willReturn( 'Foresheet' );
+
+ $this->setService( 'MessageCache', $mock );
$this->assertSame(
'Foresheet',