[MCR] pass $queryFlags into RevisionStore::getTitle
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiWatchTest.php
1 <?php
2
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();
13 }
14
15 function getTokens() {
16 return $this->getTokenList( self::$users['sysop'] );
17 }
18
19 public function testWatchEdit() {
20 $tokens = $this->getTokens();
21
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'] );
31
32 return $data;
33 }
34
35 /**
36 * @depends testWatchEdit
37 */
38 public function testWatchClear() {
39 $tokens = $this->getTokens();
40
41 $data = $this->doApiRequest( [
42 'action' => 'query',
43 'wllimit' => 'max',
44 'list' => 'watchlist' ] );
45
46 if ( isset( $data[0]['query']['watchlist'] ) ) {
47 $wl = $data[0]['query']['watchlist'];
48
49 foreach ( $wl as $page ) {
50 $data = $this->doApiRequest( [
51 'action' => 'watch',
52 'title' => $page['title'],
53 'unwatch' => true,
54 'token' => $tokens['watchtoken'] ] );
55 }
56 }
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] );
68 }
69 }
70 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
71
72 return $data;
73 }
74
75 public function testWatchProtect() {
76 $tokens = $this->getTokens();
77
78 $data = $this->doApiRequest( [
79 'action' => 'protect',
80 'token' => $tokens['protecttoken'],
81 'title' => 'Help:UTPage',
82 'protections' => 'edit=sysop',
83 'watchlist' => 'unwatch' ] );
84
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] );
89 }
90
91 public function testGetRollbackToken() {
92 $this->getTokens();
93
94 if ( !Title::newFromText( 'Help:UTPage' )->exists() ) {
95 $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); // TODO: just create it?
96 }
97
98 $data = $this->doApiRequest( [
99 'action' => 'query',
100 'prop' => 'revisions',
101 'titles' => 'Help:UTPage',
102 'rvtoken' => 'rollback' ] );
103
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 );
108
109 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
110 $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" );
111 }
112
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] );
117
118 return $data;
119 }
120
121 /**
122 * @group Broken
123 * Broken because there is currently no revision info in the $pageinfo
124 *
125 * @depends testGetRollbackToken
126 */
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];
132
133 try {
134 $data = $this->doApiRequest( [
135 'action' => 'rollback',
136 'title' => 'Help:UTPage',
137 'user' => $revinfo['user'],
138 'token' => $pageinfo['rollbacktoken'],
139 'watchlist' => 'watch' ] );
140
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() . "'" );
148 }
149 }
150 }
151 }