Merge "[FileBackend] Made Swift clear the auth cache on certain HTTP errors."
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTest.php
index 9d38852..c3eacd5 100644 (file)
@@ -1,25 +1,10 @@
 <?php
 
-class MockApi extends ApiBase {
-       public function execute() { }
-       public function getVersion() { }
-
-       public function __construct() { }
-
-       public function getAllowedParams() {
-               return array(
-                       'filename' => null,
-                       'enablechunks' => false,
-                       'sessionkey' => null,
-               );
-       }
-}
-
 /**
+ * @group API
  * @group Database
- * @group Destructive
  */
-class ApiTest extends ApiTestSetup {
+class ApiTest extends ApiTestCase {
 
        function testRequireOnlyOneParameterDefault() {
                $mock = new MockApi();
@@ -58,6 +43,7 @@ class ApiTest extends ApiTestSetup {
         * @expectedException UsageException
         */
        function testApi() {
+       
                $api = new ApiMain(
                        new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
                );
@@ -77,7 +63,7 @@ class ApiTest extends ApiTestSetup {
         */
        function testApiLoginNoName() {
                $data = $this->doApiRequest( array( 'action' => 'login',
-                       'lgname' => '', 'lgpassword' => $this->user->password,
+                       'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
                ) );
                $this->assertEquals( 'NoName', $data[0]['login']['result'] );
        }
@@ -85,14 +71,15 @@ class ApiTest extends ApiTestSetup {
        function testApiLoginBadPass() {
                global $wgServer;
 
-               $user = $this->user;
+               $user = self::$users['sysop'];
+               $user->user->logOut();
 
                if ( !isset( $wgServer ) ) {
                        $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
                }
                $ret = $this->doApiRequest( array(
                        "action" => "login",
-                       "lgname" => $user->userName,
+                       "lgname" => $user->username,
                        "lgpassword" => "bad",
                        )
                );
@@ -108,9 +95,9 @@ class ApiTest extends ApiTestSetup {
                $ret = $this->doApiRequest( array(
                        "action" => "login",
                        "lgtoken" => $token,
-                       "lgname" => $user->userName,
-                       "lgpassword" => "bad",
-                       )
+                       "lgname" => $user->username,
+                       "lgpassword" => "badnowayinhell",
+                       ), $ret[2]
                );
 
                $result = $ret[0];
@@ -128,11 +115,12 @@ class ApiTest extends ApiTestSetup {
                        $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
                }
 
-               $user = $this->user;
+               $user = self::$users['sysop'];
+               $user->user->logOut();
 
                $ret = $this->doApiRequest( array(
                        "action" => "login",
-                       "lgname" => $user->userName,
+                       "lgname" => $user->username,
                        "lgpassword" => $user->password,
                        )
                );
@@ -148,9 +136,9 @@ class ApiTest extends ApiTestSetup {
                $ret = $this->doApiRequest( array(
                        "action" => "login",
                        "lgtoken" => $token,
-                       "lgname" => $user->userName,
+                       "lgname" => $user->username,
                        "lgpassword" => $user->password,
-                       )
+                       ), $ret[2]
                );
 
                $result = $ret[0];
@@ -161,6 +149,9 @@ class ApiTest extends ApiTestSetup {
                $this->assertEquals( "Success", $a );
        }
 
+       /**
+        * @group Broken
+        */
        function testApiGotCookie() {
                $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies"  );
 
@@ -169,11 +160,13 @@ class ApiTest extends ApiTestSetup {
                if ( !isset( $wgServer ) ) {
                        $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
                }
+               $user = self::$users['sysop'];
+
                $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
                        array( "method" => "POST",
                                "postData" => array(
-                               "lgname" => $this->user->userName,
-                               "lgpassword" => $this->user->password ) ) );
+                               "lgname" => $user->username,
+                               "lgpassword" => $user->password ) ) );
                $req->execute();
 
                libxml_use_internal_errors( true );
@@ -188,8 +181,8 @@ class ApiTest extends ApiTestSetup {
 
                $req->setData( array(
                        "lgtoken" => $token,
-                       "lgname" => $this->user->userName,
-                       "lgpassword" => $this->user->password ) );
+                       "lgname" => $user->username,
+                       "lgpassword" => $user->password ) );
                $req->execute();
 
                $cj = $req->getCookieJar();
@@ -197,29 +190,91 @@ class ApiTest extends ApiTestSetup {
                $this->assertNotEquals( false, $serverName );
                $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
                $this->assertNotEquals( '', $serializedCookie );
-               $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $this->user->userName . '; .*Token=/', $serializedCookie );
+               $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
 
                return $cj;
        }
 
        /**
-        * @depends testApiGotCookie
+        * @todo Finish filling me out...what are we trying to test here?
         */
-       function testApiListPages( CookieJar $cj ) {
-               $this->markTestIncomplete( "Not done with this yet" );
+       function testApiListPages() {
                global $wgServer;
-
-               if ( $wgServer == "http://localhost" ) {
+               if ( !isset( $wgServer ) ) {
                        $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
                }
-               $req = MWHttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&" .
-                                                                        "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
-               $req->setCookieJar( $cj );
-               $req->execute();
-               libxml_use_internal_errors( true );
-               $sxe = simplexml_load_string( $req->getContent() );
-               $this->assertNotInternalType( "bool", $sxe );
-               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-               $a = $sxe->query[0]->pages[0]->page[0]->attributes();
+
+               $ret = $this->doApiRequest( array(
+                       'action' => 'query',
+                       'prop'   => 'revisions',
+                       'titles' => 'Main Page',
+                       'rvprop' => 'timestamp|user|comment|content',
+               ) );
+
+               $result = $ret[0]['query']['pages'];
+               $this->markTestIncomplete( "Somebody needs to finish loving me" );
+       }
+       
+       function testRunLogin() {
+               $sysopUser = self::$users['sysop'];
+               $data = $this->doApiRequest( array(
+                       'action' => 'login',
+                       'lgname' => $sysopUser->username,
+                       'lgpassword' => $sysopUser->password ) );
+
+               $this->assertArrayHasKey( "login", $data[0] );
+               $this->assertArrayHasKey( "result", $data[0]['login'] );
+               $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
+               $token = $data[0]['login']['token'];
+
+               $data = $this->doApiRequest( array(
+                       'action' => 'login',
+                       "lgtoken" => $token,
+                       "lgname" => $sysopUser->username,
+                       "lgpassword" => $sysopUser->password ), $data[2] );
+
+               $this->assertArrayHasKey( "login", $data[0] );
+               $this->assertArrayHasKey( "result", $data[0]['login'] );
+               $this->assertEquals( "Success", $data[0]['login']['result'] );
+               $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
+               
+               return $data;
+       }
+       
+       function testGettingToken() {
+               foreach ( self::$users as $user ) {
+                       $this->runTokenTest( $user );
+               }
+       }
+
+       function runTokenTest( $user ) {
+               
+               $data = $this->getTokenList( $user );
+
+               $this->assertArrayHasKey( 'query', $data[0] );
+               $this->assertArrayHasKey( 'pages', $data[0]['query'] );
+               $keys = array_keys( $data[0]['query']['pages'] );
+               $key = array_pop( $keys );
+
+               $rights = $user->user->getRights();
+
+               $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
+               $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
+               $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
+
+               if ( isset( $rights['delete'] ) ) {
+                       $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
+               }
+
+               if ( isset( $rights['block'] ) ) {
+                       $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
+                       $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
+               }
+
+               if ( isset( $rights['protect'] ) ) {
+                       $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
+               }
+
+               return $data;
        }
 }