MediaWiki REL1_32
FauxRequestTest.php
Go to the documentation of this file.
1<?php
2
4
5class 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(
32 FauxRequest::class,
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}
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
testGetRequestURL()
FauxRequest::getRequestURL.
testDummies()
FauxRequest::getRawQueryString FauxRequest::getRawPostString FauxRequest::getRawInput.
testConstructInvalidSession()
FauxRequest::__construct.
testSetRequestURL()
FauxRequest::setRequestURL FauxRequest::getRequestURL.
testWasPosted()
FauxRequest::wasPosted.
testProtocol()
FauxRequest::__construct FauxRequest::getProtocol.
testGetText()
FauxRequest::getText.
testConstructInvalidData()
FauxRequest::__construct.
testSessionData()
FauxRequest::__construct FauxRequest::getSessionArray.
testCookies()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
testGetVal()
Integration test for parent method FauxRequest::getVal.
testCookiesDefaultPrefix()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
testConstructWithSession()
FauxRequest::__construct.
testGetMethod()
FauxRequest::getMethod.
testGetSetHeader()
FauxRequest::setHeader FauxRequest::setHeaders FauxRequest::getHeader.
testGetValues()
FauxRequest::getValues.
testGetRawVal()
Integration test for parent method FauxRequest::getRawVal.
testGetQueryValues()
FauxRequest::getQueryValues.
testGetAllHeaders()
FauxRequest::initHeaders.
WebRequest clone which takes values from a provided array.
This serves as the entry point to the MediaWiki session handling system.
this hook is for auditing only $req
Definition hooks.txt:1018
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:2880