Clean and repair many phpunit tests (+ fix implied configuration)
[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 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' => 'UTPage',
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' => '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
98 $pageinfo = $this->getTokens();
99
100 if ( !Title::newFromText( 'UTPage' )->exists() ) {
101 $this->markTestSkipped( "The article [[UTPage]] does not exist" ); //TODO: just create it?
102 }
103
104 $data = $this->doApiRequest( array(
105 'action' => 'query',
106 'prop' => 'revisions',
107 'titles' => 'UTPage',
108 'rvtoken' => 'rollback' ) );
109
110 $this->assertArrayHasKey( 'query', $data[0] );
111 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
112 $keys = array_keys( $data[0]['query']['pages'] );
113 $key = array_pop( $keys );
114
115 if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
116 $this->markTestSkipped( "Target page (UTPage) doesn't exist" );
117 }
118
119 $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
120 $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
121 $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
122 $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
123
124 return $data;
125 }
126
127 /**
128 * @group Broken
129 * Broken because there is currently no revision info in the $pageinfo
130 *
131 * @depends testGetRollbackToken
132 */
133 function testWatchRollback( $data ) {
134 $keys = array_keys( $data[0]['query']['pages'] );
135 $key = array_pop( $keys );
136 $pageinfo = $data[0]['query']['pages'][$key];
137 $revinfo = $pageinfo['revisions'][0];
138
139 try {
140 $data = $this->doApiRequest( array(
141 'action' => 'rollback',
142 'title' => 'UTPage',
143 'user' => $revinfo['user'],
144 'token' => $pageinfo['rollbacktoken'],
145 'watchlist' => 'watch' ) );
146
147 $this->assertArrayHasKey( 'rollback', $data[0] );
148 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
149 } catch( UsageException $ue ) {
150 if( $ue->getCodeString() == 'onlyauthor' ) {
151 $this->markTestIncomplete( "Only one author to 'UTPage', cannot test rollback" );
152 } else {
153 $this->fail( "Received error '" . $ue->getCodeString() . "'" );
154 }
155 }
156 }
157
158 /**
159 */
160 function testWatchDelete() {
161 $pageinfo = $this->getTokens();
162
163 $data = $this->doApiRequest( array(
164 'action' => 'delete',
165 'token' => $pageinfo['deletetoken'],
166 'title' => 'UTPage' ) );
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' ) );
173
174 $this->markTestIncomplete( 'This test needs to verify the deleted article was added to the users watchlist' );
175 }
176 }