mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-24 06:03:49 +00:00
Add tests for 'upload' action
This adds some coverage for the *VariableGenerator classes. It's still not perfect, but something to start with in sight of future refactorings. Bug: T201193 Change-Id: Iafa85fb8623ea278ce6e42118df72751806382c2
This commit is contained in:
parent
241a5db604
commit
8fa9e6625a
|
@ -223,7 +223,7 @@ class RunVariableGenerator extends VariableGenerator {
|
|||
* @param UploadBase $upload
|
||||
* @param string|null $summary
|
||||
* @param string|null $text
|
||||
* @param array $props
|
||||
* @param array|null $props
|
||||
* @return AbuseFilterVariableHolder|null
|
||||
*/
|
||||
public function getUploadVars(
|
||||
|
@ -231,7 +231,7 @@ class RunVariableGenerator extends VariableGenerator {
|
|||
UploadBase $upload,
|
||||
?string $summary,
|
||||
?string $text,
|
||||
array $props
|
||||
?array $props
|
||||
) : ?AbuseFilterVariableHolder {
|
||||
$mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
|
||||
if ( !$props ) {
|
||||
|
|
|
@ -43,7 +43,6 @@ use PHPUnit\Framework\MockObject\MockObject;
|
|||
* @covers \MediaWiki\Extension\AbuseFilter\VariableGenerator\RunVariableGenerator
|
||||
* @covers AbuseFilterPreAuthenticationProvider
|
||||
* @covers AbuseFilterParser
|
||||
* @todo Add upload actions everywhere
|
||||
*/
|
||||
class AbuseFilterConsequencesTest extends MediaWikiTestCase {
|
||||
/**
|
||||
|
@ -297,6 +296,17 @@ class AbuseFilterConsequencesTest extends MediaWikiTestCase {
|
|||
'actions' => [
|
||||
'blockautopromote' => []
|
||||
]
|
||||
],
|
||||
22 => [
|
||||
'af_pattern' => 'action contains "upload" & "Block me" in added_lines & file_size > 0 & ' .
|
||||
'file_mime contains "/" & file_width + file_height > 0 & summary !== ""',
|
||||
'af_public_comments' => 'Filter for uploads',
|
||||
'actions' => [
|
||||
'warn' => [
|
||||
'abusefilter-random-upload'
|
||||
],
|
||||
'blockautopromote' => []
|
||||
]
|
||||
]
|
||||
];
|
||||
// phpcs:enable Generic.Files.LineLength
|
||||
|
@ -360,6 +370,8 @@ class AbuseFilterConsequencesTest extends MediaWikiTestCase {
|
|||
protected function tearDown() : void {
|
||||
// Paranoia: ensure no fake timestamp leftover
|
||||
MWTimestamp::setFakeTime( false );
|
||||
// Clear any upload
|
||||
$_FILES = [];
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
|
@ -506,6 +518,35 @@ class AbuseFilterConsequencesTest extends MediaWikiTestCase {
|
|||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a file. This is based on ApiUploadTestCase::fakeUploadFile
|
||||
* @param string $fileName
|
||||
* @param string $pageText
|
||||
* @param string $summary
|
||||
* @return Status
|
||||
*/
|
||||
private function doUpload( string $fileName, string $pageText, string $summary ) : Status {
|
||||
$imgGen = new RandomImageGenerator();
|
||||
// Use SVG, since the ImageGenerator doesn't need anything special to create it
|
||||
$format = 'svg';
|
||||
$mime = 'image/svg+xml';
|
||||
$filePath = $imgGen->writeImages( 1, $format, $this->getNewTempDirectory() )[0];
|
||||
clearstatcache();
|
||||
$_FILES[ 'wpUploadFile' ] = [
|
||||
'name' => $fileName,
|
||||
'type' => $mime,
|
||||
'tmp_name' => $filePath,
|
||||
'error' => UPLOAD_ERR_OK,
|
||||
'size' => filesize( $filePath ),
|
||||
];
|
||||
$request = new FauxRequest( [
|
||||
'wpDestFile' => $fileName
|
||||
] );
|
||||
$ub = UploadBase::createFromRequest( $request );
|
||||
$ub->verifyUpload();
|
||||
return $ub->performUpload( $summary, $pageText, false, $this->user );
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes an action to filter
|
||||
*
|
||||
|
@ -569,6 +610,13 @@ class AbuseFilterConsequencesTest extends MediaWikiTestCase {
|
|||
$logid = $logEntry->insert();
|
||||
$logEntry->publish( $logid );
|
||||
break;
|
||||
case 'upload':
|
||||
$status = $this->doUpload(
|
||||
$params['target'],
|
||||
$params['newText'] ?? 'AbuseFilter test upload',
|
||||
$params['summary'] ?? 'Test'
|
||||
);
|
||||
break;
|
||||
default:
|
||||
throw new UnexpectedValueException( 'Unrecognized action ' . $params['action'] );
|
||||
}
|
||||
|
@ -967,6 +1015,24 @@ class AbuseFilterConsequencesTest extends MediaWikiTestCase {
|
|||
],
|
||||
[]
|
||||
],
|
||||
'Test upload action' => [
|
||||
[ 5 ],
|
||||
[
|
||||
'action' => 'upload',
|
||||
'target' => 'MyFile.jpg',
|
||||
],
|
||||
[ 'tag' => [ 5 ] ]
|
||||
],
|
||||
'Test upload action 2' => [
|
||||
[ 22 ],
|
||||
[
|
||||
'action' => 'upload',
|
||||
'target' => 'MyFile.jpg',
|
||||
'newText' => 'Block me please!',
|
||||
'summary' => 'Asking to be blocked'
|
||||
],
|
||||
[ 'warn' => [ 22 ] ]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue