From 485eaec771fd5efca1009f40b7bac8fe34fc83b3 Mon Sep 17 00:00:00 2001
From: Kosta Harlan <kharlan@wikimedia.org>
Date: Wed, 3 Oct 2018 12:47:14 -0400
Subject: [PATCH] Permissions: Return existing errors instead of empty array

Add test coverage for patrol action.

Bug: T206130
Change-Id: I2df0551c5837adc578b27082ab6ba2ac95d937f8
---
 includes/Title.php                            |  2 +-
 .../phpunit/includes/TitlePermissionTest.php  | 49 ++++++++++++++++---
 2 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/includes/Title.php b/includes/Title.php
index 5b0c3bc2dd..de551b4d23 100644
--- a/includes/Title.php
+++ b/includes/Title.php
@@ -2447,7 +2447,7 @@ class Title implements LinkTarget {
 		# XXX: this might be better using restrictions
 
 		if ( $action === 'patrol' ) {
-			return [];
+			return $errors;
 		}
 
 		if ( preg_match( '/^' . preg_quote( $user->getName(), '/' ) . '\//', $this->mTextform ) ) {
diff --git a/tests/phpunit/includes/TitlePermissionTest.php b/tests/phpunit/includes/TitlePermissionTest.php
index dd84b7eb01..9fb3e634e4 100644
--- a/tests/phpunit/includes/TitlePermissionTest.php
+++ b/tests/phpunit/includes/TitlePermissionTest.php
@@ -466,7 +466,8 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
 
 			[ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
 			[ [ 'badaccess-group0' ], [ 'mycustomjsprotected', 'bogus' ] ],
-			[ [ 'badaccess-group0' ] ]
+			[ [ 'badaccess-group0' ] ],
+			[ [ 'badaccess-groups', '[[Project:Administrators|Administrators]]', 1 ] ]
 		);
 	}
 
@@ -488,7 +489,8 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
 
 			[ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
 			[ [ 'badaccess-group0' ] ],
-			[ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ]
+			[ [ 'badaccess-group0' ], [ 'mycustomjsonprotected', 'bogus' ] ],
+			[ [ 'badaccess-groups', '[[Project:Administrators|Administrators]]', 1 ] ]
 		);
 	}
 
@@ -510,7 +512,8 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
 
 			[ [ 'badaccess-group0' ] ],
 			[ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
-			[ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ]
+			[ [ 'badaccess-group0' ], [ 'mycustomcssprotected', 'bogus' ] ],
+			[ [ 'badaccess-groups', '[[Project:Administrators|Administrators]]', 1 ] ]
 		);
 	}
 
@@ -532,7 +535,8 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
 
 			[ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
 			[ [ 'badaccess-group0' ], [ 'customjsprotected', 'bogus' ] ],
-			[ [ 'badaccess-group0' ] ]
+			[ [ 'badaccess-group0' ] ],
+			[ [ 'badaccess-groups', '[[Project:Administrators|Administrators]]', 1 ] ]
 		);
 	}
 
@@ -554,7 +558,8 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
 
 			[ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
 			[ [ 'badaccess-group0' ] ],
-			[ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ]
+			[ [ 'badaccess-group0' ], [ 'customjsonprotected', 'bogus' ] ],
+			[ [ 'badaccess-groups', '[[Project:Administrators|Administrators]]', 1 ] ]
 		);
 	}
 
@@ -576,7 +581,8 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
 
 			[ [ 'badaccess-group0' ] ],
 			[ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
-			[ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ]
+			[ [ 'badaccess-group0' ], [ 'customcssprotected', 'bogus' ] ],
+			[ [ 'badaccess-groups', '[[Project:Administrators|Administrators]]', 1 ] ]
 		);
 	}
 
@@ -598,7 +604,29 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
 
 			[ [ 'badaccess-group0' ] ],
 			[ [ 'badaccess-group0' ] ],
-			[ [ 'badaccess-group0' ] ]
+			[ [ 'badaccess-group0' ] ],
+			[ [ 'badaccess-groups', '[[Project:Administrators|Administrators]]', 1 ] ]
+		);
+	}
+
+	/**
+	 * @todo This should use data providers like the other methods here.
+	 * @covers Title::checkUserConfigPermissions
+	 */
+	public function testPatrolActionConfigEditPermissions() {
+		$this->setUser( 'anon' );
+		$this->setTitle( NS_USER, 'ToPatrolOrNotToPatrol' );
+		$this->runConfigEditPermissions(
+			[ [ 'badaccess-group0' ] ],
+
+			[ [ 'badaccess-group0' ] ],
+			[ [ 'badaccess-group0' ] ],
+			[ [ 'badaccess-group0' ] ],
+
+			[ [ 'badaccess-group0' ] ],
+			[ [ 'badaccess-group0' ] ],
+			[ [ 'badaccess-group0' ] ],
+			[ [ 'badaccess-groups', '[[Project:Administrators|Administrators]]', 1 ] ]
 		);
 	}
 
@@ -609,12 +637,17 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
 		$resultMyJs,
 		$resultUserCss,
 		$resultUserJson,
-		$resultUserJs
+		$resultUserJs,
+		$resultPatrol
 	) {
 		$this->setUserPerm( '' );
 		$result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
 		$this->assertEquals( $resultNone, $result );
 
+		$this->setUserPerm( '' );
+		$result = $this->title->getUserPermissionsErrors( 'patrol', $this->user );
+		$this->assertEquals( $resultPatrol, $result );
+
 		$this->setUserPerm( 'editmyusercss' );
 		$result = $this->title->getUserPermissionsErrors( 'bogus', $this->user );
 		$this->assertEquals( $resultMyCss, $result );
-- 
2.19.0

