MediaWiki  1.33.0
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( __CLASS__ );
23  $this->obLevel = ob_get_level();
24 
25  $this->setMwGlobals( 'wgArticlePath', '/wiki/$1' );
26  }
27 
28  protected function tearDown() {
29  $obLevel = ob_get_level();
30 
31  while ( ob_get_level() > $this->obLevel ) {
32  ob_end_clean();
33  }
34 
35  if ( $obLevel !== $this->obLevel ) {
36  $this->fail( "Test changed output buffer level: was {$this->obLevel}" .
37  "before test, but $obLevel after test."
38  );
39  }
40 
41  parent::tearDown();
42  }
43 
47  protected function newHandler() {
48  return new PageDataRequestHandler();
49  }
50 
57  protected function makeOutputPage( array $params, array $headers ) {
58  // construct request
59  $request = new FauxRequest( $params );
60  $request->response()->header( 'Status: 200 OK', true, 200 ); // init/reset
61 
62  foreach ( $headers as $name => $value ) {
63  $request->setHeader( strtoupper( $name ), $value );
64  }
65 
66  // construct Context and OutputPage
68  $context->setRequest( $request );
69 
70  $output = new OutputPage( $context );
71  $output->setTitle( $this->interfaceTitle );
72  $context->setOutput( $output );
73 
74  return $output;
75  }
76 
77  public function handleRequestProvider() {
78  $cases = [];
79 
80  $cases[] = [ '', [], [], 'Invalid title', 400 ];
81 
82  $cases[] = [
83  '',
84  [ 'target' => 'Helsinki' ],
85  [],
86  '',
87  303,
88  [ 'Location' => '?title=Helsinki&action=raw' ]
89  ];
90 
91  $subpageCases = [];
92  foreach ( $cases as $c ) {
93  $case = $c;
94  $case[0] = 'main/';
95 
96  if ( isset( $case[1]['target'] ) ) {
97  $case[0] .= $case[1]['target'];
98  unset( $case[1]['target'] );
99  }
100 
101  $subpageCases[] = $case;
102  }
103 
104  $cases = array_merge( $cases, $subpageCases );
105 
106  $cases[] = [
107  '',
108  [ 'target' => 'Helsinki' ],
109  [ 'Accept' => 'text/HTML' ],
110  '',
111  303,
112  [ 'Location' => '/wiki/Helsinki' ]
113  ];
114 
115  $cases[] = [
116  '',
117  [
118  'target' => 'Helsinki',
119  'revision' => '4242',
120  ],
121  [ 'Accept' => 'text/HTML' ],
122  '',
123  303,
124  [ 'Location' => '?title=Helsinki&oldid=4242' ]
125  ];
126 
127  $cases[] = [
128  '/Helsinki',
129  [],
130  [],
131  '',
132  303,
133  [ 'Location' => '?title=Helsinki&action=raw' ]
134  ];
135 
136  // #31: /Q5 with "Accept: text/foobar" triggers a 406
137  $cases[] = [
138  'main/Helsinki',
139  [],
140  [ 'Accept' => 'text/foobar' ],
141  'No matching format found',
142  406,
143  ];
144 
145  $cases[] = [
146  'no slash',
147  [],
148  [ 'Accept' => 'text/HTML' ],
149  'Invalid title',
150  400,
151  ];
152 
153  $cases[] = [
154  'main',
155  [],
156  [ 'Accept' => 'text/HTML' ],
157  'Invalid title',
158  400,
159  ];
160 
161  $cases[] = [
162  'xyz/Helsinki',
163  [],
164  [ 'Accept' => 'text/HTML' ],
165  'Invalid title',
166  400,
167  ];
168 
169  $cases[] = [
170  'main/Helsinki',
171  [],
172  [ 'Accept' => 'text/HTML' ],
173  '',
174  303,
175  [ 'Location' => '/wiki/Helsinki' ]
176  ];
177 
178  $cases[] = [
179  '/Helsinki',
180  [],
181  [ 'Accept' => 'text/HTML' ],
182  '',
183  303,
184  [ 'Location' => '/wiki/Helsinki' ]
185  ];
186 
187  $cases[] = [
188  'main/AC/DC',
189  [],
190  [ 'Accept' => 'text/HTML' ],
191  '',
192  303,
193  [ 'Location' => '/wiki/AC/DC' ]
194  ];
195 
196  return $cases;
197  }
198 
209  public function testHandleRequest(
210  $subpage,
211  array $params,
212  array $headers,
213  $expectedOutput = '',
214  $expectedStatusCode = 200,
215  array $expectedHeaders = []
216  ) {
217  $output = $this->makeOutputPage( $params, $headers );
218  $request = $output->getRequest();
219 
220  /* @var FauxResponse $response */
221  $response = $request->response();
222 
223  // construct handler
224  $handler = $this->newHandler();
225 
226  try {
227  ob_start();
228  $handler->handleRequest( $subpage, $request, $output );
229 
230  if ( $output->getRedirect() !== '' ) {
231  // hack to apply redirect to web response
232  $output->output();
233  }
234 
235  $text = ob_get_clean();
236 
237  $this->assertEquals( $expectedStatusCode, $response->getStatusCode(), 'status code' );
238  $this->assertSame( $expectedOutput, $text, 'output' );
239 
240  foreach ( $expectedHeaders as $name => $exp ) {
241  $value = $response->getHeader( $name );
242  $this->assertNotNull( $value, "header: $name" );
243  $this->assertInternalType( 'string', $value, "header: $name" );
244  $this->assertStringEndsWith( $exp, $value, "header: $name" );
245  }
246  } catch ( HttpError $e ) {
247  ob_end_clean();
248  $this->assertEquals( $expectedStatusCode, $e->getStatusCode(), 'status code' );
249  $this->assertContains( $expectedOutput, $e->getHTML(), 'error output' );
250  }
251 
252  // We always set "Access-Control-Allow-Origin: *"
253  $this->assertSame( '*', $response->getHeader( 'Access-Control-Allow-Origin' ) );
254  }
255 
256  public function provideHttpContentNegotiation() {
257  $helsinki = Title::newFromText( 'Helsinki' );
258  return [
259  'Accept Header of HTML' => [
260  $helsinki,
261  [ 'ACCEPT' => 'text/html' ], // headers
262  'Helsinki'
263  ],
264  'Accept Header without weights' => [
265  $helsinki,
266  [ 'ACCEPT' => '*/*, text/html, text/x-wiki' ],
267  'Helsinki&action=raw'
268  ],
269  'Accept Header with weights' => [
270  $helsinki,
271  [ 'ACCEPT' => 'text/*; q=0.5, text/json; q=0.7, application/rdf+xml; q=0.8' ],
272  'Helsinki&action=raw'
273  ],
274  'Accept Header accepting evertyhing and HTML' => [
275  $helsinki,
276  [ 'ACCEPT' => 'text/html, */*' ],
277  'Helsinki&action=raw'
278  ],
279  'No Accept Header' => [
280  $helsinki,
281  [],
282  'Helsinki&action=raw'
283  ],
284  ];
285  }
286 
296  public function testHttpContentNegotiation(
297  Title $title,
298  array $headers,
299  $expectedRedirectSuffix
300  ) {
301  /* @var FauxResponse $response */
302  $output = $this->makeOutputPage( [], $headers );
303  $request = $output->getRequest();
304 
305  $handler = $this->newHandler();
306  $handler->httpContentNegotiation( $request, $output, $title );
307 
308  $this->assertStringEndsWith(
309  $expectedRedirectSuffix,
310  $output->getRedirect(),
311  'redirect target'
312  );
313  }
314 }
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:306
PageDataRequestHandlerTest\makeOutputPage
makeOutputPage(array $params, array $headers)
Definition: PageDataRequestHandlerTest.php:57
PageDataRequestHandlerTest
PageDataRequestHandler PageData.
Definition: PageDataRequestHandlerTest.php:7
$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:2636
PageDataRequestHandler
Request handler implementing a data interface for mediawiki pages.
Definition: PageDataRequestHandler.php:30
PageDataRequestHandlerTest\provideHttpContentNegotiation
provideHttpContentNegotiation()
Definition: PageDataRequestHandlerTest.php:256
PageDataRequestHandlerTest\testHttpContentNegotiation
testHttpContentNegotiation(Title $title, array $headers, $expectedRedirectSuffix)
provideHttpContentNegotiation
Definition: PageDataRequestHandlerTest.php:296
$params
$params
Definition: styleTest.css.php:44
HttpError
Show an error that looks like an HTTP server error.
Definition: HttpError.php:30
PageDataRequestHandlerTest\newHandler
newHandler()
Definition: PageDataRequestHandlerTest.php:47
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:30
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
$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 When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:780
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
$output
$output
Definition: SyntaxHighlight.php:334
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$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:2636
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
PageDataRequestHandlerTest\$interfaceTitle
Title $interfaceTitle
Definition: PageDataRequestHandlerTest.php:12
$value
$value
Definition: styleTest.css.php:49
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:8
PageDataRequestHandlerTest\setUp
setUp()
Definition: PageDataRequestHandlerTest.php:19
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:430
$response
this hook is for auditing only $response
Definition: hooks.txt:780
PageDataRequestHandlerTest\$obLevel
int $obLevel
Definition: PageDataRequestHandlerTest.php:17
PageDataRequestHandlerTest\handleRequestProvider
handleRequestProvider()
Definition: PageDataRequestHandlerTest.php:77
Title
Represents a title within MediaWiki.
Definition: Title.php:40
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:28
PageDataRequestHandlerTest\testHandleRequest
testHandleRequest( $subpage, array $params, array $headers, $expectedOutput='', $expectedStatusCode=200, array $expectedHeaders=[])
handleRequestProvider
Definition: PageDataRequestHandlerTest.php:209