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