Revert r74122, making r74117 live again
authorSam Reed <reedy@users.mediawiki.org>
Sun, 3 Oct 2010 15:24:47 +0000 (15:24 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Sun, 3 Oct 2010 15:24:47 +0000 (15:24 +0000)
Some refactoring, to encapsulate users. Added a UserWrapper, contains the setup username/password and user object. Allows for ease of adding of multiple users (and groups)

Per Brion, if logging in user == wguser, we skip login tests, so use a secondary user

Fixup testApiLoginBadPass, we should get a WrongPass when the passwords wrong, but we've given a token

maintenance/tests/phpunit/includes/api/ApiSetup.php
maintenance/tests/phpunit/includes/api/ApiTest.php
maintenance/tests/phpunit/includes/api/ApiWatchTest.php

index 5524469..8bdc107 100644 (file)
@@ -1,9 +1,8 @@
 <?php
 
 abstract class ApiTestSetup extends PHPUnit_Framework_TestCase {
-       protected static $userName;
-       protected static $passWord;
        protected static $user;
+       protected static $sysopUser;
        protected static $apiUrl;
 
        function setUp() {
@@ -33,22 +32,12 @@ abstract class ApiTestSetup extends PHPUnit_Framework_TestCase {
        }
 
        static function setupUser() {
-               if ( self::$user == NULL ) {
-                       self::$userName = "Useruser";
-                       self::$passWord = 'Passpass';
-
-                       self::$user = User::newFromName( self::$userName );
-                       if ( !self::$user->getID() ) {
-                               self::$user = User::createNew( self::$userName, array(
-                                       "email" => "test@example.com",
-                                       "real_name" => "Test User" ) );
-                       }
-                       self::$user->setPassword( self::$passWord );
-                       self::$user->addGroup( 'sysop' );
-                       self::$user->saveSettings();
+               if ( self::$user == NULL || self::$sysopUser == NULL ) {
+                       self::$user = new UserWrapper( 'Useruser', 'Passpass' );
+                   self::$sysopUser = new UserWrapper( 'Useruser1', 'Passpass1', 'sysop' );
                }
                
-               $GLOBALS['wgUser'] = self::$user;
+               $GLOBALS['wgUser'] = self::$user->user;
        }
 
        function tearDown() {
@@ -56,3 +45,25 @@ abstract class ApiTestSetup extends PHPUnit_Framework_TestCase {
                $wgMemc = null;
        }
 }
+
+class UserWrapper {
+       var $userName, $password, $user;
+
+       public function __construct( $userName, $password, $group = '' ) {
+               $this->userName = $userName;
+           $this->password = $password;
+
+           $this->user = User::newFromName( $this->userName );
+               if ( !$this->user->getID() ) {
+                       $this->user = User::createNew( $this->userName, array(
+                               "email" => "test@example.com",
+                               "real_name" => "Test User" ) );
+               }
+               $this->user->setPassword( $this->password );
+
+               if ( $group !== '' ) {
+                       $this->user->addGroup( $group );
+               }
+               $this->user->saveSettings();
+       }
+}
index 178e6ed..7de7906 100644 (file)
@@ -79,7 +79,7 @@ class ApiTest extends ApiTestSetup {
         */
        function testApiLoginNoName() {
                $data = $this->doApiRequest( array( 'action' => 'login',
-                       'lgname' => '', 'lgpassword' => self::$passWord
+                       'lgname' => '', 'lgpassword' => self::$user->password,
                ) );
                $this->assertEquals( 'NoName', $data[0]['login']['result'] );
        }
@@ -90,32 +90,35 @@ class ApiTest extends ApiTestSetup {
                if ( !isset( $wgServer ) ) {
                        $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
                }
-               $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
-                                                  array( "postData" => array(
-                                                                        "lgname" => self::$userName,
-                                                                        "lgpassword" => "bad" ) ) );
-               libxml_use_internal_errors( true );
-               $sxe = simplexml_load_string( $resp );
-               $this->assertNotType( "bool", $sxe );
-               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-               $a = $sxe->login[0]->attributes()->result[0];
-               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
+               $ret = $this->doApiRequest( array(
+                       "action" => "login",
+                       "lgname" => self::$sysopUser->userName,
+                       "lgpassword" => "bad",
+                       )
+               );
+               
+               $result = $ret[0];
 
-               $token = (string)$sxe->login[0]->attributes()->token;
+               $this->assertNotType( "bool", $result );
+               $a = $result["login"]["result"];
+               $this->assertEquals( "NeedToken", $a );
 
-               $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
-                                                  array( "postData" => array(
-                                                                       "lgtoken" => $token,
-                                                                       "lgname" => self::$userName,
-                                                                       "lgpassword" => "bad" ) ) );
+               $token = $result["login"]["token"];
 
+               $ret = $this->doApiRequest( array(
+                       "action" => "login",
+                       "lgtoken" => $token,
+                       "lgname" => self::$sysopUser->userName,
+                       "lgpassword" => "bad",
+                       )
+               );
 
-               $sxe = simplexml_load_string( $resp );
-               $this->assertNotType( "bool", $sxe );
-               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-               $a = $sxe->login[0]->attributes()->result[0];
+               $result = $ret[0];
 
-               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
+               $this->assertNotType( "bool", $result );
+               $a = $result["login"]["result"];
+
+               $this->assertEquals( "WrongPass", $a );
        }
 
        function testApiLoginGoodPass() {
@@ -127,12 +130,11 @@ class ApiTest extends ApiTestSetup {
 
                $ret = $this->doApiRequest( array(
                        "action" => "login",
-                       "lgname" => self::$userName,
-                       "lgpassword" => self::$passWord,
+                       "lgname" => self::$user->userName,
+                       "lgpassword" => self::$user->password,
                        )
                );
 
-               libxml_use_internal_errors( true );
                $result = $ret[0];
                $this->assertNotType( "bool", $result );
                $this->assertNotType( "null", $result["login"] );
@@ -144,8 +146,8 @@ class ApiTest extends ApiTestSetup {
                $ret = $this->doApiRequest( array(
                        "action" => "login",
                        "lgtoken" => $token,
-                       "lgname" => self::$userName,
-                       "lgpassword" => self::$passWord,
+                       "lgname" => self::$user->userName,
+                       "lgpassword" => self::$user->password,
                        )
                );
 
@@ -166,8 +168,8 @@ class ApiTest extends ApiTestSetup {
                $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
                        array( "method" => "POST",
                                "postData" => array(
-                               "lgname" => self::$userName,
-                               "lgpassword" => self::$passWord ) ) );
+                               "lgname" => self::$user->userName,
+                               "lgpassword" => self::$user->password ) ) );
                $req->execute();
 
                libxml_use_internal_errors( true );
@@ -182,8 +184,8 @@ class ApiTest extends ApiTestSetup {
 
                $req->setData( array(
                        "lgtoken" => $token,
-                       "lgname" => self::$userName,
-                       "lgpassword" => self::$passWord ) );
+                       "lgname" => self::$user->userName,
+                       "lgpassword" => self::$user->password ) );
                $req->execute();
 
                $cj = $req->getCookieJar();
@@ -191,7 +193,7 @@ class ApiTest extends ApiTestSetup {
                $this->assertNotEquals( false, $serverName );
                $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
                $this->assertNotEquals( '', $serializedCookie );
-               $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/', $serializedCookie );
+               $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$user->userName . '; .*Token=/', $serializedCookie );
 
                return $cj;
        }
index 032e244..bf1d010 100644 (file)
@@ -11,8 +11,8 @@ class ApiWatchTest extends ApiTestSetup {
        function testLogin() {
                $data = $this->doApiRequest( array(
                        'action' => 'login',
-                       'lgname' => self::$userName,
-                       'lgpassword' => self::$passWord ) );
+                       'lgname' => self::$user->userName,
+                       'lgpassword' => self::$user->password ) );
 
                $this->assertArrayHasKey( "login", $data[0] );
                $this->assertArrayHasKey( "result", $data[0]['login'] );
@@ -22,8 +22,8 @@ class ApiWatchTest extends ApiTestSetup {
                $data = $this->doApiRequest( array(
                        'action' => 'login',
                        "lgtoken" => $token,
-                       "lgname" => self::$userName,
-                       "lgpassword" => self::$passWord ), $data );
+                       "lgname" => self::$user->userName,
+                       "lgpassword" => self::$user->password ), $data );
 
                $this->assertArrayHasKey( "login", $data[0] );
                $this->assertArrayHasKey( "result", $data[0]['login'] );
@@ -163,7 +163,7 @@ class ApiWatchTest extends ApiTestSetup {
                        $data = $this->doApiRequest( array(
                                'action' => 'rollback',
                                'title' => 'Main Page',
-                               'user' => self::$userName,
+                               'user' => self::$user->userName,
                                'token' => $pageinfo['rollbacktoken'],
                                'watchlist' => 'watch' ), $data );
                } catch( UsageException $ue ) {