MediaWiki  1.29.1
FauxRequestTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class FauxRequestTest extends PHPUnit_Framework_TestCase {
9  public function testConstructInvalidData() {
10  $this->setExpectedException( MWException::class, 'bogus data' );
11  $req = new FauxRequest( 'x' );
12  }
13 
17  public function testConstructInvalidSession() {
18  $this->setExpectedException( MWException::class, 'bogus session' );
19  $req = new FauxRequest( [], false, 'x' );
20  }
21 
25  public function testConstructWithSession() {
26  $session = SessionManager::singleton()->getEmptySession( new FauxRequest( [] ) );
27  $this->assertInstanceOf(
29  new FauxRequest( [], false, $session )
30  );
31  }
32 
36  public function testGetText() {
37  $req = new FauxRequest( [ 'x' => 'Value' ] );
38  $this->assertEquals( 'Value', $req->getText( 'x' ) );
39  $this->assertEquals( '', $req->getText( 'z' ) );
40  }
41 
42  // Integration test for parent method.
43  public function testGetVal() {
44  $req = new FauxRequest( [ 'crlf' => "A\r\nb" ] );
45  $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
46  }
47 
48  // Integration test for parent method.
49  public function testGetRawVal() {
50  $req = new FauxRequest( [
51  'x' => 'Value',
52  'y' => [ 'a' ],
53  'crlf' => "A\r\nb"
54  ] );
55  $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
56  $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
57  $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
58  $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
59  }
60 
64  public function testGetValues() {
65  $values = [ 'x' => 'Value', 'y' => '' ];
66  $req = new FauxRequest( $values );
67  $this->assertEquals( $values, $req->getValues() );
68  }
69 
73  public function testGetQueryValues() {
74  $values = [ 'x' => 'Value', 'y' => '' ];
75 
76  $req = new FauxRequest( $values );
77  $this->assertEquals( $values, $req->getQueryValues() );
78  $req = new FauxRequest( $values, /*wasPosted*/ true );
79  $this->assertEquals( [], $req->getQueryValues() );
80  }
81 
85  public function testGetMethod() {
86  $req = new FauxRequest( [] );
87  $this->assertEquals( 'GET', $req->getMethod() );
88  $req = new FauxRequest( [], /*wasPosted*/ true );
89  $this->assertEquals( 'POST', $req->getMethod() );
90  }
91 
95  public function testWasPosted() {
96  $req = new FauxRequest( [] );
97  $this->assertFalse( $req->wasPosted() );
98  $req = new FauxRequest( [], /*wasPosted*/ true );
99  $this->assertTrue( $req->wasPosted() );
100  }
101 
107  public function testCookies() {
108  $req = new FauxRequest();
109  $this->assertSame( null, $req->getCookie( 'z', '' ) );
110 
111  $req->setCookie( 'x', 'Value', '' );
112  $this->assertEquals( 'Value', $req->getCookie( 'x', '' ) );
113 
114  $req->setCookies( [ 'x' => 'One', 'y' => 'Two' ], '' );
115  $this->assertEquals( 'One', $req->getCookie( 'x', '' ) );
116  $this->assertEquals( 'Two', $req->getCookie( 'y', '' ) );
117  }
118 
124  public function testCookiesDefaultPrefix() {
126  $oldPrefix = $wgCookiePrefix;
127  $wgCookiePrefix = '_';
128 
129  $req = new FauxRequest();
130  $this->assertSame( null, $req->getCookie( 'z' ) );
131 
132  $req->setCookie( 'x', 'Value' );
133  $this->assertEquals( 'Value', $req->getCookie( 'x' ) );
134 
135  $wgCookiePrefix = $oldPrefix;
136  }
137 
141  public function testGetRequestURL() {
142  $req = new FauxRequest();
143  $this->setExpectedException( MWException::class );
144  $req->getRequestURL();
145  }
146 
151  public function testSetRequestURL() {
152  $req = new FauxRequest();
153  $req->setRequestURL( 'https://example.org' );
154  $this->assertEquals( 'https://example.org', $req->getRequestURL() );
155  }
156 
161  public function testProtocol() {
162  $req = new FauxRequest();
163  $this->assertEquals( 'http', $req->getProtocol() );
164  $req = new FauxRequest( [], false, null, 'http' );
165  $this->assertEquals( 'http', $req->getProtocol() );
166  $req = new FauxRequest( [], false, null, 'https' );
167  $this->assertEquals( 'https', $req->getProtocol() );
168  }
169 
175  public function testGetSetHeader() {
176  $value = 'text/plain, text/html';
177 
178  $request = new FauxRequest();
179  $request->setHeader( 'Accept', $value );
180 
181  $this->assertEquals( $request->getHeader( 'Nonexistent' ), false );
182  $this->assertEquals( $request->getHeader( 'Accept' ), $value );
183  $this->assertEquals( $request->getHeader( 'ACCEPT' ), $value );
184  $this->assertEquals( $request->getHeader( 'accept' ), $value );
185  $this->assertEquals(
186  $request->getHeader( 'Accept', WebRequest::GETHEADER_LIST ),
187  [ 'text/plain', 'text/html' ]
188  );
189  }
190 
194  public function testGetAllHeaders() {
195  $_SERVER['HTTP_TEST'] = 'Example';
196 
197  $request = new FauxRequest();
198 
199  $this->assertEquals(
200  [],
201  $request->getAllHeaders()
202  );
203 
204  $this->assertEquals(
205  false,
206  $request->getHeader( 'test' )
207  );
208  }
209 
214  public function testSessionData() {
215  $values = [ 'x' => 'Value', 'y' => '' ];
216 
217  $req = new FauxRequest( [], false, /*session*/ $values );
218  $this->assertEquals( $values, $req->getSessionArray() );
219 
220  $req = new FauxRequest();
221  $this->assertSame( null, $req->getSessionArray() );
222  }
223 
229  public function testDummies() {
230  $req = new FauxRequest();
231  $this->assertEquals( '', $req->getRawQueryString() );
232  $this->assertEquals( '', $req->getRawPostString() );
233  $this->assertEquals( '', $req->getRawInput() );
234  }
235 }
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
$request
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2612
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
$wgCookiePrefix
if( $wgLocalInterwiki) if( $wgSharedPrefix===false) if( $wgSharedSchema===false) if(! $wgCookiePrefix) $wgCookiePrefix
Definition: Setup.php:326
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$req
this hook is for auditing only $req
Definition: hooks.txt:990
FauxRequestTest\testProtocol
testProtocol()
FauxRequest::__construct FauxRequest::getProtocol.
Definition: FauxRequestTest.php:161
php
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
FauxRequestTest\testConstructInvalidSession
testConstructInvalidSession()
FauxRequest::__construct.
Definition: FauxRequestTest.php:17
FauxRequestTest\testGetText
testGetText()
FauxRequest::getText.
Definition: FauxRequestTest.php:36
FauxRequestTest\testGetVal
testGetVal()
Definition: FauxRequestTest.php:43
FauxRequestTest\testGetRequestURL
testGetRequestURL()
FauxRequest::getRequestURL.
Definition: FauxRequestTest.php:141
FauxRequestTest\testGetSetHeader
testGetSetHeader()
FauxRequest::setHeader FauxRequest::setHeaders FauxRequest::getHeader.
Definition: FauxRequestTest.php:175
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
FauxRequestTest\testConstructInvalidData
testConstructInvalidData()
FauxRequest::__construct.
Definition: FauxRequestTest.php:9
FauxRequestTest
Definition: FauxRequestTest.php:5
$value
$value
Definition: styleTest.css.php:45
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:49
FauxRequestTest\testCookies
testCookies()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
Definition: FauxRequestTest.php:107
FauxRequestTest\testWasPosted
testWasPosted()
FauxRequest::wasPosted.
Definition: FauxRequestTest.php:95
FauxRequestTest\testSessionData
testSessionData()
FauxRequest::__construct FauxRequest::getSessionArray.
Definition: FauxRequestTest.php:214
FauxRequestTest\testGetMethod
testGetMethod()
FauxRequest::getMethod.
Definition: FauxRequestTest.php:85
WebRequest\GETHEADER_LIST
const GETHEADER_LIST
Flag to make WebRequest::getHeader return an array of values.
Definition: WebRequest.php:45
FauxRequestTest\testGetRawVal
testGetRawVal()
Definition: FauxRequestTest.php:49
class
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
FauxRequestTest\testConstructWithSession
testConstructWithSession()
FauxRequest::__construct.
Definition: FauxRequestTest.php:25
FauxRequestTest\testGetAllHeaders
testGetAllHeaders()
FauxRequest::initHeaders.
Definition: FauxRequestTest.php:194
FauxRequestTest\testCookiesDefaultPrefix
testCookiesDefaultPrefix()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
Definition: FauxRequestTest.php:124
FauxRequestTest\testGetQueryValues
testGetQueryValues()
FauxRequest::getQueryValues.
Definition: FauxRequestTest.php:73
FauxRequestTest\testSetRequestURL
testSetRequestURL()
FauxRequest::setRequestURL FauxRequest::getRequestURL.
Definition: FauxRequestTest.php:151
FauxRequestTest\testDummies
testDummies()
FauxRequest::getRawQueryString FauxRequest::getRawPostString FauxRequest::getRawInput.
Definition: FauxRequestTest.php:229
FauxRequestTest\testGetValues
testGetValues()
FauxRequest::getValues.
Definition: FauxRequestTest.php:64