Slight improvements to FormSpecialPage behavior.
[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( array(
51 'action' => 'createaccount',
52 'name' => 'Apitestnew',
53 'password' => $password,
54 'token' => $token,
55 'email' => 'test@domain.test',
56 'realname' => 'Test Name' ), $ret[2]
57 );
58
59 $result = $ret[0];
60 $this->assertNotInternalType( 'bool', $result );
61 $this->assertEquals( 'success', $result['createaccount']['result'] );
62
63 // Try logging in with the new user.
64 $ret = $this->doApiRequest( array(
65 'action' => 'login',
66 'lgname' => 'Apitestnew',
67 'lgpassword' => $password,
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(
79 array(
80 'action' => 'login',
81 'lgtoken' => $token,
82 'lgname' => 'Apitestnew',
83 'lgpassword' => $password,
84 ),
85 $ret[2]
86 );
87
88 $result = $ret[0];
89
90 $this->assertNotInternalType( 'bool', $result );
91 $a = $result['login']['result'];
92
93 $this->assertEquals( 'Success', $a );
94
95 // log out to destroy the session
96 $ret = $this->doApiRequest(
97 array(
98 'action' => 'logout',
99 ),
100 $ret[2]
101 );
102 $this->assertEquals( array(), $ret[0] );
103 }
104
105 /**
106 * Make sure requests with no names are invalid.
107 * @expectedException UsageException
108 */
109 function testNoName() {
110 $ret = $this->doApiRequest( array(
111 'action' => 'createaccount',
112 'token' => LoginForm::getCreateaccountToken(),
113 'password' => 'password',
114 ) );
115 }
116
117 /**
118 * Make sure requests with no password are invalid.
119 * @expectedException UsageException
120 */
121 function testNoPassword() {
122 $ret = $this->doApiRequest( array(
123 'action' => 'createaccount',
124 'name' => 'testName',
125 'token' => LoginForm::getCreateaccountToken(),
126 ) );
127 }
128
129 /**
130 * Make sure requests with existing users are invalid.
131 * @expectedException UsageException
132 */
133 function testExistingUser() {
134 $this->doApiRequest( array(
135 'action' => 'createaccount',
136 'name' => 'Apitestsysop',
137 'token' => LoginForm::getCreateaccountToken(),
138 'password' => 'password',
139 'email' => 'test@domain.test',
140 ) );
141 }
142
143 /**
144 * Make sure requests with invalid emails are invalid.
145 * @expectedException UsageException
146 */
147 function testInvalidEmail() {
148 $this->doApiRequest( array(
149 'action' => 'createaccount',
150 'name' => 'Test User',
151 'token' => LoginForm::getCreateaccountToken(),
152 'password' => 'password',
153 'email' => 'invalid',
154 ) );
155 }
156 }