| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\FileRepo; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use LocalRepo; |
| 7 |
use LogicException; |
| 8 |
use MediaWiki\FileBackend\FileBackendGroup; |
| 9 |
use MediaWiki\MediaWikiServices; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use PHPUnit\Framework\Assert; |
| 12 |
use RepoGroup; |
| 13 |
use Wikimedia\FileBackend\FileBackend; |
| 14 |
use Wikimedia\FileBackend\FSFileBackend; |
| 15 |
|
| 16 |
trait TestRepoTrait { |
| 17 |
|
| 18 |
private static ?string $mockRepoTraitDir = null; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private function initTestRepoGroup(): RepoGroup { |
| 26 |
if ( self::$mockRepoTraitDir ) { |
| 27 |
throw new LogicException( 'Mock repo already initialized. ' . |
| 28 |
'initTestRepogroup() must only be called from addDBDataOnce() ' . |
| 29 |
'and must be paired with a call to destroyTestRepo() in ' . |
| 30 |
'tearDownAfterClass().' ); |
| 31 |
} |
| 32 |
|
| 33 |
$tmp = tempnam( wfTempDir(), 'mw-mock-repo-' ); |
| 34 |
|
| 35 |
|
| 36 |
if ( file_exists( $tmp ) ) { |
| 37 |
unlink( $tmp ); |
| 38 |
} |
| 39 |
mkdir( $tmp ); |
| 40 |
|
| 41 |
self::$mockRepoTraitDir = $tmp; |
| 42 |
$this->installTestRepoGroup(); |
| 43 |
return $this->getTestRepoGroup(); |
| 44 |
} |
| 45 |
|
| 46 |
private function getTestRepoGroup(): RepoGroup { |
| 47 |
if ( self::$mockRepoTraitDir === null ) { |
| 48 |
throw new LogicException( 'Mock repo not initialized. ' . |
| 49 |
'Call initTestRepo() from addDBDataOnce() and a call ' . |
| 50 |
'to destroyTestRepo() in tearDownAfterClass().' ); |
| 51 |
} |
| 52 |
|
| 53 |
return $this->getServiceContainer()->getRepoGroup(); |
| 54 |
} |
| 55 |
|
| 56 |
private function getTestRepo(): LocalRepo { |
| 57 |
return $this->getTestRepoGroup()->getLocalRepo(); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private static function destroyTestRepo() { |
| 65 |
if ( !self::$mockRepoTraitDir ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
$dir = self::$mockRepoTraitDir; |
| 70 |
|
| 71 |
if ( !is_dir( $dir ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
if ( !str_starts_with( $dir, wfTempDir() ) ) { |
| 76 |
throw new InvalidArgumentException( "Not in temp dir: $dir" ); |
| 77 |
} |
| 78 |
|
| 79 |
$name = basename( $dir ); |
| 80 |
if ( !str_starts_with( $name, 'mw-mock-repo-' ) ) { |
| 81 |
throw new InvalidArgumentException( "Not a mock repo dir: $dir" ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
self::$mockRepoTraitDir = null; |
| 87 |
} |
| 88 |
|
| 89 |
private function installTestRepoGroup( array $options = [] ) { |
| 90 |
$repoGroup = $this->createTestRepoGroup( $options ); |
| 91 |
$this->setService( 'RepoGroup', $repoGroup ); |
| 92 |
|
| 93 |
$this->installTestBackendGroup( $repoGroup->getLocalRepo()->getBackend() ); |
| 94 |
} |
| 95 |
|
| 96 |
private function createTestRepoGroup( $options = [], ?MediaWikiServices $services = null ) { |
| 97 |
$services ??= $this->getServiceContainer(); |
| 98 |
$localFileRepo = $this->getLocalFileRepoConfig( $options ); |
| 99 |
|
| 100 |
$mimeAnalyzer = $services->getMimeAnalyzer(); |
| 101 |
|
| 102 |
$repoGroup = new RepoGroup( |
| 103 |
$localFileRepo, |
| 104 |
[], |
| 105 |
$services->getMainWANObjectCache(), |
| 106 |
$mimeAnalyzer |
| 107 |
); |
| 108 |
return $repoGroup; |
| 109 |
} |
| 110 |
|
| 111 |
private function installTestBackendGroup( FileBackend $backend ) { |
| 112 |
$this->setService( 'FileBackendGroup', $this->createTestBackendGroup( $backend ) ); |
| 113 |
} |
| 114 |
|
| 115 |
private function createTestBackendGroup( FileBackend $backend ) { |
| 116 |
$expected = "mwstore://{$backend->getName()}/"; |
| 117 |
|
| 118 |
$backendGroup = $this->createNoOpMock( FileBackendGroup::class, [ 'backendFromPath' ] ); |
| 119 |
$backendGroup->method( 'backendFromPath' )->willReturnCallback( |
| 120 |
static function ( $path ) use ( $expected, $backend ) { |
| 121 |
if ( str_starts_with( $path, $expected ) ) { |
| 122 |
return $backend; |
| 123 |
} |
| 124 |
|
| 125 |
return null; |
| 126 |
} |
| 127 |
); |
| 128 |
|
| 129 |
return $backendGroup; |
| 130 |
} |
| 131 |
|
| 132 |
private function getLocalFileRepoConfig( $options = [] ): array { |
| 133 |
if ( self::$mockRepoTraitDir === null ) { |
| 134 |
throw new LogicException( 'Mock repo not initialized. ' . |
| 135 |
'Call initTestRepo() from addDBDataOnce() and a call ' . |
| 136 |
'to destroyTestRepo() in tearDownAfterClass().' ); |
| 137 |
} |
| 138 |
|
| 139 |
$options['directory'] ??= self::$mockRepoTraitDir; |
| 140 |
$options['scriptDirUrl'] ??= '/w'; |
| 141 |
|
| 142 |
$scriptPath = $options['scriptDirUrl']; |
| 143 |
$dir = $options['directory']; |
| 144 |
|
| 145 |
$info = $options + [ |
| 146 |
"class" => LocalRepo::class, |
| 147 |
"name" => "test", |
| 148 |
"domainId" => "mywiki", |
| 149 |
"directory" => $dir, |
| 150 |
"scriptDirUrl" => $scriptPath, |
| 151 |
"favicon" => "/favicon.ico", |
| 152 |
"url" => "$scriptPath/images", |
| 153 |
"hashLevels" => 2, |
| 154 |
"abbrvThreshold" => 16, |
| 155 |
"thumbScriptUrl" => "$scriptPath/thumb.php", |
| 156 |
"transformVia404" => false, |
| 157 |
"deletedDir" => "$dir/deleted", |
| 158 |
"deletedHashLevels" => 0, |
| 159 |
"updateCompatibleMetadata" => false, |
| 160 |
"reserializeMetadata" => false, |
| 161 |
"backend" => 'local-backend', |
| 162 |
]; |
| 163 |
|
| 164 |
if ( !$info['backend'] instanceof FileBackend ) { |
| 165 |
$info['backend'] = $this->createFileBackend( $info ); |
| 166 |
} |
| 167 |
|
| 168 |
return $info; |
| 169 |
} |
| 170 |
|
| 171 |
private function createFileBackend( array $info = [] ) { |
| 172 |
$dir = $info['directory'] ?? self::$mockRepoTraitDir; |
| 173 |
$name = $info['name'] ?? 'test'; |
| 174 |
|
| 175 |
$info += [ |
| 176 |
"domainId" => "mywiki", |
| 177 |
'name' => $info['backend'] ?? 'local-backend', |
| 178 |
'basePath' => $dir, |
| 179 |
'obResetFunc' => static function () { |
| 180 |
ob_end_flush(); |
| 181 |
}, |
| 182 |
'headerFunc' => function ( string $header ) { |
| 183 |
$this->recordHeader( $header ); |
| 184 |
}, |
| 185 |
'containerPaths' => [ |
| 186 |
"$name-public" => "$dir", |
| 187 |
"$name-thumb" => "$dir/thumb", |
| 188 |
"$name-transcoded" => "$dir/transcoded", |
| 189 |
"$name-deleted" => "$dir/deleted", |
| 190 |
"$name-temp" => "$dir/temp", |
| 191 |
] |
| 192 |
]; |
| 193 |
|
| 194 |
$overrides = $info['overrides'] ?? []; |
| 195 |
unset( $info['overrides'] ); |
| 196 |
|
| 197 |
if ( !$overrides ) { |
| 198 |
return new FSFileBackend( $info ); |
| 199 |
} |
| 200 |
|
| 201 |
$backend = $this->getMockBuilder( FSFileBackend::class ) |
| 202 |
->setConstructorArgs( [ $info ] ) |
| 203 |
->onlyMethods( array_keys( $overrides ) ) |
| 204 |
->getMock(); |
| 205 |
|
| 206 |
foreach ( $overrides as $name => $will ) { |
| 207 |
if ( is_callable( $will ) ) { |
| 208 |
$backend->method( $name )->willReturnCallback( $will ); |
| 209 |
} else { |
| 210 |
$backend->method( $name )->willReturn( $will ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return $backend; |
| 215 |
} |
| 216 |
|
| 217 |
private function importDirToTestRepo( string $dir ) { |
| 218 |
foreach ( new \DirectoryIterator( $dir ) as $name ) { |
| 219 |
$path = "$dir/$name"; |
| 220 |
if ( is_file( $path ) ) { |
| 221 |
$this->importFileToTestRepo( $path ); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
private function importFileToTestRepo( string $path, ?string $destName = null ) { |
| 227 |
$repo = self::getTestRepo(); |
| 228 |
|
| 229 |
$destName ??= pathinfo( $path, PATHINFO_BASENAME ); |
| 230 |
|
| 231 |
$title = Title::makeTitleSafe( NS_FILE, $destName ); |
| 232 |
$name = $title->getDBkey(); |
| 233 |
|
| 234 |
$file = $repo->newFile( $name ); |
| 235 |
$status = $file->upload( $path, 'test import', 'test image' ); |
| 236 |
|
| 237 |
if ( !$status->isOK() ) { |
| 238 |
Assert::fail( "Error recording file $name: " . $status->getWikiText() ); |
| 239 |
} |
| 240 |
|
| 241 |
return $file; |
| 242 |
} |
| 243 |
|
| 244 |
private function copyFileToTestBackend( string $src, string $dst ) { |
| 245 |
$repo = self::getTestRepo(); |
| 246 |
$backend = $repo->getBackend(); |
| 247 |
|
| 248 |
$zone = strstr( ltrim( $dst, '/' ), '/', true ); |
| 249 |
$name = basename( $dst ); |
| 250 |
|
| 251 |
$dstFile = $repo->newFile( $name ); |
| 252 |
$dst = $dstFile->getRel(); |
| 253 |
|
| 254 |
if ( $zone !== null ) { |
| 255 |
$zonePath = $repo->getZonePath( $zone ); |
| 256 |
|
| 257 |
if ( $zonePath ) { |
| 258 |
$dst = "$zonePath/$dst"; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
$dir = dirname( $dst ); |
| 263 |
|
| 264 |
if ( $dir !== '' ) { |
| 265 |
$status = $backend->prepare( |
| 266 |
[ 'op' => 'prepare', 'dir' => $dir ] |
| 267 |
); |
| 268 |
|
| 269 |
if ( !$status->isOK() ) { |
| 270 |
Assert::fail( "Error copying file $src to $dst: " . $status ); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
$status = $backend->store( |
| 275 |
[ 'op' => 'store', 'src' => $src, 'dst' => $dst, ], |
| 276 |
); |
| 277 |
|
| 278 |
if ( !$status->isOK() ) { |
| 279 |
Assert::fail( "Error copying file $src to $dst: " . $status ); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
private function recordHeader( string $header ) { |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
} |