testInvalidEmail() passes now without local config
[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
71 $result = $ret[0];
72 $this->assertNotInternalType( 'bool', $result );
73 $this->assertNotInternalType( 'null', $result['login'] );
74
75 $a = $result['login']['result'];
76 $this->assertEquals( 'NeedToken', $a );
77 $token = $result['login']['token'];
78
79 $ret = $this->doApiRequest( array(
80 'action' => 'login',
81 'lgtoken' => $token,
82 'lgname' => 'Apitestnew',
83 'lgpassword' => $password,
84 ), $ret[2]
85 );
86
87 $result = $ret[0];
88
89 $this->assertNotInternalType( 'bool', $result );
90 $a = $result['login']['result'];
91
92 $this->assertEquals( 'Success', $a );
93
94 // log out to destroy the session
95 $ret = $this->doApiRequest( array(
96 'action' => 'logout',
97 ), $ret[2]
98 );
99 $this->assertEquals( array(), $ret[0] );
100 }
101
102 /**
103 * Make sure requests with no names are invalid.
104 * @expectedException UsageException
105 */
106 function testNoName() {
107 $ret = $this->doApiRequest( array(
108 'action' => 'createaccount',
109 'token' => LoginForm::getCreateaccountToken(),
110 'password' => 'password',
111 ) );
112 }
113
114 /**
115 * Make sure requests with no password are invalid.
116 * @expectedException UsageException
117 */
118 function testNoPassword() {
119 $ret = $this->doApiRequest( array(
120 'action' => 'createaccount',
121 'name' => 'testName',
122 'token' => LoginForm::getCreateaccountToken(),
123 ) );
124 }
125
126 /**
127 * Make sure requests with existing users are invalid.
128 * @expectedException UsageException
129 */
130 function testExistingUser() {
131 $this->doApiRequest( array(
132 'action' => 'createaccount',
133 'name' => 'Apitestsysop',
134 'token' => LoginForm::getCreateaccountToken(),
135 'password' => 'password',
136 'email' => 'test@domain.test',
137 ) );
138 }
139
140 /**
141 * Make sure requests with invalid emails are invalid.
142 * @expectedException UsageException
143 */
144 function testInvalidEmail() {
145 $this->doApiRequest( array(
146 'action' => 'createaccount',
147 'name' => 'Test User',
148 'token' => LoginForm::getCreateaccountToken(),
149 'password' => 'password',
150 'email' => 'invalid',
151 ) );
152 }
153 }