3 namespace MediaWiki\Tests\Api
;
5 use MediaWiki\Context\RequestContext
;
6 use MediaWiki\MainConfigNames
;
7 use MediaWiki\Permissions\Authority
;
8 use MediaWiki\Permissions\PermissionStatus
;
15 * @covers \MediaWiki\Api\ApiPurge
17 class ApiPurgeTest
extends ApiTestCase
{
19 public function testPurgePage() {
20 $existingPageTitle = 'TestPurgePageExists';
21 $this->getExistingTestPage( $existingPageTitle );
22 $nonexistingPageTitle = 'TestPurgePageDoesNotExists';
23 $this->getNonexistingTestPage( $nonexistingPageTitle );
25 [ $data ] = $this->doApiRequest( [
27 'titles' => "$existingPageTitle|$nonexistingPageTitle|%5D"
31 foreach ( $data['purge'] as $entry ) {
32 $key = $entry['title'];
33 // Ignore localised or redundant field
34 unset( $entry['invalidreason'] );
35 unset( $entry['title'] );
36 $resultByTitle[$key] = $entry;
41 $existingPageTitle => [ 'purged' => true, 'ns' => NS_MAIN
],
42 $nonexistingPageTitle => [ 'missing' => true, 'ns' => NS_MAIN
],
43 '%5D' => [ 'invalid' => true ],
50 public function testAuthorize() {
53 $this->getExistingTestPage( $page1 );
54 $this->getExistingTestPage( $page2 );
56 $user = RequestContext
::getMain()->getUser();
58 $authority = $this->createNoOpMock(
68 $authority->method( 'getUser' )->willReturn( $user );
69 $authority->method( 'getBlock' )->willReturn( null );
70 $authority->method( 'isAllowed' )->willReturn( true );
71 $authority->method( 'authorizeAction' )->willReturnCallback(
72 static function ( string $action, PermissionStatus
$status ) {
73 $status->setRateLimitExceeded();
79 [ $data ] = $this->doApiRequest( [
81 'titles' => "$page1|$page2"
82 ], null, false, $authority );
84 $this->assertNotEmpty( $data['warnings']['purge']['warnings'] );
85 $warnings = $data['warnings']['purge']['warnings'];
87 $this->assertStringContainsString( 'exceeded your rate limit', $warnings );
90 public function testAuthorizeRateLimit() {
93 $this->getExistingTestPage( $page1 );
94 $this->getExistingTestPage( $page2 );
96 $authority = $this->getTestUser()->getAuthority();
98 // purge is limited, linkpurge is not limited
99 $this->overrideConfigValue( MainConfigNames
::RateLimits
,
100 [ 'purge' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ]
102 [ $data ] = $this->doApiRequest( [
104 'titles' => "$page1|$page2",
105 'forcelinkupdate' => '',
106 ], null, false, $authority );
108 $this->assertNotEmpty( $data['warnings']['purge']['warnings'] );
109 $warnings = $data['warnings']['purge']['warnings'];
111 $this->assertStringContainsString( 'exceeded your rate limit', $warnings );
113 // purge is not limited, linkpurge is limited
114 $this->overrideConfigValue( MainConfigNames
::RateLimits
,
115 [ 'linkpurge' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ]
117 [ $data ] = $this->doApiRequest( [
119 'titles' => "$page1|$page2",
120 'forcelinkupdate' => '',
121 ], null, false, $authority );
123 $this->assertNotEmpty( $data['warnings']['purge']['warnings'] );
124 $warnings = $data['warnings']['purge']['warnings'];
126 $this->assertStringContainsString( 'exceeded your rate limit', $warnings );