User: Mostly remove password handling
[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 LoginForm::setCreateaccountToken();
14 $this->setMwGlobals( array( 'wgEnableEmail' => true ) );
15 }
16
17 /**
18 * Test the account creation API with a valid request. Also
19 * make sure the new account can log in and is valid.
20 *
21 * This test does multiple API requests so it might end up being
22 * a bit slow. Raise the default timeout.
23 * @group medium
24 */
25 public function testValid() {
26 global $wgServer;
27
28 if ( !isset( $wgServer ) ) {
29 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
30 }
31
32 $password = PasswordFactory::generateRandomPasswordString();
33
34 $ret = $this->doApiRequest( array(
35 'action' => 'createaccount',
36 'name' => 'Apitestnew',
37 'password' => $password,
38 'email' => 'test@domain.test',
39 'realname' => 'Test Name'
40 ) );
41
42 $result = $ret[0];
43 $this->assertNotInternalType( 'bool', $result );
44 $this->assertNotInternalType( 'null', $result['createaccount'] );
45
46 // Should first ask for token.
47 $a = $result['createaccount'];
48 $this->assertEquals( 'NeedToken', $a['result'] );
49 $token = $a['token'];
50
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'
60 ),
61 $ret[2]
62 );
63
64 $result = $ret[0];
65 $this->assertNotInternalType( 'bool', $result );
66 $this->assertEquals( 'Success', $result['createaccount']['result'] );
67
68 // Try logging in with the new user.
69 $ret = $this->doApiRequest( array(
70 'action' => 'login',
71 'lgname' => 'Apitestnew',
72 'lgpassword' => $password,
73 ) );
74
75 $result = $ret[0];
76 $this->assertNotInternalType( 'bool', $result );
77 $this->assertNotInternalType( 'null', $result['login'] );
78
79 $a = $result['login']['result'];
80 $this->assertEquals( 'NeedToken', $a );
81 $token = $result['login']['token'];
82
83 $ret = $this->doApiRequest(
84 array(
85 'action' => 'login',
86 'lgtoken' => $token,
87 'lgname' => 'Apitestnew',
88 'lgpassword' => $password,
89 ),
90 $ret[2]
91 );
92
93 $result = $ret[0];
94
95 $this->assertNotInternalType( 'bool', $result );
96 $a = $result['login']['result'];
97
98 $this->assertEquals( 'Success', $a );
99
100 // log out to destroy the session
101 $ret = $this->doApiRequest(
102 array(
103 'action' => 'logout',
104 ),
105 $ret[2]
106 );
107 $this->assertEquals( array(), $ret[0] );
108 }
109
110 /**
111 * Make sure requests with no names are invalid.
112 * @expectedException UsageException
113 */
114 public function testNoName() {
115 $this->doApiRequest( array(
116 'action' => 'createaccount',
117 'token' => LoginForm::getCreateaccountToken(),
118 'password' => 'password',
119 ) );
120 }
121
122 /**
123 * Make sure requests with no password are invalid.
124 * @expectedException UsageException
125 */
126 public function testNoPassword() {
127 $this->doApiRequest( array(
128 'action' => 'createaccount',
129 'name' => 'testName',
130 'token' => LoginForm::getCreateaccountToken(),
131 ) );
132 }
133
134 /**
135 * Make sure requests with existing users are invalid.
136 * @expectedException UsageException
137 */
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 ) );
146 }
147
148 /**
149 * Make sure requests with invalid emails are invalid.
150 * @expectedException UsageException
151 */
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 ) );
160 }
161 }