MediaWiki  1.30.0
ApiWatchTest.php
Go to the documentation of this file.
1 <?php
2 
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  public function testWatchEdit() {
20  $tokens = $this->getTokens();
21 
22  $data = $this->doApiRequest( [
23  'action' => 'edit',
24  'title' => 'Help:UTPage', // Help namespace is hopefully wikitext
25  'text' => 'new text',
26  'token' => $tokens['edittoken'],
27  'watchlist' => 'watch' ] );
28  $this->assertArrayHasKey( 'edit', $data[0] );
29  $this->assertArrayHasKey( 'result', $data[0]['edit'] );
30  $this->assertEquals( 'Success', $data[0]['edit']['result'] );
31 
32  return $data;
33  }
34 
38  public function testWatchClear() {
39  $tokens = $this->getTokens();
40 
41  $data = $this->doApiRequest( [
42  'action' => 'query',
43  'wllimit' => 'max',
44  'list' => 'watchlist' ] );
45 
46  if ( isset( $data[0]['query']['watchlist'] ) ) {
47  $wl = $data[0]['query']['watchlist'];
48 
49  foreach ( $wl as $page ) {
50  $data = $this->doApiRequest( [
51  'action' => 'watch',
52  'title' => $page['title'],
53  'unwatch' => true,
54  'token' => $tokens['watchtoken'] ] );
55  }
56  }
57  $data = $this->doApiRequest( [
58  'action' => 'query',
59  'list' => 'watchlist' ], $data );
60  $this->assertArrayHasKey( 'query', $data[0] );
61  $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
62  foreach ( $data[0]['query']['watchlist'] as $index => $item ) {
63  // Previous tests may insert an invalid title
64  // like ":ApiEditPageTest testNonTextEdit", which
65  // can't be cleared.
66  if ( strpos( $item['title'], ':' ) === 0 ) {
67  unset( $data[0]['query']['watchlist'][$index] );
68  }
69  }
70  $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
71 
72  return $data;
73  }
74 
75  public function testWatchProtect() {
76  $tokens = $this->getTokens();
77 
78  $data = $this->doApiRequest( [
79  'action' => 'protect',
80  'token' => $tokens['protecttoken'],
81  'title' => 'Help:UTPage',
82  'protections' => 'edit=sysop',
83  'watchlist' => 'unwatch' ] );
84 
85  $this->assertArrayHasKey( 'protect', $data[0] );
86  $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
87  $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
88  $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
89  }
90 
91  public function testGetRollbackToken() {
92  $this->getTokens();
93 
94  if ( !Title::newFromText( 'Help:UTPage' )->exists() ) {
95  $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); // TODO: just create it?
96  }
97 
98  $data = $this->doApiRequest( [
99  'action' => 'query',
100  'prop' => 'revisions',
101  'titles' => 'Help:UTPage',
102  'rvtoken' => 'rollback' ] );
103 
104  $this->assertArrayHasKey( 'query', $data[0] );
105  $this->assertArrayHasKey( 'pages', $data[0]['query'] );
106  $keys = array_keys( $data[0]['query']['pages'] );
107  $key = array_pop( $keys );
108 
109  if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
110  $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" );
111  }
112 
113  $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
114  $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
115  $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
116  $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
117 
118  return $data;
119  }
120 
127  public function testWatchRollback( $data ) {
128  $keys = array_keys( $data[0]['query']['pages'] );
129  $key = array_pop( $keys );
130  $pageinfo = $data[0]['query']['pages'][$key];
131  $revinfo = $pageinfo['revisions'][0];
132 
133  try {
134  $data = $this->doApiRequest( [
135  'action' => 'rollback',
136  'title' => 'Help:UTPage',
137  'user' => $revinfo['user'],
138  'token' => $pageinfo['rollbacktoken'],
139  'watchlist' => 'watch' ] );
140 
141  $this->assertArrayHasKey( 'rollback', $data[0] );
142  $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
143  } catch ( ApiUsageException $ue ) {
144  if ( self::apiExceptionHasCode( $ue, 'onlyauthor' ) ) {
145  $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" );
146  } else {
147  $this->fail( "Received error '" . $ue->getMessage() . "'" );
148  }
149  }
150  }
151 }
ApiWatchTest\testWatchProtect
testWatchProtect()
Definition: ApiWatchTest.php:75
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:268
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:104
captcha-old.count
count
Definition: captcha-old.py:249
ApiWatchTest\setUp
setUp()
Definition: ApiWatchTest.php:10
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiWatchTest\testGetRollbackToken
testGetRollbackToken()
Definition: ApiWatchTest.php:91
ApiTestCase\getTokenList
getTokenList(TestUser $user, $session=null)
Definition: ApiTestCase.php:188
ApiWatchTest\testWatchEdit
testWatchEdit()
Definition: ApiWatchTest.php:19
ApiWatchTest
API Database medium.
Definition: ApiWatchTest.php:9
ApiTestCase\doLogin
doLogin( $testUser='sysop')
Definition: ApiTestCase.php:152
ApiTestCase
Definition: ApiTestCase.php:3
$tokens
$tokens
Definition: mwdoc-filter.php:47
ApiWatchTest\getTokens
getTokens()
Definition: ApiWatchTest.php:15
ApiWatchTest\testWatchRollback
testWatchRollback( $data)
Broken Broken because there is currently no revision info in the $pageinfo.
Definition: ApiWatchTest.php:127
ApiWatchTest\testWatchClear
testWatchClear()
@depends testWatchEdit
Definition: ApiWatchTest.php:38
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:73
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$keys
$keys
Definition: testCompression.php:65