Refactor ApiTestCase to get token from ApiQueryTokens
[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 * @covers ApiWatch
10 */
11 class ApiWatchTest extends ApiTestCase {
12 protected function setUp() {
13 parent::setUp();
14 $this->doLogin();
15 }
16
17 function getTokens() {
18 return $this->getTokenList( self::$users['sysop'] );
19 }
20
21 public function testWatchEdit() {
22 $tokens = $this->getTokens();
23
24 $data = $this->doApiRequest( [
25 'action' => 'edit',
26 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext
27 'text' => 'new text',
28 'token' => $tokens['edittoken'],
29 'watchlist' => 'watch' ] );
30 $this->assertArrayHasKey( 'edit', $data[0] );
31 $this->assertArrayHasKey( 'result', $data[0]['edit'] );
32 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
33
34 return $data;
35 }
36
37 /**
38 * @depends testWatchEdit
39 */
40 public function testWatchClear() {
41 $tokens = $this->getTokens();
42
43 $data = $this->doApiRequest( [
44 'action' => 'query',
45 'wllimit' => 'max',
46 'list' => 'watchlist' ] );
47
48 if ( isset( $data[0]['query']['watchlist'] ) ) {
49 $wl = $data[0]['query']['watchlist'];
50
51 foreach ( $wl as $page ) {
52 $data = $this->doApiRequest( [
53 'action' => 'watch',
54 'title' => $page['title'],
55 'unwatch' => true,
56 'token' => $tokens['watchtoken'] ] );
57 }
58 }
59 $data = $this->doApiRequest( [
60 'action' => 'query',
61 'list' => 'watchlist' ], $data );
62 $this->assertArrayHasKey( 'query', $data[0] );
63 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
64 foreach ( $data[0]['query']['watchlist'] as $index => $item ) {
65 // Previous tests may insert an invalid title
66 // like ":ApiEditPageTest testNonTextEdit", which
67 // can't be cleared.
68 if ( strpos( $item['title'], ':' ) === 0 ) {
69 unset( $data[0]['query']['watchlist'][$index] );
70 }
71 }
72 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
73
74 return $data;
75 }
76
77 public function testWatchProtect() {
78 $tokens = $this->getTokens();
79
80 $data = $this->doApiRequest( [
81 'action' => 'protect',
82 'token' => $tokens['protecttoken'],
83 'title' => 'Help:UTPage',
84 'protections' => 'edit=sysop',
85 'watchlist' => 'unwatch' ] );
86
87 $this->assertArrayHasKey( 'protect', $data[0] );
88 $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
89 $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
90 $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
91 }
92
93 public function testGetRollbackToken() {
94 $this->getTokens();
95
96 if ( !Title::newFromText( 'Help:UTPage' )->exists() ) {
97 $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); // TODO: just create it?
98 }
99
100 $data = $this->doApiRequest( [
101 'action' => 'query',
102 'prop' => 'revisions',
103 'titles' => 'Help:UTPage',
104 'rvtoken' => 'rollback' ] );
105
106 $this->assertArrayHasKey( 'query', $data[0] );
107 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
108 $keys = array_keys( $data[0]['query']['pages'] );
109 $key = array_pop( $keys );
110
111 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
112 $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" );
113 }
114
115 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
116 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
117 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
118 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
119
120 return $data;
121 }
122
123 /**
124 * @group Broken
125 * Broken because there is currently no revision info in the $pageinfo
126 *
127 * @depends testGetRollbackToken
128 */
129 public function testWatchRollback( $data ) {
130 $keys = array_keys( $data[0]['query']['pages'] );
131 $key = array_pop( $keys );
132 $pageinfo = $data[0]['query']['pages'][$key];
133 $revinfo = $pageinfo['revisions'][0];
134
135 try {
136 $data = $this->doApiRequest( [
137 'action' => 'rollback',
138 'title' => 'Help:UTPage',
139 'user' => $revinfo['user'],
140 'token' => $pageinfo['rollbacktoken'],
141 'watchlist' => 'watch' ] );
142
143 $this->assertArrayHasKey( 'rollback', $data[0] );
144 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
145 } catch ( ApiUsageException $ue ) {
146 if ( self::apiExceptionHasCode( $ue, 'onlyauthor' ) ) {
147 $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" );
148 } else {
149 $this->fail( "Received error '" . $ue->getMessage() . "'" );
150 }
151 }
152 }
153 }