Merge branch 'master' into Wikidata
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiWatchTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @todo This test suite is severly broken and need a full review
7 */
8 class ApiWatchTest extends ApiTestCase {
9
10 function setUp() {
11 parent::setUp();
12 $this->doLogin();
13 }
14
15 function getTokens() {
16 return $this->getTokenList( self::$users['sysop'] );
17 }
18
19 /**
20 * @group Broken
21 */
22 function testWatchEdit() {
23
24 $data = $this->getTokens();
25
26 $keys = array_keys( $data[0]['query']['pages'] );
27 $key = array_pop( $keys );
28 $pageinfo = $data[0]['query']['pages'][$key];
29
30 $data = $this->doApiRequest( array(
31 'action' => 'edit',
32 'title' => 'UTPage',
33 'text' => 'new text',
34 'token' => $pageinfo['edittoken'],
35 'watchlist' => 'watch' ), $data );
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 * @group Broken
46 */
47 function testWatchClear() {
48
49 $data = $this->doApiRequest( array(
50 'action' => 'query',
51 'list' => 'watchlist' ), $data );
52
53 if ( isset( $data[0]['query']['watchlist'] ) ) {
54 $wl = $data[0]['query']['watchlist'];
55
56 foreach ( $wl as $page ) {
57 $data = $this->doApiRequest( array(
58 'action' => 'watch',
59 'title' => $page['title'],
60 'unwatch' => true ), $data );
61 }
62 }
63 $data = $this->doApiRequest( array(
64 'action' => 'query',
65 'list' => 'watchlist' ), $data );
66 $this->assertArrayHasKey( 'query', $data[0] );
67 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
68 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
69
70 return $data;
71 }
72
73 /**
74 * @group Broken
75 */
76 function testWatchProtect() {
77
78 $data = $this->getTokens();
79
80 $keys = array_keys( $data[0]['query']['pages'] );
81 $key = array_pop( $keys );
82 $pageinfo = $data[0]['query']['pages'][$key];
83
84 $data = $this->doApiRequest( array(
85 'action' => 'protect',
86 'token' => $pageinfo['protecttoken'],
87 'title' => 'UTPage',
88 'protections' => 'edit=sysop',
89 'watchlist' => 'unwatch' ), $data );
90
91 $this->assertArrayHasKey( 'protect', $data[0] );
92 $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
93 $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
94 $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
95 }
96
97 /**
98 * @group Broken
99 */
100 function testGetRollbackToken() {
101
102 $data = $this->getTokens();
103
104 if ( !Title::newFromText( 'UTPage' )->exists() ) {
105 $this->markTestIncomplete( "The article [[UTPage]] does not exist" );
106 }
107
108 $data = $this->doApiRequest( array(
109 'action' => 'query',
110 'prop' => 'revisions',
111 'titles' => 'UTPage',
112 'rvtoken' => 'rollback' ), $data );
113
114 $this->assertArrayHasKey( 'query', $data[0] );
115 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
116 $keys = array_keys( $data[0]['query']['pages'] );
117 $key = array_pop( $keys );
118
119 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
120 $this->markTestIncomplete( "Target page (UTPage) doesn't exist" );
121 }
122
123 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
124 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
125 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
126 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
127
128 return $data;
129 }
130
131 /**
132 * @depends testGetRollbackToken
133 * @group Broken
134 */
135 function testWatchRollback( $data ) {
136 $keys = array_keys( $data[0]['query']['pages'] );
137 $key = array_pop( $keys );
138 $pageinfo = $data[0]['query']['pages'][$key]['revisions'][0];
139
140 try {
141 $data = $this->doApiRequest( array(
142 'action' => 'rollback',
143 'title' => 'UTPage',
144 'user' => $pageinfo['user'],
145 'token' => $pageinfo['rollbacktoken'],
146 'watchlist' => 'watch' ), $data );
147 } catch( UsageException $ue ) {
148 if( $ue->getCodeString() == 'onlyauthor' ) {
149 $this->markTestIncomplete( "Only one author to 'UTPage', cannot test rollback" );
150 } else {
151 $this->fail( "Received error '" . $ue->getCodeString() . "'" );
152 }
153 }
154
155 $this->assertArrayHasKey( 'rollback', $data[0] );
156 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
157 }
158
159 /**
160 * @group Broken
161 */
162 function testWatchDelete() {
163
164 $data = $this->getTokens();
165
166 $keys = array_keys( $data[0]['query']['pages'] );
167 $key = array_pop( $keys );
168 $pageinfo = $data[0]['query']['pages'][$key];
169
170 $data = $this->doApiRequest( array(
171 'action' => 'delete',
172 'token' => $pageinfo['deletetoken'],
173 'title' => 'UTPage' ), $data );
174 $this->assertArrayHasKey( 'delete', $data[0] );
175 $this->assertArrayHasKey( 'title', $data[0]['delete'] );
176
177 $data = $this->doApiRequest( array(
178 'action' => 'query',
179 'list' => 'watchlist' ), $data );
180
181 $this->markTestIncomplete( 'This test needs to verify the deleted article was added to the users watchlist' );
182 }
183 }