22
33namespace MediaWiki \Extension \CrawlerProtection \Tests ;
44
5- use MediaWiki \Actions \ActionEntryPoint ;
65use MediaWiki \Extension \CrawlerProtection \Hooks ;
7- use MediaWiki \Output \OutputPage ;
8- use MediaWiki \Page \Article ;
9- use MediaWiki \Title \Title ;
106use PHPUnit \Framework \TestCase ;
11- use User ;
12- use WebRequest ;
137
148/**
159 * @coversDefaultClass \MediaWiki\Extension\CrawlerProtection\Hooks
1610 */
1711class HooksTest extends TestCase {
12+ /** @var string */
13+ private static string $ actionEntryPointClassName ;
14+
15+ /** @var string */
16+ private static string $ articleClassName ;
17+
18+ /** @var string */
19+ private static string $ outputPageClassName ;
20+
21+ /** @var string */
22+ private static string $ titleClassName ;
23+
24+ /** @var string */
25+ private static string $ userClassName ;
26+
27+ /** @var string */
28+ private static string $ webRequestClassName ;
29+
30+ public static function setUpBeforeClass (): void {
31+ self ::$ actionEntryPointClassName = class_exists ( '\MediaWiki\Actions\ActionEntryPoint ' )
32+ ? '\MediaWiki\Actions\ActionEntryPoint '
33+ : '\MediaWiki ' ;
34+
35+ self ::$ articleClassName = class_exists ( '\MediaWiki\Page\Article ' )
36+ ? '\MediaWiki\Page\Article '
37+ : '\Article ' ;
38+
39+ self ::$ outputPageClassName = class_exists ( '\MediaWiki\Output\OutputPage ' )
40+ ? '\MediaWiki\Output\OutputPage '
41+ : '\OutputPage ' ;
42+
43+ self ::$ titleClassName = class_exists ( '\MediaWiki\Title\Title ' )
44+ ? '\MediaWiki\Title\Title '
45+ : '\Title ' ;
46+
47+ self ::$ userClassName = class_exists ( '\MediaWiki\User\User ' )
48+ ? '\MediaWiki\User\User '
49+ : '\User ' ;
50+
51+ self ::$ webRequestClassName = class_exists ( '\MediaWiki\Request\WebRequest ' )
52+ ? '\MediaWiki\Request\WebRequest '
53+ : '\WebRequest ' ;
54+ }
55+
1856 /**
1957 * @covers ::onMediaWikiPerformAction
2058 */
2159 public function testRevisionTypeBlocksAnonymous () {
22- $ output = $ this ->createMock ( OutputPage::class );
60+ $ output = $ this ->createMock ( self :: $ outputPageClassName );
2361 $ output ->expects ( $ this ->once () )->method ( 'setPageTitle ' );
2462 $ output ->expects ( $ this ->once () )->method ( 'addWikiTextAsInterface ' );
2563 $ output ->expects ( $ this ->once () )->method ( 'setStatusCode ' )->with ( 403 );
2664
27- $ request = $ this ->createMock ( WebRequest::class );
28- $ request ->method ( 'getVal ' )->with ( 'type ' )->willReturn ( 'revision ' );
65+ $ request = $ this ->createMock ( self ::$ webRequestClassName );
66+ $ request ->method ( 'getVal ' )->willReturnMap ( [
67+ [ 'type ' , null , 'revision ' ],
68+ ] );
2969
30- $ user = $ this ->createMock ( User::class );
70+ $ user = $ this ->createMock ( self :: $ userClassName );
3171 $ user ->method ( 'isRegistered ' )->willReturn ( false );
3272
33- $ article = $ this ->createMock ( Article::class );
34- $ title = $ this ->createMock ( Title::class );
35- $ wiki = $ this ->createMock ( ActionEntryPoint::class );
73+ $ article = $ this ->createMock ( self :: $ articleClassName );
74+ $ title = $ this ->createMock ( self :: $ titleClassName );
75+ $ wiki = $ this ->createMock ( self :: $ actionEntryPointClassName );
3676
3777 $ runner = new Hooks ();
3878 $ result = $ runner ->onMediaWikiPerformAction ( $ output , $ article , $ title , $ user , $ request , $ wiki );
@@ -43,17 +83,19 @@ public function testRevisionTypeBlocksAnonymous() {
4383 * @covers ::onMediaWikiPerformAction
4484 */
4585 public function testRevisionTypeAllowsLoggedIn () {
46- $ output = $ this ->createMock ( OutputPage::class );
86+ $ output = $ this ->createMock ( self :: $ outputPageClassName );
4787
48- $ request = $ this ->createMock ( WebRequest::class );
49- $ request ->method ( 'getVal ' )->with ( 'type ' )->willReturn ( 'revision ' );
88+ $ request = $ this ->createMock ( self ::$ webRequestClassName );
89+ $ request ->method ( 'getVal ' )->willReturnMap ( [
90+ [ 'type ' , null , 'revision ' ],
91+ ] );
5092
51- $ user = $ this ->createMock ( User::class );
93+ $ user = $ this ->createMock ( self :: $ userClassName );
5294 $ user ->method ( 'isRegistered ' )->willReturn ( true );
5395
54- $ article = $ this ->createMock ( Article::class );
55- $ title = $ this ->createMock ( Title::class );
56- $ wiki = $ this ->createMock ( ActionEntryPoint::class );
96+ $ article = $ this ->createMock ( self :: $ articleClassName );
97+ $ title = $ this ->createMock ( self :: $ titleClassName );
98+ $ wiki = $ this ->createMock ( self :: $ actionEntryPointClassName );
5799
58100 $ runner = new Hooks ();
59101 $ result = $ runner ->onMediaWikiPerformAction ( $ output , $ article , $ title , $ user , $ request , $ wiki );
@@ -64,17 +106,19 @@ public function testRevisionTypeAllowsLoggedIn() {
64106 * @covers ::onMediaWikiPerformAction
65107 */
66108 public function testNonRevisionTypeAlwaysAllowed () {
67- $ output = $ this ->createMock ( OutputPage::class );
109+ $ output = $ this ->createMock ( self :: $ outputPageClassName );
68110
69- $ request = $ this ->createMock ( WebRequest::class );
70- $ request ->method ( 'getVal ' )->with ( 'type ' )->willReturn ( 'view ' );
111+ $ request = $ this ->createMock ( self ::$ webRequestClassName );
112+ $ request ->method ( 'getVal ' )->willReturnMap ( [
113+ [ 'type ' , null , 'view ' ],
114+ ] );
71115
72- $ user = $ this ->createMock ( User::class );
116+ $ user = $ this ->createMock ( self :: $ userClassName );
73117 $ user ->method ( 'isRegistered ' )->willReturn ( false );
74118
75- $ article = $ this ->createMock ( Article::class );
76- $ title = $ this ->createMock ( Title::class );
77- $ wiki = $ this ->createMock ( ActionEntryPoint::class );
119+ $ article = $ this ->createMock ( self :: $ articleClassName );
120+ $ title = $ this ->createMock ( self :: $ titleClassName );
121+ $ wiki = $ this ->createMock ( self :: $ actionEntryPointClassName );
78122
79123 $ runner = new Hooks ();
80124 $ result = $ runner ->onMediaWikiPerformAction ( $ output , $ article , $ title , $ user , $ request , $ wiki );
0 commit comments