8 class ApiCreateAccountTest
extends ApiTestCase
{
11 LoginForm
::setCreateaccountToken();
12 $this->setMwGlobals( array( 'wgEnableEmail' => true ) );
16 * Test the account creation API with a valid request. Also
17 * make sure the new account can log in and is valid.
19 * This test does multiple API requests so it might end up being
20 * a bit slow. Raise the default timeout.
23 function testValid() {
26 if ( !isset( $wgServer ) ) {
27 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
30 $password = User
::randomPassword();
32 $ret = $this->doApiRequest( array(
33 'action' => 'createaccount',
34 'name' => 'Apitestnew',
35 'password' => $password,
36 'email' => 'test@domain.test',
37 'realname' => 'Test Name'
41 $this->assertNotInternalType( 'bool', $result );
42 $this->assertNotInternalType( 'null', $result['createaccount'] );
44 // Should first ask for token.
45 $a = $result['createaccount'];
46 $this->assertEquals( 'needtoken', $a['result'] );
49 // Finally create the account
50 $ret = $this->doApiRequest(
52 'action' => 'createaccount',
53 'name' => 'Apitestnew',
54 'password' => $password,
56 'email' => 'test@domain.test',
57 'realname' => 'Test Name'
63 $this->assertNotInternalType( 'bool', $result );
64 $this->assertEquals( 'success', $result['createaccount']['result'] );
66 // Try logging in with the new user.
67 $ret = $this->doApiRequest( array(
69 'lgname' => 'Apitestnew',
70 'lgpassword' => $password,
74 $this->assertNotInternalType( 'bool', $result );
75 $this->assertNotInternalType( 'null', $result['login'] );
77 $a = $result['login']['result'];
78 $this->assertEquals( 'NeedToken', $a );
79 $token = $result['login']['token'];
81 $ret = $this->doApiRequest(
85 'lgname' => 'Apitestnew',
86 'lgpassword' => $password,
93 $this->assertNotInternalType( 'bool', $result );
94 $a = $result['login']['result'];
96 $this->assertEquals( 'Success', $a );
98 // log out to destroy the session
99 $ret = $this->doApiRequest(
101 'action' => 'logout',
105 $this->assertEquals( array(), $ret[0] );
109 * Make sure requests with no names are invalid.
110 * @expectedException UsageException
112 function testNoName() {
113 $this->doApiRequest( array(
114 'action' => 'createaccount',
115 'token' => LoginForm
::getCreateaccountToken(),
116 'password' => 'password',
121 * Make sure requests with no password are invalid.
122 * @expectedException UsageException
124 function testNoPassword() {
125 $this->doApiRequest( array(
126 'action' => 'createaccount',
127 'name' => 'testName',
128 'token' => LoginForm
::getCreateaccountToken(),
133 * Make sure requests with existing users are invalid.
134 * @expectedException UsageException
136 function testExistingUser() {
137 $this->doApiRequest( array(
138 'action' => 'createaccount',
139 'name' => 'Apitestsysop',
140 'token' => LoginForm
::getCreateaccountToken(),
141 'password' => 'password',
142 'email' => 'test@domain.test',
147 * Make sure requests with invalid emails are invalid.
148 * @expectedException UsageException
150 function testInvalidEmail() {
151 $this->doApiRequest( array(
152 'action' => 'createaccount',
153 'name' => 'Test User',
154 'token' => LoginForm
::getCreateaccountToken(),
155 'password' => 'password',
156 'email' => 'invalid',