MediaWiki
REL1_24
|
00001 <?php 00002 00009 class ApiWatchTest extends ApiTestCase { 00010 protected function setUp() { 00011 parent::setUp(); 00012 $this->doLogin(); 00013 } 00014 00015 function getTokens() { 00016 return $this->getTokenList( self::$users['sysop'] ); 00017 } 00018 00021 public function testWatchEdit() { 00022 $tokens = $this->getTokens(); 00023 00024 $data = $this->doApiRequest( array( 00025 'action' => 'edit', 00026 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext 00027 'text' => 'new text', 00028 'token' => $tokens['edittoken'], 00029 'watchlist' => 'watch' ) ); 00030 $this->assertArrayHasKey( 'edit', $data[0] ); 00031 $this->assertArrayHasKey( 'result', $data[0]['edit'] ); 00032 $this->assertEquals( 'Success', $data[0]['edit']['result'] ); 00033 00034 return $data; 00035 } 00036 00040 public function testWatchClear() { 00041 $tokens = $this->getTokens(); 00042 00043 $data = $this->doApiRequest( array( 00044 'action' => 'query', 00045 'wllimit' => 'max', 00046 'list' => 'watchlist' ) ); 00047 00048 if ( isset( $data[0]['query']['watchlist'] ) ) { 00049 $wl = $data[0]['query']['watchlist']; 00050 00051 foreach ( $wl as $page ) { 00052 $data = $this->doApiRequest( array( 00053 'action' => 'watch', 00054 'title' => $page['title'], 00055 'unwatch' => true, 00056 'token' => $tokens['watchtoken'] ) ); 00057 } 00058 } 00059 $data = $this->doApiRequest( array( 00060 'action' => 'query', 00061 'list' => 'watchlist' ), $data ); 00062 $this->assertArrayHasKey( 'query', $data[0] ); 00063 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] ); 00064 foreach ( $data[0]['query']['watchlist'] as $index => $item ) { 00065 // Previous tests may insert an invalid title 00066 // like ":ApiEditPageTest testNonTextEdit", which 00067 // can't be cleared. 00068 if ( strpos( $item['title'], ':' ) === 0 ) { 00069 unset( $data[0]['query']['watchlist'][$index] ); 00070 } 00071 } 00072 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) ); 00073 00074 return $data; 00075 } 00076 00079 public function testWatchProtect() { 00080 $tokens = $this->getTokens(); 00081 00082 $data = $this->doApiRequest( array( 00083 'action' => 'protect', 00084 'token' => $tokens['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 public function testGetRollbackToken() { 00098 $this->getTokens(); 00099 00100 if ( !Title::newFromText( 'Help:UTPage' )->exists() ) { 00101 $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); //TODO: just create it? 00102 } 00103 00104 $data = $this->doApiRequest( array( 00105 'action' => 'query', 00106 'prop' => 'revisions', 00107 'titles' => 'Help:UTPage', 00108 'rvtoken' => 'rollback' ) ); 00109 00110 $this->assertArrayHasKey( 'query', $data[0] ); 00111 $this->assertArrayHasKey( 'pages', $data[0]['query'] ); 00112 $keys = array_keys( $data[0]['query']['pages'] ); 00113 $key = array_pop( $keys ); 00114 00115 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) { 00116 $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" ); 00117 } 00118 00119 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] ); 00120 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] ); 00121 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] ); 00122 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] ); 00123 00124 return $data; 00125 } 00126 00133 public function testWatchRollback( $data ) { 00134 $keys = array_keys( $data[0]['query']['pages'] ); 00135 $key = array_pop( $keys ); 00136 $pageinfo = $data[0]['query']['pages'][$key]; 00137 $revinfo = $pageinfo['revisions'][0]; 00138 00139 try { 00140 $data = $this->doApiRequest( array( 00141 'action' => 'rollback', 00142 'title' => 'Help:UTPage', 00143 'user' => $revinfo['user'], 00144 'token' => $pageinfo['rollbacktoken'], 00145 'watchlist' => 'watch' ) ); 00146 00147 $this->assertArrayHasKey( 'rollback', $data[0] ); 00148 $this->assertArrayHasKey( 'title', $data[0]['rollback'] ); 00149 } catch ( UsageException $ue ) { 00150 if ( $ue->getCodeString() == 'onlyauthor' ) { 00151 $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" ); 00152 } else { 00153 $this->fail( "Received error '" . $ue->getCodeString() . "'" ); 00154 } 00155 } 00156 } 00157 }