| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use MediaWiki\Context\RequestContext; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Permissions\Authority; |
| 8 |
use MediaWiki\Permissions\PermissionStatus; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ApiPurgeTest extends ApiTestCase { |
| 18 |
|
| 19 |
public function testPurgePage() { |
| 20 |
$existingPageTitle = 'TestPurgePageExists'; |
| 21 |
$this->getExistingTestPage( $existingPageTitle ); |
| 22 |
$nonexistingPageTitle = 'TestPurgePageDoesNotExists'; |
| 23 |
$this->getNonexistingTestPage( $nonexistingPageTitle ); |
| 24 |
|
| 25 |
[ $data ] = $this->doApiRequest( [ |
| 26 |
'action' => 'purge', |
| 27 |
'titles' => "$existingPageTitle|$nonexistingPageTitle|%5D" |
| 28 |
] ); |
| 29 |
|
| 30 |
$resultByTitle = []; |
| 31 |
foreach ( $data['purge'] as $entry ) { |
| 32 |
$key = $entry['title']; |
| 33 |
|
| 34 |
unset( $entry['invalidreason'] ); |
| 35 |
unset( $entry['title'] ); |
| 36 |
$resultByTitle[$key] = $entry; |
| 37 |
} |
| 38 |
|
| 39 |
$this->assertEquals( |
| 40 |
[ |
| 41 |
$existingPageTitle => [ 'purged' => true, 'ns' => NS_MAIN ], |
| 42 |
$nonexistingPageTitle => [ 'missing' => true, 'ns' => NS_MAIN ], |
| 43 |
'%5D' => [ 'invalid' => true ], |
| 44 |
], |
| 45 |
$resultByTitle, |
| 46 |
'Result' |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
public function testAuthorize() { |
| 51 |
$page1 = 'TestPage1'; |
| 52 |
$page2 = 'TestPage2'; |
| 53 |
$this->getExistingTestPage( $page1 ); |
| 54 |
$this->getExistingTestPage( $page2 ); |
| 55 |
|
| 56 |
$user = RequestContext::getMain()->getUser(); |
| 57 |
|
| 58 |
$authority = $this->createNoOpMock( |
| 59 |
Authority::class, |
| 60 |
[ |
| 61 |
'authorizeAction', |
| 62 |
'getUser', |
| 63 |
'isAllowed', |
| 64 |
'getBlock' |
| 65 |
] |
| 66 |
); |
| 67 |
|
| 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(); |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
); |
| 78 |
|
| 79 |
[ $data ] = $this->doApiRequest( [ |
| 80 |
'action' => 'purge', |
| 81 |
'titles' => "$page1|$page2" |
| 82 |
], null, false, $authority ); |
| 83 |
|
| 84 |
$this->assertNotEmpty( $data['warnings']['purge']['warnings'] ); |
| 85 |
$warnings = $data['warnings']['purge']['warnings']; |
| 86 |
|
| 87 |
$this->assertStringContainsString( 'exceeded your rate limit', $warnings ); |
| 88 |
} |
| 89 |
|
| 90 |
public function testAuthorizeRateLimit() { |
| 91 |
$page1 = 'TestPage1'; |
| 92 |
$page2 = 'TestPage2'; |
| 93 |
$this->getExistingTestPage( $page1 ); |
| 94 |
$this->getExistingTestPage( $page2 ); |
| 95 |
|
| 96 |
$authority = $this->getTestUser()->getAuthority(); |
| 97 |
|
| 98 |
|
| 99 |
$this->overrideConfigValue( MainConfigNames::RateLimits, |
| 100 |
[ 'purge' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] |
| 101 |
); |
| 102 |
[ $data ] = $this->doApiRequest( [ |
| 103 |
'action' => 'purge', |
| 104 |
'titles' => "$page1|$page2", |
| 105 |
'forcelinkupdate' => '', |
| 106 |
], null, false, $authority ); |
| 107 |
|
| 108 |
$this->assertNotEmpty( $data['warnings']['purge']['warnings'] ); |
| 109 |
$warnings = $data['warnings']['purge']['warnings']; |
| 110 |
|
| 111 |
$this->assertStringContainsString( 'exceeded your rate limit', $warnings ); |
| 112 |
|
| 113 |
|
| 114 |
$this->overrideConfigValue( MainConfigNames::RateLimits, |
| 115 |
[ 'linkpurge' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] |
| 116 |
); |
| 117 |
[ $data ] = $this->doApiRequest( [ |
| 118 |
'action' => 'purge', |
| 119 |
'titles' => "$page1|$page2", |
| 120 |
'forcelinkupdate' => '', |
| 121 |
], null, false, $authority ); |
| 122 |
|
| 123 |
$this->assertNotEmpty( $data['warnings']['purge']['warnings'] ); |
| 124 |
$warnings = $data['warnings']['purge']['warnings']; |
| 125 |
|
| 126 |
$this->assertStringContainsString( 'exceeded your rate limit', $warnings ); |
| 127 |
} |
| 128 |
} |