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