Index: trunk/phase3/tests/phpunit/includes/upload/UploadFromUrlTest.php |
— | — | @@ -1,12 +1,10 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -require_once dirname( dirname( __FILE__ ) ) . '/api/ApiSetup.php'; |
5 | | - |
6 | 4 | /** |
7 | 5 | * @group Broken |
8 | 6 | * @group Upload |
9 | 7 | */ |
10 | | -class UploadFromUrlTest extends ApiTestSetup { |
| 8 | +class UploadFromUrlTest extends ApiTestCase { |
11 | 9 | |
12 | 10 | public function setUp() { |
13 | 11 | global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads; |
Index: trunk/phase3/tests/phpunit/includes/api/ApiSetup.php |
— | — | @@ -1,96 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -abstract class ApiTestSetup extends MediaWikiLangTestCase { |
5 | | - protected $user; |
6 | | - protected $sysopUser; |
7 | | - protected static $apiUrl; |
8 | | - |
9 | | - function setUp() { |
10 | | - global $wgServer, $wgContLang, $wgAuth, $wgMemc, $wgRequest; |
11 | | - |
12 | | - parent::setUp(); |
13 | | - self::$apiUrl = $wgServer . wfScript( 'api' ); |
14 | | - |
15 | | - $wgMemc = new EmptyBagOStuff; |
16 | | - $wgContLang = Language::factory( 'en' ); |
17 | | - $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' ); |
18 | | - $wgRequest = new FauxRequest( array() ); |
19 | | - $this->setupUser(); |
20 | | - } |
21 | | - |
22 | | - protected function doApiRequest( $params, $data = null, $appendModule = false ) { |
23 | | - $_SESSION = isset( $data[2] ) ? $data[2] : array(); |
24 | | - |
25 | | - $req = new FauxRequest( $params, true, $_SESSION ); |
26 | | - $module = new ApiMain( $req, true ); |
27 | | - $module->execute(); |
28 | | - |
29 | | - $data[0] = $module->getResultData(); |
30 | | - $data[1] = $req; |
31 | | - $data[2] = $_SESSION; |
32 | | - |
33 | | - if( $appendModule ) $data[3] = $module; |
34 | | - |
35 | | - return $data; |
36 | | - } |
37 | | - |
38 | | - function setupUser() { |
39 | | - if ( $this->user == null || $this->sysopUser == null ) { |
40 | | - $this->user = new UserWrapper( 'User for MediaWiki automated tests', User::randomPassword() ); |
41 | | - $this->sysopUser = new UserWrapper( 'Sysop for MediaWiki automated tests', User::randomPassword(), 'sysop' ); |
42 | | - } |
43 | | - |
44 | | - $GLOBALS['wgUser'] = $this->sysopUser->user; |
45 | | - } |
46 | | - |
47 | | - function doLogin() { |
48 | | - $data = $this->doApiRequest( array( |
49 | | - 'action' => 'login', |
50 | | - 'lgname' => $this->sysopUser->userName, |
51 | | - 'lgpassword' => $this->sysopUser->password ) ); |
52 | | - |
53 | | - $token = $data[0]['login']['token']; |
54 | | - |
55 | | - $data = $this->doApiRequest( array( |
56 | | - 'action' => 'login', |
57 | | - "lgtoken" => $token, |
58 | | - "lgname" => $this->sysopUser->userName, |
59 | | - "lgpassword" => $this->sysopUser->password ), $data ); |
60 | | - |
61 | | - return $data; |
62 | | - } |
63 | | - |
64 | | - function getTokenList( $user ) { |
65 | | - $GLOBALS['wgUser'] = $user->user; |
66 | | - $data = $this->doApiRequest( array( |
67 | | - 'action' => 'query', |
68 | | - 'titles' => 'Main Page', |
69 | | - 'intoken' => 'edit|delete|protect|move|block|unblock', |
70 | | - 'prop' => 'info' ) ); |
71 | | - return $data; |
72 | | - } |
73 | | - |
74 | | -} |
75 | | - |
76 | | -class UserWrapper { |
77 | | - public $userName, $password, $user; |
78 | | - |
79 | | - public function __construct( $userName, $password, $group = '' ) { |
80 | | - $this->userName = $userName; |
81 | | - $this->password = $password; |
82 | | - |
83 | | - $this->user = User::newFromName( $this->userName ); |
84 | | - if ( !$this->user->getID() ) { |
85 | | - $this->user = User::createNew( $this->userName, array( |
86 | | - "email" => "test@example.com", |
87 | | - "real_name" => "Test User" ) ); |
88 | | - } |
89 | | - $this->user->setPassword( $this->password ); |
90 | | - |
91 | | - if ( $group !== '' ) { |
92 | | - $this->user->addGroup( $group ); |
93 | | - } |
94 | | - $this->user->saveSettings(); |
95 | | - } |
96 | | -} |
97 | | - |
Index: trunk/phase3/tests/phpunit/includes/api/ApiTest.php |
— | — | @@ -1,25 +1,10 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -class MockApi extends ApiBase { |
5 | | - public function execute() { } |
6 | | - public function getVersion() { } |
7 | | - |
8 | | - public function __construct() { } |
9 | | - |
10 | | - public function getAllowedParams() { |
11 | | - return array( |
12 | | - 'filename' => null, |
13 | | - 'enablechunks' => false, |
14 | | - 'sessionkey' => null, |
15 | | - ); |
16 | | - } |
17 | | -} |
18 | | - |
19 | 4 | /** |
20 | 5 | * @group Database |
21 | 6 | * @group Destructive |
22 | 7 | */ |
23 | | -class ApiTest extends ApiTestSetup { |
| 8 | +class ApiTest extends ApiTestCase { |
24 | 9 | |
25 | 10 | function testRequireOnlyOneParameterDefault() { |
26 | 11 | $mock = new MockApi(); |
— | — | @@ -78,7 +63,7 @@ |
79 | 64 | */ |
80 | 65 | function testApiLoginNoName() { |
81 | 66 | $data = $this->doApiRequest( array( 'action' => 'login', |
82 | | - 'lgname' => '', 'lgpassword' => $this->user->password, |
| 67 | + 'lgname' => '', 'lgpassword' => self::$users['sysop']->password, |
83 | 68 | ) ); |
84 | 69 | $this->assertEquals( 'NoName', $data[0]['login']['result'] ); |
85 | 70 | } |
— | — | @@ -86,14 +71,15 @@ |
87 | 72 | function testApiLoginBadPass() { |
88 | 73 | global $wgServer; |
89 | 74 | |
90 | | - $user = $this->user; |
| 75 | + $user = self::$users['sysop']; |
| 76 | + $user->user->logOut(); |
91 | 77 | |
92 | 78 | if ( !isset( $wgServer ) ) { |
93 | 79 | $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); |
94 | 80 | } |
95 | 81 | $ret = $this->doApiRequest( array( |
96 | 82 | "action" => "login", |
97 | | - "lgname" => $user->userName, |
| 83 | + "lgname" => $user->username, |
98 | 84 | "lgpassword" => "bad", |
99 | 85 | ) |
100 | 86 | ); |
— | — | @@ -109,8 +95,8 @@ |
110 | 96 | $ret = $this->doApiRequest( array( |
111 | 97 | "action" => "login", |
112 | 98 | "lgtoken" => $token, |
113 | | - "lgname" => $user->userName, |
114 | | - "lgpassword" => "bad", |
| 99 | + "lgname" => $user->username, |
| 100 | + "lgpassword" => "badnowayinhell", |
115 | 101 | ) |
116 | 102 | ); |
117 | 103 | |
— | — | @@ -129,11 +115,12 @@ |
130 | 116 | $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); |
131 | 117 | } |
132 | 118 | |
133 | | - $user = $this->user; |
| 119 | + $user = self::$users['sysop']; |
| 120 | + $user->user->logOut(); |
134 | 121 | |
135 | 122 | $ret = $this->doApiRequest( array( |
136 | 123 | "action" => "login", |
137 | | - "lgname" => $user->userName, |
| 124 | + "lgname" => $user->username, |
138 | 125 | "lgpassword" => $user->password, |
139 | 126 | ) |
140 | 127 | ); |
— | — | @@ -149,7 +136,7 @@ |
150 | 137 | $ret = $this->doApiRequest( array( |
151 | 138 | "action" => "login", |
152 | 139 | "lgtoken" => $token, |
153 | | - "lgname" => $user->userName, |
| 140 | + "lgname" => $user->username, |
154 | 141 | "lgpassword" => $user->password, |
155 | 142 | ) |
156 | 143 | ); |
— | — | @@ -170,11 +157,13 @@ |
171 | 158 | if ( !isset( $wgServer ) ) { |
172 | 159 | $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); |
173 | 160 | } |
| 161 | + $user = self::$users['sysop']; |
| 162 | + |
174 | 163 | $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml", |
175 | 164 | array( "method" => "POST", |
176 | 165 | "postData" => array( |
177 | | - "lgname" => $this->user->userName, |
178 | | - "lgpassword" => $this->user->password ) ) ); |
| 166 | + "lgname" => $user->username, |
| 167 | + "lgpassword" => $user->password ) ) ); |
179 | 168 | $req->execute(); |
180 | 169 | |
181 | 170 | libxml_use_internal_errors( true ); |
— | — | @@ -189,8 +178,8 @@ |
190 | 179 | |
191 | 180 | $req->setData( array( |
192 | 181 | "lgtoken" => $token, |
193 | | - "lgname" => $this->user->userName, |
194 | | - "lgpassword" => $this->user->password ) ); |
| 182 | + "lgname" => $user->username, |
| 183 | + "lgpassword" => $user->password ) ); |
195 | 184 | $req->execute(); |
196 | 185 | |
197 | 186 | $cj = $req->getCookieJar(); |
— | — | @@ -198,7 +187,7 @@ |
199 | 188 | $this->assertNotEquals( false, $serverName ); |
200 | 189 | $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName ); |
201 | 190 | $this->assertNotEquals( '', $serializedCookie ); |
202 | | - $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $this->user->userName . '; .*Token=/', $serializedCookie ); |
| 191 | + $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie ); |
203 | 192 | |
204 | 193 | return $cj; |
205 | 194 | } |
— | — | @@ -225,10 +214,11 @@ |
226 | 215 | } |
227 | 216 | |
228 | 217 | function testRunLogin() { |
| 218 | + $sysopUser = self::$users['sysop']; |
229 | 219 | $data = $this->doApiRequest( array( |
230 | 220 | 'action' => 'login', |
231 | | - 'lgname' => $this->sysopUser->userName, |
232 | | - 'lgpassword' => $this->sysopUser->password ) ); |
| 221 | + 'lgname' => $sysopUser->username, |
| 222 | + 'lgpassword' => $sysopUser->password ) ); |
233 | 223 | |
234 | 224 | $this->assertArrayHasKey( "login", $data[0] ); |
235 | 225 | $this->assertArrayHasKey( "result", $data[0]['login'] ); |
— | — | @@ -238,8 +228,8 @@ |
239 | 229 | $data = $this->doApiRequest( array( |
240 | 230 | 'action' => 'login', |
241 | 231 | "lgtoken" => $token, |
242 | | - "lgname" => $this->sysopUser->userName, |
243 | | - "lgpassword" => $this->sysopUser->password ), $data ); |
| 232 | + "lgname" => $sysopUser->username, |
| 233 | + "lgpassword" => $sysopUser->password ), $data ); |
244 | 234 | |
245 | 235 | $this->assertArrayHasKey( "login", $data[0] ); |
246 | 236 | $this->assertArrayHasKey( "result", $data[0]['login'] ); |
— | — | @@ -250,7 +240,7 @@ |
251 | 241 | } |
252 | 242 | |
253 | 243 | function testGettingToken() { |
254 | | - foreach ( array( $this->user, $this->sysopUser ) as $user ) { |
| 244 | + foreach ( self::$users as $user ) { |
255 | 245 | $this->runTokenTest( $user ); |
256 | 246 | } |
257 | 247 | } |
Index: trunk/phase3/tests/phpunit/includes/api/ApiPurgeTest.php |
— | — | @@ -1,11 +1,9 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -require_once dirname( __FILE__ ) . '/ApiSetup.php'; |
5 | | - |
6 | 4 | /** |
7 | 5 | * @group Database |
8 | 6 | */ |
9 | | -class ApiPurgeTest extends ApiTestSetup { |
| 7 | +class ApiPurgeTest extends ApiTestCase { |
10 | 8 | |
11 | 9 | function setUp() { |
12 | 10 | parent::setUp(); |
Index: trunk/phase3/tests/phpunit/includes/api/ApiQueryTest.php |
— | — | @@ -1,11 +1,9 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -require_once dirname( __FILE__ ) . '/ApiSetup.php'; |
5 | | - |
6 | 4 | /** |
7 | 5 | * @group Database |
8 | 6 | */ |
9 | | -class ApiQueryTest extends ApiTestSetup { |
| 7 | +class ApiQueryTest extends ApiTestCase { |
10 | 8 | |
11 | 9 | function setUp() { |
12 | 10 | parent::setUp(); |
Index: trunk/phase3/tests/phpunit/includes/api/ApiWatchTest.php |
— | — | @@ -1,13 +1,11 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -require_once dirname( __FILE__ ) . '/ApiSetup.php'; |
5 | | - |
6 | 4 | /** |
7 | 5 | * @group Database |
8 | 6 | * @group Destructive |
9 | 7 | * @todo This test suite is severly broken and need a full review |
10 | 8 | */ |
11 | | -class ApiWatchTest extends ApiTestSetup { |
| 9 | +class ApiWatchTest extends ApiTestCase { |
12 | 10 | |
13 | 11 | function setUp() { |
14 | 12 | parent::setUp(); |
— | — | @@ -15,7 +13,7 @@ |
16 | 14 | } |
17 | 15 | |
18 | 16 | function getTokens() { |
19 | | - return $this->getTokenList( $this->sysopUser ); |
| 17 | + return $this->getTokenList( self::$users['sysop'] ); |
20 | 18 | } |
21 | 19 | |
22 | 20 | /** |
Index: trunk/phase3/tests/phpunit/includes/api/format/ApiFormatPhpTest.php |
— | — | @@ -1,19 +1,10 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -require_once dirname( __FILE__ ) . '/ApiFormatTestBase.php'; |
5 | | - |
6 | 4 | /** |
7 | 5 | * @group API |
8 | 6 | * @group Database |
9 | 7 | */ |
10 | 8 | class ApiFormatPhpTest extends ApiFormatTestBase { |
11 | | - |
12 | | - /*function setUp() { |
13 | | - parent::setUp(); |
14 | | - $this->doLogin(); |
15 | | - }*/ |
16 | | - |
17 | | - |
18 | 9 | function testValidPHPSyntax() { |
19 | 10 | |
20 | 11 | $data = $this->apiRequest( 'php', array( 'action' => 'query', 'meta' => 'siteinfo' ) ); |
Index: trunk/phase3/tests/phpunit/includes/api/format/ApiFormatTestBase.php |
— | — | @@ -1,13 +1,9 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -require_once dirname( dirname( __FILE__ ) ) . '/ApiSetup.php'; |
5 | | - |
6 | | -abstract class ApiFormatTestBase extends ApiTestSetup { |
7 | | - |
| 4 | +abstract class ApiFormatTestBase extends ApiTestCase { |
8 | 5 | protected function apiRequest( $format, $params, $data = null ) { |
9 | | - |
10 | 6 | $data = parent::doApiRequest( $params, $data, true ); |
11 | | - |
| 7 | + |
12 | 8 | $module = $data[3]; |
13 | 9 | |
14 | 10 | $printer = $module->createPrinterByName( $format ); |
— | — | @@ -23,9 +19,4 @@ |
24 | 20 | |
25 | 21 | return $out; |
26 | 22 | } |
27 | | - |
28 | | - function setupUser() { |
29 | | - /* Do not setup a user here */ |
30 | | - } |
31 | 23 | } |
32 | | - |
Index: trunk/phase3/tests/phpunit/includes/api/ApiBlockTest.php |
— | — | @@ -1,12 +1,10 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -require_once dirname( __FILE__ ) . '/ApiSetup.php'; |
5 | | - |
6 | 4 | /** |
7 | 5 | * @group Database |
8 | 6 | * @group Destructive |
9 | 7 | */ |
10 | | -class ApiBlockTest extends ApiTestSetup { |
| 8 | +class ApiBlockTest extends ApiTestCase { |
11 | 9 | |
12 | 10 | function setUp() { |
13 | 11 | parent::setUp(); |
— | — | @@ -14,7 +12,7 @@ |
15 | 13 | } |
16 | 14 | |
17 | 15 | function getTokens() { |
18 | | - return $this->getTokenList( $this->sysopUser ); |
| 16 | + return $this->getTokenList( self::$users['sysop'] ); |
19 | 17 | } |
20 | 18 | |
21 | 19 | function addDBData() { |
Index: trunk/phase3/tests/phpunit/includes/api/ApiTestCase.php |
— | — | @@ -1,12 +1,17 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | abstract class ApiTestCase extends MediaWikiLangTestCase { |
| 5 | + /** |
| 6 | + * @var Array of ApiTestUser |
| 7 | + */ |
5 | 8 | public static $users; |
| 9 | + protected static $apiUrl; |
6 | 10 | |
7 | 11 | function setUp() { |
8 | | - global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser; |
| 12 | + global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser, $wgServer; |
9 | 13 | |
10 | 14 | parent::setUp(); |
| 15 | + self::$apiUrl = $wgServer . wfScript( 'api' ); |
11 | 16 | $wgMemc = new EmptyBagOStuff(); |
12 | 17 | $wgContLang = Language::factory( 'en' ); |
13 | 18 | $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' ); |
— | — | @@ -35,12 +40,18 @@ |
36 | 41 | if ( is_null( $session ) ) { |
37 | 42 | $session = array(); |
38 | 43 | } |
| 44 | + $_SESSION = $session; // paranoia |
39 | 45 | |
40 | 46 | $request = new FauxRequest( $params, true, $session ); |
41 | 47 | $module = new ApiMain( $request, true ); |
42 | 48 | $module->execute(); |
43 | 49 | |
44 | | - return array( $module->getResultData(), $request, $request->getSessionArray() ); |
| 50 | + $results = array( $module->getResultData(), $request, $request->getSessionArray() ); |
| 51 | + if( $appendModule ) { |
| 52 | + $results[] = $module; |
| 53 | + } |
| 54 | + |
| 55 | + return $results; |
45 | 56 | } |
46 | 57 | |
47 | 58 | /** |
— | — | @@ -62,4 +73,68 @@ |
63 | 74 | } |
64 | 75 | } |
65 | 76 | |
| 77 | + protected function doLogin() { |
| 78 | + $data = $this->doApiRequest( array( |
| 79 | + 'action' => 'login', |
| 80 | + 'lgname' => self::$users['sysop']->username, |
| 81 | + 'lgpassword' => self::$users['sysop']->password ) ); |
| 82 | + |
| 83 | + $token = $data[0]['login']['token']; |
| 84 | + |
| 85 | + $data = $this->doApiRequest( array( |
| 86 | + 'action' => 'login', |
| 87 | + 'lgtoken' => $token, |
| 88 | + 'lgname' => self::$users['sysop']->username, |
| 89 | + 'lgpassword' => self::$users['sysop']->password |
| 90 | + ), $data ); |
| 91 | + |
| 92 | + return $data; |
| 93 | + } |
| 94 | + |
| 95 | + protected function getTokenList( $user ) { |
| 96 | + $GLOBALS['wgUser'] = $user->user; |
| 97 | + $data = $this->doApiRequest( array( |
| 98 | + 'action' => 'query', |
| 99 | + 'titles' => 'Main Page', |
| 100 | + 'intoken' => 'edit|delete|protect|move|block|unblock', |
| 101 | + 'prop' => 'info' ) ); |
| 102 | + return $data; |
| 103 | + } |
66 | 104 | } |
| 105 | + |
| 106 | +class UserWrapper { |
| 107 | + public $userName, $password, $user; |
| 108 | + |
| 109 | + public function __construct( $userName, $password, $group = '' ) { |
| 110 | + $this->userName = $userName; |
| 111 | + $this->password = $password; |
| 112 | + |
| 113 | + $this->user = User::newFromName( $this->userName ); |
| 114 | + if ( !$this->user->getID() ) { |
| 115 | + $this->user = User::createNew( $this->userName, array( |
| 116 | + "email" => "test@example.com", |
| 117 | + "real_name" => "Test User" ) ); |
| 118 | + } |
| 119 | + $this->user->setPassword( $this->password ); |
| 120 | + |
| 121 | + if ( $group !== '' ) { |
| 122 | + $this->user->addGroup( $group ); |
| 123 | + } |
| 124 | + $this->user->saveSettings(); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +class MockApi extends ApiBase { |
| 129 | + public function execute() { } |
| 130 | + public function getVersion() { } |
| 131 | + |
| 132 | + public function __construct() { } |
| 133 | + |
| 134 | + public function getAllowedParams() { |
| 135 | + return array( |
| 136 | + 'filename' => null, |
| 137 | + 'enablechunks' => false, |
| 138 | + 'sessionkey' => null, |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
Index: trunk/phase3/tests/TestsAutoLoader.php |
— | — | @@ -14,10 +14,12 @@ |
15 | 15 | 'BlockTest' => "$testFolder/phpunit/includes/BlockTest.php", |
16 | 16 | |
17 | 17 | //API |
18 | | - 'ApiTestSetup' => "$testFolder/phpunit/includes/api/ApiSetup.php", |
| 18 | + 'ApiFormatTestBase' => "$testFolder/phpunit/includes/api/format/ApiFormatTestBase.php", |
19 | 19 | 'ApiTestCase' => "$testFolder/phpunit/includes/api/ApiTestCase.php", |
20 | 20 | 'ApiTestUser' => "$testFolder/phpunit/includes/api/ApiTestUser.php", |
| 21 | + 'MockApi' => "$testFolder/phpunit/includes/api/ApiTestCase.php", |
21 | 22 | 'RandomImageGenerator' => "$testFolder/phpunit/includes/api/RandomImageGenerator.php", |
| 23 | + 'UserWrapper' => "$testFolder/phpunit/includes/api/ApiTestCase.php", |
22 | 24 | |
23 | 25 | //Parser |
24 | 26 | 'ParserTestFileIterator' => "$testFolder/phpunit/includes/parser/NewParserHelpers.php", |