Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / api / ApiCreateAccountTest.php
blob8d134f7635c7617414f30c2339980381ccb3610b
1 <?php
3 /**
4 * @group Database
5 * @group API
6 * @group medium
8 * @covers ApiCreateAccount
9 */
10 class ApiCreateAccountTest extends ApiTestCase {
11 protected function setUp() {
12 parent::setUp();
13 LoginForm::setCreateaccountToken();
14 $this->setMwGlobals( array( 'wgEnableEmail' => true ) );
17 /**
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.
23 * @group medium
25 public function testValid() {
26 global $wgServer;
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'
40 ) );
42 $result = $ret[0];
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'] );
49 $token = $a['token'];
51 // Finally create the account
52 $ret = $this->doApiRequest(
53 array(
54 'action' => 'createaccount',
55 'name' => 'Apitestnew',
56 'password' => $password,
57 'token' => $token,
58 'email' => 'test@domain.test',
59 'realname' => 'Test Name'
61 $ret[2]
64 $result = $ret[0];
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(
70 'action' => 'login',
71 'lgname' => 'Apitestnew',
72 'lgpassword' => $password,
73 ) );
75 $result = $ret[0];
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(
84 array(
85 'action' => 'login',
86 'lgtoken' => $token,
87 'lgname' => 'Apitestnew',
88 'lgpassword' => $password,
90 $ret[2]
93 $result = $ret[0];
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(
102 array(
103 'action' => 'logout',
105 $ret[2]
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',
119 ) );
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(),
131 ) );
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',
145 ) );
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',
159 ) );