Merge "Linker: more consistent whitespace parsing in formatLinksInComment"
[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 testDeleteNonexistent() {
45 $this->setExpectedException( ApiUsageException::class,
46 "The page you specified doesn't exist." );
47
48 $this->doApiRequestWithToken( [
49 'action' => 'delete',
50 'title' => 'This page deliberately left nonexistent',
51 ] );
52 }
53
54 public function testDeletionWithoutPermission() {
55 $this->setExpectedException( ApiUsageException::class,
56 'The action you have requested is limited to users in the group:' );
57
58 $name = 'Help:' . ucfirst( __FUNCTION__ );
59
60 // create new page
61 $this->editPage( $name, 'Some text' );
62
63 // test deletion without permission
64 try {
65 $user = new User();
66 $apiResult = $this->doApiRequest( [
67 'action' => 'delete',
68 'title' => $name,
69 'token' => $user->getEditToken(),
70 ], null, null, $user );
71 } finally {
72 $this->assertTrue( Title::newFromText( $name )->exists() );
73 }
74 }
75
76 public function testDeleteWithTag() {
77 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
78 $name = 'Help:' . ucfirst( __FUNCTION__ );
79
80 ChangeTags::defineTag( 'custom tag' );
81
82 $this->editPage( $name, 'Some text' );
83
84 $this->doApiRequestWithToken( [
85 'action' => 'delete',
86 'title' => $name,
87 'tags' => 'custom tag',
88 ] );
89
90 $this->assertFalse( Title::newFromText( $name )->exists() );
91
92 $dbw = wfGetDB( DB_MASTER );
93 $this->assertSame( 'custom tag', $dbw->selectField(
94 [ 'change_tag', 'logging' ],
95 'ct_tag',
96 [
97 'log_namespace' => NS_HELP,
98 'log_title' => ucfirst( __FUNCTION__ ),
99 ],
100 __METHOD__,
101 [],
102 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
103 ) );
104 }
105
106 public function testDeleteWithTagNewBackend() {
107 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
108 $name = 'Help:' . ucfirst( __FUNCTION__ );
109
110 ChangeTags::defineTag( 'custom tag' );
111
112 $this->editPage( $name, 'Some text' );
113
114 $this->doApiRequestWithToken( [
115 'action' => 'delete',
116 'title' => $name,
117 'tags' => 'custom tag',
118 ] );
119
120 $this->assertFalse( Title::newFromText( $name )->exists() );
121
122 $dbw = wfGetDB( DB_MASTER );
123 $this->assertSame( 'custom tag', $dbw->selectField(
124 [ 'change_tag', 'logging', 'change_tag_def' ],
125 'ctd_name',
126 [
127 'log_namespace' => NS_HELP,
128 'log_title' => ucfirst( __FUNCTION__ ),
129 ],
130 __METHOD__,
131 [],
132 [
133 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
134 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ]
135 ]
136 ) );
137 }
138
139 public function testDeleteWithoutTagPermission() {
140 $this->setExpectedException( ApiUsageException::class,
141 'You do not have permission to apply change tags along with your changes.' );
142
143 $name = 'Help:' . ucfirst( __FUNCTION__ );
144
145 ChangeTags::defineTag( 'custom tag' );
146 $this->setMwGlobals( 'wgRevokePermissions',
147 [ 'user' => [ 'applychangetags' => true ] ] );
148
149 $this->editPage( $name, 'Some text' );
150
151 try {
152 $this->doApiRequestWithToken( [
153 'action' => 'delete',
154 'title' => $name,
155 'tags' => 'custom tag',
156 ] );
157 } finally {
158 $this->assertTrue( Title::newFromText( $name )->exists() );
159 }
160 }
161
162 public function testDeleteAbortedByHook() {
163 $this->setExpectedException( ApiUsageException::class,
164 'Deletion aborted by hook. It gave no explanation.' );
165
166 $name = 'Help:' . ucfirst( __FUNCTION__ );
167
168 $this->editPage( $name, 'Some text' );
169
170 $this->setTemporaryHook( 'ArticleDelete',
171 function () {
172 return false;
173 }
174 );
175
176 try {
177 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
178 } finally {
179 $this->assertTrue( Title::newFromText( $name )->exists() );
180 }
181 }
182
183 public function testDeleteWatch() {
184 $name = 'Help:' . ucfirst( __FUNCTION__ );
185 $user = self::$users['sysop']->getUser();
186
187 $this->editPage( $name, 'Some text' );
188 $this->assertTrue( Title::newFromText( $name )->exists() );
189 $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
190
191 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );
192
193 $this->assertFalse( Title::newFromText( $name )->exists() );
194 $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
195 }
196
197 public function testDeleteUnwatch() {
198 $name = 'Help:' . ucfirst( __FUNCTION__ );
199 $user = self::$users['sysop']->getUser();
200
201 $this->editPage( $name, 'Some text' );
202 $this->assertTrue( Title::newFromText( $name )->exists() );
203 $user->addWatch( Title::newFromText( $name ) );
204 $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
205
206 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );
207
208 $this->assertFalse( Title::newFromText( $name )->exists() );
209 $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
210 }
211 }