Debian Codegraph @

tests/phpunit/includes/api/ApiDeleteTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api;
4
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Title\Title;
7 use MediaWiki\User\User;
8 use Wikimedia\Rdbms\IDBAccessObject;
9
10 /**
11 * Tests for MediaWiki api.php?action=delete.
12 *
13 * @author Yifei He
14 *
15 * @group API
16 * @group Database
17 * @group medium
18 *
19 * @covers \MediaWiki\Api\ApiDelete
20 */
21 class ApiDeleteTest extends ApiTestCase {
22
23 protected function setUp(): void {
24 parent::setUp();
25
26 $this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
27 }
28
29 public function testDelete() {
30 $title = Title::makeTitle( NS_HELP, 'TestDelete' );
31
32 // create new page
33 $this->editPage( $title, 'Some text' );
34
35 // test deletion
36 $apiResult = $this->doApiRequestWithToken( [
37 'action' => 'delete',
38 'title' => $title->getPrefixedText(),
39 ] )[0];
40
41 $this->assertArrayHasKey( 'delete', $apiResult );
42 $this->assertArrayHasKey( 'title', $apiResult['delete'] );
43 $this->assertSame( $title->getPrefixedText(), $apiResult['delete']['title'] );
44 $this->assertArrayHasKey( 'logid', $apiResult['delete'] );
45
46 $this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) );
47 }
48
49 public function testBatchedDelete() {
50 $this->overrideConfigValue(
51 MainConfigNames::DeleteRevisionsBatchSize, 1
52 );
53
54 $title = Title::makeTitle( NS_HELP, 'TestBatchedDelete' );
55 for ( $i = 1; $i <= 3; $i++ ) {
56 $this->editPage( $title, "Revision $i" );
57 }
58
59 $apiResult = $this->doApiRequestWithToken( [
60 'action' => 'delete',
61 'title' => $title->getPrefixedText(),
62 ] )[0];
63
64 $this->assertArrayHasKey( 'delete', $apiResult );
65 $this->assertArrayHasKey( 'title', $apiResult['delete'] );
66 $this->assertSame( $title->getPrefixedText(), $apiResult['delete']['title'] );
67 $this->assertArrayHasKey( 'scheduled', $apiResult['delete'] );
68 $this->assertTrue( $apiResult['delete']['scheduled'] );
69 $this->assertArrayNotHasKey( 'logid', $apiResult['delete'] );
70
71 // Run the jobs
72 $this->runJobs();
73
74 $this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) );
75 }
76
77 public function testDeleteNonexistent() {
78 $this->expectApiErrorCode( 'missingtitle' );
79
80 $this->doApiRequestWithToken( [
81 'action' => 'delete',
82 'title' => 'This page deliberately left nonexistent',
83 ] );
84 }
85
86 public function testDeleteAssociatedTalkPage() {
87 $title = Title::makeTitle( NS_HELP, 'TestDeleteAssociatedTalkPage' );
88 $talkPage = $title->getTalkPageIfDefined();
89 $this->editPage( $title, 'Some text' );
90 $this->editPage( $talkPage, 'Some text' );
91 $apiResult = $this->doApiRequestWithToken( [
92 'action' => 'delete',
93 'title' => $title->getPrefixedText(),
94 'deletetalk' => true,
95 ] )[0];
96
97 $this->assertSame( $title->getPrefixedText(), $apiResult['delete']['title'] );
98 $this->assertFalse( $talkPage->exists( IDBAccessObject::READ_LATEST ) );
99 }
100
101 public function testDeleteAssociatedTalkPageNonexistent() {
102 $title = Title::makeTitle( NS_HELP, 'TestDeleteAssociatedTalkPageNonexistent' );
103 $this->editPage( $title, 'Some text' );
104 $apiResult = $this->doApiRequestWithToken( [
105 'action' => 'delete',
106 'title' => $title->getPrefixedText(),
107 'deletetalk' => true,
108 ] )[0];
109
110 $this->assertSame( $title->getPrefixedText(), $apiResult['delete']['title'] );
111 $this->assertArrayHasKey( 'warnings', $apiResult );
112 }
113
114 public function testDeletionWithoutPermission() {
115 $this->expectApiErrorCode( 'permissiondenied' );
116
117 $title = Title::makeTitle( NS_HELP, 'TestDeletionWithoutPermission' );
118
119 // create new page
120 $this->editPage( $title, 'Some text' );
121
122 // test deletion without permission
123 try {
124 $user = new User();
125 $this->doApiRequest( [
126 'action' => 'delete',
127 'title' => $title->getPrefixedText(),
128 'token' => $user->getEditToken(),
129 ], null, null, $user );
130 } finally {
131 $this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) );
132 }
133 }
134
135 public function testDeleteWithTag() {
136 $title = Title::makeTitle( NS_HELP, 'TestDeleteWithTag' );
137
138 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
139
140 $this->editPage( $title, 'Some text' );
141
142 $this->doApiRequestWithToken( [
143 'action' => 'delete',
144 'title' => $title->getPrefixedText(),
145 'tags' => 'custom tag',
146 ] );
147
148 $this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) );
149
150 $this->assertSame( 'custom tag', $this->getDb()->newSelectQueryBuilder()
151 ->select( 'ctd_name' )
152 ->from( 'logging' )
153 ->join( 'change_tag', null, 'ct_log_id = log_id' )
154 ->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
155 ->where( [ 'log_namespace' => $title->getNamespace(), 'log_title' => $title->getDBkey(), ] )
156 ->caller( __METHOD__ )->fetchField() );
157 }
158
159 public function testDeleteWithoutTagPermission() {
160 $this->expectApiErrorCode( 'tags-apply-no-permission' );
161
162 $title = Title::makeTitle( NS_HELP, 'TestDeleteWithoutTagPermission' );
163
164 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
165 $this->overrideConfigValue(
166 MainConfigNames::RevokePermissions,
167 [ 'user' => [ 'applychangetags' => true ] ]
168 );
169
170 $this->editPage( $title, 'Some text' );
171
172 try {
173 $this->doApiRequestWithToken( [
174 'action' => 'delete',
175 'title' => $title->getPrefixedText(),
176 'tags' => 'custom tag',
177 ] );
178 } finally {
179 $this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) );
180 }
181 }
182
183 public function testDeleteAbortedByHook() {
184 $this->expectApiErrorCode( 'delete-hook-aborted' );
185
186 $title = Title::makeTitle( NS_HELP, 'TestDeleteAbortedByHook' );
187
188 $this->editPage( $title, 'Some text' );
189
190 $this->setTemporaryHook( 'ArticleDelete',
191 static function () {
192 return false;
193 }
194 );
195
196 try {
197 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $title->getPrefixedText() ] );
198 } finally {
199 $this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) );
200 }
201 }
202
203 public function testDeleteWatch() {
204 $title = Title::makeTitle( NS_HELP, 'TestDeleteWatch' );
205 $page = $this->getExistingTestPage( $title );
206 $performer = $this->getTestSysop()->getUser();
207 $watchlistManager = $this->getServiceContainer()->getWatchlistManager();
208
209 $this->assertFalse( $watchlistManager->isWatched( $performer, $page ) );
210
211 $res = $this->doApiRequestWithToken(
212 [
213 'action' => 'delete',
214 'title' => $title->getPrefixedText(),
215 'watch' => '',
216 'watchlistexpiry' => '99990123000000',
217 ],
218 null,
219 $performer
220 );
221 $this->assertArrayHasKey( 'delete', $res[0] );
222 $page->clear();
223
224 $this->assertFalse( $page->exists() );
225 $this->assertTrue( $watchlistManager->isWatched( $performer, $page ) );
226 $this->assertTrue( $watchlistManager->isTempWatched( $performer, $page ) );
227 }
228
229 public function testDeleteUnwatch() {
230 $title = Title::makeTitle( NS_HELP, 'TestDeleteUnwatch' );
231 $user = $this->getTestSysop()->getUser();
232
233 $this->editPage( $title, 'Some text' );
234 $this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) );
235 $watchlistManager = $this->getServiceContainer()->getWatchlistManager();
236 $watchlistManager->addWatch( $user, $title );
237 $this->assertTrue( $watchlistManager->isWatched( $user, $title ) );
238
239 $this->doApiRequestWithToken( [
240 'action' => 'delete',
241 'title' => $title->getPrefixedText(),
242 'watchlist' => 'unwatch',
243 ] );
244
245 $this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) );
246 $this->assertFalse( $watchlistManager->isWatched( $user, $title ) );
247 }
248 }