Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / api / ApiPurgeTest.php
blobaa79fa94f42d66df4e8a977d77b9f0e8c5d81434
1 <?php
3 namespace MediaWiki\Tests\Api;
5 use MediaWiki\Context\RequestContext;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Permissions\Authority;
8 use MediaWiki\Permissions\PermissionStatus;
10 /**
11 * @group API
12 * @group Database
13 * @group medium
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( [
26 'action' => 'purge',
27 'titles' => "$existingPageTitle|$nonexistingPageTitle|%5D"
28 ] );
30 $resultByTitle = [];
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;
39 $this->assertEquals(
41 $existingPageTitle => [ 'purged' => true, 'ns' => NS_MAIN ],
42 $nonexistingPageTitle => [ 'missing' => true, 'ns' => NS_MAIN ],
43 '%5D' => [ 'invalid' => true ],
45 $resultByTitle,
46 'Result'
50 public function testAuthorize() {
51 $page1 = 'TestPage1';
52 $page2 = 'TestPage2';
53 $this->getExistingTestPage( $page1 );
54 $this->getExistingTestPage( $page2 );
56 $user = RequestContext::getMain()->getUser();
58 $authority = $this->createNoOpMock(
59 Authority::class,
61 'authorizeAction',
62 'getUser',
63 'isAllowed',
64 'getBlock'
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();
75 return false;
79 [ $data ] = $this->doApiRequest( [
80 'action' => 'purge',
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() {
91 $page1 = 'TestPage1';
92 $page2 = 'TestPage2';
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( [
103 'action' => 'purge',
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( [
118 'action' => 'purge',
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 );