Merge "Allow to pass a rev id to a log entry without making it unpatrolled"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiCreateAccountTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group API
6 * @group medium
7 *
8 * @covers ApiCreateAccount
9 */
10 class ApiCreateAccountTest extends ApiTestCase {
11 protected function setUp() {
12 parent::setUp();
13 $this->setMwGlobals( [ 'wgEnableEmail' => true ] );
14 }
15
16 /**
17 * Test the account creation API with a valid request. Also
18 * make sure the new account can log in and is valid.
19 *
20 * This test does multiple API requests so it might end up being
21 * a bit slow. Raise the default timeout.
22 * @group medium
23 */
24 public function testValid() {
25 global $wgServer;
26
27 if ( !isset( $wgServer ) ) {
28 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
29 }
30
31 $password = PasswordFactory::generateRandomPasswordString();
32
33 $ret = $this->doApiRequest( [
34 'action' => 'createaccount',
35 'name' => 'Apitestnew',
36 'password' => $password,
37 'email' => 'test@domain.test',
38 'realname' => 'Test Name'
39 ] );
40
41 $result = $ret[0];
42 $this->assertNotInternalType( 'bool', $result );
43 $this->assertNotInternalType( 'null', $result['createaccount'] );
44
45 // Should first ask for token.
46 $a = $result['createaccount'];
47 $this->assertEquals( 'NeedToken', $a['result'] );
48 $token = $a['token'];
49
50 // Finally create the account
51 $ret = $this->doApiRequest(
52 [
53 'action' => 'createaccount',
54 'name' => 'Apitestnew',
55 'password' => $password,
56 'token' => $token,
57 'email' => 'test@domain.test',
58 'realname' => 'Test Name'
59 ],
60 $ret[2]
61 );
62
63 $result = $ret[0];
64 $this->assertNotInternalType( 'bool', $result );
65 $this->assertEquals( 'Success', $result['createaccount']['result'] );
66
67 // Try logging in with the new user.
68 $ret = $this->doApiRequest( [
69 'action' => 'login',
70 'lgname' => 'Apitestnew',
71 'lgpassword' => $password,
72 ] );
73
74 $result = $ret[0];
75 $this->assertNotInternalType( 'bool', $result );
76 $this->assertNotInternalType( 'null', $result['login'] );
77
78 $a = $result['login']['result'];
79 $this->assertEquals( 'NeedToken', $a );
80 $token = $result['login']['token'];
81
82 $ret = $this->doApiRequest(
83 [
84 'action' => 'login',
85 'lgtoken' => $token,
86 'lgname' => 'Apitestnew',
87 'lgpassword' => $password,
88 ],
89 $ret[2]
90 );
91
92 $result = $ret[0];
93
94 $this->assertNotInternalType( 'bool', $result );
95 $a = $result['login']['result'];
96
97 $this->assertEquals( 'Success', $a );
98
99 // log out to destroy the session
100 $ret = $this->doApiRequest(
101 [
102 'action' => 'logout',
103 ],
104 $ret[2]
105 );
106 $this->assertEquals( [], $ret[0] );
107 }
108
109 /**
110 * Make sure requests with no names are invalid.
111 * @expectedException UsageException
112 */
113 public function testNoName() {
114 $this->doApiRequest( [
115 'action' => 'createaccount',
116 'token' => LoginForm::getCreateaccountToken()->toString(),
117 'password' => 'password',
118 ] );
119 }
120
121 /**
122 * Make sure requests with no password are invalid.
123 * @expectedException UsageException
124 */
125 public function testNoPassword() {
126 $this->doApiRequest( [
127 'action' => 'createaccount',
128 'name' => 'testName',
129 'token' => LoginForm::getCreateaccountToken()->toString(),
130 ] );
131 }
132
133 /**
134 * Make sure requests with existing users are invalid.
135 * @expectedException UsageException
136 */
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',
144 ] );
145 }
146
147 /**
148 * Make sure requests with invalid emails are invalid.
149 * @expectedException UsageException
150 */
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',
158 ] );
159 }
160 }