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