3 class MediaWikiTest
extends MediaWikiTestCase
{
4 private $oldServer, $oldGet, $oldPost;
6 protected function setUp() {
10 'wgServer' => 'http://example.org',
11 'wgScriptPath' => '/w',
12 'wgScript' => '/w/index.php',
13 'wgArticlePath' => '/wiki/$1',
14 'wgActionPaths' => [],
17 // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
18 $this->oldServer
= $_SERVER;
19 $this->oldGet
= $_GET;
20 $this->oldPost
= $_POST;
23 protected function tearDown() {
25 $_SERVER = $this->oldServer
;
26 $_GET = $this->oldGet
;
27 $_POST = $this->oldPost
;
30 public static function provideTryNormaliseRedirect() {
34 'url' => 'http://example.org/wiki/Foo_Bar',
40 // View: Escaped title
41 'url' => 'http://example.org/wiki/Foo%20Bar',
44 'redirect' => 'http://example.org/wiki/Foo_Bar',
48 'url' => 'http://example.org/w/index.php?title=Foo_Bar',
49 'query' => [ 'title' => 'Foo_Bar' ],
54 // View: Script path with implicit title from page id
55 'url' => 'http://example.org/w/index.php?curid=123',
56 'query' => [ 'curid' => '123' ],
61 // View: Script path with implicit title from revision id
62 'url' => 'http://example.org/w/index.php?oldid=123',
63 'query' => [ 'oldid' => '123' ],
68 // View: Script path without title
69 'url' => 'http://example.org/w/index.php',
71 'title' => 'Main_Page',
72 'redirect' => 'http://example.org/wiki/Main_Page',
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',
82 // View: Index with escaped title
83 'url' => 'http://example.org/w/index.php?title=Foo%20Bar',
84 'query' => [ 'title' => 'Foo Bar' ],
86 'redirect' => 'http://example.org/wiki/Foo_Bar',
89 // View: Script path with escaped title
90 'url' => 'http://example.org/w/?title=Foo_Bar',
91 'query' => [ 'title' => 'Foo_Bar' ],
96 // View: Root path with escaped title
97 'url' => 'http://example.org/?title=Foo_Bar',
98 'query' => [ 'title' => 'Foo_Bar' ],
103 // View: Canonical with redundant query
104 'url' => 'http://example.org/wiki/Foo_Bar?action=view',
105 'query' => [ 'action' => 'view' ],
106 'title' => 'Foo_Bar',
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',
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',
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',
131 // Path with double slash prefix (T100782)
132 'url' => 'http://example.org//wiki/Double_slash',
134 'title' => 'Double_slash',
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',
148 * @dataProvider provideTryNormaliseRedirect
149 * @covers MediaWiki::tryNormaliseRedirect
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;
158 $req = new WebRequest
;
160 // This adds a virtual 'title' query parameter. Normally called from Setup.php
161 $req->interpolateTitle();
163 $titleObj = Title
::newFromText( $title );
165 // Set global context since some involved code paths don't yet have context
166 $context = RequestContext
::getMain();
167 $context->setRequest( $req );
168 $context->setTitle( $titleObj );
170 $mw = new MediaWiki( $context );
172 $method = new ReflectionMethod( $mw, 'tryNormaliseRedirect' );
173 $method->setAccessible( true );
174 $ret = $method->invoke( $mw, $titleObj );
177 $expectedRedirect !== false,
179 'Return true only when redirecting'
183 $expectedRedirect ?
: '',
184 $context->getOutput()->getRedirect()
189 * Test a post-send job can not set cookies (T191537).
192 public function testPostSendJobDoesNotSetCookie() {
193 // Prevent updates from running
194 $this->setMwGlobals( 'wgCommandLineMode', false );
196 $response = new WebResponse
;
198 // A job that attempts to set a cookie
200 DeferredUpdates
::addCallableUpdate( function () use ( $response, &$jobHasRun ) {
202 $response->setCookie( 'JobCookie', 'yes' );
203 $response->header( 'Foo: baz' );
207 $this->setTemporaryHook( 'WebResponseSetCookie', function () use ( &$hookWasRun ) {
212 $logger = new TestLogger();
213 $logger->setCollect( true );
214 $this->setLogger( 'cookie', $logger );
215 $this->setLogger( 'header', $logger );
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
222 if ( $jobHasRun === false ) {
226 $this->assertTrue( $jobHasRun, 'post-send job has run' );
227 $this->assertFalse( $hookWasRun,
228 'post-send job must not trigger WebResponseSetCookie hook' );
231 [ 'info', 'ignored post-send cookie {cookie}' ],
232 [ 'info', 'ignored post-send header {header}' ],