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