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,
72 $this->assertNotInternalType( 'bool', $result );
73 $this->assertNotInternalType( 'null', $result['login'] );
75 $a = $result['login']['result'];
76 $this->assertEquals( 'NeedToken', $a );
77 $token = $result['login']['token'];
79 $ret = $this->doApiRequest( array(
82 'lgname' => 'Apitestnew',
83 'lgpassword' => $password,
89 $this->assertNotInternalType( 'bool', $result );
90 $a = $result['login']['result'];
92 $this->assertEquals( 'Success', $a );
94 // log out to destroy the session
95 $ret = $this->doApiRequest( array(
99 $this->assertEquals( array(), $ret[0] );
103 * Make sure requests with no names are invalid.
104 * @expectedException UsageException
106 function testNoName() {
107 $ret = $this->doApiRequest( array(
108 'action' => 'createaccount',
109 'token' => LoginForm
::getCreateaccountToken(),
110 'password' => 'password',
115 * Make sure requests with no password are invalid.
116 * @expectedException UsageException
118 function testNoPassword() {
119 $ret = $this->doApiRequest( array(
120 'action' => 'createaccount',
121 'name' => 'testName',
122 'token' => LoginForm
::getCreateaccountToken(),
127 * Make sure requests with existing users are invalid.
128 * @expectedException UsageException
130 function testExistingUser() {
131 $this->doApiRequest( array(
132 'action' => 'createaccount',
133 'name' => 'Apitestsysop',
134 'token' => LoginForm
::getCreateaccountToken(),
135 'password' => 'password',
136 'email' => 'test@domain.test',
141 * Make sure requests with invalid emails are invalid.
142 * @expectedException UsageException
144 function testInvalidEmail() {
145 $this->doApiRequest( array(
146 'action' => 'createaccount',
147 'name' => 'Test User',
148 'token' => LoginForm
::getCreateaccountToken(),
149 'password' => 'password',
150 'email' => 'invalid',