| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\HookContainer\HookContainer; |
| 4 |
use MediaWiki\Page\File\BadFileLookup; |
| 5 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 6 |
use MediaWiki\Title\TitleValue; |
| 7 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class BadFileLookupTest extends MediaWikiUnitTestCase { |
| 13 |
use DummyServicesTrait; |
| 14 |
|
| 15 |
|
| 16 |
public const BAD_FILE_LIST = <<<WIKITEXT |
| 17 |
Comment line, no effect [[File:Good.jpg]] |
| 18 |
* Indented list is also a comment [[File:Good.jpg]] |
| 19 |
* [[File:Bad.jpg]] except [[Nasty page]] |
| 20 |
*[[Image:Bad2.jpg]] also works |
| 21 |
* So does [[Bad3.jpg]] |
| 22 |
* [[User:Bad4.jpg]] works although it is silly |
| 23 |
* [[File:Redirect to good.jpg]] does not do anything if RepoGroup is working, because we only look at |
| 24 |
the final name, but will work if RepoGroup returns null |
| 25 |
* List line with no link |
| 26 |
* [[Malformed title<>]] does not break anything, the line is ignored [[File:Good.jpg]] |
| 27 |
* [[File:Bad5.jpg]] before [[malformed title<>]] does not ignore the line |
| 28 |
WIKITEXT; |
| 29 |
|
| 30 |
private HookContainer $hookContainer; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static function badImageHook( $name, &$bad ) { |
| 39 |
switch ( $name ) { |
| 40 |
case 'Hook_bad.jpg': |
| 41 |
case 'Redirect_to_hook_good.jpg': |
| 42 |
$bad = true; |
| 43 |
return false; |
| 44 |
|
| 45 |
case 'Hook_good.jpg': |
| 46 |
case 'Redirect_to_hook_bad.jpg': |
| 47 |
$bad = false; |
| 48 |
return false; |
| 49 |
} |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
private function getMockRepoGroup() { |
| 54 |
$mock = $this->createNoOpMock( RepoGroup::class, [ 'findFile' ] ); |
| 55 |
$mock->expects( $this->once() )->method( 'findFile' ) |
| 56 |
->willReturnCallback( function ( $name ) { |
| 57 |
$mockFile = $this->createNoOpMock( File::class, [ 'getTitle' ] ); |
| 58 |
$mockFile->expects( $this->once() )->method( 'getTitle' ) |
| 59 |
->willReturnCallback( static function () use ( $name ) { |
| 60 |
$redirectMap = [ |
| 61 |
'Redirect to bad.jpg' => 'Bad.jpg', |
| 62 |
'Redirect_to_good.jpg' => 'Good.jpg', |
| 63 |
'Redirect to hook bad.jpg' => 'Hook_bad.jpg', |
| 64 |
'Redirect to hook good.jpg' => 'Hook_good.jpg', |
| 65 |
]; |
| 66 |
$redirectTarget = $redirectMap[$name] ?? $name; |
| 67 |
return new TitleValue( NS_FILE, $redirectTarget ); |
| 68 |
} ); |
| 69 |
return $mockFile; |
| 70 |
} ); |
| 71 |
|
| 72 |
return $mock; |
| 73 |
} |
| 74 |
|
| 75 |
protected function setUp(): void { |
| 76 |
parent::setUp(); |
| 77 |
$this->hookContainer = $this->createHookContainer( [ |
| 78 |
'BadImage' => __CLASS__ . '::badImageHook' |
| 79 |
] ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function testIsBadFile( $name, $title, $expected ) { |
| 88 |
$bfl = new BadFileLookup( |
| 89 |
static function () { |
| 90 |
return self::BAD_FILE_LIST; |
| 91 |
}, |
| 92 |
new EmptyBagOStuff, |
| 93 |
$this->getMockRepoGroup(), |
| 94 |
$this->getDummyTitleParser( [ 'throwMockExceptions' => true ] ), |
| 95 |
$this->hookContainer |
| 96 |
); |
| 97 |
|
| 98 |
$this->assertSame( $expected, $bfl->isBadFile( $name, $title ) ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function testIsBadFile_nullRepoGroup( $name, $title, $expected ) { |
| 107 |
$nullRepoGroup = $this->createNoOpMock( RepoGroup::class, [ 'findFile' ] ); |
| 108 |
$nullRepoGroup->expects( $this->once() )->method( 'findFile' )->willReturn( null ); |
| 109 |
|
| 110 |
$bfl = new BadFileLookup( |
| 111 |
static function () { |
| 112 |
return self::BAD_FILE_LIST; |
| 113 |
}, |
| 114 |
new EmptyBagOStuff, |
| 115 |
$nullRepoGroup, |
| 116 |
$this->getDummyTitleParser( [ 'throwMockExceptions' => true ] ), |
| 117 |
$this->hookContainer |
| 118 |
); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if ( in_array( $name, [ |
| 124 |
'Redirect to bad.jpg', |
| 125 |
'Redirect_to_good.jpg', |
| 126 |
'Hook bad.jpg', |
| 127 |
'Redirect to hook bad.jpg', |
| 128 |
] ) ) { |
| 129 |
$expected = !$expected; |
| 130 |
} |
| 131 |
|
| 132 |
$this->assertSame( $expected, $bfl->isBadFile( $name, $title ) ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
public static function provideIsBadFile() { |
| 137 |
return [ |
| 138 |
'No context page' => [ 'Bad.jpg', null, true ], |
| 139 |
'Context page not whitelisted' => |
| 140 |
[ 'Bad.jpg', new TitleValue( NS_MAIN, 'A page' ), true ], |
| 141 |
'Good image' => [ 'Good.jpg', null, false ], |
| 142 |
'Whitelisted context page' => |
| 143 |
[ 'Bad.jpg', new TitleValue( NS_MAIN, 'Nasty page' ), false ], |
| 144 |
'Bad image with Image:' => [ 'Image:Bad.jpg', null, false ], |
| 145 |
'Bad image with File:' => [ 'File:Bad.jpg', null, false ], |
| 146 |
'Bad image with Image: in blacklist' => [ 'Bad2.jpg', null, true ], |
| 147 |
'Bad image without prefix in blacklist' => [ 'Bad3.jpg', null, true ], |
| 148 |
'Bad image with different namespace in blacklist' => [ 'Bad4.jpg', null, true ], |
| 149 |
'Redirect to bad image' => [ 'Redirect to bad.jpg', null, true ], |
| 150 |
'Redirect to good image' => [ 'Redirect_to_good.jpg', null, false ], |
| 151 |
'Hook says bad (with space)' => [ 'Hook bad.jpg', null, true ], |
| 152 |
'Hook says bad (with underscore)' => [ 'Hook_bad.jpg', null, true ], |
| 153 |
'Hook says good' => [ 'Hook good.jpg', null, false ], |
| 154 |
'Redirect to hook bad image' => [ 'Redirect to hook bad.jpg', null, true ], |
| 155 |
'Redirect to hook good image' => [ 'Redirect to hook good.jpg', null, false ], |
| 156 |
'Malformed title does not break the line' => [ 'Bad5.jpg', null, true ], |
| 157 |
]; |
| 158 |
} |
| 159 |
} |