MediaWiki REL1_31
ApiDeleteTest.php
Go to the documentation of this file.
1<?php
2
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
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
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}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Tests for MediaWiki api.php?action=delete.
testDeleteWithoutTagPermission()
testDeletionWithoutPermission()
editPage( $pageName, $text, $summary='', $defaultNs=NS_MAIN)
Edits or creates a page/revision.
doApiRequestWithToken(array $params, array $session=null, User $user=null, $tokenType='auto')
Convenience function to access the token parameter of doApiRequest() more succinctly.
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
static defineTag( $tag)
Defines a tag in the valid_tag table, without checking that the tag name is valid.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
const NS_HELP
Definition Defines.php:86
const DB_MASTER
Definition defines.php:29