123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- class MediaWikiTest extends MediaWikiTestCase {
- private $oldServer, $oldGet, $oldPost;
- protected function setUp() {
- parent::setUp();
- $this->setMwGlobals( [
- 'wgServer' => 'http://example.org',
- 'wgScriptPath' => '/w',
- 'wgScript' => '/w/index.php',
- 'wgArticlePath' => '/wiki/$1',
- 'wgActionPaths' => [],
- ] );
- // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
- $this->oldServer = $_SERVER;
- $this->oldGet = $_GET;
- $this->oldPost = $_POST;
- }
- protected function tearDown() {
- parent::tearDown();
- $_SERVER = $this->oldServer;
- $_GET = $this->oldGet;
- $_POST = $this->oldPost;
- }
- public static function provideTryNormaliseRedirect() {
- return [
- [
- // View: Canonical
- 'url' => 'http://example.org/wiki/Foo_Bar',
- 'query' => [],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // View: Escaped title
- 'url' => 'http://example.org/wiki/Foo%20Bar',
- 'query' => [],
- 'title' => 'Foo_Bar',
- 'redirect' => 'http://example.org/wiki/Foo_Bar',
- ],
- [
- // View: Script path
- 'url' => 'http://example.org/w/index.php?title=Foo_Bar',
- 'query' => [ 'title' => 'Foo_Bar' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // View: Script path with implicit title from page id
- 'url' => 'http://example.org/w/index.php?curid=123',
- 'query' => [ 'curid' => '123' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // View: Script path with implicit title from revision id
- 'url' => 'http://example.org/w/index.php?oldid=123',
- 'query' => [ 'oldid' => '123' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // View: Script path without title
- 'url' => 'http://example.org/w/index.php',
- 'query' => [],
- 'title' => 'Main_Page',
- 'redirect' => 'http://example.org/wiki/Main_Page',
- ],
- [
- // View: Script path with empty title
- 'url' => 'http://example.org/w/index.php?title=',
- 'query' => [ 'title' => '' ],
- 'title' => 'Main_Page',
- 'redirect' => 'http://example.org/wiki/Main_Page',
- ],
- [
- // View: Index with escaped title
- 'url' => 'http://example.org/w/index.php?title=Foo%20Bar',
- 'query' => [ 'title' => 'Foo Bar' ],
- 'title' => 'Foo_Bar',
- 'redirect' => 'http://example.org/wiki/Foo_Bar',
- ],
- [
- // View: Script path with escaped title
- 'url' => 'http://example.org/w/?title=Foo_Bar',
- 'query' => [ 'title' => 'Foo_Bar' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // View: Root path with escaped title
- 'url' => 'http://example.org/?title=Foo_Bar',
- 'query' => [ 'title' => 'Foo_Bar' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // View: Canonical with redundant query
- 'url' => 'http://example.org/wiki/Foo_Bar?action=view',
- 'query' => [ 'action' => 'view' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // Edit: Canonical view url with action query
- 'url' => 'http://example.org/wiki/Foo_Bar?action=edit',
- 'query' => [ 'action' => 'edit' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // View: Index with action query
- 'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=view',
- 'query' => [ 'title' => 'Foo_Bar', 'action' => 'view' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // Edit: Index with action query
- 'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=edit',
- 'query' => [ 'title' => 'Foo_Bar', 'action' => 'edit' ],
- 'title' => 'Foo_Bar',
- 'redirect' => false,
- ],
- [
- // Path with double slash prefix (T100782)
- 'url' => 'http://example.org//wiki/Double_slash',
- 'query' => [],
- 'title' => 'Double_slash',
- 'redirect' => false,
- ],
- ];
- }
- /**
- * @dataProvider provideTryNormaliseRedirect
- * @covers MediaWiki::tryNormaliseRedirect
- */
- public function testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect = false ) {
- // Set SERVER because interpolateTitle() doesn't use getRequestURL(),
- // whereas tryNormaliseRedirect does(). Also, using WebRequest allows
- // us to test some quirks in that class.
- $_SERVER['REQUEST_URI'] = $url;
- $_POST = [];
- $_GET = $query;
- $req = new WebRequest;
- // 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()
- );
- }
- /**
- * Test a post-send job can not set cookies (T191537).
- */
- public function testPostSendJobDoesNotSetCookie() {
- // Prevent updates from running
- $this->setMwGlobals( 'wgCommandLineMode', false );
- $response = new WebResponse;
- // A job that attempts to set a cookie
- $jobHasRun = false;
- DeferredUpdates::addCallableUpdate( function () use ( $response, &$jobHasRun ) {
- $jobHasRun = true;
- $response->setCookie( 'JobCookie', 'yes' );
- $response->header( 'Foo: baz' );
- } );
- $hookWasRun = false;
- $this->setTemporaryHook( 'WebResponseSetCookie', function () use ( &$hookWasRun ) {
- $hookWasRun = true;
- return true;
- } );
- $logger = new TestLogger();
- $logger->setCollect( true );
- $this->setLogger( 'cookie', $logger );
- $this->setLogger( 'header', $logger );
- $mw = new MediaWiki();
- $mw->doPostOutputShutdown();
- // restInPeace() might have been registered to a callback of
- // register_postsend_function() and thus can not be triggered from
- // PHPUnit.
- if ( $jobHasRun === false ) {
- $mw->restInPeace();
- }
- $this->assertTrue( $jobHasRun, 'post-send job has run' );
- $this->assertFalse( $hookWasRun,
- 'post-send job must not trigger WebResponseSetCookie hook' );
- $this->assertEquals(
- [
- [ 'info', 'ignored post-send cookie {cookie}' ],
- [ 'info', 'ignored post-send header {header}' ],
- ],
- $logger->getBuffer()
- );
- }
- }
|