2015-06-19 19:56:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class MediaWikiTest extends MediaWikiTestCase {
|
|
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2015-06-19 19:56:36 +00:00
|
|
|
'wgServer' => 'http://example.org',
|
|
|
|
|
'wgScriptPath' => '/w',
|
|
|
|
|
'wgScript' => '/w/index.php',
|
|
|
|
|
'wgArticlePath' => '/wiki/$1',
|
2016-02-17 09:09:32 +00:00
|
|
|
'wgActionPaths' => [],
|
|
|
|
|
] );
|
2015-06-19 19:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideTryNormaliseRedirect() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Canonical
|
|
|
|
|
'url' => 'http://example.org/wiki/Foo_Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
|
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Escaped title
|
|
|
|
|
'url' => 'http://example.org/wiki/Foo%20Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
|
|
|
|
'redirect' => 'http://example.org/wiki/Foo_Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Script path
|
|
|
|
|
'url' => 'http://example.org/w/index.php?title=Foo_Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'title' => 'Foo_Bar' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
2016-09-09 14:53:23 +00:00
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Script path with implicit title from page id
|
|
|
|
|
'url' => 'http://example.org/w/index.php?curid=123',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'curid' => '123' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
|
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Script path with implicit title from revision id
|
|
|
|
|
'url' => 'http://example.org/w/index.php?oldid=123',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'oldid' => '123' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
|
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Script path without title
|
|
|
|
|
'url' => 'http://example.org/w/index.php',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Main_Page',
|
|
|
|
|
'redirect' => 'http://example.org/wiki/Main_Page',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Script path with empty title
|
|
|
|
|
'url' => 'http://example.org/w/index.php?title=',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'title' => '' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Main_Page',
|
|
|
|
|
'redirect' => 'http://example.org/wiki/Main_Page',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Index with escaped title
|
|
|
|
|
'url' => 'http://example.org/w/index.php?title=Foo%20Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'title' => 'Foo Bar' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
|
|
|
|
'redirect' => 'http://example.org/wiki/Foo_Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Script path with escaped title
|
|
|
|
|
'url' => 'http://example.org/w/?title=Foo_Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'title' => 'Foo_Bar' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
2016-09-09 14:53:23 +00:00
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Root path with escaped title
|
|
|
|
|
'url' => 'http://example.org/?title=Foo_Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'title' => 'Foo_Bar' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
2016-09-09 14:53:23 +00:00
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Canonical with redundant query
|
|
|
|
|
'url' => 'http://example.org/wiki/Foo_Bar?action=view',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'action' => 'view' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
2016-09-09 14:53:23 +00:00
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// Edit: Canonical view url with action query
|
|
|
|
|
'url' => 'http://example.org/wiki/Foo_Bar?action=edit',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'action' => 'edit' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
|
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// View: Index with action query
|
|
|
|
|
'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=view',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'title' => 'Foo_Bar', 'action' => 'view' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
2016-09-09 14:53:23 +00:00
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-19 19:56:36 +00:00
|
|
|
// Edit: Index with action query
|
|
|
|
|
'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=edit',
|
2016-02-17 09:09:32 +00:00
|
|
|
'query' => [ 'title' => 'Foo_Bar', 'action' => 'edit' ],
|
2015-06-19 19:56:36 +00:00
|
|
|
'title' => 'Foo_Bar',
|
|
|
|
|
'redirect' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-06-19 19:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTryNormaliseRedirect
|
|
|
|
|
* @covers MediaWiki::tryNormaliseRedirect
|
|
|
|
|
*/
|
|
|
|
|
public function testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect = false ) {
|
|
|
|
|
// Set SERVER because interpolateTitle() doesn't use getRequestURL(),
|
|
|
|
|
// whereas tryNormaliseRedirect does().
|
|
|
|
|
$_SERVER['REQUEST_URI'] = $url;
|
|
|
|
|
|
|
|
|
|
$req = new FauxRequest( $query );
|
|
|
|
|
$req->setRequestURL( $url );
|
|
|
|
|
// This adds a virtual 'title' query parameter. Normally called from Setup.php
|
|
|
|
|
$req->interpolateTitle();
|
|
|
|
|
|
|
|
|
|
$titleObj = Title::newFromText( $title );
|
|
|
|
|
|
|
|
|
|
// Set global context since some involved code paths don't yet have context
|
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
$context->setRequest( $req );
|
|
|
|
|
$context->setTitle( $titleObj );
|
|
|
|
|
|
|
|
|
|
$mw = new MediaWiki( $context );
|
|
|
|
|
|
|
|
|
|
$method = new ReflectionMethod( $mw, 'tryNormaliseRedirect' );
|
|
|
|
|
$method->setAccessible( true );
|
|
|
|
|
$ret = $method->invoke( $mw, $titleObj );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedRedirect !== false,
|
|
|
|
|
$ret,
|
|
|
|
|
'Return true only when redirecting'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedRedirect ?: '',
|
|
|
|
|
$context->getOutput()->getRedirect()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|