test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / api / ApiAccountCreationTest.php
blob50638ca716ec0c24eae3a709dd34b29d5b053b55
1 <?php
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 ) );
15 /**
16 * Test the account creation API with a valid request. Also
17 * make sure the new account can log in and is valid.
19 * This test does multiple API requests so it might end up being
20 * a bit slow. Raise the default timeout.
21 * @group medium
23 function testValid() {
24 global $wgServer;
26 if ( !isset( $wgServer ) ) {
27 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
30 $password = User::randomPassword();
32 $ret = $this->doApiRequest( array(
33 'action' => 'createaccount',
34 'name' => 'Apitestnew',
35 'password' => $password,
36 'email' => 'test@domain.test',
37 'realname' => 'Test Name'
38 ) );
40 $result = $ret[0];
41 $this->assertNotInternalType( 'bool', $result );
42 $this->assertNotInternalType( 'null', $result['createaccount'] );
44 // Should first ask for token.
45 $a = $result['createaccount'];
46 $this->assertEquals( 'needtoken', $a['result'] );
47 $token = $a['token'];
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'
59 $ret[2]
62 $result = $ret[0];
63 $this->assertNotInternalType( 'bool', $result );
64 $this->assertEquals( 'success', $result['createaccount']['result'] );
66 // Try logging in with the new user.
67 $ret = $this->doApiRequest( array(
68 'action' => 'login',
69 'lgname' => 'Apitestnew',
70 'lgpassword' => $password,
71 ) );
73 $result = $ret[0];
74 $this->assertNotInternalType( 'bool', $result );
75 $this->assertNotInternalType( 'null', $result['login'] );
77 $a = $result['login']['result'];
78 $this->assertEquals( 'NeedToken', $a );
79 $token = $result['login']['token'];
81 $ret = $this->doApiRequest(
82 array(
83 'action' => 'login',
84 'lgtoken' => $token,
85 'lgname' => 'Apitestnew',
86 'lgpassword' => $password,
88 $ret[2]
91 $result = $ret[0];
93 $this->assertNotInternalType( 'bool', $result );
94 $a = $result['login']['result'];
96 $this->assertEquals( 'Success', $a );
98 // log out to destroy the session
99 $ret = $this->doApiRequest(
100 array(
101 'action' => 'logout',
103 $ret[2]
105 $this->assertEquals( array(), $ret[0] );
109 * Make sure requests with no names are invalid.
110 * @expectedException UsageException
112 function testNoName() {
113 $this->doApiRequest( array(
114 'action' => 'createaccount',
115 'token' => LoginForm::getCreateaccountToken(),
116 'password' => 'password',
117 ) );
121 * Make sure requests with no password are invalid.
122 * @expectedException UsageException
124 function testNoPassword() {
125 $this->doApiRequest( array(
126 'action' => 'createaccount',
127 'name' => 'testName',
128 'token' => LoginForm::getCreateaccountToken(),
129 ) );
133 * Make sure requests with existing users are invalid.
134 * @expectedException UsageException
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 ) );
147 * Make sure requests with invalid emails are invalid.
148 * @expectedException UsageException
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 ) );