Per wikitech-l discussion: Move tests from maintenance/tests/ to tests/. They're...
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiWatchTest.php
1 <?php
2
3 require_once dirname( __FILE__ ) . '/ApiSetup.php';
4
5 /**
6 * @group Database
7 * @group Destructive
8 */
9 class ApiWatchTest extends ApiTestSetup {
10
11 function setUp() {
12 parent::setUp();
13 }
14
15 function testLogin() {
16 $data = $this->doApiRequest( array(
17 'action' => 'login',
18 'lgname' => self::$sysopUser->userName,
19 'lgpassword' => self::$sysopUser->password ) );
20
21 $this->assertArrayHasKey( "login", $data[0] );
22 $this->assertArrayHasKey( "result", $data[0]['login'] );
23 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
24 $token = $data[0]['login']['token'];
25
26 $data = $this->doApiRequest( array(
27 'action' => 'login',
28 "lgtoken" => $token,
29 "lgname" => self::$sysopUser->userName,
30 "lgpassword" => self::$sysopUser->password ), $data );
31
32 $this->assertArrayHasKey( "login", $data[0] );
33 $this->assertArrayHasKey( "result", $data[0]['login'] );
34 $this->assertEquals( "Success", $data[0]['login']['result'] );
35 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
36
37 return $data;
38 }
39
40 function testGettingToken() {
41 foreach ( array( self::$user, self::$sysopUser ) as $user ) {
42 $this->getUserTokens( $user );
43 }
44 }
45
46 function getUserTokens( $user ) {
47 $GLOBALS['wgUser'] = $user->user;
48 $data = $this->doApiRequest( array(
49 'action' => 'query',
50 'titles' => 'Main Page',
51 'intoken' => 'edit|delete|protect|move|block|unblock',
52 'prop' => 'info' ) );
53
54 $this->assertArrayHasKey( 'query', $data[0] );
55 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
56 $keys = array_keys( $data[0]['query']['pages'] );
57 $key = array_pop( $keys );
58
59 $rights = $user->user->getRights();
60
61 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
62 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
63 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
64
65 if ( isset( $rights['delete'] ) ) {
66 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
67 }
68
69 if ( isset( $rights['block'] ) ) {
70 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
71 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
72 }
73
74 if ( isset( $rights['protect'] ) ) {
75 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
76 }
77
78 return $data;
79 }
80
81 function testGetToken() {
82 return $this->getUserTokens( self::$sysopUser );
83 }
84
85 /**
86 * @depends testGetToken
87 */
88 function testWatchEdit( $data ) {
89 $this->markTestIncomplete( "Broken" );
90 $keys = array_keys( $data[0]['query']['pages'] );
91 $key = array_pop( $keys );
92 $pageinfo = $data[0]['query']['pages'][$key];
93
94 $data = $this->doApiRequest( array(
95 'action' => 'edit',
96 'title' => 'Main Page',
97 'text' => 'new text',
98 'token' => $pageinfo['edittoken'],
99 'watchlist' => 'watch' ), $data );
100 $this->assertArrayHasKey( 'edit', $data[0] );
101 $this->assertArrayHasKey( 'result', $data[0]['edit'] );
102 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
103
104 return $data;
105 }
106
107 /**
108 * @depends testWatchEdit
109 */
110 function testWatchClear( $data ) {
111 $data = $this->doApiRequest( array(
112 'action' => 'query',
113 'list' => 'watchlist' ), $data );
114
115 if ( isset( $data[0]['query']['watchlist'] ) ) {
116 $wl = $data[0]['query']['watchlist'];
117
118 foreach ( $wl as $page ) {
119 $data = $this->doApiRequest( array(
120 'action' => 'watch',
121 'title' => $page['title'],
122 'unwatch' => true ), $data );
123 }
124 }
125 $data = $this->doApiRequest( array(
126 'action' => 'query',
127 'list' => 'watchlist' ), $data );
128 $this->assertArrayHasKey( 'query', $data[0] );
129 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
130 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
131
132 return $data;
133 }
134
135 /**
136 * @depends testGetToken
137 */
138 function testWatchProtect( $data ) {
139 $this->markTestIncomplete( "Broken" );
140 $keys = array_keys( $data[0]['query']['pages'] );
141 $key = array_pop( $keys );
142 $pageinfo = $data[0]['query']['pages'][$key];
143
144 $data = $this->doApiRequest( array(
145 'action' => 'protect',
146 'token' => $pageinfo['protecttoken'],
147 'title' => 'Main Page',
148 'protections' => 'edit=sysop',
149 'watchlist' => 'unwatch' ), $data );
150
151 $this->assertArrayHasKey( 'protect', $data[0] );
152 $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
153 $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
154 $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
155 }
156
157 /**
158 * @depends testGetToken
159 */
160 function testGetRollbackToken( $data ) {
161 if ( !Title::newFromText( 'Main Page' )->exists() ) {
162 $this->markTestIncomplete( "The article [[Main Page]] does not exist" );
163 }
164
165 $data = $this->doApiRequest( array(
166 'action' => 'query',
167 'prop' => 'revisions',
168 'titles' => 'Main Page',
169 'rvtoken' => 'rollback' ), $data );
170
171 $this->assertArrayHasKey( 'query', $data[0] );
172 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
173 $keys = array_keys( $data[0]['query']['pages'] );
174 $key = array_pop( $keys );
175
176 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
177 $this->markTestIncomplete( "Target page (Main Page) doesn't exist" );
178 }
179
180 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
181 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
182 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
183 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
184
185 return $data;
186 }
187
188 /**
189 * @depends testGetRollbackToken
190 */
191 function testWatchRollback( $data ) {
192 $keys = array_keys( $data[0]['query']['pages'] );
193 $key = array_pop( $keys );
194 $pageinfo = $data[0]['query']['pages'][$key]['revisions'][0];
195
196 try {
197 $data = $this->doApiRequest( array(
198 'action' => 'rollback',
199 'title' => 'Main Page',
200 'user' => $pageinfo['user'],
201 'token' => $pageinfo['rollbacktoken'],
202 'watchlist' => 'watch' ), $data );
203 } catch( UsageException $ue ) {
204 if( $ue->getCodeString() == 'onlyauthor' ) {
205 $this->markTestIncomplete( "Only one author to 'Main Page', cannot test rollback" );
206 } else {
207 $this->fail( "Received error " . $ue->getCodeString() );
208 }
209 }
210
211 $this->assertArrayHasKey( 'rollback', $data[0] );
212 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
213 }
214
215 /**
216 * @depends testGetToken
217 */
218 function testWatchDelete( $data ) {
219 $this->markTestIncomplete( "Broken" );
220 $keys = array_keys( $data[0]['query']['pages'] );
221 $key = array_pop( $keys );
222 $pageinfo = $data[0]['query']['pages'][$key];
223
224 $data = $this->doApiRequest( array(
225 'action' => 'delete',
226 'token' => $pageinfo['deletetoken'],
227 'title' => 'Main Page' ), $data );
228 $this->assertArrayHasKey( 'delete', $data[0] );
229 $this->assertArrayHasKey( 'title', $data[0]['delete'] );
230
231 $data = $this->doApiRequest( array(
232 'action' => 'query',
233 'list' => 'watchlist' ), $data );
234
235 $this->markTestIncomplete( 'This test needs to verify the deleted article was added to the users watchlist' );
236 }
237 }