MediaWiki  master
MWBasicRequestAuthorizerTest.php
Go to the documentation of this file.
1 <?php
2 
4 
14 use User;
15 
25  private function createRouter( $userRights ) {
26  $user = User::newFromName( 'Test user' );
27 
28  $pm = new class( $user, $userRights ) extends PermissionManager {
29  private $testUser;
30  private $testUserRights;
31 
32  public function __construct( $user, $userRights ) {
33  $this->testUser = $user;
34  $this->testUserRights = $userRights;
35  }
36 
37  public function userHasRight( UserIdentity $user, $action = '' ) {
38  if ( $user === $this->testUser ) {
39  return $this->testUserRights[$action] ?? false;
40  }
41  return parent::userHasRight( $user, $action );
42  }
43  };
44 
45  global $IP;
46 
47  return new Router(
48  [ "$IP/tests/phpunit/unit/includes/Rest/testRoutes.json" ],
49  [],
50  '/rest',
51  new \EmptyBagOStuff(),
52  new ResponseFactory(),
53  new MWBasicAuthorizer( $user, $pm ) );
54  }
55 
56  public function testReadDenied() {
57  $router = $this->createRouter( [ 'read' => false ] );
58  $request = new RequestData( [ 'uri' => new Uri( '/rest/user/joe/hello' ) ] );
59  $response = $router->execute( $request );
60  $this->assertSame( 403, $response->getStatusCode() );
61 
62  $body = $response->getBody();
63  $body->rewind();
64  $data = json_decode( $body->getContents(), true );
65  $this->assertSame( 'rest-read-denied', $data['error'] );
66  }
67 
68  public function testReadAllowed() {
69  $router = $this->createRouter( [ 'read' => true ] );
70  $request = new RequestData( [ 'uri' => new Uri( '/rest/user/joe/hello' ) ] );
71  $response = $router->execute( $request );
72  $this->assertSame( 200, $response->getStatusCode() );
73  }
74 
75  public static function writeHandlerFactory() {
76  return new class extends Handler {
77  public function needsWriteAccess() {
78  return true;
79  }
80 
81  public function execute() {
82  return '';
83  }
84  };
85  }
86 
87  public function testWriteDenied() {
88  $router = $this->createRouter( [ 'read' => true, 'writeapi' => false ] );
89  $request = new RequestData( [
90  'uri' => new Uri( '/rest/mock/MWBasicRequestAuthorizerTest/write' )
91  ] );
92  $response = $router->execute( $request );
93  $this->assertSame( 403, $response->getStatusCode() );
94 
95  $body = $response->getBody();
96  $body->rewind();
97  $data = json_decode( $body->getContents(), true );
98  $this->assertSame( 'rest-write-denied', $data['error'] );
99  }
100 
101  public function testWriteAllowed() {
102  $router = $this->createRouter( [ 'read' => true, 'writeapi' => true ] );
103  $request = new RequestData( [
104  'uri' => new Uri( '/rest/mock/MWBasicRequestAuthorizerTest/write' )
105  ] );
106  $response = $router->execute( $request );
107 
108  $this->assertSame( 200, $response->getStatusCode() );
109  }
110 }
$batch execute()
$IP
Definition: WebStart.php:41
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Generates standardized response objects.
string $action
Cache what action this request is.
Definition: MediaWiki.php:48
The REST router is responsible for gathering handler configuration, matching an input path and HTTP m...
Definition: Router.php:16
this hook is for auditing only $response
Definition: hooks.txt:767
__construct(IContextSource $context=null)
Definition: MediaWiki.php:53
A factory for MWBasicRequestAuthorizer which passes through a User object.
Interface for objects representing user identity.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1972
This is a Request class that allows data to be injected, for the purposes of testing or internal requ...
Definition: RequestData.php:14
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:535
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2621
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1454