<?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() {
}
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() {
$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();
+ }
+}
*/
function testApiLoginNoName() {
$data = $this->doApiRequest( array( 'action' => 'login',
- 'lgname' => '', 'lgpassword' => self::$passWord
+ 'lgname' => '', 'lgpassword' => self::$user->password,
) );
$this->assertEquals( 'NoName', $data[0]['login']['result'] );
}
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() {
$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"] );
$ret = $this->doApiRequest( array(
"action" => "login",
"lgtoken" => $token,
- "lgname" => self::$userName,
- "lgpassword" => self::$passWord,
+ "lgname" => self::$user->userName,
+ "lgpassword" => self::$user->password,
)
);
$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 );
$req->setData( array(
"lgtoken" => $token,
- "lgname" => self::$userName,
- "lgpassword" => self::$passWord ) );
+ "lgname" => self::$user->userName,
+ "lgpassword" => self::$user->password ) );
$req->execute();
$cj = $req->getCookieJar();
$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;
}
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'] );
$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'] );
$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 ) {