8 * @covers ApiCreateAccount
10 class ApiCreateAccountTest
extends ApiTestCase
{
11 protected function setUp() {
13 LoginForm
::setCreateaccountToken();
14 $this->setMwGlobals( array( 'wgEnableEmail' => true ) );
18 * Test the account creation API with a valid request. Also
19 * make sure the new account can log in and is valid.
21 * This test does multiple API requests so it might end up being
22 * a bit slow. Raise the default timeout.
25 public function testValid() {
28 if ( !isset( $wgServer ) ) {
29 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
32 $password = User
::randomPassword();
34 $ret = $this->doApiRequest( array(
35 'action' => 'createaccount',
36 'name' => 'Apitestnew',
37 'password' => $password,
38 'email' => 'test@domain.test',
39 'realname' => 'Test Name'
43 $this->assertNotInternalType( 'bool', $result );
44 $this->assertNotInternalType( 'null', $result['createaccount'] );
46 // Should first ask for token.
47 $a = $result['createaccount'];
48 $this->assertEquals( 'NeedToken', $a['result'] );
51 // Finally create the account
52 $ret = $this->doApiRequest(
54 'action' => 'createaccount',
55 'name' => 'Apitestnew',
56 'password' => $password,
58 'email' => 'test@domain.test',
59 'realname' => 'Test Name'
65 $this->assertNotInternalType( 'bool', $result );
66 $this->assertEquals( 'Success', $result['createaccount']['result'] );
68 // Try logging in with the new user.
69 $ret = $this->doApiRequest( array(
71 'lgname' => 'Apitestnew',
72 'lgpassword' => $password,
76 $this->assertNotInternalType( 'bool', $result );
77 $this->assertNotInternalType( 'null', $result['login'] );
79 $a = $result['login']['result'];
80 $this->assertEquals( 'NeedToken', $a );
81 $token = $result['login']['token'];
83 $ret = $this->doApiRequest(
87 'lgname' => 'Apitestnew',
88 'lgpassword' => $password,
95 $this->assertNotInternalType( 'bool', $result );
96 $a = $result['login']['result'];
98 $this->assertEquals( 'Success', $a );
100 // log out to destroy the session
101 $ret = $this->doApiRequest(
103 'action' => 'logout',
107 $this->assertEquals( array(), $ret[0] );
111 * Make sure requests with no names are invalid.
112 * @expectedException UsageException
114 public function testNoName() {
115 $this->doApiRequest( array(
116 'action' => 'createaccount',
117 'token' => LoginForm
::getCreateaccountToken(),
118 'password' => 'password',
123 * Make sure requests with no password are invalid.
124 * @expectedException UsageException
126 public function testNoPassword() {
127 $this->doApiRequest( array(
128 'action' => 'createaccount',
129 'name' => 'testName',
130 'token' => LoginForm
::getCreateaccountToken(),
135 * Make sure requests with existing users are invalid.
136 * @expectedException UsageException
138 public function testExistingUser() {
139 $this->doApiRequest( array(
140 'action' => 'createaccount',
141 'name' => 'Apitestsysop',
142 'token' => LoginForm
::getCreateaccountToken(),
143 'password' => 'password',
144 'email' => 'test@domain.test',
149 * Make sure requests with invalid emails are invalid.
150 * @expectedException UsageException
152 public function testInvalidEmail() {
153 $this->doApiRequest( array(
154 'action' => 'createaccount',
155 'name' => 'Test User',
156 'token' => LoginForm
::getCreateaccountToken(),
157 'password' => 'password',
158 'email' => 'invalid',