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