MediaWiki  REL1_31
PageDataRequestHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
8 
12  private $interfaceTitle;
13 
17  private $obLevel;
18 
19  protected function setUp() {
20  parent::setUp();
21 
22  $this->interfaceTitle = Title::newFromText( "Special:PageDataRequestHandlerTest" );
23 
24  $this->obLevel = ob_get_level();
25  }
26 
27  protected function tearDown() {
28  $obLevel = ob_get_level();
29 
30  while ( ob_get_level() > $this->obLevel ) {
31  ob_end_clean();
32  }
33 
34  if ( $obLevel !== $this->obLevel ) {
35  $this->fail( "Test changed output buffer level: was {$this->obLevel}" .
36  "before test, but $obLevel after test."
37  );
38  }
39 
40  parent::tearDown();
41  }
42 
46  protected function newHandler() {
47  return new PageDataRequestHandler( 'json' );
48  }
49 
56  protected function makeOutputPage( array $params, array $headers ) {
57  // construct request
58  $request = new FauxRequest( $params );
59  $request->response()->header( 'Status: 200 OK', true, 200 ); // init/reset
60 
61  foreach ( $headers as $name => $value ) {
62  $request->setHeader( strtoupper( $name ), $value );
63  }
64 
65  // construct Context and OutputPage
67  $context->setRequest( $request );
68 
69  $output = new OutputPage( $context );
70  $output->setTitle( $this->interfaceTitle );
71  $context->setOutput( $output );
72 
73  return $output;
74  }
75 
76  public function handleRequestProvider() {
77  $cases = [];
78 
79  $cases[] = [ '', [], [], '!!', 400 ];
80 
81  $cases[] = [ '', [ 'target' => 'Helsinki' ], [], '!!', 303, [ 'Location' => '!.+!' ] ];
82 
83  $subpageCases = [];
84  foreach ( $cases as $c ) {
85  $case = $c;
86  $case[0] = 'main/';
87 
88  if ( isset( $case[1]['target'] ) ) {
89  $case[0] .= $case[1]['target'];
90  unset( $case[1]['target'] );
91  }
92 
93  $subpageCases[] = $case;
94  }
95 
96  $cases = array_merge( $cases, $subpageCases );
97 
98  $cases[] = [
99  '',
100  [ 'target' => 'Helsinki' ],
101  [ 'Accept' => 'text/HTML' ],
102  '!!',
103  303,
104  [ 'Location' => '!Helsinki$!' ]
105  ];
106 
107  $cases[] = [
108  '',
109  [
110  'target' => 'Helsinki',
111  'revision' => '4242',
112  ],
113  [ 'Accept' => 'text/HTML' ],
114  '!!',
115  303,
116  [ 'Location' => '!Helsinki(\?|&)oldid=4242!' ]
117  ];
118 
119  $cases[] = [
120  '/Helsinki',
121  [],
122  [],
123  '!!',
124  303,
125  [ 'Location' => '!Helsinki&action=raw!' ]
126  ];
127 
128  // #31: /Q5 with "Accept: text/foobar" triggers a 406
129  $cases[] = [
130  'main/Helsinki',
131  [],
132  [ 'Accept' => 'text/foobar' ],
133  '!!',
134  406,
135  [],
136  ];
137 
138  $cases[] = [
139  'main/Helsinki',
140  [],
141  [ 'Accept' => 'text/HTML' ],
142  '!!',
143  303,
144  [ 'Location' => '!Helsinki$!' ]
145  ];
146 
147  $cases[] = [
148  '/Helsinki',
149  [],
150  [ 'Accept' => 'text/HTML' ],
151  '!!',
152  303,
153  [ 'Location' => '!Helsinki$!' ]
154  ];
155 
156  $cases[] = [
157  'main/AC/DC',
158  [],
159  [ 'Accept' => 'text/HTML' ],
160  '!!',
161  303,
162  [ 'Location' => '!AC/DC$!' ]
163  ];
164 
165  return $cases;
166  }
167 
178  public function testHandleRequest(
179  $subpage,
180  array $params,
181  array $headers,
182  $expectedOutput,
183  $expectedStatusCode = 200,
184  array $expectedHeaders = []
185  ) {
186  $output = $this->makeOutputPage( $params, $headers );
187  $request = $output->getRequest();
188 
189  /* @var FauxResponse $response */
190  $response = $request->response();
191 
192  // construct handler
193  $handler = $this->newHandler();
194 
195  try {
196  ob_start();
197  $handler->handleRequest( $subpage, $request, $output );
198 
199  if ( $output->getRedirect() !== '' ) {
200  // hack to apply redirect to web response
201  $output->output();
202  }
203 
204  $text = ob_get_contents();
205  ob_end_clean();
206 
207  $this->assertEquals( $expectedStatusCode, $response->getStatusCode(), 'status code' );
208  $this->assertRegExp( $expectedOutput, $text, 'output' );
209 
210  foreach ( $expectedHeaders as $name => $exp ) {
211  $value = $response->getHeader( $name );
212  $this->assertNotNull( $value, "header: $name" );
213  $this->assertInternalType( 'string', $value, "header: $name" );
214  $this->assertRegExp( $exp, $value, "header: $name" );
215  }
216  } catch ( HttpError $e ) {
217  ob_end_clean();
218  $this->assertEquals( $expectedStatusCode, $e->getStatusCode(), 'status code' );
219  $this->assertRegExp( $expectedOutput, $e->getHTML(), 'error output' );
220  }
221 
222  // We always set "Access-Control-Allow-Origin: *"
223  $this->assertSame( '*', $response->getHeader( 'Access-Control-Allow-Origin' ) );
224  }
225 
226  public function provideHttpContentNegotiation() {
227  $helsinki = Title::newFromText( 'Helsinki' );
228  return [
229  'Accept Header of HTML' => [
230  $helsinki,
231  [ 'ACCEPT' => 'text/html' ], // headers
232  'Helsinki'
233  ],
234  'Accept Header without weights' => [
235  $helsinki,
236  [ 'ACCEPT' => '*/*, text/html, text/x-wiki' ],
237  'Helsinki&action=raw'
238  ],
239  'Accept Header with weights' => [
240  $helsinki,
241  [ 'ACCEPT' => 'text/*; q=0.5, text/json; q=0.7, application/rdf+xml; q=0.8' ],
242  'Helsinki&action=raw'
243  ],
244  'Accept Header accepting evertyhing and HTML' => [
245  $helsinki,
246  [ 'ACCEPT' => 'text/html, */*' ],
247  'Helsinki&action=raw'
248  ],
249  'No Accept Header' => [
250  $helsinki,
251  [],
252  'Helsinki&action=raw'
253  ],
254  ];
255  }
256 
266  public function testHttpContentNegotiation(
267  Title $title,
268  array $headers,
269  $expectedRedirectSuffix
270  ) {
271  /* @var FauxResponse $response */
272  $output = $this->makeOutputPage( [], $headers );
273  $request = $output->getRequest();
274 
275  $handler = $this->newHandler();
276  $handler->httpContentNegotiation( $request, $output, $title );
277 
278  $this->assertStringEndsWith(
279  $expectedRedirectSuffix,
280  $output->getRedirect(),
281  'redirect target'
282  );
283  }
284 }
$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:903
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:273
PageDataRequestHandlerTest\makeOutputPage
makeOutputPage(array $params, array $headers)
Definition: PageDataRequestHandlerTest.php:56
PageDataRequestHandlerTest
PageDataRequestHandler PageData.
Definition: PageDataRequestHandlerTest.php:7
$response
this hook is for auditing only $response
Definition: hooks.txt:783
array
the array() calling protocol came about after MediaWiki 1.4rc1.
PageDataRequestHandler
Request handler implementing a data interface for mediawiki pages.
Definition: PageDataRequestHandler.php:30
$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:2255
PageDataRequestHandlerTest\provideHttpContentNegotiation
provideHttpContentNegotiation()
Definition: PageDataRequestHandlerTest.php:226
PageDataRequestHandlerTest\testHttpContentNegotiation
testHttpContentNegotiation(Title $title, array $headers, $expectedRedirectSuffix)
provideHttpContentNegotiation
Definition: PageDataRequestHandlerTest.php:266
$params
$params
Definition: styleTest.css.php:40
HttpError
Show an error that looks like an HTTP server error.
Definition: HttpError.php:30
PageDataRequestHandlerTest\newHandler
newHandler()
Definition: PageDataRequestHandlerTest.php:46
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:37
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
OutputPage
This class should be covered by a general architecture document which does not exist as of January 20...
Definition: OutputPage.php:45
PageDataRequestHandlerTest\$interfaceTitle
Title $interfaceTitle
Definition: PageDataRequestHandlerTest.php:12
$value
$value
Definition: styleTest.css.php:45
PageDataRequestHandlerTest\testHandleRequest
testHandleRequest( $subpage, array $params, array $headers, $expectedOutput, $expectedStatusCode=200, array $expectedHeaders=[])
handleRequestProvider
Definition: PageDataRequestHandlerTest.php:178
PageDataRequestHandlerTest\setUp
setUp()
Definition: PageDataRequestHandlerTest.php:19
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:434
PageDataRequestHandlerTest\$obLevel
int $obLevel
Definition: PageDataRequestHandlerTest.php:17
PageDataRequestHandlerTest\handleRequestProvider
handleRequestProvider()
Definition: PageDataRequestHandlerTest.php:76
Title
Represents a title within MediaWiki.
Definition: Title.php:39
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
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:22
$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:2806
PageDataRequestHandlerTest\tearDown
tearDown()
Definition: PageDataRequestHandlerTest.php:27
$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:2811
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2171