MediaWiki  1.28.1
FauxRequestTest.php
Go to the documentation of this file.
1 <?php
2 
3 class FauxRequestTest extends PHPUnit_Framework_TestCase {
7  public function testConstructInvalidData() {
8  $this->setExpectedException( MWException::class, 'bogus data' );
9  $req = new FauxRequest( 'x' );
10  }
11 
15  public function testConstructInvalidSession() {
16  $this->setExpectedException( MWException::class, 'bogus session' );
17  $req = new FauxRequest( [], false, 'x' );
18  }
19 
23  public function testGetText() {
24  $req = new FauxRequest( [ 'x' => 'Value' ] );
25  $this->assertEquals( 'Value', $req->getText( 'x' ) );
26  $this->assertEquals( '', $req->getText( 'z' ) );
27  }
28 
29  // Integration test for parent method.
30  public function testGetVal() {
31  $req = new FauxRequest( [ 'crlf' => "A\r\nb" ] );
32  $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
33  }
34 
35  // Integration test for parent method.
36  public function testGetRawVal() {
37  $req = new FauxRequest( [
38  'x' => 'Value',
39  'y' => [ 'a' ],
40  'crlf' => "A\r\nb"
41  ] );
42  $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
43  $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
44  $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
45  $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
46  }
47 
51  public function testGetValues() {
52  $values = [ 'x' => 'Value', 'y' => '' ];
53  $req = new FauxRequest( $values );
54  $this->assertEquals( $values, $req->getValues() );
55  }
56 
60  public function testGetQueryValues() {
61  $values = [ 'x' => 'Value', 'y' => '' ];
62 
63  $req = new FauxRequest( $values );
64  $this->assertEquals( $values, $req->getQueryValues() );
65  $req = new FauxRequest( $values, /*wasPosted*/ true );
66  $this->assertEquals( [], $req->getQueryValues() );
67  }
68 
72  public function testGetMethod() {
73  $req = new FauxRequest( [] );
74  $this->assertEquals( 'GET', $req->getMethod() );
75  $req = new FauxRequest( [], /*wasPosted*/ true );
76  $this->assertEquals( 'POST', $req->getMethod() );
77  }
78 
82  public function testWasPosted() {
83  $req = new FauxRequest( [] );
84  $this->assertFalse( $req->wasPosted() );
85  $req = new FauxRequest( [], /*wasPosted*/ true );
86  $this->assertTrue( $req->wasPosted() );
87  }
88 
94  public function testCookies() {
95  $req = new FauxRequest();
96  $this->assertSame( null, $req->getCookie( 'z', '' ) );
97 
98  $req->setCookie( 'x', 'Value', '' );
99  $this->assertEquals( 'Value', $req->getCookie( 'x', '' ) );
100 
101  $req->setCookies( [ 'x' => 'One', 'y' => 'Two' ], '' );
102  $this->assertEquals( 'One', $req->getCookie( 'x', '' ) );
103  $this->assertEquals( 'Two', $req->getCookie( 'y', '' ) );
104  }
105 
111  public function testCookiesDefaultPrefix() {
113  $oldPrefix = $wgCookiePrefix;
114  $wgCookiePrefix = '_';
115 
116  $req = new FauxRequest();
117  $this->assertSame( null, $req->getCookie( 'z' ) );
118 
119  $req->setCookie( 'x', 'Value' );
120  $this->assertEquals( 'Value', $req->getCookie( 'x' ) );
121 
122  $wgCookiePrefix = $oldPrefix;
123  }
124 
128  public function testGetRequestURL() {
129  $req = new FauxRequest();
130  $this->setExpectedException( MWException::class );
131  $req->getRequestURL();
132  }
133 
138  public function testSetRequestURL() {
139  $req = new FauxRequest();
140  $req->setRequestURL( 'https://example.org' );
141  $this->assertEquals( 'https://example.org', $req->getRequestURL() );
142  }
143 
148  public function testProtocol() {
149  $req = new FauxRequest();
150  $this->assertEquals( 'http', $req->getProtocol() );
151  $req = new FauxRequest( [], false, null, 'http' );
152  $this->assertEquals( 'http', $req->getProtocol() );
153  $req = new FauxRequest( [], false, null, 'https' );
154  $this->assertEquals( 'https', $req->getProtocol() );
155  }
156 
162  public function testGetSetHeader() {
163  $value = 'text/plain, text/html';
164 
165  $request = new FauxRequest();
166  $request->setHeader( 'Accept', $value );
167 
168  $this->assertEquals( $request->getHeader( 'Nonexistent' ), false );
169  $this->assertEquals( $request->getHeader( 'Accept' ), $value );
170  $this->assertEquals( $request->getHeader( 'ACCEPT' ), $value );
171  $this->assertEquals( $request->getHeader( 'accept' ), $value );
172  $this->assertEquals(
173  $request->getHeader( 'Accept', WebRequest::GETHEADER_LIST ),
174  [ 'text/plain', 'text/html' ]
175  );
176  }
177 
181  public function testGetAllHeaders() {
182  $_SERVER['HTTP_TEST'] = 'Example';
183 
184  $request = new FauxRequest();
185 
186  $this->assertEquals(
187  [],
188  $request->getAllHeaders()
189  );
190 
191  $this->assertEquals(
192  false,
193  $request->getHeader( 'test' )
194  );
195  }
196 
201  public function testSessionData() {
202  $values = [ 'x' => 'Value', 'y' => '' ];
203 
204  $req = new FauxRequest( [], false, /*session*/ $values );
205  $this->assertEquals( $values, $req->getSessionArray() );
206 
207  $req = new FauxRequest();
208  $this->assertSame( null, $req->getSessionArray() );
209  }
210 
216  public function testDummies() {
217  $req = new FauxRequest();
218  $this->assertEquals( '', $req->getRawQueryString() );
219  $this->assertEquals( '', $req->getRawPostString() );
220  $this->assertEquals( '', $req->getRawInput() );
221  }
222 }
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:189
$value
const GETHEADER_LIST
Flag to make WebRequest::getHeader return an array of values.
Definition: WebRequest.php:45
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
testConstructInvalidData()
FauxRequest::__construct.
testSetRequestURL()
FauxRequest::setRequestURL FauxRequest::getRequestURL.
testWasPosted()
FauxRequest::wasPosted.
testConstructInvalidSession()
FauxRequest::__construct.
testGetMethod()
FauxRequest::getMethod.
testProtocol()
FauxRequest::__construct FauxRequest::getProtocol.
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:1007
testGetQueryValues()
FauxRequest::getQueryValues.
testGetValues()
FauxRequest::getValues.
if($wgLocalInterwiki) if($wgSharedPrefix===false) if($wgSharedSchema===false) if(!$wgCookiePrefix) $wgCookiePrefix
Definition: Setup.php:324
testGetText()
FauxRequest::getText.
testGetRequestURL()
FauxRequest::getRequestURL.
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2573
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.