Debian Codegraph @

extensions/AbuseFilter/tests/phpunit/AbuseFilterUploadTestTrait.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\MediaWikiServices;
5 use MediaWiki\Request\FauxRequest;
6 use MediaWiki\Tests\Api\RandomImageGenerator;
7 use MediaWiki\User\User;
8
9 /**
10 * This trait can be used to perform uploads in integration tests.
11 * NOTE: The implementing classes MUST extend MediaWikiIntegrationTestCase
12 * The tearDown method must clear the local file
13 * @todo This might be moved to MediaWikiIntegrationTestCase
14 *
15 * @method string getNewTempDirectory()
16 * @method overrideConfigValue( string $key, $value )
17 */
18 trait AbuseFilterUploadTestTrait {
19 /**
20 * @var string|null The path represented by this variable will be cleared in tearDown
21 */
22 protected $clearPath;
23
24 /**
25 * Clear any temporary uploads, should be called from tearDown
26 */
27 protected function clearUploads(): void {
28 if ( $this->clearPath ) {
29 $backend = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()->getBackend();
30 $backend->delete( [ 'src' => $this->clearPath ], [ 'force' => 1 ] );
31 $this->clearPath = null;
32 }
33 }
34
35 /**
36 * This is based on ApiUploadTestCase::fakeUploadFile
37 *
38 * @param User $user
39 * @param string $fileName
40 * @param string $pageText
41 * @param string $summary
42 * @return array [ Status, file path ]
43 */
44 protected function doUpload( User $user, string $fileName, string $pageText, string $summary ): array {
45 global $wgFileExtensions;
46
47 $this->overrideConfigValue( MainConfigNames::FileExtensions, [ ...$wgFileExtensions, 'svg' ] );
48 $imgGen = new RandomImageGenerator();
49 // Use SVG, since the ImageGenerator doesn't need anything special to create it
50 $format = 'svg';
51 $mime = 'image/svg+xml';
52 $filePath = $imgGen->writeImages( 1, $format, $this->getNewTempDirectory() )[0];
53 clearstatcache();
54 $request = new FauxRequest( [
55 'wpDestFile' => $fileName
56 ] );
57 $request->setUpload( 'wpUploadFile', [
58 'name' => $fileName,
59 'type' => $mime,
60 'tmp_name' => $filePath,
61 'error' => UPLOAD_ERR_OK,
62 'size' => filesize( $filePath ),
63 ] );
64 /** @var UploadFromFile $ub */
65 $ub = UploadBase::createFromRequest( $request );
66 $ub->verifyUpload();
67 $status = $ub->performUpload( $summary, $pageText, false, $user );
68 return [ $status, $ub->getLocalFile()->getPath() ];
69 }
70 }