MediaWiki  REL1_31
ApiDeleteTest.php
Go to the documentation of this file.
1 <?php
2 
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 }
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:273
NS_HELP
const NS_HELP
Definition: Defines.php:86
ApiDeleteTest\testDeleteUnwatch
testDeleteUnwatch()
Definition: ApiDeleteTest.php:154
ApiDeleteTest\testDeleteWithTag
testDeleteWithTag()
Definition: ApiDeleteTest.php:67
User
User
Definition: All_system_messages.txt:425
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:100
ApiDeleteTest
Tests for MediaWiki api.php?action=delete.
Definition: ApiDeleteTest.php:14
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
ApiTestCase\doApiRequestWithToken
doApiRequestWithToken(array $params, array $session=null, User $user=null, $tokenType='auto')
Convenience function to access the token parameter of doApiRequest() more succinctly.
Definition: ApiTestCase.php:170
ChangeTags\defineTag
static defineTag( $tag)
Defines a tag in the valid_tag table, without checking that the tag name is valid.
Definition: ChangeTags.php:825
DB_MASTER
const DB_MASTER
Definition: defines.php:29
ApiTestCase
Definition: ApiTestCase.php:5
ApiTestCase\editPage
editPage( $pageName, $text, $summary='', $defaultNs=NS_MAIN)
Edits or creates a page/revision.
Definition: ApiTestCase.php:52
ApiDeleteTest\testDeleteNonexistent
testDeleteNonexistent()
Definition: ApiDeleteTest.php:35
ApiDeleteTest\testDeleteWithoutTagPermission
testDeleteWithoutTagPermission()
Definition: ApiDeleteTest.php:96
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ApiDeleteTest\testDeleteAbortedByHook
testDeleteAbortedByHook()
Definition: ApiDeleteTest.php:119
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
MediaWikiTestCase\setTemporaryHook
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
Definition: MediaWikiTestCase.php:2014
ApiDeleteTest\testDeleteWatch
testDeleteWatch()
Definition: ApiDeleteTest.php:140
ApiDeleteTest\testDeletionWithoutPermission
testDeletionWithoutPermission()
Definition: ApiDeleteTest.php:45
ApiDeleteTest\testDelete
testDelete()
Definition: ApiDeleteTest.php:15