MediaWiki  1.32.5
FauxRequestTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class FauxRequestTest extends PHPUnit\Framework\TestCase {
6 
7  use MediaWikiCoversValidator;
8  use PHPUnit4And6Compat;
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 }
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
$req
this hook is for auditing only $req
Definition: hooks.txt:1018
FauxRequestTest\testProtocol
testProtocol()
FauxRequest::__construct FauxRequest::getProtocol.
Definition: FauxRequestTest.php:171
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:21
FauxRequestTest\testGetText
testGetText()
FauxRequest::getText.
Definition: FauxRequestTest.php:40
FauxRequestTest\testGetVal
testGetVal()
Integration test for parent method FauxRequest::getVal.
Definition: FauxRequestTest.php:50
FauxRequestTest\testGetRequestURL
testGetRequestURL()
FauxRequest::getRequestURL.
Definition: FauxRequestTest.php:151
FauxRequestTest\testGetSetHeader
testGetSetHeader()
FauxRequest::setHeader FauxRequest::setHeaders FauxRequest::getHeader.
Definition: FauxRequestTest.php:185
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
FauxRequestTest\testConstructInvalidData
testConstructInvalidData()
FauxRequest::__construct.
Definition: FauxRequestTest.php:13
FauxRequestTest
Definition: FauxRequestTest.php:5
$request
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:2683
$value
$value
Definition: styleTest.css.php:49
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:50
FauxRequestTest\testCookies
testCookies()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
Definition: FauxRequestTest.php:117
FauxRequestTest\testWasPosted
testWasPosted()
FauxRequest::wasPosted.
Definition: FauxRequestTest.php:105
FauxRequestTest\testSessionData
testSessionData()
FauxRequest::__construct FauxRequest::getSessionArray.
Definition: FauxRequestTest.php:224
FauxRequestTest\testGetMethod
testGetMethod()
FauxRequest::getMethod.
Definition: FauxRequestTest.php:95
WebRequest\GETHEADER_LIST
const GETHEADER_LIST
Flag to make WebRequest::getHeader return an array of values.
Definition: WebRequest.php:48
FauxRequestTest\testGetRawVal
testGetRawVal()
Integration test for parent method FauxRequest::getRawVal.
Definition: FauxRequestTest.php:59
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:29
FauxRequestTest\testGetAllHeaders
testGetAllHeaders()
FauxRequest::initHeaders.
Definition: FauxRequestTest.php:204
FauxRequestTest\testCookiesDefaultPrefix
testCookiesDefaultPrefix()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
Definition: FauxRequestTest.php:134
FauxRequestTest\testGetQueryValues
testGetQueryValues()
FauxRequest::getQueryValues.
Definition: FauxRequestTest.php:83
$wgCookiePrefix
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
Definition: DefaultSettings.php:6052
FauxRequestTest\testSetRequestURL
testSetRequestURL()
FauxRequest::setRequestURL FauxRequest::getRequestURL.
Definition: FauxRequestTest.php:161
FauxRequestTest\testDummies
testDummies()
FauxRequest::getRawQueryString FauxRequest::getRawPostString FauxRequest::getRawInput.
Definition: FauxRequestTest.php:239
FauxRequestTest\testGetValues
testGetValues()
FauxRequest::getValues.
Definition: FauxRequestTest.php:74