MediaWiki  1.33.0
ApiWatchTest.php
Go to the documentation of this file.
1 <?php
2 
11 class ApiWatchTest extends ApiTestCase {
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 }
ApiWatchTest\testWatchProtect
testWatchProtect()
Definition: ApiWatchTest.php:72
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:306
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:28
captcha-old.count
count
Definition: captcha-old.py:249
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:88
ApiTestCase\getTokenList
getTokenList(TestUser $user, $session=null)
Definition: ApiTestCase.php:163
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:62
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
ApiWatchTest\testWatchEdit
testWatchEdit()
Definition: ApiWatchTest.php:16
ApiWatchTest
API Database medium.
Definition: ApiWatchTest.php:11
ApiTestCase
Definition: ApiTestCase.php:5
$tokens
$tokens
Definition: mwdoc-filter.php:47
ApiWatchTest\getTokens
getTokens()
Definition: ApiWatchTest.php:12
ApiWatchTest\testWatchRollback
testWatchRollback( $data)
Broken Broken because there is currently no revision info in the $pageinfo.
Definition: ApiWatchTest.php:124
ApiWatchTest\testWatchClear
testWatchClear()
@depends testWatchEdit
Definition: ApiWatchTest.php:35
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:67