rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / linkeddata / PageDataRequestHandlerTest.php
blob2b18b08b2432f3a1b772f5454ca7cf540a7f1329
1 <?php
3 /**
4 * @covers PageDataRequestHandler
6 * @group PageData
8 * @license GPL-2.0+
9 */
10 class PageDataRequestHandlerTest extends \MediaWikiTestCase {
12 /**
13 * @var Title
15 private $interfaceTitle;
17 /**
18 * @var int
20 private $obLevel;
22 protected function setUp() {
23 parent::setUp();
25 $this->interfaceTitle = Title::newFromText( "Special:PageDataRequestHandlerTest" );
27 $this->obLevel = ob_get_level();
30 protected function tearDown() {
31 $obLevel = ob_get_level();
33 while ( ob_get_level() > $this->obLevel ) {
34 ob_end_clean();
37 if ( $obLevel !== $this->obLevel ) {
38 $this->fail( "Test changed output buffer level: was {$this->obLevel}" .
39 "before test, but $obLevel after test."
43 parent::tearDown();
46 /**
47 * @return PageDataRequestHandler
49 protected function newHandler() {
50 return new PageDataRequestHandler( 'json' );
53 /**
54 * @param array $params
55 * @param string[] $headers
57 * @return OutputPage
59 protected function makeOutputPage( array $params, array $headers ) {
60 // construct request
61 $request = new FauxRequest( $params );
62 $request->response()->header( 'Status: 200 OK', true, 200 ); // init/reset
64 foreach ( $headers as $name => $value ) {
65 $request->setHeader( strtoupper( $name ), $value );
68 // construct Context and OutputPage
69 $context = new DerivativeContext( RequestContext::getMain() );
70 $context->setRequest( $request );
72 $output = new OutputPage( $context );
73 $output->setTitle( $this->interfaceTitle );
74 $context->setOutput( $output );
76 return $output;
79 public function handleRequestProvider() {
80 $cases = [];
82 $cases[] = [ '', [], [], '!!', 400 ];
84 $cases[] = [ '', [ 'target' => 'Helsinki' ], [], '!!', 303, [ 'Location' => '!.+!' ] ];
86 $subpageCases = [];
87 foreach ( $cases as $c ) {
88 $case = $c;
89 $case[0] = 'main/';
91 if ( isset( $case[1]['target'] ) ) {
92 $case[0] .= $case[1]['target'];
93 unset( $case[1]['target'] );
96 $subpageCases[] = $case;
99 $cases = array_merge( $cases, $subpageCases );
101 $cases[] = [
103 [ 'target' => 'Helsinki' ],
104 [ 'Accept' => 'text/HTML' ],
105 '!!',
106 303,
107 [ 'Location' => '!Helsinki$!' ]
110 $cases[] = [
113 'target' => 'Helsinki',
114 'revision' => '4242',
116 [ 'Accept' => 'text/HTML' ],
117 '!!',
118 303,
119 [ 'Location' => '!Helsinki(\?|&)oldid=4242!' ]
122 $cases[] = [
123 '/Helsinki',
126 '!!',
127 303,
128 [ 'Location' => '!Helsinki&action=raw!' ]
131 // #31: /Q5 with "Accept: text/foobar" triggers a 406
132 $cases[] = [
133 'main/Helsinki',
135 [ 'Accept' => 'text/foobar' ],
136 '!!',
137 406,
141 $cases[] = [
142 'main/Helsinki',
144 [ 'Accept' => 'text/HTML' ],
145 '!!',
146 303,
147 [ 'Location' => '!Helsinki$!' ]
150 $cases[] = [
151 '/Helsinki',
153 [ 'Accept' => 'text/HTML' ],
154 '!!',
155 303,
156 [ 'Location' => '!Helsinki$!' ]
159 $cases[] = [
160 'main/AC/DC',
162 [ 'Accept' => 'text/HTML' ],
163 '!!',
164 303,
165 [ 'Location' => '!AC/DC$!' ]
168 return $cases;
172 * @dataProvider handleRequestProvider
174 * @param string $subpage The subpage to request (or '')
175 * @param array $params Request parameters
176 * @param array $headers Request headers
177 * @param string $expectedOutput Regex to match the output against.
178 * @param int $expectedStatusCode Expected HTTP status code.
179 * @param string[] $expectedHeaders Expected HTTP response headers.
181 public function testHandleRequest(
182 $subpage,
183 array $params,
184 array $headers,
185 $expectedOutput,
186 $expectedStatusCode = 200,
187 array $expectedHeaders = []
189 $output = $this->makeOutputPage( $params, $headers );
190 $request = $output->getRequest();
192 /* @var FauxResponse $response */
193 $response = $request->response();
195 // construct handler
196 $handler = $this->newHandler();
198 try {
199 ob_start();
200 $handler->handleRequest( $subpage, $request, $output );
202 if ( $output->getRedirect() !== '' ) {
203 // hack to apply redirect to web response
204 $output->output();
207 $text = ob_get_contents();
208 ob_end_clean();
210 $this->assertEquals( $expectedStatusCode, $response->getStatusCode(), 'status code' );
211 $this->assertRegExp( $expectedOutput, $text, 'output' );
213 foreach ( $expectedHeaders as $name => $exp ) {
214 $value = $response->getHeader( $name );
215 $this->assertNotNull( $value, "header: $name" );
216 $this->assertInternalType( 'string', $value, "header: $name" );
217 $this->assertRegExp( $exp, $value, "header: $name" );
219 } catch ( HttpError $e ) {
220 ob_end_clean();
221 $this->assertEquals( $expectedStatusCode, $e->getStatusCode(), 'status code' );
222 $this->assertRegExp( $expectedOutput, $e->getHTML(), 'error output' );
225 // We always set "Access-Control-Allow-Origin: *"
226 $this->assertSame( '*', $response->getHeader( 'Access-Control-Allow-Origin' ) );
229 public function provideHttpContentNegotiation() {
230 $helsinki = Title::newFromText( 'Helsinki' );
231 return [
232 'Accept Header of HTML' => [
233 $helsinki,
234 [ 'ACCEPT' => 'text/html' ], // headers
235 'Helsinki'
237 'Accept Header without weights' => [
238 $helsinki,
239 [ 'ACCEPT' => '*/*, text/html, text/x-wiki' ],
240 'Helsinki&action=raw'
242 'Accept Header with weights' => [
243 $helsinki,
244 [ 'ACCEPT' => 'text/*; q=0.5, text/json; q=0.7, application/rdf+xml; q=0.8' ],
245 'Helsinki&action=raw'
247 'Accept Header accepting evertyhing and HTML' => [
248 $helsinki,
249 [ 'ACCEPT' => 'text/html, */*' ],
250 'Helsinki&action=raw'
252 'No Accept Header' => [
253 $helsinki,
255 'Helsinki&action=raw'
261 * @dataProvider provideHttpContentNegotiation
263 * @param Title $title
264 * @param array $headers Request headers
265 * @param string $expectedRedirectSuffix Expected suffix of the HTTP Location header.
267 * @throws HttpError
269 public function testHttpContentNegotiation(
270 Title $title,
271 array $headers,
272 $expectedRedirectSuffix
274 /* @var FauxResponse $response */
275 $output = $this->makeOutputPage( [], $headers );
276 $request = $output->getRequest();
278 $handler = $this->newHandler();
279 $handler->httpContentNegotiation( $request, $output, $title );
281 $this->assertStringEndsWith(
282 $expectedRedirectSuffix,
283 $output->getRedirect(),
284 'redirect target'