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