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