1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
<?php
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleValue;
/**
* @covers \WikiFilePage
* @group Database
*/
class WikiFilePageTest extends MediaWikiLangTestCase {
public static function provideFollowRedirect() {
yield 'local nonexisting' => [ null, [ 'exists' => false ], false ];
yield 'local existing' => [ 'Bla bla', [], false ];
yield 'local redirect' => [
'#REDIRECT [[Image:Target.png]]',
[],
new TitleValue( NS_FILE, 'Target.png' ),
];
yield 'remote nonexisting' => [ null,
[
'isLocal' => false,
'exists' => false,
],
false,
];
yield 'remote existing' => [
null,
[ 'isLocal' => false, ],
false,
];
yield 'remote redirect' => [
null,
[
'isLocal' => false,
'redirectedFrom' => 'Test.png',
'name' => 'Target.png',
],
new TitleValue( NS_FILE, 'Target.png' ),
];
}
/**
* @dataProvider provideFollowRedirect
*/
public function testFollowRedirect( ?string $content, array $fileProps, $expected ) {
$fileProps += [ 'name' => 'Test.png' ];
$this->installMockFileRepo( $fileProps );
if ( $content === null ) {
$pageIdentity = $this->getNonexistingTestPage( 'Image:Test.png' );
} else {
$status = $this->editPage( 'Image:Test.png', $content );
$pageIdentity = $status->getNewRevision()->getPage();
}
$page = new WikiFilePage( Title::newFromPageIdentity( $pageIdentity ) );
$target = $page->followRedirect();
if ( $expected instanceof LinkTarget ) {
$this->assertTrue( $expected->isSameLinkAs( $target ) );
} else {
$this->assertSame( $expected, $target );
}
}
private function installMockFileRepo( array $props = [] ): void {
$repo = $this->createNoOpMock(
FileRepo::class,
[]
);
$file = $this->createNoOpMock(
File::class,
[
'isLocal',
'exists',
'getRepo',
'getRedirected',
'getName',
]
);
$file->method( 'isLocal' )->willReturn( $props['isLocal'] ?? true );
$file->method( 'exists' )->willReturn( $props['exists'] ?? true );
$file->method( 'getRepo' )->willReturn( $repo );
$file->method( 'getRedirected' )->willReturn( $props['redirectedFrom'] ?? null );
$file->method( 'getName' )->willReturn( $props['name'] ?? 'Test.png' );
$localRepo = $this->createNoOpMock(
FileRepo::class,
[ 'invalidateImageRedirect' ]
);
$repoGroup = $this->createNoOpMock(
RepoGroup::class,
[ 'findFile', 'getLocalRepo' ]
);
$repoGroup->method( 'getLocalRepo' )->willReturn( $localRepo );
$repoGroup->method( 'findFile' )->willReturn( $file );
$this->setService(
'RepoGroup',
$repoGroup
);
}
}
|