Merge "tiny optimization Title::isValidRedirectTarget()"
[lhc/web/wiklou.git] / tests / phpunit / includes / MediaWikiTest.php
index 3ca8d88..e196243 100644 (file)
 <?php
-/**
- * Test class for MediaWiki.
- * Generated by PHPUnit on 2011-02-06 at 11:41:23.
- */
-class MediaWikiTest extends MediaWikiTestCase {
-       /**
-        * @var MediaWiki
-        */
-       protected $object;
 
+class MediaWikiTest extends MediaWikiTestCase {
        protected function setUp() {
-               $context = new RequestContext;
-               $this->object = new MediaWiki( $context );
-       }
-
-       protected function tearDown() {
-               $this->object = NULL;
-       }
-
-       /**
-        * Test case insentiveness for get / set
-        */
-       public function testSetGetValKeyInsentiveness() {
-
-               // set with lower case key 
-               $value = 'SomeValue';
-               $this->object->setVal( 'foobar', $value );
-
-               $this->assertEquals(
-                       $this->object->getVal( 'foobar' ), 'SomeValue',
-                       'lower case key set, getting lower case key'
-               );
-               $this->assertEquals(
-                       $this->object->getVal( 'FOOBAR' ), 'SomeValue',
-                       'lower case key set, getting upper case key'
-               );
-
-               // set with Mixed case key 
-               $value = 'SomeValue2';
-               $this->object->setVal( 'FooBar', $value );
-
-               $this->assertEquals(
-                       $this->object->getVal( 'foobar' ), 'SomeValue2',
-                       'mixed case key set, getting lower case key'
-               );
-               $this->assertEquals(
-                       $this->object->getVal( 'FOOBAR' ), 'SomeValue2',
-                       'mixed case key set, getting upper case key'
-               );
-       }
-
-       public function testGetValWithDefault() {
-               $this->assertEmpty(
-                       $this->object->getVal( 'NonExistent' ),
-                       'Non existent key return empty string'
-               );
-               $this->assertEquals(
-                       $this->object->getVal( 'NonExistent2', 'Default Value' ), 'Default Value',
-                       'Non existent key with default given, should give default'
+               parent::setUp();
+
+               $this->setMwGlobals( array(
+                       'wgServer' => 'http://example.org',
+                       'wgScriptPath' => '/w',
+                       'wgScript' => '/w/index.php',
+                       'wgArticlePath' => '/wiki/$1',
+                       'wgActionPaths' => array(),
+               ) );
+       }
+
+       public static function provideTryNormaliseRedirect() {
+               return array(
+                       array(
+                               // View: Canonical
+                               'url' => 'http://example.org/wiki/Foo_Bar',
+                               'query' => array(),
+                               'title' => 'Foo_Bar',
+                               'redirect' => false,
+                       ),
+                       array(
+                               // View: Escaped title
+                               'url' => 'http://example.org/wiki/Foo%20Bar',
+                               'query' => array(),
+                               'title' => 'Foo_Bar',
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
+                       ),
+                       array(
+                               // View: Script path
+                               'url' => 'http://example.org/w/index.php?title=Foo_Bar',
+                               'query' => array( 'title' => 'Foo_Bar' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
+                       ),
+                       array(
+                               // View: Script path with implicit title from page id
+                               'url' => 'http://example.org/w/index.php?curid=123',
+                               'query' => array( 'curid' => '123' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => false,
+                       ),
+                       array(
+                               // View: Script path with implicit title from revision id
+                               'url' => 'http://example.org/w/index.php?oldid=123',
+                               'query' => array( 'oldid' => '123' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => false,
+                       ),
+                       array(
+                               // View: Script path without title
+                               'url' => 'http://example.org/w/index.php',
+                               'query' => array(),
+                               'title' => 'Main_Page',
+                               'redirect' => 'http://example.org/wiki/Main_Page',
+                       ),
+                       array(
+                               // View: Script path with empty title
+                               'url' => 'http://example.org/w/index.php?title=',
+                               'query' => array( 'title' => '' ),
+                               'title' => 'Main_Page',
+                               'redirect' => 'http://example.org/wiki/Main_Page',
+                       ),
+                       array(
+                               // View: Index with escaped title
+                               'url' => 'http://example.org/w/index.php?title=Foo%20Bar',
+                               'query' => array( 'title' => 'Foo Bar' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
+                       ),
+                       array(
+                               // View: Script path with escaped title
+                               'url' => 'http://example.org/w/?title=Foo_Bar',
+                               'query' => array( 'title' => 'Foo_Bar' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
+                       ),
+                       array(
+                               // View: Root path with escaped title
+                               'url' => 'http://example.org/?title=Foo_Bar',
+                               'query' => array( 'title' => 'Foo_Bar' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
+                       ),
+                       array(
+                               // View: Canonical with redundant query
+                               'url' => 'http://example.org/wiki/Foo_Bar?action=view',
+                               'query' => array( 'action' => 'view' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
+                       ),
+                       array(
+                               // Edit: Canonical view url with action query
+                               'url' => 'http://example.org/wiki/Foo_Bar?action=edit',
+                               'query' => array( 'action' => 'edit' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => false,
+                       ),
+                       array(
+                               // View: Index with action query
+                               'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=view',
+                               'query' => array( 'title' => 'Foo_Bar', 'action' => 'view' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
+                       ),
+                       array(
+                               // Edit: Index with action query
+                               'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=edit',
+                               'query' => array( 'title' => 'Foo_Bar', 'action' => 'edit' ),
+                               'title' => 'Foo_Bar',
+                               'redirect' => false,
+                       ),
                );
        }
 
        /**
-        * @todo Implement testPerformRequestForTitle().
+        * @dataProvider provideTryNormaliseRedirect
+        * @covers MediaWiki::tryNormaliseRedirect
         */
-       public function testPerformRequestForTitle() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
+       public function testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect = false ) {
+               // Set SERVER because interpolateTitle() doesn't use getRequestURL(),
+               // whereas tryNormaliseRedirect does().
+               $_SERVER['REQUEST_URI'] = $url;
 
-       /**
-        * @todo Implement testCheckMaxLag().
-        */
-       public function testCheckMaxLag() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
+               $req = new FauxRequest( $query );
+               $req->setRequestURL( $url );
+               // This adds a virtual 'title' query parameter. Normally called from Setup.php
+               $req->interpolateTitle();
 
-       /**
-        * @todo Implement testCheckInitialQueries().
-        */
-       public function testCheckInitialQueries() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
+               $titleObj = Title::newFromText( $title );
 
-       /**
-        * @todo Implement testPreliminaryChecks().
-        */
-       public function testPreliminaryChecks() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
+               // Set global context since some involved code paths don't yet have context
+               $context = RequestContext::getMain();
+               $context->setRequest( $req );
+               $context->setTitle( $titleObj );
 
-       /**
-        * @todo Implement testHandleSpecialCases().
-        */
-       public function testHandleSpecialCases() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
+               $mw = new MediaWiki( $context );
 
-       /**
-        * @todo Implement testArticleFromTitle().
-        */
-       public function testArticleFromTitle() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
+               $method = new ReflectionMethod( $mw, 'tryNormaliseRedirect' );
+               $method->setAccessible( true );
+               $ret = $method->invoke( $mw, $titleObj );
 
-       /**
-        * @todo Implement testGetAction().
-        */
-       public function testGetAction() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
-
-       /**
-        * @todo Implement testInitializeArticle().
-        */
-       public function testInitializeArticle() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
-
-       /**
-        * @todo Implement testFinalCleanup().
-        */
-       public function testFinalCleanup() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
-
-       /**
-        * @todo Implement testDoJobs().
-        */
-       public function testDoJobs() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
-               );
-       }
-
-       /**
-        * @todo Implement testRestInPeace().
-        */
-       public function testRestInPeace() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
+               $this->assertEquals(
+                       $expectedRedirect !== false,
+                       $ret,
+                       'Return true only when redirecting'
                );
-       }
 
-       /**
-        * @todo Implement testPerformAction().
-        */
-       public function testPerformAction() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                 'This test has not been implemented yet.'
+               $this->assertEquals(
+                       $expectedRedirect ?: '',
+                       $context->getOutput()->getRedirect()
                );
        }
 }