Merge "Allow partially blocked users to tag unrelated revisions"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiDeleteTest.php
1 <?php
2
3 /**
4 * Tests for MediaWiki api.php?action=delete.
5 *
6 * @author Yifei He
7 *
8 * @group API
9 * @group Database
10 * @group medium
11 *
12 * @covers ApiDelete
13 */
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
65 JobQueueGroup::destroySingletons();
66 $jobs = new RunJobs;
67 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
68 $jobs->execute();
69
70 $this->assertFalse( Title::newFromText( $name )->exists( Title::READ_LATEST ) );
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
147 $this->editPage( $name, 'Some text' );
148
149 try {
150 $this->doApiRequestWithToken( [
151 'action' => 'delete',
152 'title' => $name,
153 'tags' => 'custom tag',
154 ] );
155 } finally {
156 $this->assertTrue( Title::newFromText( $name )->exists() );
157 }
158 }
159
160 public function testDeleteAbortedByHook() {
161 $this->setExpectedException( ApiUsageException::class,
162 'Deletion aborted by hook. It gave no explanation.' );
163
164 $name = 'Help:' . ucfirst( __FUNCTION__ );
165
166 $this->editPage( $name, 'Some text' );
167
168 $this->setTemporaryHook( 'ArticleDelete',
169 function () {
170 return false;
171 }
172 );
173
174 try {
175 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
176 } finally {
177 $this->assertTrue( Title::newFromText( $name )->exists() );
178 }
179 }
180
181 public function testDeleteWatch() {
182 $name = 'Help:' . ucfirst( __FUNCTION__ );
183 $user = self::$users['sysop']->getUser();
184
185 $this->editPage( $name, 'Some text' );
186 $this->assertTrue( Title::newFromText( $name )->exists() );
187 $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
188
189 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );
190
191 $this->assertFalse( Title::newFromText( $name )->exists() );
192 $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
193 }
194
195 public function testDeleteUnwatch() {
196 $name = 'Help:' . ucfirst( __FUNCTION__ );
197 $user = self::$users['sysop']->getUser();
198
199 $this->editPage( $name, 'Some text' );
200 $this->assertTrue( Title::newFromText( $name )->exists() );
201 $user->addWatch( Title::newFromText( $name ) );
202 $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
203
204 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );
205
206 $this->assertFalse( Title::newFromText( $name )->exists() );
207 $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
208 }
209 }