9a83e61d575fdedd881117ef1412bd245e2f6b80
8 * @covers ApiCreateAccount
10 class ApiCreateAccountTest
extends ApiTestCase
{
11 protected function setUp() {
13 $this->setMwGlobals( [ 'wgEnableEmail' => true ] );
17 * Test the account creation API with a valid request. Also
18 * make sure the new account can log in and is valid.
20 * This test does multiple API requests so it might end up being
21 * a bit slow. Raise the default timeout.
24 public function testValid() {
27 if ( !isset( $wgServer ) ) {
28 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
31 $password = PasswordFactory
::generateRandomPasswordString();
33 $ret = $this->doApiRequest( [
34 'action' => 'createaccount',
35 'name' => 'Apitestnew',
36 'password' => $password,
37 'email' => 'test@domain.test',
38 'realname' => 'Test Name'
42 $this->assertNotInternalType( 'bool', $result );
43 $this->assertNotInternalType( 'null', $result['createaccount'] );
45 // Should first ask for token.
46 $a = $result['createaccount'];
47 $this->assertEquals( 'NeedToken', $a['result'] );
50 // Finally create the account
51 $ret = $this->doApiRequest(
53 'action' => 'createaccount',
54 'name' => 'Apitestnew',
55 'password' => $password,
57 'email' => 'test@domain.test',
58 'realname' => 'Test Name'
64 $this->assertNotInternalType( 'bool', $result );
65 $this->assertEquals( 'Success', $result['createaccount']['result'] );
67 // Try logging in with the new user.
68 $ret = $this->doApiRequest( [
70 'lgname' => 'Apitestnew',
71 'lgpassword' => $password,
75 $this->assertNotInternalType( 'bool', $result );
76 $this->assertNotInternalType( 'null', $result['login'] );
78 $a = $result['login']['result'];
79 $this->assertEquals( 'NeedToken', $a );
80 $token = $result['login']['token'];
82 $ret = $this->doApiRequest(
86 'lgname' => 'Apitestnew',
87 'lgpassword' => $password,
94 $this->assertNotInternalType( 'bool', $result );
95 $a = $result['login']['result'];
97 $this->assertEquals( 'Success', $a );
99 // log out to destroy the session
100 $ret = $this->doApiRequest(
102 'action' => 'logout',
106 $this->assertEquals( [], $ret[0] );
110 * Make sure requests with no names are invalid.
111 * @expectedException UsageException
113 public function testNoName() {
114 $this->doApiRequest( [
115 'action' => 'createaccount',
116 'token' => LoginForm
::getCreateaccountToken()->toString(),
117 'password' => 'password',
122 * Make sure requests with no password are invalid.
123 * @expectedException UsageException
125 public function testNoPassword() {
126 $this->doApiRequest( [
127 'action' => 'createaccount',
128 'name' => 'testName',
129 'token' => LoginForm
::getCreateaccountToken()->toString(),
134 * Make sure requests with existing users are invalid.
135 * @expectedException UsageException
137 public function testExistingUser() {
138 $this->doApiRequest( [
139 'action' => 'createaccount',
140 'name' => 'Apitestsysop',
141 'token' => LoginForm
::getCreateaccountToken()->toString(),
142 'password' => 'password',
143 'email' => 'test@domain.test',
148 * Make sure requests with invalid emails are invalid.
149 * @expectedException UsageException
151 public function testInvalidEmail() {
152 $this->doApiRequest( [
153 'action' => 'createaccount',
154 'name' => 'Test User',
155 'token' => LoginForm
::getCreateaccountToken()->toString(),
156 'password' => 'password',
157 'email' => 'invalid',