3 use MediaWiki\Linker\LinkTarget
;
4 use MediaWiki\Title\Title
;
5 use MediaWiki\Title\TitleValue
;
8 * @covers \WikiFilePage
11 class WikiFilePageTest
extends MediaWikiLangTestCase
{
13 public static function provideFollowRedirect() {
14 yield
'local nonexisting' => [ null, [ 'exists' => false ], false ];
15 yield
'local existing' => [ 'Bla bla', [], false ];
16 yield
'local redirect' => [
17 '#REDIRECT [[Image:Target.png]]',
19 new TitleValue( NS_FILE
, 'Target.png' ),
22 yield
'remote nonexisting' => [ null,
29 yield
'remote existing' => [
31 [ 'isLocal' => false, ],
34 yield
'remote redirect' => [
38 'redirectedFrom' => 'Test.png',
39 'name' => 'Target.png',
41 new TitleValue( NS_FILE
, 'Target.png' ),
46 * @dataProvider provideFollowRedirect
48 public function testFollowRedirect( ?
string $content, array $fileProps, $expected ) {
49 $fileProps +
= [ 'name' => 'Test.png' ];
50 $this->installMockFileRepo( $fileProps );
52 if ( $content === null ) {
53 $pageIdentity = $this->getNonexistingTestPage( 'Image:Test.png' );
55 $status = $this->editPage( 'Image:Test.png', $content );
56 $pageIdentity = $status->getNewRevision()->getPage();
59 $page = new WikiFilePage( Title
::newFromPageIdentity( $pageIdentity ) );
60 $target = $page->followRedirect();
62 if ( $expected instanceof LinkTarget
) {
63 $this->assertTrue( $expected->isSameLinkAs( $target ) );
65 $this->assertSame( $expected, $target );
69 private function installMockFileRepo( array $props = [] ): void
{
70 $repo = $this->createNoOpMock(
74 $file = $this->createNoOpMock(
84 $file->method( 'isLocal' )->willReturn( $props['isLocal'] ??
true );
85 $file->method( 'exists' )->willReturn( $props['exists'] ??
true );
86 $file->method( 'getRepo' )->willReturn( $repo );
87 $file->method( 'getRedirected' )->willReturn( $props['redirectedFrom'] ??
null );
88 $file->method( 'getName' )->willReturn( $props['name'] ??
'Test.png' );
90 $localRepo = $this->createNoOpMock(
92 [ 'invalidateImageRedirect' ]
95 $repoGroup = $this->createNoOpMock(
97 [ 'findFile', 'getLocalRepo' ]
99 $repoGroup->method( 'getLocalRepo' )->willReturn( $localRepo );
100 $repoGroup->method( 'findFile' )->willReturn( $file );