Merge "HTMLCheckMatrix support for forcing options on/off"
[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 $data = $this->getTokenList( self::$users['sysop'] );
17
18 $keys = array_keys( $data[0]['query']['pages'] );
19 $key = array_pop( $keys );
20 $pageinfo = $data[0]['query']['pages'][$key];
21
22 return $pageinfo;
23 }
24
25 /**
26 */
27 function testWatchEdit() {
28 $pageinfo = $this->getTokens();
29
30 $data = $this->doApiRequest( array(
31 'action' => 'edit',
32 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext
33 'text' => 'new text',
34 'token' => $pageinfo['edittoken'],
35 'watchlist' => 'watch' ) );
36 $this->assertArrayHasKey( 'edit', $data[0] );
37 $this->assertArrayHasKey( 'result', $data[0]['edit'] );
38 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
39
40 return $data;
41 }
42
43 /**
44 * @depends testWatchEdit
45 */
46 function testWatchClear() {
47
48 $pageinfo = $this->getTokens();
49
50 $data = $this->doApiRequest( array(
51 'action' => 'query',
52 'list' => 'watchlist' ) );
53
54 if ( isset( $data[0]['query']['watchlist'] ) ) {
55 $wl = $data[0]['query']['watchlist'];
56
57 foreach ( $wl as $page ) {
58 $data = $this->doApiRequest( array(
59 'action' => 'watch',
60 'title' => $page['title'],
61 'unwatch' => true,
62 'token' => $pageinfo['watchtoken'] ) );
63 }
64 }
65 $data = $this->doApiRequest( array(
66 'action' => 'query',
67 'list' => 'watchlist' ), $data );
68 $this->assertArrayHasKey( 'query', $data[0] );
69 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
70 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
71
72 return $data;
73 }
74
75 /**
76 */
77 function testWatchProtect() {
78
79 $pageinfo = $this->getTokens();
80
81 $data = $this->doApiRequest( array(
82 'action' => 'protect',
83 'token' => $pageinfo['protecttoken'],
84 'title' => 'Help:UTPage',
85 'protections' => 'edit=sysop',
86 'watchlist' => 'unwatch' ) );
87
88 $this->assertArrayHasKey( 'protect', $data[0] );
89 $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
90 $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
91 $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
92 }
93
94 /**
95 */
96 function testGetRollbackToken() {
97 $this->getTokens();
98
99 if ( !Title::newFromText( 'Help:UTPage' )->exists() ) {
100 $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); //TODO: just create it?
101 }
102
103 $data = $this->doApiRequest( array(
104 'action' => 'query',
105 'prop' => 'revisions',
106 'titles' => 'Help:UTPage',
107 'rvtoken' => 'rollback' ) );
108
109 $this->assertArrayHasKey( 'query', $data[0] );
110 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
111 $keys = array_keys( $data[0]['query']['pages'] );
112 $key = array_pop( $keys );
113
114 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
115 $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" );
116 }
117
118 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
119 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
120 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
121 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
122
123 return $data;
124 }
125
126 /**
127 * @group Broken
128 * Broken because there is currently no revision info in the $pageinfo
129 *
130 * @depends testGetRollbackToken
131 */
132 function testWatchRollback( $data ) {
133 $keys = array_keys( $data[0]['query']['pages'] );
134 $key = array_pop( $keys );
135 $pageinfo = $data[0]['query']['pages'][$key];
136 $revinfo = $pageinfo['revisions'][0];
137
138 try {
139 $data = $this->doApiRequest( array(
140 'action' => 'rollback',
141 'title' => 'Help:UTPage',
142 'user' => $revinfo['user'],
143 'token' => $pageinfo['rollbacktoken'],
144 'watchlist' => 'watch' ) );
145
146 $this->assertArrayHasKey( 'rollback', $data[0] );
147 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
148 } catch ( UsageException $ue ) {
149 if ( $ue->getCodeString() == 'onlyauthor' ) {
150 $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" );
151 } else {
152 $this->fail( "Received error '" . $ue->getCodeString() . "'" );
153 }
154 }
155 }
156
157 /**
158 */
159 function testWatchDelete() {
160 $pageinfo = $this->getTokens();
161
162 $data = $this->doApiRequest( array(
163 'action' => 'delete',
164 'token' => $pageinfo['deletetoken'],
165 'title' => 'Help:UTPage' ) );
166 $this->assertArrayHasKey( 'delete', $data[0] );
167 $this->assertArrayHasKey( 'title', $data[0]['delete'] );
168
169 $this->doApiRequest( array(
170 'action' => 'query',
171 'list' => 'watchlist' ) );
172
173 $this->markTestIncomplete( 'This test needs to verify the deleted article was added to the users watchlist' );
174 }
175 }