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( array(
51 'action' => 'createaccount',
52 'name' => 'Apitestnew',
53 'password' => $password,
55 'email' => 'test@domain.test',
56 'realname' => 'Test Name' ), $ret[2]
60 $this->assertNotInternalType( 'bool', $result );
61 $this->assertEquals( 'success', $result['createaccount']['result'] );
63 // Try logging in with the new user.
64 $ret = $this->doApiRequest( array(
66 'lgname' => 'Apitestnew',
67 '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(
82 'lgname' => 'Apitestnew',
83 'lgpassword' => $password,
90 $this->assertNotInternalType( 'bool', $result );
91 $a = $result['login']['result'];
93 $this->assertEquals( 'Success', $a );
95 // log out to destroy the session
96 $ret = $this->doApiRequest(
102 $this->assertEquals( array(), $ret[0] );
106 * Make sure requests with no names are invalid.
107 * @expectedException UsageException
109 function testNoName() {
110 $ret = $this->doApiRequest( array(
111 'action' => 'createaccount',
112 'token' => LoginForm
::getCreateaccountToken(),
113 'password' => 'password',
118 * Make sure requests with no password are invalid.
119 * @expectedException UsageException
121 function testNoPassword() {
122 $ret = $this->doApiRequest( array(
123 'action' => 'createaccount',
124 'name' => 'testName',
125 'token' => LoginForm
::getCreateaccountToken(),
130 * Make sure requests with existing users are invalid.
131 * @expectedException UsageException
133 function testExistingUser() {
134 $this->doApiRequest( array(
135 'action' => 'createaccount',
136 'name' => 'Apitestsysop',
137 'token' => LoginForm
::getCreateaccountToken(),
138 'password' => 'password',
139 'email' => 'test@domain.test',
144 * Make sure requests with invalid emails are invalid.
145 * @expectedException UsageException
147 function testInvalidEmail() {
148 $this->doApiRequest( array(
149 'action' => 'createaccount',
150 'name' => 'Test User',
151 'token' => LoginForm
::getCreateaccountToken(),
152 'password' => 'password',
153 'email' => 'invalid',