MediaWiki  REL1_22
ApiWatchTest.php
Go to the documentation of this file.
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             'list' => 'watchlist' ) );
00046 
00047         if ( isset( $data[0]['query']['watchlist'] ) ) {
00048             $wl = $data[0]['query']['watchlist'];
00049 
00050             foreach ( $wl as $page ) {
00051                 $data = $this->doApiRequest( array(
00052                     'action' => 'watch',
00053                     'title' => $page['title'],
00054                     'unwatch' => true,
00055                     'token' => $tokens['watchtoken'] ) );
00056             }
00057         }
00058         $data = $this->doApiRequest( array(
00059             'action' => 'query',
00060             'list' => 'watchlist' ), $data );
00061         $this->assertArrayHasKey( 'query', $data[0] );
00062         $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
00063         $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
00064 
00065         return $data;
00066     }
00067 
00070     public function testWatchProtect() {
00071         $tokens = $this->getTokens();
00072 
00073         $data = $this->doApiRequest( array(
00074             'action' => 'protect',
00075             'token' => $tokens['protecttoken'],
00076             'title' => 'Help:UTPage',
00077             'protections' => 'edit=sysop',
00078             'watchlist' => 'unwatch' ) );
00079 
00080         $this->assertArrayHasKey( 'protect', $data[0] );
00081         $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
00082         $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
00083         $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
00084     }
00085 
00088     public function testGetRollbackToken() {
00089         $this->getTokens();
00090 
00091         if ( !Title::newFromText( 'Help:UTPage' )->exists() ) {
00092             $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); //TODO: just create it?
00093         }
00094 
00095         $data = $this->doApiRequest( array(
00096             'action' => 'query',
00097             'prop' => 'revisions',
00098             'titles' => 'Help:UTPage',
00099             'rvtoken' => 'rollback' ) );
00100 
00101         $this->assertArrayHasKey( 'query', $data[0] );
00102         $this->assertArrayHasKey( 'pages', $data[0]['query'] );
00103         $keys = array_keys( $data[0]['query']['pages'] );
00104         $key = array_pop( $keys );
00105 
00106         if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
00107             $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" );
00108         }
00109 
00110         $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
00111         $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
00112         $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
00113         $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
00114 
00115         return $data;
00116     }
00117 
00124     public function testWatchRollback( $data ) {
00125         $keys = array_keys( $data[0]['query']['pages'] );
00126         $key = array_pop( $keys );
00127         $pageinfo = $data[0]['query']['pages'][$key];
00128         $revinfo = $pageinfo['revisions'][0];
00129 
00130         try {
00131             $data = $this->doApiRequest( array(
00132                 'action' => 'rollback',
00133                 'title' => 'Help:UTPage',
00134                 'user' => $revinfo['user'],
00135                 'token' => $pageinfo['rollbacktoken'],
00136                 'watchlist' => 'watch' ) );
00137 
00138             $this->assertArrayHasKey( 'rollback', $data[0] );
00139             $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
00140         } catch ( UsageException $ue ) {
00141             if ( $ue->getCodeString() == 'onlyauthor' ) {
00142                 $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" );
00143             } else {
00144                 $this->fail( "Received error '" . $ue->getCodeString() . "'" );
00145             }
00146         }
00147     }
00148 }