aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/api/ApiPurgeTest.php
blob: aa79fa94f42d66df4e8a977d77b9f0e8c5d81434 (plain) (blame)
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
<?php

namespace MediaWiki\Tests\Api;

use MediaWiki\Context\RequestContext;
use MediaWiki\MainConfigNames;
use MediaWiki\Permissions\Authority;
use MediaWiki\Permissions\PermissionStatus;

/**
 * @group API
 * @group Database
 * @group medium
 *
 * @covers \MediaWiki\Api\ApiPurge
 */
class ApiPurgeTest extends ApiTestCase {

	public function testPurgePage() {
		$existingPageTitle = 'TestPurgePageExists';
		$this->getExistingTestPage( $existingPageTitle );
		$nonexistingPageTitle = 'TestPurgePageDoesNotExists';
		$this->getNonexistingTestPage( $nonexistingPageTitle );

		[ $data ] = $this->doApiRequest( [
			'action' => 'purge',
			'titles' => "$existingPageTitle|$nonexistingPageTitle|%5D"
		] );

		$resultByTitle = [];
		foreach ( $data['purge'] as $entry ) {
			$key = $entry['title'];
			// Ignore localised or redundant field
			unset( $entry['invalidreason'] );
			unset( $entry['title'] );
			$resultByTitle[$key] = $entry;
		}

		$this->assertEquals(
			[
				$existingPageTitle => [ 'purged' => true, 'ns' => NS_MAIN ],
				$nonexistingPageTitle => [ 'missing' => true, 'ns' => NS_MAIN ],
				'%5D' => [ 'invalid' => true ],
			],
			$resultByTitle,
			'Result'
		);
	}

	public function testAuthorize() {
		$page1 = 'TestPage1';
		$page2 = 'TestPage2';
		$this->getExistingTestPage( $page1 );
		$this->getExistingTestPage( $page2 );

		$user = RequestContext::getMain()->getUser();

		$authority = $this->createNoOpMock(
			Authority::class,
			[
				'authorizeAction',
				'getUser',
				'isAllowed',
				'getBlock'
			]
		);

		$authority->method( 'getUser' )->willReturn( $user );
		$authority->method( 'getBlock' )->willReturn( null );
		$authority->method( 'isAllowed' )->willReturn( true );
		$authority->method( 'authorizeAction' )->willReturnCallback(
			static function ( string $action, PermissionStatus $status ) {
				$status->setRateLimitExceeded();

				return false;
			}
		);

		[ $data ] = $this->doApiRequest( [
			'action' => 'purge',
			'titles' => "$page1|$page2"
		], null, false, $authority );

		$this->assertNotEmpty( $data['warnings']['purge']['warnings'] );
		$warnings = $data['warnings']['purge']['warnings'];

		$this->assertStringContainsString( 'exceeded your rate limit', $warnings );
	}

	public function testAuthorizeRateLimit() {
		$page1 = 'TestPage1';
		$page2 = 'TestPage2';
		$this->getExistingTestPage( $page1 );
		$this->getExistingTestPage( $page2 );

		$authority = $this->getTestUser()->getAuthority();

		// purge is limited, linkpurge is not limited
		$this->overrideConfigValue( MainConfigNames::RateLimits,
			[ 'purge' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ]
		);
		[ $data ] = $this->doApiRequest( [
			'action' => 'purge',
			'titles' => "$page1|$page2",
			'forcelinkupdate' => '',
		], null, false, $authority );

		$this->assertNotEmpty( $data['warnings']['purge']['warnings'] );
		$warnings = $data['warnings']['purge']['warnings'];

		$this->assertStringContainsString( 'exceeded your rate limit', $warnings );

		// purge is not limited, linkpurge is limited
		$this->overrideConfigValue( MainConfigNames::RateLimits,
			[ 'linkpurge' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ]
		);
		[ $data ] = $this->doApiRequest( [
			'action' => 'purge',
			'titles' => "$page1|$page2",
			'forcelinkupdate' => '',
		], null, false, $authority );

		$this->assertNotEmpty( $data['warnings']['purge']['warnings'] );
		$warnings = $data['warnings']['purge']['warnings'];

		$this->assertStringContainsString( 'exceeded your rate limit', $warnings );
	}
}