8 class ApiCreateAccountTest
extends ApiTestCase
{
11 LoginForm
::setCreateaccountToken();
15 * Test the account creation API with a valid request. Also
16 * make sure the new account can log in and is valid.
18 * This test does multiple API requests so it might end up being
19 * a bit slow. Raise the default timeout.
22 function testValid() {
25 if ( !isset( $wgServer ) ) {
26 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
29 $password = User
::randomPassword();
31 $ret = $this->doApiRequest( array(
32 'action' => 'createaccount',
33 'name' => 'Apitestnew',
34 'password' => $password,
35 'email' => 'test@domain.test',
36 'realname' => 'Test Name'
40 $this->assertNotInternalType( 'bool', $result );
41 $this->assertNotInternalType( 'null', $result['createaccount'] );
43 // Should first ask for token.
44 $a = $result['createaccount'];
45 $this->assertEquals( 'needtoken', $a['result'] );
48 // Finally create the account
49 $ret = $this->doApiRequest( array(
50 'action' => 'createaccount',
51 'name' => 'Apitestnew',
52 'password' => $password,
54 'email' => 'test@domain.test',
55 'realname' => 'Test Name' ), $ret[2]
59 $this->assertNotInternalType( 'bool', $result );
60 $this->assertEquals( 'success', $result['createaccount']['result'] );
62 // Try logging in with the new user.
63 $ret = $this->doApiRequest( array(
65 'lgname' => 'Apitestnew',
66 'lgpassword' => $password,
71 $this->assertNotInternalType( 'bool', $result );
72 $this->assertNotInternalType( 'null', $result['login'] );
74 $a = $result['login']['result'];
75 $this->assertEquals( 'NeedToken', $a );
76 $token = $result['login']['token'];
78 $ret = $this->doApiRequest( array(
81 'lgname' => 'Apitestnew',
82 'lgpassword' => $password,
88 $this->assertNotInternalType( 'bool', $result );
89 $a = $result['login']['result'];
91 $this->assertEquals( 'Success', $a );
93 // log out to destroy the session
94 $ret = $this->doApiRequest( array(
98 $this->assertEquals( array(), $ret[0] );
102 * Make sure requests with no names are invalid.
103 * @expectedException UsageException
105 function testNoName() {
106 $ret = $this->doApiRequest( array(
107 'action' => 'createaccount',
108 'token' => LoginForm
::getCreateaccountToken(),
109 'password' => 'password',
114 * Make sure requests with no password are invalid.
115 * @expectedException UsageException
117 function testNoPassword() {
118 $ret = $this->doApiRequest( array(
119 'action' => 'createaccount',
120 'name' => 'testName',
121 'token' => LoginForm
::getCreateaccountToken(),
126 * Make sure requests with existing users are invalid.
127 * @expectedException UsageException
129 function testExistingUser() {
130 $this->doApiRequest( array(
131 'action' => 'createaccount',
132 'name' => 'Apitestsysop',
133 'token' => LoginForm
::getCreateaccountToken(),
134 'password' => 'password',
135 'email' => 'test@domain.test',
140 * Make sure requests with invalid emails are invalid.
141 * @expectedException UsageException
143 function testInvalidEmail() {
144 global $wgEnableEmail;
145 if( !$wgEnableEmail ) {
146 $this->markTestSkipped( 'email is not enabled, so createaccount does not check it' );
148 $this->doApiRequest( array(
149 'action' => 'createaccount',
150 'name' => 'Test User',
151 'token' => LoginForm
::getCreateaccountToken(),
152 'password' => 'password',
153 'email' => 'invalid',