MediaWiki
REL1_22
|
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 public 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( 00051 array( 00052 'action' => 'createaccount', 00053 'name' => 'Apitestnew', 00054 'password' => $password, 00055 'token' => $token, 00056 'email' => '[email protected]', 00057 'realname' => 'Test Name' 00058 ), 00059 $ret[2] 00060 ); 00061 00062 $result = $ret[0]; 00063 $this->assertNotInternalType( 'bool', $result ); 00064 $this->assertEquals( 'success', $result['createaccount']['result'] ); 00065 00066 // Try logging in with the new user. 00067 $ret = $this->doApiRequest( array( 00068 'action' => 'login', 00069 'lgname' => 'Apitestnew', 00070 'lgpassword' => $password, 00071 ) ); 00072 00073 $result = $ret[0]; 00074 $this->assertNotInternalType( 'bool', $result ); 00075 $this->assertNotInternalType( 'null', $result['login'] ); 00076 00077 $a = $result['login']['result']; 00078 $this->assertEquals( 'NeedToken', $a ); 00079 $token = $result['login']['token']; 00080 00081 $ret = $this->doApiRequest( 00082 array( 00083 'action' => 'login', 00084 'lgtoken' => $token, 00085 'lgname' => 'Apitestnew', 00086 'lgpassword' => $password, 00087 ), 00088 $ret[2] 00089 ); 00090 00091 $result = $ret[0]; 00092 00093 $this->assertNotInternalType( 'bool', $result ); 00094 $a = $result['login']['result']; 00095 00096 $this->assertEquals( 'Success', $a ); 00097 00098 // log out to destroy the session 00099 $ret = $this->doApiRequest( 00100 array( 00101 'action' => 'logout', 00102 ), 00103 $ret[2] 00104 ); 00105 $this->assertEquals( array(), $ret[0] ); 00106 } 00107 00112 public function testNoName() { 00113 $this->doApiRequest( array( 00114 'action' => 'createaccount', 00115 'token' => LoginForm::getCreateaccountToken(), 00116 'password' => 'password', 00117 ) ); 00118 } 00119 00124 public function testNoPassword() { 00125 $this->doApiRequest( array( 00126 'action' => 'createaccount', 00127 'name' => 'testName', 00128 'token' => LoginForm::getCreateaccountToken(), 00129 ) ); 00130 } 00131 00136 public function testExistingUser() { 00137 $this->doApiRequest( array( 00138 'action' => 'createaccount', 00139 'name' => 'Apitestsysop', 00140 'token' => LoginForm::getCreateaccountToken(), 00141 'password' => 'password', 00142 'email' => '[email protected]', 00143 ) ); 00144 } 00145 00150 public function testInvalidEmail() { 00151 $this->doApiRequest( array( 00152 'action' => 'createaccount', 00153 'name' => 'Test User', 00154 'token' => LoginForm::getCreateaccountToken(), 00155 'password' => 'password', 00156 'email' => 'invalid', 00157 ) ); 00158 } 00159 }