MediaWiki  REL1_21
ApiAccountCreationTest.php
Go to the documentation of this file.
00001 <?php
00002 
00008 class ApiCreateAccountTest extends ApiTestCase {
00009         function setUp() {
00010                 parent::setUp();
00011                 LoginForm::setCreateaccountToken();
00012                 $this->setMwGlobals( array( 'wgEnableEmail' => true ) );
00013         }
00014 
00023         function testValid() {
00024                 global $wgServer;
00025 
00026                 if ( !isset( $wgServer ) ) {
00027                         $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00028                 }
00029 
00030                 $password = User::randomPassword();
00031 
00032                 $ret = $this->doApiRequest( array(
00033                         'action' => 'createaccount',
00034                         'name' => 'Apitestnew',
00035                         'password' => $password,
00036                         'email' => '[email protected]',
00037                         'realname' => 'Test Name'
00038                 ) );
00039 
00040                 $result = $ret[0];
00041                 $this->assertNotInternalType( 'bool', $result );
00042                 $this->assertNotInternalType( 'null', $result['createaccount'] );
00043 
00044                 // Should first ask for token.
00045                 $a = $result['createaccount'];
00046                 $this->assertEquals( 'needtoken', $a['result'] );
00047                 $token = $a['token'];
00048 
00049                 // Finally create the account
00050                 $ret = $this->doApiRequest( array(
00051                         'action' => 'createaccount',
00052                         'name' => 'Apitestnew',
00053                         'password' => $password,
00054                         'token' => $token,
00055                         'email' => '[email protected]',
00056                         'realname' => 'Test Name' ), $ret[2]
00057                 );
00058 
00059                 $result = $ret[0];
00060                 $this->assertNotInternalType( 'bool', $result );
00061                 $this->assertEquals( 'success', $result['createaccount']['result'] );
00062 
00063                 // Try logging in with the new user.
00064                 $ret = $this->doApiRequest( array(
00065                         'action' => 'login',
00066                         'lgname' => 'Apitestnew',
00067                         'lgpassword' => $password,
00068                         )
00069                 );
00070 
00071                 $result = $ret[0];
00072                 $this->assertNotInternalType( 'bool', $result );
00073                 $this->assertNotInternalType( 'null', $result['login'] );
00074 
00075                 $a = $result['login']['result'];
00076                 $this->assertEquals( 'NeedToken', $a );
00077                 $token = $result['login']['token'];
00078 
00079                 $ret = $this->doApiRequest( array(
00080                         'action' => 'login',
00081                         'lgtoken' => $token,
00082                         'lgname' => 'Apitestnew',
00083                         'lgpassword' => $password,
00084                         ), $ret[2]
00085                 );
00086 
00087                 $result = $ret[0];
00088 
00089                 $this->assertNotInternalType( 'bool', $result );
00090                 $a = $result['login']['result'];
00091 
00092                 $this->assertEquals( 'Success', $a );
00093 
00094                 // log out to destroy the session
00095                 $ret = $this->doApiRequest( array(
00096                         'action' => 'logout',
00097                         ), $ret[2]
00098                 );
00099                 $this->assertEquals( array(), $ret[0] );
00100         }
00101 
00106         function testNoName() {
00107                 $ret = $this->doApiRequest( array(
00108                         'action' => 'createaccount',
00109                         'token' => LoginForm::getCreateaccountToken(),
00110                         'password' => 'password',
00111                 ) );
00112         }
00113 
00118         function testNoPassword() {
00119                 $ret = $this->doApiRequest( array(
00120                         'action' => 'createaccount',
00121                         'name' => 'testName',
00122                         'token' => LoginForm::getCreateaccountToken(),
00123                 ) );
00124         }
00125 
00130         function testExistingUser() {
00131                 $this->doApiRequest( array(
00132                         'action' => 'createaccount',
00133                         'name' => 'Apitestsysop',
00134                         'token' => LoginForm::getCreateaccountToken(),
00135                         'password' => 'password',
00136                         'email' => '[email protected]',
00137                 ) );
00138         }
00139 
00144         function testInvalidEmail() {
00145                 $this->doApiRequest( array(
00146                         'action' => 'createaccount',
00147                         'name' => 'Test User',
00148                         'token' => LoginForm::getCreateaccountToken(),
00149                         'password' => 'password',
00150                         'email' => 'invalid',
00151                 ) );
00152         }
00153 }