Debian Codegraph @

tests/phpunit/includes/actions/ActionEntryPointTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Action;
4
5 use BadTitleError;
6 use MediaWiki\Actions\ActionEntryPoint;
7 use MediaWiki\Context\RequestContext;
8 use MediaWiki\Deferred\DeferredUpdates;
9 use MediaWiki\Deferred\DeferredUpdatesScopeMediaWikiStack;
10 use MediaWiki\Deferred\DeferredUpdatesScopeStack;
11 use MediaWiki\MainConfigNames;
12 use MediaWiki\Request\FauxRequest;
13 use MediaWiki\Request\FauxResponse;
14 use MediaWiki\Request\WebRequest;
15 use MediaWiki\SpecialPage\SpecialPage;
16 use MediaWiki\Tests\MockEnvironment;
17 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
18 use MediaWiki\Title\MalformedTitleException;
19 use MediaWiki\Title\Title;
20 use MediaWikiIntegrationTestCase;
21 use PHPUnit\Framework\Assert;
22 use Wikimedia\TestingAccessWrapper;
23 use WikiPage;
24
25 // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
26
27 /**
28 * @group Database
29 * @covers \MediaWiki\Actions\ActionEntryPoint
30 */
31 class ActionEntryPointTest extends MediaWikiIntegrationTestCase {
32 use TempUserTestTrait;
33
34 protected function setUp(): void {
35 parent::setUp();
36
37 $this->overrideConfigValues( [
38 MainConfigNames::Server => 'http://example.org',
39 MainConfigNames::ScriptPath => '/w',
40 MainConfigNames::Script => '/w/index.php',
41 MainConfigNames::LanguageCode => 'en',
42 ] );
43
44 // Needed to test redirects to My* special pages as an anonymous user.
45 $this->disableAutoCreateTempUser();
46 }
47
48 protected function tearDown(): void {
49 // Restore a scope stack that will run updates immediately
50 DeferredUpdates::setScopeStack( new DeferredUpdatesScopeMediaWikiStack() );
51 parent::tearDown();
52 }
53
54 /**
55 * @param MockEnvironment|WebRequest|array|null $environment
56 * @param RequestContext|null $context
57 *
58 * @return ActionEntryPoint
59 */
60 private function getEntryPoint( $environment = null, ?RequestContext $context = null ) {
61 if ( !$environment ) {
62 $environment = new MockEnvironment();
63 }
64
65 if ( is_array( $environment ) ) {
66 $environment = new FauxRequest( $environment );
67 }
68
69 if ( $environment instanceof WebRequest ) {
70 $environment = new MockEnvironment( $environment );
71 }
72
73 $entryPoint = new ActionEntryPoint(
74 $context ?? $environment->makeFauxContext(),
75 $environment,
76 $this->getServiceContainer()
77 );
78 $entryPoint->enableOutputCapture();
79
80 return $entryPoint;
81 }
82
83 public static function provideTryNormaliseRedirect() {
84 return [
85 [
86 // View: Canonical
87 'url' => 'http://example.org/wiki/Foo_Bar',
88 'query' => [],
89 'title' => 'Foo_Bar',
90 'redirect' => false,
91 ],
92 [
93 // View: Escaped title
94 'url' => 'http://example.org/wiki/Foo%20Bar',
95 'query' => [],
96 'title' => 'Foo_Bar',
97 'redirect' => 'http://example.org/wiki/Foo_Bar',
98 ],
99 [
100 // View: Script path
101 'url' => 'http://example.org/w/index.php?title=Foo_Bar',
102 'query' => [ 'title' => 'Foo_Bar' ],
103 'title' => 'Foo_Bar',
104 'redirect' => false,
105 ],
106 [
107 // View: Script path with implicit title from page id
108 'url' => 'http://example.org/w/index.php?curid=123',
109 'query' => [ 'curid' => '123' ],
110 'title' => 'Foo_Bar',
111 'redirect' => false,
112 ],
113 [
114 // View: Script path with implicit title from revision id
115 'url' => 'http://example.org/w/index.php?oldid=123',
116 'query' => [ 'oldid' => '123' ],
117 'title' => 'Foo_Bar',
118 'redirect' => false,
119 ],
120 [
121 // View: Script path without title
122 'url' => 'http://example.org/w/index.php',
123 'query' => [],
124 'title' => 'Main_Page',
125 'redirect' => 'http://example.org/wiki/Main_Page',
126 ],
127 [
128 // View: Script path with empty title
129 'url' => 'http://example.org/w/index.php?title=',
130 'query' => [ 'title' => '' ],
131 'title' => 'Main_Page',
132 'redirect' => 'http://example.org/wiki/Main_Page',
133 ],
134 [
135 // View: Index with escaped title
136 'url' => 'http://example.org/w/index.php?title=Foo%20Bar',
137 'query' => [ 'title' => 'Foo Bar' ],
138 'title' => 'Foo_Bar',
139 'redirect' => 'http://example.org/wiki/Foo_Bar',
140 ],
141 [
142 // View: Script path with escaped title
143 'url' => 'http://example.org/w/?title=Foo_Bar',
144 'query' => [ 'title' => 'Foo_Bar' ],
145 'title' => 'Foo_Bar',
146 'redirect' => false,
147 ],
148 [
149 // View: Root path with escaped title
150 'url' => 'http://example.org/?title=Foo_Bar',
151 'query' => [ 'title' => 'Foo_Bar' ],
152 'title' => 'Foo_Bar',
153 'redirect' => false,
154 ],
155 [
156 // View: Canonical with redundant query
157 'url' => 'http://example.org/wiki/Foo_Bar?action=view',
158 'query' => [ 'action' => 'view' ],
159 'title' => 'Foo_Bar',
160 'redirect' => false,
161 ],
162 [
163 // Edit: Canonical view url with action query
164 'url' => 'http://example.org/wiki/Foo_Bar?action=edit',
165 'query' => [ 'action' => 'edit' ],
166 'title' => 'Foo_Bar',
167 'redirect' => false,
168 ],
169 [
170 // View: Index with action query
171 'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=view',
172 'query' => [ 'title' => 'Foo_Bar', 'action' => 'view' ],
173 'title' => 'Foo_Bar',
174 'redirect' => false,
175 ],
176 [
177 // Edit: Index with action query
178 'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=edit',
179 'query' => [ 'title' => 'Foo_Bar', 'action' => 'edit' ],
180 'title' => 'Foo_Bar',
181 'redirect' => false,
182 ],
183 [
184 // Path with double slash prefix (T100782)
185 'url' => 'http://example.org//wiki/Double_slash',
186 'query' => [],
187 'title' => 'Double_slash',
188 'redirect' => false,
189 ],
190 [
191 // View: Media namespace redirect (T203942)
192 'url' => 'http://example.org/w/index.php?title=Media:Foo_Bar',
193 'query' => [ 'title' => 'Foo_Bar' ],
194 'title' => 'File:Foo_Bar',
195 'redirect' => 'http://example.org/wiki/File:Foo_Bar',
196 ],
197 ];
198 }
199
200 /**
201 * @dataProvider provideTryNormaliseRedirect
202 */
203 public function testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect = false ) {
204 $environment = new MockEnvironment();
205 $environment->setRequestInfo( $url, $query );
206
207 $titleObj = Title::newFromText( $title );
208
209 // Set global context since some involved code paths don't yet have context
210 $context = $environment->makeFauxContext();
211 $context->setTitle( $titleObj );
212
213 /** @var ActionEntryPoint $mw */
214 $mw = TestingAccessWrapper::newFromObject( $this->getEntryPoint( $environment, $context ) );
215
216 $ret = $mw->tryNormaliseRedirect( $titleObj );
217
218 $this->assertEquals(
219 $expectedRedirect !== false,
220 $ret,
221 'Return true only when redirecting'
222 );
223
224 $this->assertEquals(
225 $expectedRedirect ?: '',
226 $context->getOutput()->getRedirect()
227 );
228 }
229
230 public function testMainPageIsDomainRoot() {
231 $this->overrideConfigValue( MainConfigNames::MainPageIsDomainRoot, true );
232
233 $environment = new MockEnvironment();
234 $environment->setRequestInfo( '/' );
235
236 // Set global context since some involved code paths don't yet have context
237 $context = $environment->makeFauxContext();
238
239 $entryPoint = $this->getEntryPoint( $environment, $context );
240 $entryPoint->run();
241
242 $expected = '<title>(pagetitle: Main Page)';
243 Assert::assertStringContainsString( $expected, $entryPoint->getCapturedOutput() );
244 }
245
246 public static function provideParseTitle() {
247 return [
248 "No title means main page" => [
249 'query' => [],
250 'expected' => 'Main Page',
251 ],
252 "Empty title also means main page" => [
253 'query' => wfCgiToArray( '?title=' ),
254 'expected' => 'Main Page',
255 ],
256 "Valid title" => [
257 'query' => wfCgiToArray( '?title=Foo' ),
258 'expected' => 'Foo',
259 ],
260 "Invalid title" => [
261 'query' => wfCgiToArray( '?title=[INVALID]' ),
262 'expected' => false,
263 ],
264 "Invalid 'oldid'… means main page? (we show an error elsewhere)" => [
265 'query' => wfCgiToArray( '?oldid=9999999' ),
266 'expected' => 'Main Page',
267 ],
268 "Invalid 'diff'… means main page? (we show an error elsewhere)" => [
269 'query' => wfCgiToArray( '?diff=9999999' ),
270 'expected' => 'Main Page',
271 ],
272 "Invalid 'curid'" => [
273 'query' => wfCgiToArray( '?curid=9999999' ),
274 'expected' => false,
275 ],
276 "'search' parameter with no title provided forces Special:Search" => [
277 'query' => wfCgiToArray( '?search=foo' ),
278 'expected' => 'Special:Search',
279 ],
280 "'action=revisiondelete' forces Special:RevisionDelete even with title" => [
281 'query' => wfCgiToArray( '?action=revisiondelete&title=Unused' ),
282 'expected' => 'Special:RevisionDelete',
283 ],
284 "'action=historysubmit&revisiondelete=1' forces Special:RevisionDelete even with title" => [
285 'query' => wfCgiToArray( '?action=historysubmit&revisiondelete=1&title=Unused' ),
286 'expected' => 'Special:RevisionDelete',
287 ],
288 "'action=editchangetags' forces Special:EditTags even with title" => [
289 'query' => wfCgiToArray( '?action=editchangetags&title=Unused' ),
290 'expected' => 'Special:EditTags',
291 ],
292 "'action=historysubmit&editchangetags=1' forces Special:EditTags even with title" => [
293 'query' => wfCgiToArray( '?action=historysubmit&editchangetags=1&title=Unused' ),
294 'expected' => 'Special:EditTags',
295 ],
296 "No title with 'action' still means main page" => [
297 'query' => wfCgiToArray( '?action=history' ),
298 'expected' => 'Main Page',
299 ],
300 "No title with 'action=delete' does not mean main page, because we want to discourage deleting it by accident :D" => [
301 'query' => wfCgiToArray( '?action=delete' ),
302 'expected' => false,
303 ],
304 ];
305 }
306
307 private function doTestParseTitle( array $query, $expected ): void {
308 if ( $expected === false ) {
309 $this->expectException( MalformedTitleException::class );
310 }
311
312 $req = new FauxRequest( $query );
313 /** @var ActionEntryPoint $mw */
314 $mw = TestingAccessWrapper::newFromObject( $this->getEntryPoint( $req ) );
315
316 $ret = $mw->parseTitle( $req );
317
318 $this->assertEquals(
319 $expected,
320 $ret->getPrefixedText()
321 );
322 }
323
324 /**
325 * @dataProvider provideParseTitle
326 */
327 public function testParseTitle( $query, $expected ) {
328 $this->doTestParseTitle( $query, $expected );
329 }
330
331 public static function provideParseTitleExistingPage(): array {
332 return [
333 "Valid 'oldid'" => [
334 static fn ( WikiPage $page ): array => wfCgiToArray( '?oldid=' . $page->getRevisionRecord()->getId() ),
335 ],
336 "Valid 'diff'" => [
337 static fn ( WikiPage $page ): array => wfCgiToArray( '?diff=' . $page->getRevisionRecord()->getId() ),
338 ],
339 "Valid 'curid'" => [
340 static fn ( WikiPage $page ): array => wfCgiToArray( '?curid=' . $page->getId() ),
341 ],
342 ];
343 }
344
345 /**
346 * @dataProvider provideParseTitleExistingPage
347 */
348 public function testParseTitle__existingPage( callable $queryBuildCallback ) {
349 $pageTitle = 'TestParseTitle test page';
350 $page = $this->getExistingTestPage( $pageTitle );
351 $query = $queryBuildCallback( $page );
352 $this->doTestParseTitle( $query, $pageTitle );
353 }
354
355 /**
356 * Test a post-send update cannot set cookies (T191537).
357 * @coversNothing
358 */
359 public function testPostSendJobDoesNotSetCookie() {
360 // Prevent updates from running immediately by setting
361 // a plain DeferredUpdatesScopeStack which doesn't allow
362 // opportunistic updates.
363 DeferredUpdates::setScopeStack( new DeferredUpdatesScopeStack() );
364
365 $mw = TestingAccessWrapper::newFromObject( $this->getEntryPoint() );
366
367 /** @var FauxResponse $response */
368 $response = $mw->getResponse();
369
370 // A update that attempts to set a cookie
371 $jobHasRun = false;
372 DeferredUpdates::addCallableUpdate( static function () use ( $response, &$jobHasRun ) {
373 $jobHasRun = true;
374 $response->setCookie( 'JobCookie', 'yes' );
375 $response->header( 'Foo: baz' );
376 } );
377
378 $mw->doPostOutputShutdown();
379
380 // restInPeace() might have been registered to a callback of
381 // register_postsend_function() and thus cannot be triggered from
382 // PHPUnit.
383 if ( $jobHasRun === false ) {
384 $mw->restInPeace();
385 }
386
387 $this->assertTrue( $jobHasRun, 'post-send job has run' );
388 $this->assertNull( $response->getCookie( 'JobCookie' ) );
389 $this->assertNull( $response->getHeader( 'Foo' ) );
390 }
391
392 public function testInvalidRedirectingOnSpecialPageWithPersonallyIdentifiableTarget() {
393 $this->overrideConfigValue( MainConfigNames::HideIdentifiableRedirects, true );
394
395 $specialTitle = SpecialPage::getTitleFor( 'Mypage', 'in<valid' );
396 $req = new FauxRequest( [
397 'title' => $specialTitle->getPrefixedDbKey(),
398 ] );
399 $req->setRequestURL( $specialTitle->getLinkURL() );
400
401 $env = new MockEnvironment( $req );
402 $context = $env->makeFauxContext();
403 $context->setTitle( $specialTitle );
404
405 $mw = TestingAccessWrapper::newFromObject( $this->getEntryPoint( $env, $context ) );
406
407 $this->expectException( BadTitleError::class );
408 $this->expectExceptionMessage( 'The requested page title contains invalid characters: "<".' );
409 $mw->performRequest();
410 }
411
412 public function testView() {
413 $page = $this->getExistingTestPage();
414
415 $request = new FauxRequest( [ 'title' => $page->getTitle()->getPrefixedDBkey() ] );
416 $env = new MockEnvironment( $request );
417
418 $entryPoint = $this->getEntryPoint( $env );
419 $entryPoint->run();
420
421 $expected = '<title>(pagetitle: ' . $page->getTitle()->getPrefixedText();
422 Assert::assertStringContainsString( $expected, $entryPoint->getCapturedOutput() );
423 }
424
425 }