mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-25 22:45:32 +00:00
6c8a29698b
Ideally, this might live in MediaWikiIntegrationTestCase. For the createaccount one, AuthManager should also provide a method to log the creation, because currently we are forced to copypaste that code here. - Add the missing tests for 'upload' in RCVariableGenerator, and adjust the existing ones (delete file afterwards, more tablesUsed, use the right extension). - Exclude from the coverage report a couple of lines which should theoretically be unreachable. Escalate logging to WARN level, where it's more likely to be spotted. - Remove an unused method (RCVariableGenerator::newFromID). This denies the need to maintain and cover it. We also don't want this generator to act as a factory. Overall, this change brings the coverage for RCVariableGenerator to 100% Bug: T201193 Change-Id: I425c3d9f6800f74eb6e4eda483b90cfb3bbbcb51
68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
/**
|
|
* This trait can be used to perform uploads in integration tests.
|
|
* NOTE: The implementing classes MUST extend MediaWikiIntegrationTestCase
|
|
* The tearDown method must clear $_FILES and the local file
|
|
* @todo This might be moved to MediaWikiIntegrationTestCase
|
|
*
|
|
* @method string getNewTempDirectory()
|
|
* @method setMwGlobals($pairs)
|
|
*/
|
|
trait AbuseFilterUploadTestTrait {
|
|
/**
|
|
* @var string|null The path represented by this variable will be cleared in tearDown
|
|
*/
|
|
protected $clearPath;
|
|
|
|
/**
|
|
* Clear any temporary uploads, should be called from tearDown
|
|
*/
|
|
protected function clearUploads() : void {
|
|
$_FILES = [];
|
|
if ( $this->clearPath ) {
|
|
$backend = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()->getBackend();
|
|
$backend->delete( [ 'src' => $this->clearPath ], [ 'force' => 1 ] );
|
|
$this->clearPath = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This is based on ApiUploadTestCase::fakeUploadFile
|
|
*
|
|
* @param User $user
|
|
* @param string $fileName
|
|
* @param string $pageText
|
|
* @param string $summary
|
|
* @return array [ Status, file path ]
|
|
*/
|
|
protected function doUpload( User $user, string $fileName, string $pageText, string $summary ) : array {
|
|
global $wgFileExtensions;
|
|
|
|
$this->setMwGlobals( [ 'wgFileExtensions' => array_merge( $wgFileExtensions, [ 'svg' ] ) ] );
|
|
$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
|
|
] );
|
|
/** @var UploadFromFile $ub */
|
|
$ub = UploadBase::createFromRequest( $request );
|
|
$ub->verifyUpload();
|
|
$status = $ub->performUpload( $summary, $pageText, false, $user );
|
|
return [ $status, $ub->getLocalFile()->getPath() ];
|
|
}
|
|
}
|