8 class ApiCreateAccountTest
extends ApiTestCase
{
12 LoginForm
::setCreateaccountToken();
15 Hooks
::clear( 'AlternateUserMailer' );
16 $hooks['AlternateUserMailer'] = array( function () { return false; } );
17 $this->setMwGlobals( array( 'wgHooks' => $hooks ) );
21 * Test the account creation API with a valid request. Also
22 * make sure the new account can log in and is valid.
24 * This test does multiple API requests so it might end up being
25 * a bit slow. Raise the default timeout.
28 function testValid() {
31 if ( !isset( $wgServer ) ) {
32 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
35 $password = User
::randomPassword();
37 $ret = $this->doApiRequest( array(
38 'action' => 'createaccount',
39 'name' => 'Apitestnew',
40 'password' => $password,
41 'email' => 'test@domain.test',
42 'realname' => 'Test Name'
46 $this->assertNotInternalType( 'bool', $result );
47 $this->assertNotInternalType( 'null', $result['createaccount'] );
49 // Should first ask for token.
50 $a = $result['createaccount'];
51 $this->assertEquals( 'needtoken', $a['result'] );
54 // Finally create the account
55 $ret = $this->doApiRequest( array(
56 'action' => 'createaccount',
57 'name' => 'Apitestnew',
58 'password' => $password,
60 'email' => 'test@domain.test',
61 'realname' => 'Test Name' ), $ret[2]
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,
77 $this->assertNotInternalType( 'bool', $result );
78 $this->assertNotInternalType( 'null', $result['login'] );
80 $a = $result['login']['result'];
81 $this->assertEquals( 'NeedToken', $a );
82 $token = $result['login']['token'];
84 $ret = $this->doApiRequest( array(
87 'lgname' => 'Apitestnew',
88 '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( array(
101 'action' => 'logout',
104 $this->assertEquals( array(), $ret[0] );
108 * Make sure requests with no names are invalid.
109 * @expectedException UsageException
111 function testNoName() {
112 $ret = $this->doApiRequest( array(
113 'action' => 'createaccount',
114 'token' => LoginForm
::getCreateaccountToken(),
115 'password' => 'password',
120 * Make sure requests with no password are invalid.
121 * @expectedException UsageException
123 function testNoPassword() {
124 $ret = $this->doApiRequest( array(
125 'action' => 'createaccount',
126 'name' => 'testName',
127 'token' => LoginForm
::getCreateaccountToken(),
132 * Make sure requests with existing users are invalid.
133 * @expectedException UsageException
135 function testExistingUser() {
136 $this->doApiRequest( array(
137 'action' => 'createaccount',
138 'name' => 'Apitestsysop',
139 'token' => LoginForm
::getCreateaccountToken(),
140 'password' => 'password',
141 'email' => 'test@domain.test',
146 * Make sure requests with invalid emails are invalid.
147 * @expectedException UsageException
149 function testInvalidEmail() {
150 global $wgEnableEmail;
151 if ( !$wgEnableEmail ) {
152 $this->markTestSkipped( 'email is not enabled, so createaccount does not check it' );
154 $this->doApiRequest( array(
155 'action' => 'createaccount',
156 'name' => 'Test User',
157 'token' => LoginForm
::getCreateaccountToken(),
158 'password' => 'password',
159 'email' => 'invalid',