Refactor much of the API testing code.
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiWatchTest.php
1 <?php
2
3 require_once dirname( __FILE__ ) . '/ApiSetup.php';
4
5 /**
6 * @group Database
7 * @group Destructive
8 */
9 class ApiWatchTest extends ApiTestSetup {
10
11 function setUp() {
12 parent::setUp();
13 $this->doLogin();
14 }
15
16 function getTokens() {
17 return $this->getTokenList( $this->sysopUser );
18 }
19
20 function testWatchEdit() {
21
22 $data = $this->getTokens();
23
24 $this->markTestIncomplete( "Broken" );
25 $keys = array_keys( $data[0]['query']['pages'] );
26 $key = array_pop( $keys );
27 $pageinfo = $data[0]['query']['pages'][$key];
28
29 $data = $this->doApiRequest( array(
30 'action' => 'edit',
31 'title' => 'UTPage',
32 'text' => 'new text',
33 'token' => $pageinfo['edittoken'],
34 'watchlist' => 'watch' ), $data );
35 $this->assertArrayHasKey( 'edit', $data[0] );
36 $this->assertArrayHasKey( 'result', $data[0]['edit'] );
37 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
38
39 return $data;
40 }
41
42 /**
43 * @depends testWatchEdit
44 */
45 function testWatchClear() {
46
47 $data = $this->doApiRequest( array(
48 'action' => 'query',
49 'list' => 'watchlist' ), $data );
50
51 if ( isset( $data[0]['query']['watchlist'] ) ) {
52 $wl = $data[0]['query']['watchlist'];
53
54 foreach ( $wl as $page ) {
55 $data = $this->doApiRequest( array(
56 'action' => 'watch',
57 'title' => $page['title'],
58 'unwatch' => true ), $data );
59 }
60 }
61 $data = $this->doApiRequest( array(
62 'action' => 'query',
63 'list' => 'watchlist' ), $data );
64 $this->assertArrayHasKey( 'query', $data[0] );
65 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
66 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
67
68 return $data;
69 }
70
71
72 function testWatchProtect() {
73
74 $data = $this->getTokens();
75
76 $this->markTestIncomplete( "Broken" );
77 $keys = array_keys( $data[0]['query']['pages'] );
78 $key = array_pop( $keys );
79 $pageinfo = $data[0]['query']['pages'][$key];
80
81 $data = $this->doApiRequest( array(
82 'action' => 'protect',
83 'token' => $pageinfo['protecttoken'],
84 'title' => 'UTPage',
85 'protections' => 'edit=sysop',
86 'watchlist' => 'unwatch' ), $data );
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 function testGetRollbackToken() {
96
97 $data = $this->getTokens();
98
99 if ( !Title::newFromText( 'UTPage' )->exists() ) {
100 $this->markTestIncomplete( "The article [[UTPage]] does not exist" );
101 }
102
103 $data = $this->doApiRequest( array(
104 'action' => 'query',
105 'prop' => 'revisions',
106 'titles' => 'UTPage',
107 'rvtoken' => 'rollback' ), $data );
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->markTestIncomplete( "Target page (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 * @depends testGetRollbackToken
128 */
129 function testWatchRollback( $data ) {
130 $keys = array_keys( $data[0]['query']['pages'] );
131 $key = array_pop( $keys );
132 $pageinfo = $data[0]['query']['pages'][$key]['revisions'][0];
133
134 try {
135 $data = $this->doApiRequest( array(
136 'action' => 'rollback',
137 'title' => 'UTPage',
138 'user' => $pageinfo['user'],
139 'token' => $pageinfo['rollbacktoken'],
140 'watchlist' => 'watch' ), $data );
141 } catch( UsageException $ue ) {
142 if( $ue->getCodeString() == 'onlyauthor' ) {
143 $this->markTestIncomplete( "Only one author to 'UTPage', cannot test rollback" );
144 } else {
145 $this->fail( "Received error " . $ue->getCodeString() );
146 }
147 }
148
149 $this->assertArrayHasKey( 'rollback', $data[0] );
150 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
151 }
152
153
154 function testWatchDelete() {
155
156 $data = $this->getTokens();
157
158 $this->markTestIncomplete( "Broken" );
159 $keys = array_keys( $data[0]['query']['pages'] );
160 $key = array_pop( $keys );
161 $pageinfo = $data[0]['query']['pages'][$key];
162
163 $data = $this->doApiRequest( array(
164 'action' => 'delete',
165 'token' => $pageinfo['deletetoken'],
166 'title' => 'UTPage' ), $data );
167 $this->assertArrayHasKey( 'delete', $data[0] );
168 $this->assertArrayHasKey( 'title', $data[0]['delete'] );
169
170 $data = $this->doApiRequest( array(
171 'action' => 'query',
172 'list' => 'watchlist' ), $data );
173
174 $this->markTestIncomplete( 'This test needs to verify the deleted article was added to the users watchlist' );
175 }
176 }