Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / tests / phpunit / includes / page / WikiFilePageTest.php
blobb838ea5faf91495c80aa3a74482fba2a4ec29c9b
1 <?php
3 use MediaWiki\Linker\LinkTarget;
4 use MediaWiki\Title\Title;
5 use MediaWiki\Title\TitleValue;
7 /**
8 * @covers \WikiFilePage
9 * @group Database
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]]',
18 [],
19 new TitleValue( NS_FILE, 'Target.png' ),
22 yield 'remote nonexisting' => [ null,
24 'isLocal' => false,
25 'exists' => false,
27 false,
29 yield 'remote existing' => [
30 null,
31 [ 'isLocal' => false, ],
32 false,
34 yield 'remote redirect' => [
35 null,
37 'isLocal' => false,
38 'redirectedFrom' => 'Test.png',
39 'name' => 'Target.png',
41 new TitleValue( NS_FILE, 'Target.png' ),
45 /**
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' );
54 } else {
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 ) );
64 } else {
65 $this->assertSame( $expected, $target );
69 private function installMockFileRepo( array $props = [] ): void {
70 $repo = $this->createNoOpMock(
71 FileRepo::class,
74 $file = $this->createNoOpMock(
75 File::class,
77 'isLocal',
78 'exists',
79 'getRepo',
80 'getRedirected',
81 'getName',
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(
91 FileRepo::class,
92 [ 'invalidateImageRedirect' ]
95 $repoGroup = $this->createNoOpMock(
96 RepoGroup::class,
97 [ 'findFile', 'getLocalRepo' ]
99 $repoGroup->method( 'getLocalRepo' )->willReturn( $localRepo );
100 $repoGroup->method( 'findFile' )->willReturn( $file );
102 $this->setService(
103 'RepoGroup',
104 $repoGroup