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