Merge "maintenance: Document secondary purpose of --server"
[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 public function testDelete() {
16 $name = 'Help:' . ucfirst( __FUNCTION__ );
17
18 // create new page
19 $this->editPage( $name, 'Some text' );
20
21 // test deletion
22 $apiResult = $this->doApiRequestWithToken( [
23 'action' => 'delete',
24 'title' => $name,
25 ] )[0];
26
27 $this->assertArrayHasKey( 'delete', $apiResult );
28 $this->assertArrayHasKey( 'title', $apiResult['delete'] );
29 $this->assertSame( $name, $apiResult['delete']['title'] );
30 $this->assertArrayHasKey( 'logid', $apiResult['delete'] );
31
32 $this->assertFalse( Title::newFromText( $name )->exists() );
33 }
34
35 public function testDeleteNonexistent() {
36 $this->setExpectedException( ApiUsageException::class,
37 "The page you specified doesn't exist." );
38
39 $this->doApiRequestWithToken( [
40 'action' => 'delete',
41 'title' => 'This page deliberately left nonexistent',
42 ] );
43 }
44
45 public function testDeletionWithoutPermission() {
46 $this->setExpectedException( ApiUsageException::class,
47 'The action you have requested is limited to users in the group:' );
48
49 $name = 'Help:' . ucfirst( __FUNCTION__ );
50
51 // create new page
52 $this->editPage( $name, 'Some text' );
53
54 // test deletion without permission
55 try {
56 $user = new User();
57 $apiResult = $this->doApiRequest( [
58 'action' => 'delete',
59 'title' => $name,
60 'token' => $user->getEditToken(),
61 ], null, null, $user );
62 } finally {
63 $this->assertTrue( Title::newFromText( $name )->exists() );
64 }
65 }
66
67 public function testDeleteWithTag() {
68 $name = 'Help:' . ucfirst( __FUNCTION__ );
69
70 ChangeTags::defineTag( 'custom tag' );
71
72 $this->editPage( $name, 'Some text' );
73
74 $this->doApiRequestWithToken( [
75 'action' => 'delete',
76 'title' => $name,
77 'tags' => 'custom tag',
78 ] );
79
80 $this->assertFalse( Title::newFromText( $name )->exists() );
81
82 $dbw = wfGetDB( DB_MASTER );
83 $this->assertSame( 'custom tag', $dbw->selectField(
84 [ 'change_tag', 'logging' ],
85 'ct_tag',
86 [
87 'log_namespace' => NS_HELP,
88 'log_title' => ucfirst( __FUNCTION__ ),
89 ],
90 __METHOD__,
91 [],
92 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
93 ) );
94 }
95
96 public function testDeleteWithoutTagPermission() {
97 $this->setExpectedException( ApiUsageException::class,
98 'You do not have permission to apply change tags along with your changes.' );
99
100 $name = 'Help:' . ucfirst( __FUNCTION__ );
101
102 ChangeTags::defineTag( 'custom tag' );
103 $this->setMwGlobals( 'wgRevokePermissions',
104 [ 'user' => [ 'applychangetags' => true ] ] );
105
106 $this->editPage( $name, 'Some text' );
107
108 try {
109 $this->doApiRequestWithToken( [
110 'action' => 'delete',
111 'title' => $name,
112 'tags' => 'custom tag',
113 ] );
114 } finally {
115 $this->assertTrue( Title::newFromText( $name )->exists() );
116 }
117 }
118
119 public function testDeleteAbortedByHook() {
120 $this->setExpectedException( ApiUsageException::class,
121 'Deletion aborted by hook. It gave no explanation.' );
122
123 $name = 'Help:' . ucfirst( __FUNCTION__ );
124
125 $this->editPage( $name, 'Some text' );
126
127 $this->setTemporaryHook( 'ArticleDelete',
128 function () {
129 return false;
130 }
131 );
132
133 try {
134 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
135 } finally {
136 $this->assertTrue( Title::newFromText( $name )->exists() );
137 }
138 }
139
140 public function testDeleteWatch() {
141 $name = 'Help:' . ucfirst( __FUNCTION__ );
142 $user = self::$users['sysop']->getUser();
143
144 $this->editPage( $name, 'Some text' );
145 $this->assertTrue( Title::newFromText( $name )->exists() );
146 $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
147
148 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );
149
150 $this->assertFalse( Title::newFromText( $name )->exists() );
151 $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
152 }
153
154 public function testDeleteUnwatch() {
155 $name = 'Help:' . ucfirst( __FUNCTION__ );
156 $user = self::$users['sysop']->getUser();
157
158 $this->editPage( $name, 'Some text' );
159 $this->assertTrue( Title::newFromText( $name )->exists() );
160 $user->addWatch( Title::newFromText( $name ) );
161 $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
162
163 $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );
164
165 $this->assertFalse( Title::newFromText( $name )->exists() );
166 $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
167 }
168 }