MediaWiki REL1_33
ApiWatchTest.php
Go to the documentation of this file.
1<?php
2
12 function getTokens() {
13 return $this->getTokenList( self::$users['sysop'] );
14 }
15
16 public function testWatchEdit() {
17 $tokens = $this->getTokens();
18
19 $data = $this->doApiRequest( [
20 'action' => 'edit',
21 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext
22 'text' => 'new text',
23 'token' => $tokens['edittoken'],
24 'watchlist' => 'watch' ] );
25 $this->assertArrayHasKey( 'edit', $data[0] );
26 $this->assertArrayHasKey( 'result', $data[0]['edit'] );
27 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
28
29 return $data;
30 }
31
35 public function testWatchClear() {
36 $tokens = $this->getTokens();
37
38 $data = $this->doApiRequest( [
39 'action' => 'query',
40 'wllimit' => 'max',
41 'list' => 'watchlist' ] );
42
43 if ( isset( $data[0]['query']['watchlist'] ) ) {
44 $wl = $data[0]['query']['watchlist'];
45
46 foreach ( $wl as $page ) {
47 $data = $this->doApiRequest( [
48 'action' => 'watch',
49 'title' => $page['title'],
50 'unwatch' => true,
51 'token' => $tokens['watchtoken'] ] );
52 }
53 }
54 $data = $this->doApiRequest( [
55 'action' => 'query',
56 'list' => 'watchlist' ], $data );
57 $this->assertArrayHasKey( 'query', $data[0] );
58 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
59 foreach ( $data[0]['query']['watchlist'] as $index => $item ) {
60 // Previous tests may insert an invalid title
61 // like ":ApiEditPageTest testNonTextEdit", which
62 // can't be cleared.
63 if ( strpos( $item['title'], ':' ) === 0 ) {
64 unset( $data[0]['query']['watchlist'][$index] );
65 }
66 }
67 $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
68
69 return $data;
70 }
71
72 public function testWatchProtect() {
73 $tokens = $this->getTokens();
74
75 $data = $this->doApiRequest( [
76 'action' => 'protect',
77 'token' => $tokens['protecttoken'],
78 'title' => 'Help:UTPage',
79 'protections' => 'edit=sysop',
80 'watchlist' => 'unwatch' ] );
81
82 $this->assertArrayHasKey( 'protect', $data[0] );
83 $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
84 $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
85 $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
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( [
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
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( [
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 ( ApiUsageException $ue ) {
141 if ( self::apiExceptionHasCode( $ue, 'onlyauthor' ) ) {
142 $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" );
143 } else {
144 $this->fail( "Received error '" . $ue->getMessage() . "'" );
145 }
146 }
147 }
148}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
getTokenList(TestUser $user, $session=null)
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Exception used to abort API execution with an error.
API Database medium.
testWatchRollback( $data)
Broken Broken because there is currently no revision info in the $pageinfo.
testWatchClear()
@depends testWatchEdit
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
$tokens