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