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