2019-08-18 18:19:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\BadFileLookup;
|
2020-11-04 19:00:40 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2019-08-18 18:19:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @coversDefaultClass MediaWiki\BadFileLookup
|
|
|
|
|
*/
|
|
|
|
|
class BadFileLookupTest extends MediaWikiUnitTestCase {
|
|
|
|
|
/** Shared with GlobalWithDBTest */
|
2020-06-08 23:53:00 +00:00
|
|
|
public const BAD_FILE_LIST = <<<WIKITEXT
|
2019-08-18 18:19:05 +00:00
|
|
|
Comment line, no effect [[File:Good.jpg]]
|
|
|
|
|
* Indented list is also a comment [[File:Good.jpg]]
|
|
|
|
|
* [[File:Bad.jpg]] except [[Nasty page]]
|
|
|
|
|
*[[Image:Bad2.jpg]] also works
|
|
|
|
|
* So does [[Bad3.jpg]]
|
|
|
|
|
* [[User:Bad4.jpg]] works although it is silly
|
|
|
|
|
* [[File:Redirect to good.jpg]] doesn't do anything if RepoGroup is working, because we only look at
|
|
|
|
|
the final name, but will work if RepoGroup returns null
|
|
|
|
|
* List line with no link
|
|
|
|
|
* [[Malformed title<>]] doesn't break anything, the line is ignored [[File:Good.jpg]]
|
|
|
|
|
* [[File:Bad5.jpg]] before [[malformed title<>]] doesn't ignore the line
|
|
|
|
|
WIKITEXT;
|
|
|
|
|
|
2020-11-04 19:00:40 +00:00
|
|
|
/** @var HookContainer */
|
|
|
|
|
private $hookContainer;
|
|
|
|
|
|
2019-08-18 18:19:05 +00:00
|
|
|
/** Shared with GlobalWithDBTest */
|
|
|
|
|
public static function badImageHook( $name, &$bad ) {
|
|
|
|
|
switch ( $name ) {
|
2020-07-17 21:37:26 +00:00
|
|
|
case 'Hook_bad.jpg':
|
|
|
|
|
case 'Redirect_to_hook_good.jpg':
|
|
|
|
|
$bad = true;
|
|
|
|
|
return false;
|
2019-08-18 18:19:05 +00:00
|
|
|
|
2020-07-17 21:37:26 +00:00
|
|
|
case 'Hook_good.jpg':
|
|
|
|
|
case 'Redirect_to_hook_bad.jpg':
|
|
|
|
|
$bad = false;
|
|
|
|
|
return false;
|
2019-08-18 18:19:05 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getMockRepoGroup() {
|
|
|
|
|
$mock = $this->createMock( RepoGroup::class );
|
|
|
|
|
$mock->expects( $this->once() )->method( 'findFile' )
|
|
|
|
|
->will( $this->returnCallback( function ( $name ) {
|
|
|
|
|
$mockFile = $this->createMock( File::class );
|
|
|
|
|
$mockFile->expects( $this->once() )->method( 'getTitle' )
|
|
|
|
|
->will( $this->returnCallback( function () use ( $name ) {
|
|
|
|
|
switch ( $name ) {
|
2020-07-17 21:37:26 +00:00
|
|
|
case 'Redirect to bad.jpg':
|
|
|
|
|
return new TitleValue( NS_FILE, 'Bad.jpg' );
|
|
|
|
|
case 'Redirect_to_good.jpg':
|
|
|
|
|
return new TitleValue( NS_FILE, 'Good.jpg' );
|
|
|
|
|
case 'Redirect to hook bad.jpg':
|
|
|
|
|
return new TitleValue( NS_FILE, 'Hook_bad.jpg' );
|
|
|
|
|
case 'Redirect to hook good.jpg':
|
|
|
|
|
return new TitleValue( NS_FILE, 'Hook_good.jpg' );
|
|
|
|
|
default:
|
|
|
|
|
return new TitleValue( NS_FILE, $name );
|
2019-08-18 18:19:05 +00:00
|
|
|
}
|
|
|
|
|
} ) );
|
|
|
|
|
$mockFile->expects( $this->never() )->method( $this->anythingBut( 'getTitle' ) );
|
|
|
|
|
return $mockFile;
|
|
|
|
|
} ) );
|
|
|
|
|
$mock->expects( $this->never() )->method( $this->anythingBut( 'findFile' ) );
|
|
|
|
|
|
|
|
|
|
return $mock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Just returns null for every findFile().
|
|
|
|
|
*/
|
|
|
|
|
private function getMockRepoGroupNull() {
|
|
|
|
|
$mock = $this->createMock( RepoGroup::class );
|
|
|
|
|
$mock->expects( $this->once() )->method( 'findFile' )->willReturn( null );
|
|
|
|
|
$mock->expects( $this->never() )->method( $this->anythingBut( 'findFile' ) );
|
|
|
|
|
|
|
|
|
|
return $mock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getMockTitleParser() {
|
|
|
|
|
$mock = $this->createMock( TitleParser::class );
|
|
|
|
|
$mock->method( 'parseTitle' )->will( $this->returnCallback( function ( $text ) {
|
|
|
|
|
if ( strpos( $text, '<' ) !== false ) {
|
|
|
|
|
throw $this->createMock( MalformedTitleException::class );
|
|
|
|
|
}
|
|
|
|
|
if ( strpos( $text, ':' ) === false ) {
|
|
|
|
|
return new TitleValue( NS_MAIN, $text );
|
|
|
|
|
}
|
|
|
|
|
list( $ns, $text ) = explode( ':', $text );
|
|
|
|
|
switch ( $ns ) {
|
2020-07-17 21:37:26 +00:00
|
|
|
case 'Image':
|
|
|
|
|
case 'File':
|
|
|
|
|
$ns = NS_FILE;
|
|
|
|
|
break;
|
2019-08-18 18:19:05 +00:00
|
|
|
|
2020-07-17 21:37:26 +00:00
|
|
|
case 'User':
|
|
|
|
|
$ns = NS_USER;
|
|
|
|
|
break;
|
2019-08-18 18:19:05 +00:00
|
|
|
}
|
|
|
|
|
return new TitleValue( $ns, $text );
|
|
|
|
|
} ) );
|
|
|
|
|
$mock->expects( $this->never() )->method( $this->anythingBut( 'parseTitle' ) );
|
|
|
|
|
|
|
|
|
|
return $mock;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 10:51:39 +00:00
|
|
|
protected function setUp() : void {
|
2019-08-18 18:19:05 +00:00
|
|
|
parent::setUp();
|
2020-11-04 19:00:40 +00:00
|
|
|
$this->hookContainer = $this->createHookContainer( [
|
|
|
|
|
'BadImage' => __CLASS__ . '::badImageHook'
|
|
|
|
|
] );
|
2019-08-18 18:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsBadFile
|
|
|
|
|
* @covers ::__construct
|
|
|
|
|
* @covers ::isBadFile
|
|
|
|
|
*/
|
|
|
|
|
public function testIsBadFile( $name, $title, $expected ) {
|
|
|
|
|
$bfl = new BadFileLookup(
|
|
|
|
|
function () {
|
2020-06-08 23:53:00 +00:00
|
|
|
return self::BAD_FILE_LIST;
|
2019-08-18 18:19:05 +00:00
|
|
|
},
|
|
|
|
|
new EmptyBagOStuff,
|
|
|
|
|
$this->getMockRepoGroup(),
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->getMockTitleParser(),
|
2020-11-04 19:00:40 +00:00
|
|
|
$this->hookContainer
|
2019-08-18 18:19:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $expected, $bfl->isBadFile( $name, $title ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsBadFile
|
|
|
|
|
* @covers ::__construct
|
|
|
|
|
* @covers ::isBadFile
|
|
|
|
|
*/
|
|
|
|
|
public function testIsBadFile_nullRepoGroup( $name, $title, $expected ) {
|
|
|
|
|
$bfl = new BadFileLookup(
|
|
|
|
|
function () {
|
2020-06-08 23:53:00 +00:00
|
|
|
return self::BAD_FILE_LIST;
|
2019-08-18 18:19:05 +00:00
|
|
|
},
|
|
|
|
|
new EmptyBagOStuff,
|
|
|
|
|
$this->getMockRepoGroupNull(),
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->getMockTitleParser(),
|
2020-11-04 19:00:40 +00:00
|
|
|
$this->hookContainer
|
2019-08-18 18:19:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Hack -- these expectations are reversed if the repo group returns null. In that case 1)
|
|
|
|
|
// we don't honor redirects, and 2) we don't replace spaces by underscores (which makes the
|
|
|
|
|
// hook not see 'Hook bad.jpg').
|
|
|
|
|
if ( in_array( $name, [
|
|
|
|
|
'Redirect to bad.jpg',
|
|
|
|
|
'Redirect_to_good.jpg',
|
|
|
|
|
'Hook bad.jpg',
|
|
|
|
|
'Redirect to hook bad.jpg',
|
|
|
|
|
] ) ) {
|
|
|
|
|
$expected = !$expected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $expected, $bfl->isBadFile( $name, $title ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Shared with GlobalWithDBTest */
|
|
|
|
|
public static function provideIsBadFile() {
|
|
|
|
|
return [
|
|
|
|
|
'No context page' => [ 'Bad.jpg', null, true ],
|
|
|
|
|
'Context page not whitelisted' =>
|
|
|
|
|
[ 'Bad.jpg', new TitleValue( NS_MAIN, 'A page' ), true ],
|
|
|
|
|
'Good image' => [ 'Good.jpg', null, false ],
|
|
|
|
|
'Whitelisted context page' =>
|
|
|
|
|
[ 'Bad.jpg', new TitleValue( NS_MAIN, 'Nasty page' ), false ],
|
|
|
|
|
'Bad image with Image:' => [ 'Image:Bad.jpg', null, false ],
|
|
|
|
|
'Bad image with File:' => [ 'File:Bad.jpg', null, false ],
|
|
|
|
|
'Bad image with Image: in blacklist' => [ 'Bad2.jpg', null, true ],
|
|
|
|
|
'Bad image without prefix in blacklist' => [ 'Bad3.jpg', null, true ],
|
|
|
|
|
'Bad image with different namespace in blacklist' => [ 'Bad4.jpg', null, true ],
|
|
|
|
|
'Redirect to bad image' => [ 'Redirect to bad.jpg', null, true ],
|
|
|
|
|
'Redirect to good image' => [ 'Redirect_to_good.jpg', null, false ],
|
|
|
|
|
'Hook says bad (with space)' => [ 'Hook bad.jpg', null, true ],
|
|
|
|
|
'Hook says bad (with underscore)' => [ 'Hook_bad.jpg', null, true ],
|
|
|
|
|
'Hook says good' => [ 'Hook good.jpg', null, false ],
|
|
|
|
|
'Redirect to hook bad image' => [ 'Redirect to hook bad.jpg', null, true ],
|
|
|
|
|
'Redirect to hook good image' => [ 'Redirect to hook good.jpg', null, false ],
|
|
|
|
|
'Malformed title doesn\'t break the line' => [ 'Bad5.jpg', null, true ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|