MediaWiki  1.30.0
PageDataRequestHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
11 
15  private $interfaceTitle;
16 
20  private $obLevel;
21 
22  protected function setUp() {
23  parent::setUp();
24 
25  $this->interfaceTitle = Title::newFromText( "Special:PageDataRequestHandlerTest" );
26 
27  $this->obLevel = ob_get_level();
28  }
29 
30  protected function tearDown() {
31  $obLevel = ob_get_level();
32 
33  while ( ob_get_level() > $this->obLevel ) {
34  ob_end_clean();
35  }
36 
37  if ( $obLevel !== $this->obLevel ) {
38  $this->fail( "Test changed output buffer level: was {$this->obLevel}" .
39  "before test, but $obLevel after test."
40  );
41  }
42 
43  parent::tearDown();
44  }
45 
49  protected function newHandler() {
50  return new PageDataRequestHandler( 'json' );
51  }
52 
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
63 
64  foreach ( $headers as $name => $value ) {
65  $request->setHeader( strtoupper( $name ), $value );
66  }
67 
68  // construct Context and OutputPage
70  $context->setRequest( $request );
71 
72  $output = new OutputPage( $context );
73  $output->setTitle( $this->interfaceTitle );
74  $context->setOutput( $output );
75 
76  return $output;
77  }
78 
79  public function handleRequestProvider() {
80  $cases = [];
81 
82  $cases[] = [ '', [], [], '!!', 400 ];
83 
84  $cases[] = [ '', [ 'target' => 'Helsinki' ], [], '!!', 303, [ 'Location' => '!.+!' ] ];
85 
86  $subpageCases = [];
87  foreach ( $cases as $c ) {
88  $case = $c;
89  $case[0] = 'main/';
90 
91  if ( isset( $case[1]['target'] ) ) {
92  $case[0] .= $case[1]['target'];
93  unset( $case[1]['target'] );
94  }
95 
96  $subpageCases[] = $case;
97  }
98 
99  $cases = array_merge( $cases, $subpageCases );
100 
101  $cases[] = [
102  '',
103  [ 'target' => 'Helsinki' ],
104  [ 'Accept' => 'text/HTML' ],
105  '!!',
106  303,
107  [ 'Location' => '!Helsinki$!' ]
108  ];
109 
110  $cases[] = [
111  '',
112  [
113  'target' => 'Helsinki',
114  'revision' => '4242',
115  ],
116  [ 'Accept' => 'text/HTML' ],
117  '!!',
118  303,
119  [ 'Location' => '!Helsinki(\?|&)oldid=4242!' ]
120  ];
121 
122  $cases[] = [
123  '/Helsinki',
124  [],
125  [],
126  '!!',
127  303,
128  [ 'Location' => '!Helsinki&action=raw!' ]
129  ];
130 
131  // #31: /Q5 with "Accept: text/foobar" triggers a 406
132  $cases[] = [
133  'main/Helsinki',
134  [],
135  [ 'Accept' => 'text/foobar' ],
136  '!!',
137  406,
138  [],
139  ];
140 
141  $cases[] = [
142  'main/Helsinki',
143  [],
144  [ 'Accept' => 'text/HTML' ],
145  '!!',
146  303,
147  [ 'Location' => '!Helsinki$!' ]
148  ];
149 
150  $cases[] = [
151  '/Helsinki',
152  [],
153  [ 'Accept' => 'text/HTML' ],
154  '!!',
155  303,
156  [ 'Location' => '!Helsinki$!' ]
157  ];
158 
159  $cases[] = [
160  'main/AC/DC',
161  [],
162  [ 'Accept' => 'text/HTML' ],
163  '!!',
164  303,
165  [ 'Location' => '!AC/DC$!' ]
166  ];
167 
168  return $cases;
169  }
170 
181  public function testHandleRequest(
182  $subpage,
183  array $params,
184  array $headers,
185  $expectedOutput,
186  $expectedStatusCode = 200,
187  array $expectedHeaders = []
188  ) {
189  $output = $this->makeOutputPage( $params, $headers );
190  $request = $output->getRequest();
191 
192  /* @var FauxResponse $response */
193  $response = $request->response();
194 
195  // construct handler
196  $handler = $this->newHandler();
197 
198  try {
199  ob_start();
200  $handler->handleRequest( $subpage, $request, $output );
201 
202  if ( $output->getRedirect() !== '' ) {
203  // hack to apply redirect to web response
204  $output->output();
205  }
206 
207  $text = ob_get_contents();
208  ob_end_clean();
209 
210  $this->assertEquals( $expectedStatusCode, $response->getStatusCode(), 'status code' );
211  $this->assertRegExp( $expectedOutput, $text, 'output' );
212 
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" );
218  }
219  } catch ( HttpError $e ) {
220  ob_end_clean();
221  $this->assertEquals( $expectedStatusCode, $e->getStatusCode(), 'status code' );
222  $this->assertRegExp( $expectedOutput, $e->getHTML(), 'error output' );
223  }
224 
225  // We always set "Access-Control-Allow-Origin: *"
226  $this->assertSame( '*', $response->getHeader( 'Access-Control-Allow-Origin' ) );
227  }
228 
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'
236  ],
237  'Accept Header without weights' => [
238  $helsinki,
239  [ 'ACCEPT' => '*/*, text/html, text/x-wiki' ],
240  'Helsinki&action=raw'
241  ],
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'
246  ],
247  'Accept Header accepting evertyhing and HTML' => [
248  $helsinki,
249  [ 'ACCEPT' => 'text/html, */*' ],
250  'Helsinki&action=raw'
251  ],
252  'No Accept Header' => [
253  $helsinki,
254  [],
255  'Helsinki&action=raw'
256  ],
257  ];
258  }
259 
269  public function testHttpContentNegotiation(
270  Title $title,
271  array $headers,
272  $expectedRedirectSuffix
273  ) {
274  /* @var FauxResponse $response */
275  $output = $this->makeOutputPage( [], $headers );
276  $request = $output->getRequest();
277 
278  $handler = $this->newHandler();
279  $handler->httpContentNegotiation( $request, $output, $title );
280 
281  $this->assertStringEndsWith(
282  $expectedRedirectSuffix,
283  $output->getRedirect(),
284  'redirect target'
285  );
286  }
287 }
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:268
PageDataRequestHandlerTest\makeOutputPage
makeOutputPage(array $params, array $headers)
Definition: PageDataRequestHandlerTest.php:59
PageDataRequestHandlerTest
PageDataRequestHandler.
Definition: PageDataRequestHandlerTest.php:10
$context
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 and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2581
PageDataRequestHandler
Request handler implementing a data interface for mediawiki pages.
Definition: PageDataRequestHandler.php:14
PageDataRequestHandlerTest\provideHttpContentNegotiation
provideHttpContentNegotiation()
Definition: PageDataRequestHandlerTest.php:229
PageDataRequestHandlerTest\testHttpContentNegotiation
testHttpContentNegotiation(Title $title, array $headers, $expectedRedirectSuffix)
provideHttpContentNegotiation
Definition: PageDataRequestHandlerTest.php:269
$params
$params
Definition: styleTest.css.php:40
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
$output
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2198
HttpError
Show an error that looks like an HTTP server error.
Definition: HttpError.php:30
PageDataRequestHandlerTest\newHandler
newHandler()
Definition: PageDataRequestHandlerTest.php:49
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
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:31
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
OutputPage
This class should be covered by a general architecture document which does not exist as of January 20...
Definition: OutputPage.php:44
$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:2581
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2141
PageDataRequestHandlerTest\$interfaceTitle
Title $interfaceTitle
Definition: PageDataRequestHandlerTest.php:15
$value
$value
Definition: styleTest.css.php:45
PageDataRequestHandlerTest\testHandleRequest
testHandleRequest( $subpage, array $params, array $headers, $expectedOutput, $expectedStatusCode=200, array $expectedHeaders=[])
handleRequestProvider
Definition: PageDataRequestHandlerTest.php:181
PageDataRequestHandlerTest\setUp
setUp()
Definition: PageDataRequestHandlerTest.php:22
$handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:781
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:470
$response
this hook is for auditing only $response
Definition: hooks.txt:781
PageDataRequestHandlerTest\$obLevel
int $obLevel
Definition: PageDataRequestHandlerTest.php:20
PageDataRequestHandlerTest\handleRequestProvider
handleRequestProvider()
Definition: PageDataRequestHandlerTest.php:79
Title
Represents a title within MediaWiki.
Definition: Title.php:39
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
PageDataRequestHandlerTest\tearDown
tearDown()
Definition: PageDataRequestHandlerTest.php:30
array
the array() calling protocol came about after MediaWiki 1.4rc1.