MediaWiki  master
FauxRequestTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
9 
13  public function testConstructInvalidData() {
14  $this->setExpectedException( MWException::class, 'bogus data' );
15  $req = new FauxRequest( 'x' );
16  }
17 
21  public function testConstructInvalidSession() {
22  $this->setExpectedException( MWException::class, 'bogus session' );
23  $req = new FauxRequest( [], false, 'x' );
24  }
25 
29  public function testConstructWithSession() {
30  $session = SessionManager::singleton()->getEmptySession( new FauxRequest( [] ) );
31  $this->assertInstanceOf(
33  new FauxRequest( [], false, $session )
34  );
35  }
36 
40  public function testGetText() {
41  $req = new FauxRequest( [ 'x' => 'Value' ] );
42  $this->assertEquals( 'Value', $req->getText( 'x' ) );
43  $this->assertEquals( '', $req->getText( 'z' ) );
44  }
45 
50  public function testGetVal() {
51  $req = new FauxRequest( [ 'crlf' => "A\r\nb" ] );
52  $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
53  }
54 
59  public function testGetRawVal() {
60  $req = new FauxRequest( [
61  'x' => 'Value',
62  'y' => [ 'a' ],
63  'crlf' => "A\r\nb"
64  ] );
65  $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
66  $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
67  $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
68  $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
69  }
70 
74  public function testGetValues() {
75  $values = [ 'x' => 'Value', 'y' => '' ];
76  $req = new FauxRequest( $values );
77  $this->assertEquals( $values, $req->getValues() );
78  }
79 
83  public function testGetQueryValues() {
84  $values = [ 'x' => 'Value', 'y' => '' ];
85 
86  $req = new FauxRequest( $values );
87  $this->assertEquals( $values, $req->getQueryValues() );
88  $req = new FauxRequest( $values, /*wasPosted*/ true );
89  $this->assertEquals( [], $req->getQueryValues() );
90  }
91 
95  public function testGetMethod() {
96  $req = new FauxRequest( [] );
97  $this->assertEquals( 'GET', $req->getMethod() );
98  $req = new FauxRequest( [], /*wasPosted*/ true );
99  $this->assertEquals( 'POST', $req->getMethod() );
100  }
101 
105  public function testWasPosted() {
106  $req = new FauxRequest( [] );
107  $this->assertFalse( $req->wasPosted() );
108  $req = new FauxRequest( [], /*wasPosted*/ true );
109  $this->assertTrue( $req->wasPosted() );
110  }
111 
117  public function testCookies() {
118  $req = new FauxRequest();
119  $this->assertSame( null, $req->getCookie( 'z', '' ) );
120 
121  $req->setCookie( 'x', 'Value', '' );
122  $this->assertEquals( 'Value', $req->getCookie( 'x', '' ) );
123 
124  $req->setCookies( [ 'x' => 'One', 'y' => 'Two' ], '' );
125  $this->assertEquals( 'One', $req->getCookie( 'x', '' ) );
126  $this->assertEquals( 'Two', $req->getCookie( 'y', '' ) );
127  }
128 
134  public function testCookiesDefaultPrefix() {
135  global $wgCookiePrefix;
136  $oldPrefix = $wgCookiePrefix;
137  $wgCookiePrefix = '_';
138 
139  $req = new FauxRequest();
140  $this->assertSame( null, $req->getCookie( 'z' ) );
141 
142  $req->setCookie( 'x', 'Value' );
143  $this->assertEquals( 'Value', $req->getCookie( 'x' ) );
144 
145  $wgCookiePrefix = $oldPrefix;
146  }
147 
151  public function testGetRequestURL() {
152  $req = new FauxRequest();
153  $this->setExpectedException( MWException::class );
154  $req->getRequestURL();
155  }
156 
161  public function testSetRequestURL() {
162  $req = new FauxRequest();
163  $req->setRequestURL( 'https://example.org' );
164  $this->assertEquals( 'https://example.org', $req->getRequestURL() );
165  }
166 
171  public function testProtocol() {
172  $req = new FauxRequest();
173  $this->assertEquals( 'http', $req->getProtocol() );
174  $req = new FauxRequest( [], false, null, 'http' );
175  $this->assertEquals( 'http', $req->getProtocol() );
176  $req = new FauxRequest( [], false, null, 'https' );
177  $this->assertEquals( 'https', $req->getProtocol() );
178  }
179 
185  public function testGetSetHeader() {
186  $value = 'text/plain, text/html';
187 
188  $request = new FauxRequest();
189  $request->setHeader( 'Accept', $value );
190 
191  $this->assertEquals( $request->getHeader( 'Nonexistent' ), false );
192  $this->assertEquals( $request->getHeader( 'Accept' ), $value );
193  $this->assertEquals( $request->getHeader( 'ACCEPT' ), $value );
194  $this->assertEquals( $request->getHeader( 'accept' ), $value );
195  $this->assertEquals(
196  $request->getHeader( 'Accept', WebRequest::GETHEADER_LIST ),
197  [ 'text/plain', 'text/html' ]
198  );
199  }
200 
204  public function testGetAllHeaders() {
205  $_SERVER['HTTP_TEST'] = 'Example';
206 
207  $request = new FauxRequest();
208 
209  $this->assertEquals(
210  [],
211  $request->getAllHeaders()
212  );
213 
214  $this->assertEquals(
215  false,
216  $request->getHeader( 'test' )
217  );
218  }
219 
224  public function testSessionData() {
225  $values = [ 'x' => 'Value', 'y' => '' ];
226 
227  $req = new FauxRequest( [], false, /*session*/ $values );
228  $this->assertEquals( $values, $req->getSessionArray() );
229 
230  $req = new FauxRequest();
231  $this->assertSame( null, $req->getSessionArray() );
232  }
233 
239  public function testDummies() {
240  $req = new FauxRequest();
241  $this->assertEquals( '', $req->getRawQueryString() );
242  $this->assertEquals( '', $req->getRawPostString() );
243  $this->assertEquals( '', $req->getRawInput() );
244  }
245 }
testCookiesDefaultPrefix()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
testDummies()
FauxRequest::getRawQueryString FauxRequest::getRawPostString FauxRequest::getRawInput.
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
$value
const GETHEADER_LIST
Flag to make WebRequest::getHeader return an array of values.
Definition: WebRequest.php:48
testGetRawVal()
Integration test for parent method FauxRequest::getRawVal.
testConstructWithSession()
FauxRequest::__construct.
testConstructInvalidData()
FauxRequest::__construct.
testSetRequestURL()
FauxRequest::setRequestURL FauxRequest::getRequestURL.
testWasPosted()
FauxRequest::wasPosted.
testConstructInvalidSession()
FauxRequest::__construct.
testGetMethod()
FauxRequest::getMethod.
testProtocol()
FauxRequest::__construct FauxRequest::getProtocol.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
testGetAllHeaders()
FauxRequest::initHeaders.
testSessionData()
FauxRequest::__construct FauxRequest::getSessionArray.
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
this hook is for auditing only $req
Definition: hooks.txt:979
testGetQueryValues()
FauxRequest::getQueryValues.
testGetValues()
FauxRequest::getValues.
testGetText()
FauxRequest::getText.
testGetRequestURL()
FauxRequest::getRequestURL.
testGetVal()
Integration test for parent method FauxRequest::getVal.
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
testCookies()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
testGetSetHeader()
FauxRequest::setHeader FauxRequest::setHeaders FauxRequest::getHeader.
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:2633