MediaWiki  1.33.0
MediaWikiTest.php
Go to the documentation of this file.
1 <?php
2 
5 
6  protected function setUp() {
7  parent::setUp();
8 
9  $this->setMwGlobals( [
10  'wgServer' => 'http://example.org',
11  'wgScriptPath' => '/w',
12  'wgScript' => '/w/index.php',
13  'wgArticlePath' => '/wiki/$1',
14  'wgActionPaths' => [],
15  ] );
16 
17  // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
18  $this->oldServer = $_SERVER;
19  $this->oldGet = $_GET;
20  $this->oldPost = $_POST;
21  }
22 
23  protected function tearDown() {
24  parent::tearDown();
25  $_SERVER = $this->oldServer;
26  $_GET = $this->oldGet;
27  $_POST = $this->oldPost;
28  }
29 
30  public static function provideTryNormaliseRedirect() {
31  return [
32  [
33  // View: Canonical
34  'url' => 'http://example.org/wiki/Foo_Bar',
35  'query' => [],
36  'title' => 'Foo_Bar',
37  'redirect' => false,
38  ],
39  [
40  // View: Escaped title
41  'url' => 'http://example.org/wiki/Foo%20Bar',
42  'query' => [],
43  'title' => 'Foo_Bar',
44  'redirect' => 'http://example.org/wiki/Foo_Bar',
45  ],
46  [
47  // View: Script path
48  'url' => 'http://example.org/w/index.php?title=Foo_Bar',
49  'query' => [ 'title' => 'Foo_Bar' ],
50  'title' => 'Foo_Bar',
51  'redirect' => false,
52  ],
53  [
54  // View: Script path with implicit title from page id
55  'url' => 'http://example.org/w/index.php?curid=123',
56  'query' => [ 'curid' => '123' ],
57  'title' => 'Foo_Bar',
58  'redirect' => false,
59  ],
60  [
61  // View: Script path with implicit title from revision id
62  'url' => 'http://example.org/w/index.php?oldid=123',
63  'query' => [ 'oldid' => '123' ],
64  'title' => 'Foo_Bar',
65  'redirect' => false,
66  ],
67  [
68  // View: Script path without title
69  'url' => 'http://example.org/w/index.php',
70  'query' => [],
71  'title' => 'Main_Page',
72  'redirect' => 'http://example.org/wiki/Main_Page',
73  ],
74  [
75  // View: Script path with empty title
76  'url' => 'http://example.org/w/index.php?title=',
77  'query' => [ 'title' => '' ],
78  'title' => 'Main_Page',
79  'redirect' => 'http://example.org/wiki/Main_Page',
80  ],
81  [
82  // View: Index with escaped title
83  'url' => 'http://example.org/w/index.php?title=Foo%20Bar',
84  'query' => [ 'title' => 'Foo Bar' ],
85  'title' => 'Foo_Bar',
86  'redirect' => 'http://example.org/wiki/Foo_Bar',
87  ],
88  [
89  // View: Script path with escaped title
90  'url' => 'http://example.org/w/?title=Foo_Bar',
91  'query' => [ 'title' => 'Foo_Bar' ],
92  'title' => 'Foo_Bar',
93  'redirect' => false,
94  ],
95  [
96  // View: Root path with escaped title
97  'url' => 'http://example.org/?title=Foo_Bar',
98  'query' => [ 'title' => 'Foo_Bar' ],
99  'title' => 'Foo_Bar',
100  'redirect' => false,
101  ],
102  [
103  // View: Canonical with redundant query
104  'url' => 'http://example.org/wiki/Foo_Bar?action=view',
105  'query' => [ 'action' => 'view' ],
106  'title' => 'Foo_Bar',
107  'redirect' => false,
108  ],
109  [
110  // Edit: Canonical view url with action query
111  'url' => 'http://example.org/wiki/Foo_Bar?action=edit',
112  'query' => [ 'action' => 'edit' ],
113  'title' => 'Foo_Bar',
114  'redirect' => false,
115  ],
116  [
117  // View: Index with action query
118  'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=view',
119  'query' => [ 'title' => 'Foo_Bar', 'action' => 'view' ],
120  'title' => 'Foo_Bar',
121  'redirect' => false,
122  ],
123  [
124  // Edit: Index with action query
125  'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=edit',
126  'query' => [ 'title' => 'Foo_Bar', 'action' => 'edit' ],
127  'title' => 'Foo_Bar',
128  'redirect' => false,
129  ],
130  [
131  // Path with double slash prefix (T100782)
132  'url' => 'http://example.org//wiki/Double_slash',
133  'query' => [],
134  'title' => 'Double_slash',
135  'redirect' => false,
136  ],
137  [
138  // View: Media namespace redirect (T203942)
139  'url' => 'http://example.org/w/index.php?title=Media:Foo_Bar',
140  'query' => [ 'title' => 'Foo_Bar' ],
141  'title' => 'File:Foo_Bar',
142  'redirect' => 'http://example.org/wiki/File:Foo_Bar',
143  ],
144  ];
145  }
146 
151  public function testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect = false ) {
152  // Set SERVER because interpolateTitle() doesn't use getRequestURL(),
153  // whereas tryNormaliseRedirect does(). Also, using WebRequest allows
154  // us to test some quirks in that class.
155  $_SERVER['REQUEST_URI'] = $url;
156  $_POST = [];
157  $_GET = $query;
158  $req = new WebRequest;
159 
160  // This adds a virtual 'title' query parameter. Normally called from Setup.php
161  $req->interpolateTitle();
162 
163  $titleObj = Title::newFromText( $title );
164 
165  // Set global context since some involved code paths don't yet have context
167  $context->setRequest( $req );
168  $context->setTitle( $titleObj );
169 
170  $mw = new MediaWiki( $context );
171 
172  $method = new ReflectionMethod( $mw, 'tryNormaliseRedirect' );
173  $method->setAccessible( true );
174  $ret = $method->invoke( $mw, $titleObj );
175 
176  $this->assertEquals(
177  $expectedRedirect !== false,
178  $ret,
179  'Return true only when redirecting'
180  );
181 
182  $this->assertEquals(
183  $expectedRedirect ?: '',
184  $context->getOutput()->getRedirect()
185  );
186  }
187 
193  // Prevent updates from running
194  $this->setMwGlobals( 'wgCommandLineMode', false );
195 
196  $response = new WebResponse;
197 
198  // A job that attempts to set a cookie
199  $jobHasRun = false;
200  DeferredUpdates::addCallableUpdate( function () use ( $response, &$jobHasRun ) {
201  $jobHasRun = true;
202  $response->setCookie( 'JobCookie', 'yes' );
203  $response->header( 'Foo: baz' );
204  } );
205 
206  $hookWasRun = false;
207  $this->setTemporaryHook( 'WebResponseSetCookie', function () use ( &$hookWasRun ) {
208  $hookWasRun = true;
209  return true;
210  } );
211 
212  $logger = new TestLogger();
213  $logger->setCollect( true );
214  $this->setLogger( 'cookie', $logger );
215  $this->setLogger( 'header', $logger );
216 
217  $mw = new MediaWiki();
218  $mw->doPostOutputShutdown();
219  // restInPeace() might have been registered to a callback of
220  // register_postsend_function() and thus can not be triggered from
221  // PHPUnit.
222  if ( $jobHasRun === false ) {
223  $mw->restInPeace();
224  }
225 
226  $this->assertTrue( $jobHasRun, 'post-send job has run' );
227  $this->assertFalse( $hookWasRun,
228  'post-send job must not trigger WebResponseSetCookie hook' );
229  $this->assertEquals(
230  [
231  [ 'info', 'ignored post-send cookie {cookie}' ],
232  [ 'info', 'ignored post-send header {header}' ],
233  ],
234  $logger->getBuffer()
235  );
236  }
237 }
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
MediaWikiTest\$oldPost
$oldPost
Definition: MediaWikiTest.php:4
$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
MediaWikiTest
Definition: MediaWikiTest.php:3
MediaWikiTest\setUp
setUp()
Definition: MediaWikiTest.php:6
$req
this hook is for auditing only $req
Definition: hooks.txt:979
TestLogger
A logger that may be configured to either buffer logs or to print them to the output where PHPUnit wi...
Definition: TestLogger.php:33
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
MediaWikiTest\provideTryNormaliseRedirect
static provideTryNormaliseRedirect()
Definition: MediaWikiTest.php:30
$query
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1588
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
MediaWikiTest\testTryNormaliseRedirect
testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect=false)
provideTryNormaliseRedirect MediaWiki::tryNormaliseRedirect
Definition: MediaWikiTest.php:151
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
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki
This document describes the state of Postgres support in MediaWiki
Definition: postgres.txt:4
MediaWikiTest\$oldGet
$oldGet
Definition: MediaWikiTest.php:4
MediaWikiTest\testPostSendJobDoesNotSetCookie
testPostSendJobDoesNotSetCookie()
Test a post-send job can not set cookies (T191537).
Definition: MediaWikiTest.php:192
MediaWikiTest\tearDown
tearDown()
Definition: MediaWikiTest.php:23
MediaWikiTest\$oldServer
$oldServer
Definition: MediaWikiTest.php:4
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1985
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
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:41
MediaWikiTestCase\setLogger
setLogger( $channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
Definition: MediaWikiTestCase.php:1118
MediaWikiTestCase\setTemporaryHook
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
Definition: MediaWikiTestCase.php:2325
WebResponse
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Definition: WebResponse.php:28
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:118