Merge "Drop $wgChangeTagsSchemaMigrationStage"
[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 $name = 'Help:' . ucfirst( __FUNCTION__ );
78
79 ChangeTags::defineTag( 'custom tag' );
80
81 $this->editPage( $name, 'Some text' );
82
83 $this->doApiRequestWithToken( [
84 'action' => 'delete',
85 'title' => $name,
86 'tags' => 'custom tag',
87 ] );
88
89 $this->assertFalse( Title::newFromText( $name )->exists() );
90
91 $dbw = wfGetDB( DB_MASTER );
92 $this->assertSame( 'custom tag', $dbw->selectField(
93 [ 'change_tag', 'logging', 'change_tag_def' ],
94 'ctd_name',
95 [
96 'log_namespace' => NS_HELP,
97 'log_title' => ucfirst( __FUNCTION__ ),
98 ],
99 __METHOD__,
100 [],
101 [
102 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
103 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ]
104 ]
105 ) );
106 }
107
108 public function testDeleteWithoutTagPermission() {
109 $this->setExpectedException( ApiUsageException::class,
110 'You do not have permission to apply change tags along with your changes.' );
111
112 $name = 'Help:' . ucfirst( __FUNCTION__ );
113
114 ChangeTags::defineTag( 'custom tag' );
115 $this->setMwGlobals( 'wgRevokePermissions',
116 [ 'user' => [ 'applychangetags' => true ] ] );
117
118 $this->editPage( $name, 'Some text' );
119
120 try {
121 $this->doApiRequestWithToken( [
122 'action' => 'delete',
123 'title' => $name,
124 'tags' => 'custom tag',
125 ] );
126 } finally {
127 $this->assertTrue( Title::newFromText( $name )->exists() );
128 }
129 }
130
131 public function testDeleteAbortedByHook() {
132 $this->setExpectedException( ApiUsageException::class,
133 'Deletion aborted by hook. It gave no explanation.' );
134
135 $name = 'Help:' . ucfirst( __FUNCTION__ );
136
137 $this->editPage( $name, 'Some text' );
138
139 $this->setTemporaryHook( 'ArticleDelete',
140 function () {
141 return false;
142 }
143 );
144
145 try {
146 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
147 } finally {
148 $this->assertTrue( Title::newFromText( $name )->exists() );
149 }
150 }
151
152 public function testDeleteWatch() {
153 $name = 'Help:' . ucfirst( __FUNCTION__ );
154 $user = self::$users['sysop']->getUser();
155
156 $this->editPage( $name, 'Some text' );
157 $this->assertTrue( Title::newFromText( $name )->exists() );
158 $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
159
160 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );
161
162 $this->assertFalse( Title::newFromText( $name )->exists() );
163 $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
164 }
165
166 public function testDeleteUnwatch() {
167 $name = 'Help:' . ucfirst( __FUNCTION__ );
168 $user = self::$users['sysop']->getUser();
169
170 $this->editPage( $name, 'Some text' );
171 $this->assertTrue( Title::newFromText( $name )->exists() );
172 $user->addWatch( Title::newFromText( $name ) );
173 $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
174
175 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );
176
177 $this->assertFalse( Title::newFromText( $name )->exists() );
178 $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
179 }
180 }