If Main page is missing for testGetRollbackToken, mark as imcomplete (bloody unit...
[mediawiki.git] / maintenance / tests / phpunit / includes / api / ApiWatchTest.php
blobcf6467864ccf57c4b1a9d336d5ad8280b1519161
1 <?php
3 require_once dirname( __FILE__ ) . '/ApiSetup.php';
5 class ApiWatchTest extends ApiTestSetup {
7 function setUp() {
8 parent::setUp();
11 function testLogin() {
12 $data = $this->doApiRequest( array(
13 'action' => 'login',
14 'lgname' => self::$sysopUser->userName,
15 'lgpassword' => self::$sysopUser->password ) );
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'];
22 $data = $this->doApiRequest( array(
23 'action' => 'login',
24 "lgtoken" => $token,
25 "lgname" => self::$sysopUser->userName,
26 "lgpassword" => self::$sysopUser->password ), $data );
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'] );
33 return $data;
36 function testGettingToken() {
37 foreach ( array( self::$user, self::$sysopUser ) as $user ) {
38 $this->getUserTokens( $user );
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' ) );
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 );
55 $rights = $user->user->getRights();
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] );
61 if ( isset( $rights['delete'] ) ) {
62 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
65 if ( isset( $rights['block'] ) ) {
66 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
67 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
70 if ( isset( $rights['protect'] ) ) {
71 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
74 return $data;
77 function testGetToken() {
78 return $this->getUserTokens( self::$sysopUser );
81 /**
82 * @depends testGetToken
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];
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'] );
100 return $data;
104 * @depends testWatchEdit
106 function testWatchClear( $data ) {
107 $data = $this->doApiRequest( array(
108 'action' => 'query',
109 'list' => 'watchlist' ), $data );
111 if ( isset( $data[0]['query']['watchlist'] ) ) {
112 $wl = $data[0]['query']['watchlist'];
114 foreach ( $wl as $page ) {
115 $data = $this->doApiRequest( array(
116 'action' => 'watch',
117 'title' => $page['title'],
118 'unwatch' => true ), $data );
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'] ) );
128 return $data;
132 * @depends testGetToken
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];
140 $data = $this->doApiRequest( array(
141 'action' => 'protect',
142 'token' => $pageinfo['protecttoken'],
143 'title' => 'Main Page',
144 'protections' => 'edit=sysop',
145 'watchlist' => 'unwatch' ), $data );
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] );
154 * @depends testGetToken
156 function testGetRollbackToken( $data ) {
157 $data = $this->doApiRequest( array(
158 'action' => 'query',
159 'prop' => 'revisions',
160 'titles' => 'Main Page',
161 'rvtoken' => 'rollback' ), $data );
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 );
168 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
169 $this->markTestIncomplete( "Target page (Main Page) doesn't exist" );
172 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
173 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
174 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
175 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
177 return $data;
181 * @depends testGetRollbackToken
183 function testWatchRollback( $data ) {
184 $keys = array_keys( $data[0]['query']['pages'] );
185 $key = array_pop( $keys );
186 $pageinfo = $data[0]['query']['pages'][$key]['revisions'][0];
188 try {
189 $data = $this->doApiRequest( array(
190 'action' => 'rollback',
191 'title' => 'Main Page',
192 'user' => self::$user->userName,
193 'token' => $pageinfo['rollbacktoken'],
194 'watchlist' => 'watch' ), $data );
195 } catch( UsageException $ue ) {
196 if( $ue->getCodeString() == 'onlyauthor' ) {
197 $this->markTestIncomplete( "Only one author to 'Main Page', cannot test rollback" );
200 $this->assertArrayHasKey( 'rollback', $data[0] );
201 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
205 * @depends testGetToken
207 function testWatchDelete( $data ) {
208 $this->markTestIncomplete( "Broken" );
209 $keys = array_keys( $data[0]['query']['pages'] );
210 $key = array_pop( $keys );
211 $pageinfo = $data[0]['query']['pages'][$key];
213 $data = $this->doApiRequest( array(
214 'action' => 'delete',
215 'token' => $pageinfo['deletetoken'],
216 'title' => 'Main Page' ), $data );
217 $this->assertArrayHasKey( 'delete', $data[0] );
218 $this->assertArrayHasKey( 'title', $data[0]['delete'] );
220 $data = $this->doApiRequest( array(
221 'action' => 'query',
222 'list' => 'watchlist' ), $data );
224 $this->markTestIncomplete( 'This test needs to verify the deleted article was added to the users watchlist' );