From: Aryeh Gregor Date: Wed, 21 Aug 2019 08:14:57 +0000 (+0300) Subject: Rewrite integration test for wfIsBadImage() X-Git-Tag: 1.34.0-rc.0~633^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22suivi_revisions%22%29%20.%20%22?a=commitdiff_plain;h=5600df580a4b44427470082281a93680cd2d5c8c;p=lhc%2Fweb%2Fwiklou.git Rewrite integration test for wfIsBadImage() In preparation for rewriting as a service. I didn't figure out how to get file redirects to be tested. Change-Id: Ic6669a19a13025744036f9f8adc4d1a25490fb42 --- diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalWithDBTest.php b/tests/phpunit/includes/GlobalFunctions/GlobalWithDBTest.php index 0765ab8b4d..f0f449bd14 100644 --- a/tests/phpunit/includes/GlobalFunctions/GlobalWithDBTest.php +++ b/tests/phpunit/includes/GlobalFunctions/GlobalWithDBTest.php @@ -5,28 +5,108 @@ * @group Database */ class GlobalWithDBTest extends MediaWikiTestCase { + const FILE_BLACKLIST = <<]] doesn't break anything, the line is ignored [[File:Good.jpg]] +* [[File:Bad5.jpg]] before [[malformed title<>]] doesn't ignore the line +WIKITEXT; + + public static function badImageHook( $name, &$bad ) { + switch ( $name ) { + case 'Hook_bad.jpg': + case 'Redirect_to_hook_good.jpg': + $bad = true; + return false; + + case 'Hook_good.jpg': + case 'Redirect_to_hook_bad.jpg': + $bad = false; + return false; + } + + return true; + } + + private function setUpBadImageTests( $name ) { + if ( in_array( $name, [ + 'Hook bad.jpg', + 'Redirect to bad.jpg', + 'Redirect_to_good.jpg', + 'Redirect to hook bad.jpg', + 'Redirect to hook good.jpg', + ] ) ) { + $this->markTestSkipped( "Didn't get RepoGroup working properly yet" ); + } + + // Don't try to fetch the files from Commons or anything, please + $this->setMwGlobals( 'wgForeignFileRepos', [] ); + // We need to reset services immediately so that editPage() doesn't use the old RepoGroup + // and hit the network + $this->resetServices(); + + // XXX How do we get file redirects to work? + $this->editPage( 'File:Redirect to bad.jpg', '#REDIRECT [[Bad.jpg]]' ); + $this->editPage( 'File:Redirect to good.jpg', '#REDIRECT [[Good.jpg]]' ); + $this->editPage( 'File:Redirect to hook bad.jpg', '#REDIRECT [[Hook bad.jpg]]' ); + $this->editPage( 'File:Redirect to hook good.jpg', '#REDIRECT [[Hook good.jpg]]' ); + + $this->setTemporaryHook( 'BadImage', __CLASS__ . '::badImageHook' ); + } + /** - * @dataProvider provideWfIsBadImageList + * @dataProvider provideIsBadFile * @covers ::wfIsBadImage */ - public function testWfIsBadImage( $name, $title, $blacklist, $expected, $desc ) { - $this->assertEquals( $expected, wfIsBadImage( $name, $title, $blacklist ), $desc ); + public function testWfIsBadImage( $name, $title, $expected ) { + $this->setUpBadImageTests( $name ); + + $this->editPage( 'MediaWiki:Bad image list', self::FILE_BLACKLIST ); + $this->resetServices(); + // Enable messages from MediaWiki namespace + MessageCache::singleton()->enable(); + + $this->assertEquals( $expected, wfIsBadImage( $name, $title ) ); } - public static function provideWfIsBadImageList() { - $blacklist = '* [[File:Bad.jpg]] except [[Nasty page]]'; + /** + * @dataProvider provideIsBadFile + * @covers ::wfIsBadImage + */ + public function testWfIsBadImage_blacklistParam( $name, $title, $expected ) { + $this->setUpBadImageTests( $name ); + + $this->assertSame( $expected, wfIsBadImage( $name, $title, self::FILE_BLACKLIST ) ); + } + public static function provideIsBadFile() { return [ - [ 'Bad.jpg', false, $blacklist, true, - 'Called on a bad image' ], - [ 'Bad.jpg', Title::makeTitle( NS_MAIN, 'A page' ), $blacklist, true, - 'Called on a bad image' ], - [ 'NotBad.jpg', false, $blacklist, false, - 'Called on a non-bad image' ], - [ 'Bad.jpg', Title::makeTitle( NS_MAIN, 'Nasty page' ), $blacklist, false, - 'Called on a bad image but is on a whitelisted page' ], - [ 'File:Bad.jpg', false, $blacklist, false, - 'Called on a bad image with File:' ], + 'No context page' => [ 'Bad.jpg', null, true ], + 'Context page not whitelisted' => + [ 'Bad.jpg', Title::makeTitleSafe( NS_MAIN, 'A page' ), true ], + 'Good image' => [ 'Good.jpg', null, false ], + 'Whitelisted context page' => + [ 'Bad.jpg', Title::makeTitleSafe( 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 ], ]; } }