MediaWiki
REL1_21
|
00001 <?php 00002 00009 class ApiWatchTest extends ApiTestCase { 00010 00011 protected function setUp() { 00012 parent::setUp(); 00013 $this->doLogin(); 00014 } 00015 00016 function getTokens() { 00017 $data = $this->getTokenList( self::$users['sysop'] ); 00018 00019 $keys = array_keys( $data[0]['query']['pages'] ); 00020 $key = array_pop( $keys ); 00021 $pageinfo = $data[0]['query']['pages'][$key]; 00022 00023 return $pageinfo; 00024 } 00025 00028 function testWatchEdit() { 00029 $pageinfo = $this->getTokens(); 00030 00031 $data = $this->doApiRequest( array( 00032 'action' => 'edit', 00033 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext 00034 'text' => 'new text', 00035 'token' => $pageinfo['edittoken'], 00036 'watchlist' => 'watch' ) ); 00037 $this->assertArrayHasKey( 'edit', $data[0] ); 00038 $this->assertArrayHasKey( 'result', $data[0]['edit'] ); 00039 $this->assertEquals( 'Success', $data[0]['edit']['result'] ); 00040 00041 return $data; 00042 } 00043 00047 function testWatchClear() { 00048 00049 $pageinfo = $this->getTokens(); 00050 00051 $data = $this->doApiRequest( array( 00052 'action' => 'query', 00053 'list' => 'watchlist' ) ); 00054 00055 if ( isset( $data[0]['query']['watchlist'] ) ) { 00056 $wl = $data[0]['query']['watchlist']; 00057 00058 foreach ( $wl as $page ) { 00059 $data = $this->doApiRequest( array( 00060 'action' => 'watch', 00061 'title' => $page['title'], 00062 'unwatch' => true, 00063 'token' => $pageinfo['watchtoken'] ) ); 00064 } 00065 } 00066 $data = $this->doApiRequest( array( 00067 'action' => 'query', 00068 'list' => 'watchlist' ), $data ); 00069 $this->assertArrayHasKey( 'query', $data[0] ); 00070 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] ); 00071 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) ); 00072 00073 return $data; 00074 } 00075 00078 function testWatchProtect() { 00079 00080 $pageinfo = $this->getTokens(); 00081 00082 $data = $this->doApiRequest( array( 00083 'action' => 'protect', 00084 'token' => $pageinfo['protecttoken'], 00085 'title' => 'Help:UTPage', 00086 'protections' => 'edit=sysop', 00087 'watchlist' => 'unwatch' ) ); 00088 00089 $this->assertArrayHasKey( 'protect', $data[0] ); 00090 $this->assertArrayHasKey( 'protections', $data[0]['protect'] ); 00091 $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) ); 00092 $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] ); 00093 } 00094 00097 function testGetRollbackToken() { 00098 00099 $pageinfo = $this->getTokens(); 00100 00101 if ( !Title::newFromText( 'Help:UTPage' )->exists() ) { 00102 $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); //TODO: just create it? 00103 } 00104 00105 $data = $this->doApiRequest( array( 00106 'action' => 'query', 00107 'prop' => 'revisions', 00108 'titles' => 'Help:UTPage', 00109 'rvtoken' => 'rollback' ) ); 00110 00111 $this->assertArrayHasKey( 'query', $data[0] ); 00112 $this->assertArrayHasKey( 'pages', $data[0]['query'] ); 00113 $keys = array_keys( $data[0]['query']['pages'] ); 00114 $key = array_pop( $keys ); 00115 00116 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) { 00117 $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" ); 00118 } 00119 00120 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] ); 00121 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] ); 00122 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] ); 00123 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] ); 00124 00125 return $data; 00126 } 00127 00134 function testWatchRollback( $data ) { 00135 $keys = array_keys( $data[0]['query']['pages'] ); 00136 $key = array_pop( $keys ); 00137 $pageinfo = $data[0]['query']['pages'][$key]; 00138 $revinfo = $pageinfo['revisions'][0]; 00139 00140 try { 00141 $data = $this->doApiRequest( array( 00142 'action' => 'rollback', 00143 'title' => 'Help:UTPage', 00144 'user' => $revinfo['user'], 00145 'token' => $pageinfo['rollbacktoken'], 00146 'watchlist' => 'watch' ) ); 00147 00148 $this->assertArrayHasKey( 'rollback', $data[0] ); 00149 $this->assertArrayHasKey( 'title', $data[0]['rollback'] ); 00150 } catch ( UsageException $ue ) { 00151 if ( $ue->getCodeString() == 'onlyauthor' ) { 00152 $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" ); 00153 } else { 00154 $this->fail( "Received error '" . $ue->getCodeString() . "'" ); 00155 } 00156 } 00157 } 00158 00161 function testWatchDelete() { 00162 $pageinfo = $this->getTokens(); 00163 00164 $data = $this->doApiRequest( array( 00165 'action' => 'delete', 00166 'token' => $pageinfo['deletetoken'], 00167 'title' => 'Help:UTPage' ) ); 00168 $this->assertArrayHasKey( 'delete', $data[0] ); 00169 $this->assertArrayHasKey( 'title', $data[0]['delete'] ); 00170 00171 $data = $this->doApiRequest( array( 00172 'action' => 'query', 00173 'list' => 'watchlist' ) ); 00174 00175 $this->markTestIncomplete( 'This test needs to verify the deleted article was added to the users watchlist' ); 00176 } 00177 }