MediaWiki  1.32.0
ApiDeleteTest.php
Go to the documentation of this file.
1 <?php
2 
14 class ApiDeleteTest extends ApiTestCase {
15 
16  protected function setUp() {
17  parent::setUp();
18  $this->tablesUsed = array_merge(
19  $this->tablesUsed,
20  [ 'change_tag', 'change_tag_def', 'logging' ]
21  );
22  }
23 
24  public function testDelete() {
25  $name = 'Help:' . ucfirst( __FUNCTION__ );
26 
27  // create new page
28  $this->editPage( $name, 'Some text' );
29 
30  // test deletion
31  $apiResult = $this->doApiRequestWithToken( [
32  'action' => 'delete',
33  'title' => $name,
34  ] )[0];
35 
36  $this->assertArrayHasKey( 'delete', $apiResult );
37  $this->assertArrayHasKey( 'title', $apiResult['delete'] );
38  $this->assertSame( $name, $apiResult['delete']['title'] );
39  $this->assertArrayHasKey( 'logid', $apiResult['delete'] );
40 
41  $this->assertFalse( Title::newFromText( $name )->exists() );
42  }
43 
44  public function testBatchedDelete() {
45  $this->setMwGlobals( 'wgDeleteRevisionsBatchSize', 1 );
46 
47  $name = 'Help:' . ucfirst( __FUNCTION__ );
48  for ( $i = 1; $i <= 3; $i++ ) {
49  $this->editPage( $name, "Revision $i" );
50  }
51 
52  $apiResult = $this->doApiRequestWithToken( [
53  'action' => 'delete',
54  'title' => $name,
55  ] )[0];
56 
57  $this->assertArrayHasKey( 'delete', $apiResult );
58  $this->assertArrayHasKey( 'title', $apiResult['delete'] );
59  $this->assertSame( $name, $apiResult['delete']['title'] );
60  $this->assertArrayHasKey( 'scheduled', $apiResult['delete'] );
61  $this->assertTrue( $apiResult['delete']['scheduled'] );
62  $this->assertArrayNotHasKey( 'logid', $apiResult['delete'] );
63 
64  // Run the jobs
66  $jobs = new RunJobs;
67  $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
68  $jobs->execute();
69 
70  $this->assertFalse( Title::newFromText( $name )->exists( Title::GAID_FOR_UPDATE ) );
71  }
72 
73  public function testDeleteNonexistent() {
74  $this->setExpectedException( ApiUsageException::class,
75  "The page you specified doesn't exist." );
76 
77  $this->doApiRequestWithToken( [
78  'action' => 'delete',
79  'title' => 'This page deliberately left nonexistent',
80  ] );
81  }
82 
83  public function testDeletionWithoutPermission() {
84  $this->setExpectedException( ApiUsageException::class,
85  'The action you have requested is limited to users in the group:' );
86 
87  $name = 'Help:' . ucfirst( __FUNCTION__ );
88 
89  // create new page
90  $this->editPage( $name, 'Some text' );
91 
92  // test deletion without permission
93  try {
94  $user = new User();
95  $apiResult = $this->doApiRequest( [
96  'action' => 'delete',
97  'title' => $name,
98  'token' => $user->getEditToken(),
99  ], null, null, $user );
100  } finally {
101  $this->assertTrue( Title::newFromText( $name )->exists() );
102  }
103  }
104 
105  public function testDeleteWithTag() {
106  $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
107  $name = 'Help:' . ucfirst( __FUNCTION__ );
108 
109  ChangeTags::defineTag( 'custom tag' );
110 
111  $this->editPage( $name, 'Some text' );
112 
113  $this->doApiRequestWithToken( [
114  'action' => 'delete',
115  'title' => $name,
116  'tags' => 'custom tag',
117  ] );
118 
119  $this->assertFalse( Title::newFromText( $name )->exists() );
120 
121  $dbw = wfGetDB( DB_MASTER );
122  $this->assertSame( 'custom tag', $dbw->selectField(
123  [ 'change_tag', 'logging' ],
124  'ct_tag',
125  [
126  'log_namespace' => NS_HELP,
127  'log_title' => ucfirst( __FUNCTION__ ),
128  ],
129  __METHOD__,
130  [],
131  [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
132  ) );
133  }
134 
135  public function testDeleteWithTagNewBackend() {
136  $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
137  $name = 'Help:' . ucfirst( __FUNCTION__ );
138 
139  ChangeTags::defineTag( 'custom tag' );
140 
141  $this->editPage( $name, 'Some text' );
142 
143  $this->doApiRequestWithToken( [
144  'action' => 'delete',
145  'title' => $name,
146  'tags' => 'custom tag',
147  ] );
148 
149  $this->assertFalse( Title::newFromText( $name )->exists() );
150 
151  $dbw = wfGetDB( DB_MASTER );
152  $this->assertSame( 'custom tag', $dbw->selectField(
153  [ 'change_tag', 'logging', 'change_tag_def' ],
154  'ctd_name',
155  [
156  'log_namespace' => NS_HELP,
157  'log_title' => ucfirst( __FUNCTION__ ),
158  ],
159  __METHOD__,
160  [],
161  [
162  'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
163  'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ]
164  ]
165  ) );
166  }
167 
168  public function testDeleteWithoutTagPermission() {
169  $this->setExpectedException( ApiUsageException::class,
170  'You do not have permission to apply change tags along with your changes.' );
171 
172  $name = 'Help:' . ucfirst( __FUNCTION__ );
173 
174  ChangeTags::defineTag( 'custom tag' );
175  $this->setMwGlobals( 'wgRevokePermissions',
176  [ 'user' => [ 'applychangetags' => true ] ] );
177 
178  $this->editPage( $name, 'Some text' );
179 
180  try {
181  $this->doApiRequestWithToken( [
182  'action' => 'delete',
183  'title' => $name,
184  'tags' => 'custom tag',
185  ] );
186  } finally {
187  $this->assertTrue( Title::newFromText( $name )->exists() );
188  }
189  }
190 
191  public function testDeleteAbortedByHook() {
192  $this->setExpectedException( ApiUsageException::class,
193  'Deletion aborted by hook. It gave no explanation.' );
194 
195  $name = 'Help:' . ucfirst( __FUNCTION__ );
196 
197  $this->editPage( $name, 'Some text' );
198 
199  $this->setTemporaryHook( 'ArticleDelete',
200  function () {
201  return false;
202  }
203  );
204 
205  try {
206  $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
207  } finally {
208  $this->assertTrue( Title::newFromText( $name )->exists() );
209  }
210  }
211 
212  public function testDeleteWatch() {
213  $name = 'Help:' . ucfirst( __FUNCTION__ );
214  $user = self::$users['sysop']->getUser();
215 
216  $this->editPage( $name, 'Some text' );
217  $this->assertTrue( Title::newFromText( $name )->exists() );
218  $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
219 
220  $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );
221 
222  $this->assertFalse( Title::newFromText( $name )->exists() );
223  $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
224  }
225 
226  public function testDeleteUnwatch() {
227  $name = 'Help:' . ucfirst( __FUNCTION__ );
228  $user = self::$users['sysop']->getUser();
229 
230  $this->editPage( $name, 'Some text' );
231  $this->assertTrue( Title::newFromText( $name )->exists() );
232  $user->addWatch( Title::newFromText( $name ) );
233  $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
234 
235  $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );
236 
237  $this->assertFalse( Title::newFromText( $name )->exists() );
238  $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
239  }
240 }
RunJobs
Maintenance script that runs pending jobs.
Definition: runJobs.php:36
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
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:280
NS_HELP
const NS_HELP
Definition: Defines.php:76
ApiDeleteTest\testDeleteUnwatch
testDeleteUnwatch()
Definition: ApiDeleteTest.php:226
ApiDeleteTest\testDeleteWithTag
testDeleteWithTag()
Definition: ApiDeleteTest.php:105
MIGRATION_NEW
const MIGRATION_NEW
Definition: Defines.php:318
Maintenance\loadParamsAndArgs
loadParamsAndArgs( $self=null, $opts=null, $args=null)
Process command line arguments $mOptions becomes an array with keys set to the option names $mArgs be...
Definition: Maintenance.php:950
MIGRATION_WRITE_BOTH
const MIGRATION_WRITE_BOTH
Definition: Defines.php:316
JobQueueGroup\destroySingletons
static destroySingletons()
Destroy the singleton instances.
Definition: JobQueueGroup.php:91
User
User
Definition: All_system_messages.txt:425
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
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:63
ApiDeleteTest
Tests for MediaWiki api.php?action=delete.
Definition: ApiDeleteTest.php:14
ApiDeleteTest\testBatchedDelete
testBatchedDelete()
Definition: ApiDeleteTest.php:44
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2693
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:706
ApiTestCase\doApiRequestWithToken
doApiRequestWithToken(array $params, array $session=null, User $user=null, $tokenType='auto')
Convenience function to access the token parameter of doApiRequest() more succinctly.
Definition: ApiTestCase.php:133
ChangeTags\defineTag
static defineTag( $tag)
Defines a tag in the valid_tag table and/or update ctd_user_defined field in change_tag_def,...
Definition: ChangeTags.php:915
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
MediaWikiTestCase\editPage
editPage( $pageName, $text, $summary='', $defaultNs=NS_MAIN)
Edits or creates a page/revision.
Definition: MediaWikiTestCase.php:2333
ApiTestCase
Definition: ApiTestCase.php:5
ApiDeleteTest\testDeleteNonexistent
testDeleteNonexistent()
Definition: ApiDeleteTest.php:73
Title\GAID_FOR_UPDATE
const GAID_FOR_UPDATE
Used to be GAID_FOR_UPDATE define.
Definition: Title.php:54
ApiDeleteTest\testDeleteWithoutTagPermission
testDeleteWithoutTagPermission()
Definition: ApiDeleteTest.php:168
ApiDeleteTest\testDeleteAbortedByHook
testDeleteAbortedByHook()
Definition: ApiDeleteTest.php:191
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiTestCase\setTemporaryHook
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
Definition: MediaWikiTestCase.php:2291
ApiDeleteTest\setUp
setUp()
Definition: ApiDeleteTest.php:16
ApiDeleteTest\testDeleteWatch
testDeleteWatch()
Definition: ApiDeleteTest.php:212
ApiDeleteTest\testDeletionWithoutPermission
testDeletionWithoutPermission()
Definition: ApiDeleteTest.php:83
ApiDeleteTest\testDeleteWithTagNewBackend
testDeleteWithTagNewBackend()
Definition: ApiDeleteTest.php:135
ApiDeleteTest\testDelete
testDelete()
Definition: ApiDeleteTest.php:24