Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / api / ApiWatchTest.php
blob7b91094b6c35550eaa4667664099de60478c677a
1 <?php
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 * @todo This test suite is severly broken and need a full review
8 */
9 class ApiWatchTest extends ApiTestCase {
10 protected function setUp() {
11 parent::setUp();
12 $this->doLogin();
15 function getTokens() {
16 return $this->getTokenList( self::$users['sysop'] );
19 public function testWatchEdit() {
20 $tokens = $this->getTokens();
22 $data = $this->doApiRequest( [
23 'action' => 'edit',
24 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext
25 'text' => 'new text',
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'] );
32 return $data;
35 /**
36 * @depends testWatchEdit
38 public function testWatchClear() {
39 $tokens = $this->getTokens();
41 $data = $this->doApiRequest( [
42 'action' => 'query',
43 'wllimit' => 'max',
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( [
51 'action' => 'watch',
52 'title' => $page['title'],
53 'unwatch' => true,
54 'token' => $tokens['watchtoken'] ] );
57 $data = $this->doApiRequest( [
58 'action' => 'query',
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
65 // can't be cleared.
66 if ( strpos( $item['title'], ':' ) === 0 ) {
67 unset( $data[0]['query']['watchlist'][$index] );
70 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
72 return $data;
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() {
92 $this->getTokens();
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( [
99 'action' => 'query',
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] );
118 return $data;
122 * @group Broken
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];
133 try {
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" );
146 } else {
147 $this->fail( "Received error '" . $ue->getMessage() . "'" );