7 * @todo This test suite is severly broken and need a full review
9 class ApiWatchTest extends ApiTestCase {
10 protected function setUp() {
15 function getTokens() {
16 return $this->getTokenList( self::$users['sysop'] );
19 public function testWatchEdit() {
20 $tokens = $this->getTokens();
22 $data = $this->doApiRequest( [
24 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext
26 'token' => $tokens['edittoken'],
27 'watchlist' => 'watch' ] );
28 $this->assertArrayHasKey( 'edit', $data[0] );
29 $this->assertArrayHasKey( 'result', $data[0]['edit'] );
30 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
36 * @depends testWatchEdit
38 public function testWatchClear() {
39 $tokens = $this->getTokens();
41 $data = $this->doApiRequest( [
44 'list' => 'watchlist' ] );
46 if ( isset( $data[0]['query']['watchlist'] ) ) {
47 $wl = $data[0]['query']['watchlist'];
49 foreach ( $wl as $page ) {
50 $data = $this->doApiRequest( [
52 'title' => $page['title'],
54 'token' => $tokens['watchtoken'] ] );
57 $data = $this->doApiRequest( [
59 'list' => 'watchlist' ], $data );
60 $this->assertArrayHasKey( 'query', $data[0] );
61 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
62 foreach ( $data[0]['query']['watchlist'] as $index => $item ) {
63 // Previous tests may insert an invalid title
64 // like ":ApiEditPageTest testNonTextEdit", which
66 if ( strpos( $item['title'], ':' ) === 0 ) {
67 unset( $data[0]['query']['watchlist'][$index] );
70 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
75 public function testWatchProtect() {
76 $tokens = $this->getTokens();
78 $data = $this->doApiRequest( [
79 'action' => 'protect',
80 'token' => $tokens['protecttoken'],
81 'title' => 'Help:UTPage',
82 'protections' => 'edit=sysop',
83 'watchlist' => 'unwatch' ] );
85 $this->assertArrayHasKey( 'protect', $data[0] );
86 $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
87 $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
88 $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
91 public function testGetRollbackToken() {
94 if ( !Title::newFromText( 'Help:UTPage' )->exists() ) {
95 $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); // TODO: just create it?
98 $data = $this->doApiRequest( [
100 'prop' => 'revisions',
101 'titles' => 'Help:UTPage',
102 'rvtoken' => 'rollback' ] );
104 $this->assertArrayHasKey( 'query', $data[0] );
105 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
106 $keys = array_keys( $data[0]['query']['pages'] );
107 $key = array_pop( $keys );
109 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
110 $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" );
113 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
114 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
115 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
116 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
123 * Broken because there is currently no revision info in the $pageinfo
125 * @depends testGetRollbackToken
127 public function testWatchRollback( $data ) {
128 $keys = array_keys( $data[0]['query']['pages'] );
129 $key = array_pop( $keys );
130 $pageinfo = $data[0]['query']['pages'][$key];
131 $revinfo = $pageinfo['revisions'][0];
134 $data = $this->doApiRequest( [
135 'action' => 'rollback',
136 'title' => 'Help:UTPage',
137 'user' => $revinfo['user'],
138 'token' => $pageinfo['rollbacktoken'],
139 'watchlist' => 'watch' ] );
141 $this->assertArrayHasKey( 'rollback', $data[0] );
142 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
143 } catch ( ApiUsageException $ue ) {
144 if ( self::apiExceptionHasCode( $ue, 'onlyauthor' ) ) {
145 $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" );
147 $this->fail( "Received error '" . $ue->getMessage() . "'" );