MediaWiki  master
FauxRequestTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
9 
10  public function setUp() {
11  parent::setUp();
12  $this->orgWgServer = $GLOBALS['wgServer'];
13  }
14 
15  public function tearDown() {
16  $GLOBALS['wgServer'] = $this->orgWgServer;
17  parent::tearDown();
18  }
19 
23  public function testConstructInvalidData() {
24  $this->setExpectedException( MWException::class, 'bogus data' );
25  $req = new FauxRequest( 'x' );
26  }
27 
31  public function testConstructInvalidSession() {
32  $this->setExpectedException( MWException::class, 'bogus session' );
33  $req = new FauxRequest( [], false, 'x' );
34  }
35 
39  public function testConstructWithSession() {
40  $session = SessionManager::singleton()->getEmptySession( new FauxRequest( [] ) );
41  $this->assertInstanceOf(
43  new FauxRequest( [], false, $session )
44  );
45  }
46 
50  public function testGetText() {
51  $req = new FauxRequest( [ 'x' => 'Value' ] );
52  $this->assertEquals( 'Value', $req->getText( 'x' ) );
53  $this->assertEquals( '', $req->getText( 'z' ) );
54  }
55 
60  public function testGetVal() {
61  $req = new FauxRequest( [ 'crlf' => "A\r\nb" ] );
62  $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
63  }
64 
69  public function testGetRawVal() {
70  $req = new FauxRequest( [
71  'x' => 'Value',
72  'y' => [ 'a' ],
73  'crlf' => "A\r\nb"
74  ] );
75  $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
76  $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
77  $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
78  $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
79  }
80 
84  public function testGetValues() {
85  $values = [ 'x' => 'Value', 'y' => '' ];
86  $req = new FauxRequest( $values );
87  $this->assertEquals( $values, $req->getValues() );
88  }
89 
93  public function testGetQueryValues() {
94  $values = [ 'x' => 'Value', 'y' => '' ];
95 
96  $req = new FauxRequest( $values );
97  $this->assertEquals( $values, $req->getQueryValues() );
98  $req = new FauxRequest( $values, /*wasPosted*/ true );
99  $this->assertEquals( [], $req->getQueryValues() );
100  }
101 
105  public function testGetMethod() {
106  $req = new FauxRequest( [] );
107  $this->assertEquals( 'GET', $req->getMethod() );
108  $req = new FauxRequest( [], /*wasPosted*/ true );
109  $this->assertEquals( 'POST', $req->getMethod() );
110  }
111 
115  public function testWasPosted() {
116  $req = new FauxRequest( [] );
117  $this->assertFalse( $req->wasPosted() );
118  $req = new FauxRequest( [], /*wasPosted*/ true );
119  $this->assertTrue( $req->wasPosted() );
120  }
121 
127  public function testCookies() {
128  $req = new FauxRequest();
129  $this->assertSame( null, $req->getCookie( 'z', '' ) );
130 
131  $req->setCookie( 'x', 'Value', '' );
132  $this->assertEquals( 'Value', $req->getCookie( 'x', '' ) );
133 
134  $req->setCookies( [ 'x' => 'One', 'y' => 'Two' ], '' );
135  $this->assertEquals( 'One', $req->getCookie( 'x', '' ) );
136  $this->assertEquals( 'Two', $req->getCookie( 'y', '' ) );
137  }
138 
144  public function testCookiesDefaultPrefix() {
145  global $wgCookiePrefix;
146  $oldPrefix = $wgCookiePrefix;
147  $wgCookiePrefix = '_';
148 
149  $req = new FauxRequest();
150  $this->assertSame( null, $req->getCookie( 'z' ) );
151 
152  $req->setCookie( 'x', 'Value' );
153  $this->assertEquals( 'Value', $req->getCookie( 'x' ) );
154 
155  $wgCookiePrefix = $oldPrefix;
156  }
157 
161  public function testGetRequestURL_disallowed() {
162  $req = new FauxRequest();
163  $this->setExpectedException( MWException::class );
164  $req->getRequestURL();
165  }
166 
171  public function testSetRequestURL() {
172  $req = new FauxRequest();
173  $req->setRequestURL( 'https://example.org' );
174  $this->assertEquals( 'https://example.org', $req->getRequestURL() );
175  }
176 
181  $GLOBALS['wgServer'] = '//wiki.test';
182  $req = new FauxRequest();
183 
184  $this->setExpectedException( MWException::class );
185  $req->getFullRequestURL();
186  }
187 
191  public function testGetFullRequestURL_http() {
192  $GLOBALS['wgServer'] = '//wiki.test';
193  $req = new FauxRequest();
194  $req->setRequestURL( '/path' );
195 
196  $this->assertSame(
197  'http://wiki.test/path',
198  $req->getFullRequestURL()
199  );
200  }
201 
205  public function testGetFullRequestURL_https() {
206  $GLOBALS['wgServer'] = '//wiki.test';
207  $req = new FauxRequest( [], false, null, 'https' );
208  $req->setRequestURL( '/path' );
209 
210  $this->assertSame(
211  'https://wiki.test/path',
212  $req->getFullRequestURL()
213  );
214  }
215 
220  public function testProtocol() {
221  $req = new FauxRequest();
222  $this->assertEquals( 'http', $req->getProtocol() );
223  $req = new FauxRequest( [], false, null, 'http' );
224  $this->assertEquals( 'http', $req->getProtocol() );
225  $req = new FauxRequest( [], false, null, 'https' );
226  $this->assertEquals( 'https', $req->getProtocol() );
227  }
228 
234  public function testGetSetHeader() {
235  $value = 'text/plain, text/html';
236 
237  $request = new FauxRequest();
238  $request->setHeader( 'Accept', $value );
239 
240  $this->assertEquals( $request->getHeader( 'Nonexistent' ), false );
241  $this->assertEquals( $request->getHeader( 'Accept' ), $value );
242  $this->assertEquals( $request->getHeader( 'ACCEPT' ), $value );
243  $this->assertEquals( $request->getHeader( 'accept' ), $value );
244  $this->assertEquals(
245  $request->getHeader( 'Accept', WebRequest::GETHEADER_LIST ),
246  [ 'text/plain', 'text/html' ]
247  );
248  }
249 
253  public function testGetAllHeaders() {
254  $_SERVER['HTTP_TEST'] = 'Example';
255 
256  $request = new FauxRequest();
257 
258  $this->assertEquals(
259  [],
260  $request->getAllHeaders()
261  );
262 
263  $this->assertEquals(
264  false,
265  $request->getHeader( 'test' )
266  );
267  }
268 
273  public function testSessionData() {
274  $values = [ 'x' => 'Value', 'y' => '' ];
275 
276  $req = new FauxRequest( [], false, /*session*/ $values );
277  $this->assertEquals( $values, $req->getSessionArray() );
278 
279  $req = new FauxRequest();
280  $this->assertSame( null, $req->getSessionArray() );
281  }
282 
288  public function testDummies() {
289  $req = new FauxRequest();
290  $this->assertEquals( '', $req->getRawQueryString() );
291  $this->assertEquals( '', $req->getRawPostString() );
292  $this->assertEquals( '', $req->getRawInput() );
293  }
294 }
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
testGetFullRequestURL_disallowed()
FauxRequest::getFullRequestURL.
$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.
testGetFullRequestURL_https()
FauxRequest::getFullRequestURL.
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
$GLOBALS['IP']
testGetAllHeaders()
FauxRequest::initHeaders.
testSessionData()
FauxRequest::__construct FauxRequest::getSessionArray.
testGetFullRequestURL_http()
FauxRequest::getFullRequestURL.
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.
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
testGetRequestURL_disallowed()
FauxRequest::getRequestURL.