MediaWiki REL1_28
FauxRequestTest.php
Go to the documentation of this file.
1<?php
2
3class 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}
$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.
testCookiesDefaultPrefix()
FauxRequest::getCookie FauxRequest::setCookie FauxRequest::setCookies.
testGetMethod()
FauxRequest::getMethod.
testGetSetHeader()
FauxRequest::setHeader FauxRequest::setHeaders FauxRequest::getHeader.
testGetValues()
FauxRequest::getValues.
testGetQueryValues()
FauxRequest::getQueryValues.
testGetAllHeaders()
FauxRequest::initHeaders.
WebRequest clone which takes values from a provided array.
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
this hook is for auditing only $req
Definition hooks.txt:1010
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2685
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:37