summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/api/ApiDeleteTest.php
blob: 0f2bcc6145d94ff89a7a17dbdd39346a4d44de5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

/**
 * Tests for MediaWiki api.php?action=delete.
 *
 * @author Yifei He
 *
 * @group API
 * @group Database
 * @group medium
 *
 * @covers ApiDelete
 */
class ApiDeleteTest extends ApiTestCase {
	public function testDelete() {
		$name = 'Help:' . ucfirst( __FUNCTION__ );

		// create new page
		$this->editPage( $name, 'Some text' );

		// test deletion
		$apiResult = $this->doApiRequestWithToken( [
			'action' => 'delete',
			'title' => $name,
		] )[0];

		$this->assertArrayHasKey( 'delete', $apiResult );
		$this->assertArrayHasKey( 'title', $apiResult['delete'] );
		$this->assertSame( $name, $apiResult['delete']['title'] );
		$this->assertArrayHasKey( 'logid', $apiResult['delete'] );

		$this->assertFalse( Title::newFromText( $name )->exists() );
	}

	public function testDeleteNonexistent() {
		$this->setExpectedException( ApiUsageException::class,
			"The page you specified doesn't exist." );

		$this->doApiRequestWithToken( [
			'action' => 'delete',
			'title' => 'This page deliberately left nonexistent',
		] );
	}

	public function testDeletionWithoutPermission() {
		$this->setExpectedException( ApiUsageException::class,
			'The action you have requested is limited to users in the group:' );

		$name = 'Help:' . ucfirst( __FUNCTION__ );

		// create new page
		$this->editPage( $name, 'Some text' );

		// test deletion without permission
		try {
			$user = new User();
			$apiResult = $this->doApiRequest( [
				'action' => 'delete',
				'title' => $name,
				'token' => $user->getEditToken(),
			], null, null, $user );
		} finally {
			$this->assertTrue( Title::newFromText( $name )->exists() );
		}
	}

	public function testDeleteWithTag() {
		$name = 'Help:' . ucfirst( __FUNCTION__ );

		ChangeTags::defineTag( 'custom tag' );

		$this->editPage( $name, 'Some text' );

		$this->doApiRequestWithToken( [
			'action' => 'delete',
			'title' => $name,
			'tags' => 'custom tag',
		] );

		$this->assertFalse( Title::newFromText( $name )->exists() );

		$dbw = wfGetDB( DB_MASTER );
		$this->assertSame( 'custom tag', $dbw->selectField(
			[ 'change_tag', 'logging' ],
			'ct_tag',
			[
				'log_namespace' => NS_HELP,
				'log_title' => ucfirst( __FUNCTION__ ),
			],
			__METHOD__,
			[],
			[ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
		) );
	}

	public function testDeleteWithoutTagPermission() {
		$this->setExpectedException( ApiUsageException::class,
			'You do not have permission to apply change tags along with your changes.' );

		$name = 'Help:' . ucfirst( __FUNCTION__ );

		ChangeTags::defineTag( 'custom tag' );
		$this->setMwGlobals( 'wgRevokePermissions',
			[ 'user' => [ 'applychangetags' => true ] ] );

		$this->editPage( $name, 'Some text' );

		try {
			$this->doApiRequestWithToken( [
				'action' => 'delete',
				'title' => $name,
				'tags' => 'custom tag',
			] );
		} finally {
			$this->assertTrue( Title::newFromText( $name )->exists() );
		}
	}

	public function testDeleteAbortedByHook() {
		$this->setExpectedException( ApiUsageException::class,
			'Deletion aborted by hook. It gave no explanation.' );

		$name = 'Help:' . ucfirst( __FUNCTION__ );

		$this->editPage( $name, 'Some text' );

		$this->setTemporaryHook( 'ArticleDelete',
			function () {
				return false;
			}
		);

		try {
			$this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
		} finally {
			$this->assertTrue( Title::newFromText( $name )->exists() );
		}
	}

	public function testDeleteWatch() {
		$name = 'Help:' . ucfirst( __FUNCTION__ );
		$user = self::$users['sysop']->getUser();

		$this->editPage( $name, 'Some text' );
		$this->assertTrue( Title::newFromText( $name )->exists() );
		$this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );

		$this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );

		$this->assertFalse( Title::newFromText( $name )->exists() );
		$this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
	}

	public function testDeleteUnwatch() {
		$name = 'Help:' . ucfirst( __FUNCTION__ );
		$user = self::$users['sysop']->getUser();

		$this->editPage( $name, 'Some text' );
		$this->assertTrue( Title::newFromText( $name )->exists() );
		$user->addWatch( Title::newFromText( $name ) );
		$this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );

		$this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );

		$this->assertFalse( Title::newFromText( $name )->exists() );
		$this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
	}
}