Merge "Rewrite integration test for wfIsBadImage()"
[lhc/web/wiklou.git] / tests / phpunit / includes / GlobalFunctions / GlobalWithDBTest.php
1 <?php
2
3 /**
4 * @group GlobalFunctions
5 * @group Database
6 */
7 class GlobalWithDBTest extends MediaWikiTestCase {
8 const FILE_BLACKLIST = <<<WIKITEXT
9 Comment line, no effect [[File:Good.jpg]]
10 * Indented list is also a comment [[File:Good.jpg]]
11 * [[File:Bad.jpg]] except [[Nasty page]]
12 *[[Image:Bad2.jpg]] also works
13 * So does [[Bad3.jpg]]
14 * [[User:Bad4.jpg]] works although it is silly
15 * [[File:Redirect to good.jpg]] doesn't do anything if RepoGroup is working, because we only look at
16 the final name, but will work if RepoGroup returns null
17 * List line with no link
18 * [[Malformed title<>]] doesn't break anything, the line is ignored [[File:Good.jpg]]
19 * [[File:Bad5.jpg]] before [[malformed title<>]] doesn't ignore the line
20 WIKITEXT;
21
22 public static function badImageHook( $name, &$bad ) {
23 switch ( $name ) {
24 case 'Hook_bad.jpg':
25 case 'Redirect_to_hook_good.jpg':
26 $bad = true;
27 return false;
28
29 case 'Hook_good.jpg':
30 case 'Redirect_to_hook_bad.jpg':
31 $bad = false;
32 return false;
33 }
34
35 return true;
36 }
37
38 private function setUpBadImageTests( $name ) {
39 if ( in_array( $name, [
40 'Hook bad.jpg',
41 'Redirect to bad.jpg',
42 'Redirect_to_good.jpg',
43 'Redirect to hook bad.jpg',
44 'Redirect to hook good.jpg',
45 ] ) ) {
46 $this->markTestSkipped( "Didn't get RepoGroup working properly yet" );
47 }
48
49 // Don't try to fetch the files from Commons or anything, please
50 $this->setMwGlobals( 'wgForeignFileRepos', [] );
51 // We need to reset services immediately so that editPage() doesn't use the old RepoGroup
52 // and hit the network
53 $this->resetServices();
54
55 // XXX How do we get file redirects to work?
56 $this->editPage( 'File:Redirect to bad.jpg', '#REDIRECT [[Bad.jpg]]' );
57 $this->editPage( 'File:Redirect to good.jpg', '#REDIRECT [[Good.jpg]]' );
58 $this->editPage( 'File:Redirect to hook bad.jpg', '#REDIRECT [[Hook bad.jpg]]' );
59 $this->editPage( 'File:Redirect to hook good.jpg', '#REDIRECT [[Hook good.jpg]]' );
60
61 $this->setTemporaryHook( 'BadImage', __CLASS__ . '::badImageHook' );
62 }
63
64 /**
65 * @dataProvider provideIsBadFile
66 * @covers ::wfIsBadImage
67 */
68 public function testWfIsBadImage( $name, $title, $expected ) {
69 $this->setUpBadImageTests( $name );
70
71 $this->editPage( 'MediaWiki:Bad image list', self::FILE_BLACKLIST );
72 $this->resetServices();
73 // Enable messages from MediaWiki namespace
74 MessageCache::singleton()->enable();
75
76 $this->assertEquals( $expected, wfIsBadImage( $name, $title ) );
77 }
78
79 /**
80 * @dataProvider provideIsBadFile
81 * @covers ::wfIsBadImage
82 */
83 public function testWfIsBadImage_blacklistParam( $name, $title, $expected ) {
84 $this->setUpBadImageTests( $name );
85
86 $this->assertSame( $expected, wfIsBadImage( $name, $title, self::FILE_BLACKLIST ) );
87 }
88
89 public static function provideIsBadFile() {
90 return [
91 'No context page' => [ 'Bad.jpg', null, true ],
92 'Context page not whitelisted' =>
93 [ 'Bad.jpg', Title::makeTitleSafe( NS_MAIN, 'A page' ), true ],
94 'Good image' => [ 'Good.jpg', null, false ],
95 'Whitelisted context page' =>
96 [ 'Bad.jpg', Title::makeTitleSafe( NS_MAIN, 'Nasty page' ), false ],
97 'Bad image with Image:' => [ 'Image:Bad.jpg', null, false ],
98 'Bad image with File:' => [ 'File:Bad.jpg', null, false ],
99 'Bad image with Image: in blacklist' => [ 'Bad2.jpg', null, true ],
100 'Bad image without prefix in blacklist' => [ 'Bad3.jpg', null, true ],
101 'Bad image with different namespace in blacklist' => [ 'Bad4.jpg', null, true ],
102 'Redirect to bad image' => [ 'Redirect to bad.jpg', null, true ],
103 'Redirect to good image' => [ 'Redirect_to_good.jpg', null, false ],
104 'Hook says bad (with space)' => [ 'Hook bad.jpg', null, true ],
105 'Hook says bad (with underscore)' => [ 'Hook_bad.jpg', null, true ],
106 'Hook says good' => [ 'Hook good.jpg', null, false ],
107 'Redirect to hook bad image' => [ 'Redirect to hook bad.jpg', null, true ],
108 'Redirect to hook good image' => [ 'Redirect to hook good.jpg', null, false ],
109 'Malformed title doesn\'t break the line' => [ 'Bad5.jpg', null, true ],
110 ];
111 }
112 }