MediaWiki  REL1_31
MediaWikiTest.php
Go to the documentation of this file.
1 <?php
2 
4  protected function setUp() {
5  parent::setUp();
6 
7  $this->setMwGlobals( [
8  'wgServer' => 'http://example.org',
9  'wgScriptPath' => '/w',
10  'wgScript' => '/w/index.php',
11  'wgArticlePath' => '/wiki/$1',
12  'wgActionPaths' => [],
13  ] );
14  }
15 
16  public static function provideTryNormaliseRedirect() {
17  return [
18  [
19  // View: Canonical
20  'url' => 'http://example.org/wiki/Foo_Bar',
21  'query' => [],
22  'title' => 'Foo_Bar',
23  'redirect' => false,
24  ],
25  [
26  // View: Escaped title
27  'url' => 'http://example.org/wiki/Foo%20Bar',
28  'query' => [],
29  'title' => 'Foo_Bar',
30  'redirect' => 'http://example.org/wiki/Foo_Bar',
31  ],
32  [
33  // View: Script path
34  'url' => 'http://example.org/w/index.php?title=Foo_Bar',
35  'query' => [ 'title' => 'Foo_Bar' ],
36  'title' => 'Foo_Bar',
37  'redirect' => false,
38  ],
39  [
40  // View: Script path with implicit title from page id
41  'url' => 'http://example.org/w/index.php?curid=123',
42  'query' => [ 'curid' => '123' ],
43  'title' => 'Foo_Bar',
44  'redirect' => false,
45  ],
46  [
47  // View: Script path with implicit title from revision id
48  'url' => 'http://example.org/w/index.php?oldid=123',
49  'query' => [ 'oldid' => '123' ],
50  'title' => 'Foo_Bar',
51  'redirect' => false,
52  ],
53  [
54  // View: Script path without title
55  'url' => 'http://example.org/w/index.php',
56  'query' => [],
57  'title' => 'Main_Page',
58  'redirect' => 'http://example.org/wiki/Main_Page',
59  ],
60  [
61  // View: Script path with empty title
62  'url' => 'http://example.org/w/index.php?title=',
63  'query' => [ 'title' => '' ],
64  'title' => 'Main_Page',
65  'redirect' => 'http://example.org/wiki/Main_Page',
66  ],
67  [
68  // View: Index with escaped title
69  'url' => 'http://example.org/w/index.php?title=Foo%20Bar',
70  'query' => [ 'title' => 'Foo Bar' ],
71  'title' => 'Foo_Bar',
72  'redirect' => 'http://example.org/wiki/Foo_Bar',
73  ],
74  [
75  // View: Script path with escaped title
76  'url' => 'http://example.org/w/?title=Foo_Bar',
77  'query' => [ 'title' => 'Foo_Bar' ],
78  'title' => 'Foo_Bar',
79  'redirect' => false,
80  ],
81  [
82  // View: Root path with escaped title
83  'url' => 'http://example.org/?title=Foo_Bar',
84  'query' => [ 'title' => 'Foo_Bar' ],
85  'title' => 'Foo_Bar',
86  'redirect' => false,
87  ],
88  [
89  // View: Canonical with redundant query
90  'url' => 'http://example.org/wiki/Foo_Bar?action=view',
91  'query' => [ 'action' => 'view' ],
92  'title' => 'Foo_Bar',
93  'redirect' => false,
94  ],
95  [
96  // Edit: Canonical view url with action query
97  'url' => 'http://example.org/wiki/Foo_Bar?action=edit',
98  'query' => [ 'action' => 'edit' ],
99  'title' => 'Foo_Bar',
100  'redirect' => false,
101  ],
102  [
103  // View: Index with action query
104  'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=view',
105  'query' => [ 'title' => 'Foo_Bar', 'action' => 'view' ],
106  'title' => 'Foo_Bar',
107  'redirect' => false,
108  ],
109  [
110  // Edit: Index with action query
111  'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=edit',
112  'query' => [ 'title' => 'Foo_Bar', 'action' => 'edit' ],
113  'title' => 'Foo_Bar',
114  'redirect' => false,
115  ],
116  ];
117  }
118 
123  public function testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect = false ) {
124  // Set SERVER because interpolateTitle() doesn't use getRequestURL(),
125  // whereas tryNormaliseRedirect does().
126  $_SERVER['REQUEST_URI'] = $url;
127 
128  $req = new FauxRequest( $query );
129  $req->setRequestURL( $url );
130  // This adds a virtual 'title' query parameter. Normally called from Setup.php
131  $req->interpolateTitle();
132 
133  $titleObj = Title::newFromText( $title );
134 
135  // Set global context since some involved code paths don't yet have context
137  $context->setRequest( $req );
138  $context->setTitle( $titleObj );
139 
140  $mw = new MediaWiki( $context );
141 
142  $method = new ReflectionMethod( $mw, 'tryNormaliseRedirect' );
143  $method->setAccessible( true );
144  $ret = $method->invoke( $mw, $titleObj );
145 
146  $this->assertEquals(
147  $expectedRedirect !== false,
148  $ret,
149  'Return true only when redirecting'
150  );
151 
152  $this->assertEquals(
153  $expectedRedirect ?: '',
154  $context->getOutput()->getRedirect()
155  );
156  }
157 
162  // Prevent updates from running
163  $this->setMwGlobals( 'wgCommandLineMode', false );
164 
165  $response = new WebResponse;
166 
167  // A job that attempts to set a cookie
168  $jobHasRun = false;
169  DeferredUpdates::addCallableUpdate( function () use ( $response, &$jobHasRun ) {
170  $jobHasRun = true;
171  $response->setCookie( 'JobCookie', 'yes' );
172  $response->header( 'Foo: baz' );
173  } );
174 
175  $hookWasRun = false;
176  $this->setTemporaryHook( 'WebResponseSetCookie', function () use ( &$hookWasRun ) {
177  $hookWasRun = true;
178  return true;
179  } );
180 
181  $logger = new TestLogger();
182  $logger->setCollect( true );
183  $this->setLogger( 'cookie', $logger );
184  $this->setLogger( 'header', $logger );
185 
186  $mw = new MediaWiki();
187  $mw->doPostOutputShutdown();
188  // restInPeace() might have been registered to a callback of
189  // register_postsend_function() and thus can not be triggered from
190  // PHPUnit.
191  if ( $jobHasRun === false ) {
192  $mw->restInPeace();
193  }
194 
195  $this->assertTrue( $jobHasRun, 'post-send job has run' );
196  $this->assertFalse( $hookWasRun,
197  'post-send job must not trigger WebResponseSetCookie hook' );
198  $this->assertEquals(
199  [
200  [ 'info', 'ignored post-send cookie {cookie}' ],
201  [ 'info', 'ignored post-send header {header}' ],
202  ],
203  $logger->getBuffer()
204  );
205  }
206 }
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
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MediaWikiTest
Definition: MediaWikiTest.php:3
$response
this hook is for auditing only $response
Definition: hooks.txt:783
$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:2005
MediaWikiTest\setUp
setUp()
Definition: MediaWikiTest.php:4
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:37
MediaWikiTest\provideTryNormaliseRedirect
static provideTryNormaliseRedirect()
Definition: MediaWikiTest.php:16
MediaWikiTest\testTryNormaliseRedirect
testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect=false)
provideTryNormaliseRedirect MediaWiki::tryNormaliseRedirect
Definition: MediaWikiTest.php:123
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:678
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWiki
This document describes the state of Postgres support in MediaWiki
Definition: postgres.txt:3
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
MediaWikiTest\testPostSendJobDoesNotSetCookie
testPostSendJobDoesNotSetCookie()
Test a post-send job can not set cookies (T191537).
Definition: MediaWikiTest.php:161
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:434
$req
this hook is for auditing only $req
Definition: hooks.txt:990
MediaWikiTestCase\setLogger
setLogger( $channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
Definition: MediaWikiTestCase.php:942
MediaWikiTestCase\setTemporaryHook
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
Definition: MediaWikiTestCase.php:2014
$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:1620
WebResponse
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Definition: WebResponse.php:30
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:111
$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