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