MediaWiki  master
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  $name = 'Help:' . ucfirst( __FUNCTION__ );
107 
108  ChangeTags::defineTag( 'custom tag' );
109 
110  $this->editPage( $name, 'Some text' );
111 
112  $this->doApiRequestWithToken( [
113  'action' => 'delete',
114  'title' => $name,
115  'tags' => 'custom tag',
116  ] );
117 
118  $this->assertFalse( Title::newFromText( $name )->exists() );
119 
120  $dbw = wfGetDB( DB_MASTER );
121  $this->assertSame( 'custom tag', $dbw->selectField(
122  [ 'change_tag', 'logging', 'change_tag_def' ],
123  'ctd_name',
124  [
125  'log_namespace' => NS_HELP,
126  'log_title' => ucfirst( __FUNCTION__ ),
127  ],
128  __METHOD__,
129  [],
130  [
131  'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
132  'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ]
133  ]
134  ) );
135  }
136 
137  public function testDeleteWithoutTagPermission() {
138  $this->setExpectedException( ApiUsageException::class,
139  'You do not have permission to apply change tags along with your changes.' );
140 
141  $name = 'Help:' . ucfirst( __FUNCTION__ );
142 
143  ChangeTags::defineTag( 'custom tag' );
144  $this->setMwGlobals( 'wgRevokePermissions',
145  [ 'user' => [ 'applychangetags' => true ] ] );
146  $this->resetServices();
147 
148  $this->editPage( $name, 'Some text' );
149 
150  try {
151  $this->doApiRequestWithToken( [
152  'action' => 'delete',
153  'title' => $name,
154  'tags' => 'custom tag',
155  ] );
156  } finally {
157  $this->assertTrue( Title::newFromText( $name )->exists() );
158  }
159  }
160 
161  public function testDeleteAbortedByHook() {
162  $this->setExpectedException( ApiUsageException::class,
163  'Deletion aborted by hook. It gave no explanation.' );
164 
165  $name = 'Help:' . ucfirst( __FUNCTION__ );
166 
167  $this->editPage( $name, 'Some text' );
168 
169  $this->setTemporaryHook( 'ArticleDelete',
170  function () {
171  return false;
172  }
173  );
174 
175  try {
176  $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
177  } finally {
178  $this->assertTrue( Title::newFromText( $name )->exists() );
179  }
180  }
181 
182  public function testDeleteWatch() {
183  $name = 'Help:' . ucfirst( __FUNCTION__ );
184  $user = self::$users['sysop']->getUser();
185 
186  $this->editPage( $name, 'Some text' );
187  $this->assertTrue( Title::newFromText( $name )->exists() );
188  $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
189 
190  $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );
191 
192  $this->assertFalse( Title::newFromText( $name )->exists() );
193  $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
194  }
195 
196  public function testDeleteUnwatch() {
197  $name = 'Help:' . ucfirst( __FUNCTION__ );
198  $user = self::$users['sysop']->getUser();
199 
200  $this->editPage( $name, 'Some text' );
201  $this->assertTrue( Title::newFromText( $name )->exists() );
202  $user->addWatch( Title::newFromText( $name ) );
203  $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
204 
205  $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );
206 
207  $this->assertFalse( Title::newFromText( $name )->exists() );
208  $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
209  }
210 }
testDeletionWithoutPermission()
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
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
const DB_MASTER
Definition: defines.php:26
doApiRequestWithToken(array $params, array $session=null, User $user=null, $tokenType='auto')
Convenience function to access the token parameter of doApiRequest() more succinctly.
Tests for MediaWiki api.php?action=delete.
const GAID_FOR_UPDATE
Used to be GAID_FOR_UPDATE define.
Definition: Title.php:57
Maintenance script that runs pending jobs.
Definition: runJobs.php:39
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:773
testDeleteWithoutTagPermission()
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
const NS_HELP
Definition: Defines.php:72
static defineTag( $tag)
Set ctd_user_defined = 1 in change_tag_def without checking that the tag name is valid.
Definition: ChangeTags.php:925
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
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:277
loadParamsAndArgs( $self=null, $opts=null, $args=null)
Process command line arguments $mOptions becomes an array with keys set to the option names $mArgs be...
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1460
static destroySingletons()
Destroy the singleton instances.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:322