| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Page\File\FileDeleteForm; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
use Wikimedia\FileBackend\FSFile\FSFile; |
| 10 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
abstract class ApiUploadTestCase extends ApiTestCase { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
protected $requestDataFiles = []; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected function setUp(): void { |
| 27 |
parent::setUp(); |
| 28 |
|
| 29 |
$this->overrideConfigValue( MainConfigNames::EnableUploads, true ); |
| 30 |
$this->clearFakeUploads(); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function deleteFileByTitle( $title ) { |
| 41 |
if ( $title->exists() ) { |
| 42 |
$file = $this->getServiceContainer()->getRepoGroup() |
| 43 |
->findFile( $title, [ 'ignoreRedirect' => true ] ); |
| 44 |
$noOldArchive = ""; |
| 45 |
$comment = "removing for test"; |
| 46 |
$restrictDeletedVersions = false; |
| 47 |
$user = $this->getTestSysop()->getUser(); |
| 48 |
$status = FileDeleteForm::doDelete( |
| 49 |
$title, |
| 50 |
$file, |
| 51 |
$noOldArchive, |
| 52 |
$comment, |
| 53 |
$restrictDeletedVersions, |
| 54 |
$user |
| 55 |
); |
| 56 |
|
| 57 |
if ( !$status->isGood() ) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 62 |
$this->deletePage( $page, "removing for test" ); |
| 63 |
} |
| 64 |
|
| 65 |
return !( $title && $title instanceof Title && $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function deleteFileByFileName( $fileName ) { |
| 76 |
return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) ); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function deleteFileByContent( $filePath ) { |
| 88 |
$hash = FSFile::getSha1Base36FromPath( $filePath ); |
| 89 |
$dupes = $this->getServiceContainer()->getRepoGroup()->findBySha1( $hash ); |
| 90 |
$success = true; |
| 91 |
foreach ( $dupes as $dupe ) { |
| 92 |
$success &= $this->deleteFileByTitle( $dupe->getTitle() ); |
| 93 |
} |
| 94 |
|
| 95 |
return $success; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
protected function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) { |
| 111 |
$tmpName = $this->getNewTempFile(); |
| 112 |
if ( !is_file( $filePath ) ) { |
| 113 |
$this->fail( "$filePath doesn't exist!" ); |
| 114 |
} |
| 115 |
|
| 116 |
if ( !copy( $filePath, $tmpName ) ) { |
| 117 |
$this->fail( "couldn't copy $filePath to $tmpName" ); |
| 118 |
} |
| 119 |
|
| 120 |
clearstatcache(); |
| 121 |
$size = filesize( $tmpName ); |
| 122 |
if ( $size === false ) { |
| 123 |
$this->fail( "couldn't stat $tmpName" ); |
| 124 |
} |
| 125 |
|
| 126 |
$this->requestDataFiles[$fieldName] = [ |
| 127 |
'name' => $fileName, |
| 128 |
'type' => $type, |
| 129 |
'tmp_name' => $tmpName, |
| 130 |
'size' => $size, |
| 131 |
'error' => UPLOAD_ERR_OK, |
| 132 |
]; |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
public function fakeUploadChunk( $fieldName, $fileName, $type, &$chunkData ) { |
| 138 |
$tmpName = $this->getNewTempFile(); |
| 139 |
|
| 140 |
if ( !file_put_contents( $tmpName, $chunkData ) ) { |
| 141 |
$this->fail( "couldn't copy chunk data to $tmpName" ); |
| 142 |
} |
| 143 |
|
| 144 |
clearstatcache(); |
| 145 |
$size = filesize( $tmpName ); |
| 146 |
if ( $size === false ) { |
| 147 |
$this->fail( "couldn't stat $tmpName" ); |
| 148 |
} |
| 149 |
|
| 150 |
$this->requestDataFiles[$fieldName] = [ |
| 151 |
'name' => $fileName, |
| 152 |
'type' => $type, |
| 153 |
'tmp_name' => $tmpName, |
| 154 |
'size' => $size, |
| 155 |
'error' => UPLOAD_ERR_OK, |
| 156 |
]; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
protected function buildFauxRequest( $params, $session ) { |
| 161 |
$request = parent::buildFauxRequest( $params, $session ); |
| 162 |
$request->setUploadData( $this->requestDataFiles ); |
| 163 |
return $request; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function clearFakeUploads() { |
| 170 |
$this->requestDataFiles = []; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
class_alias( ApiUploadTestCase::class, 'ApiUploadTestCase' ); |