Skip to content

Commit bd316b4

Browse files
committed
Migrate to proper unit test
This requires moving to a unit/ subdirectory to indicate that we do not need to load MW when running these tests. Additionally, instead of checking that we call various methods on OutputPage when denying access, refactor denyAccess to a protected method and mock it during testing, so that we can ensure denyAccess is called without running any of its code. The code itself requires the services container to be instantiated in order for wfMessage()->plain() to work, which we cannot do during pure unit tests. We should probably also add some integration tests to verify that denyAccess() behaves as expected, in addition to tests that exercise other aspects of the extension.
1 parent fa90cbb commit bd316b4

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

includes/Hooks.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function onMediaWikiPerformAction(
7575
|| $oldId > 0
7676
)
7777
) {
78-
self::denyAccess( $output );
78+
$this->denyAccess( $output );
7979
return false;
8080
}
8181

@@ -99,7 +99,7 @@ public function onSpecialPageBeforeExecute( $special, $subPage ) {
9999
$name = strtolower( $special->getName() );
100100
if ( in_array( $name, [ 'recentchangeslinked', 'whatlinkshere' ], true ) ) {
101101
$out = $special->getContext()->getOutput();
102-
self::denyAccess( $out );
102+
$this->denyAccess( $out );
103103
return false;
104104
}
105105

@@ -112,9 +112,15 @@ public function onSpecialPageBeforeExecute( $special, $subPage ) {
112112
* @param OutputPage $output
113113
* @return void
114114
*/
115-
private static function denyAccess( OutputPage $output ): void {
115+
protected function denyAccess( OutputPage $output ): void {
116116
$output->setStatusCode( 403 );
117-
$output->setPageTitle( wfMessage( 'crawlerprotection-accessdenied-title' )->text() );
118-
$output->addWikiTextAsInterface( wfMessage( 'crawlerprotection-accessdenied-text' )->text() );
117+
$output->addWikiTextAsInterface( wfMessage( 'crawlerprotection-accessdenied-text' )->plain() );
118+
119+
if ( version_compare( MW_VERSION, '1.41', '<' ) ) {
120+
$output->setPageTitle( wfMessage( 'crawlerprotection-accessdenied-title' ) );
121+
} else {
122+
// @phan-suppress-next-line PhanUndeclaredMethod Exists in 1.41+
123+
$output->setPageTitleMsg( wfMessage( 'crawlerprotection-accessdenied-title' ) );
124+
}
119125
}
120126
}
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ public static function setUpBeforeClass(): void {
5858
*/
5959
public function testRevisionTypeBlocksAnonymous() {
6060
$output = $this->createMock( self::$outputPageClassName );
61-
$output->expects( $this->once() )->method( 'setPageTitle' );
62-
$output->expects( $this->once() )->method( 'addWikiTextAsInterface' );
63-
$output->expects( $this->once() )->method( 'setStatusCode' )->with( 403 );
6461

6562
$request = $this->createMock( self::$webRequestClassName );
6663
$request->method( 'getVal' )->willReturnMap( [
@@ -74,7 +71,11 @@ public function testRevisionTypeBlocksAnonymous() {
7471
$title = $this->createMock( self::$titleClassName );
7572
$wiki = $this->createMock( self::$actionEntryPointClassName );
7673

77-
$runner = new Hooks();
74+
$runner = $this->getMockBuilder( Hooks::class )
75+
->onlyMethods( [ 'denyAccess' ] )
76+
->getMock();
77+
$runner->expects( $this->once() )->method( 'denyAccess' );
78+
7879
$result = $runner->onMediaWikiPerformAction( $output, $article, $title, $user, $request, $wiki );
7980
$this->assertFalse( $result );
8081
}
@@ -97,7 +98,11 @@ public function testRevisionTypeAllowsLoggedIn() {
9798
$title = $this->createMock( self::$titleClassName );
9899
$wiki = $this->createMock( self::$actionEntryPointClassName );
99100

100-
$runner = new Hooks();
101+
$runner = $this->getMockBuilder( Hooks::class )
102+
->onlyMethods( [ 'denyAccess' ] )
103+
->getMock();
104+
$runner->expects( $this->never() )->method( 'denyAccess' );
105+
101106
$result = $runner->onMediaWikiPerformAction( $output, $article, $title, $user, $request, $wiki );
102107
$this->assertTrue( $result );
103108
}
@@ -120,7 +125,11 @@ public function testNonRevisionTypeAlwaysAllowed() {
120125
$title = $this->createMock( self::$titleClassName );
121126
$wiki = $this->createMock( self::$actionEntryPointClassName );
122127

123-
$runner = new Hooks();
128+
$runner = $this->getMockBuilder( Hooks::class )
129+
->onlyMethods( [ 'denyAccess' ] )
130+
->getMock();
131+
$runner->expects( $this->never() )->method( 'denyAccess' );
132+
124133
$result = $runner->onMediaWikiPerformAction( $output, $article, $title, $user, $request, $wiki );
125134
$this->assertTrue( $result );
126135
}

0 commit comments

Comments
 (0)