3 use MediaWiki\WikiMap\WikiMap
;
4 use Wikimedia\FileBackend\FSFileBackend
;
7 * Specificly for testing Media handlers. Sets up a FileRepo backend
9 abstract class MediaWikiMediaTestCase
extends MediaWikiIntegrationTestCase
{
13 /** @var FSFileBackend */
18 protected function setUp(): void
{
21 $this->filePath
= $this->getFilePath();
22 $containers = [ 'data' => $this->filePath
];
23 if ( $this->createsThumbnails() ) {
24 // We need a temp directory for the thumbnails
25 // the container is named 'temp-thumb' because it is the
26 // thumb directory for a repo named "temp".
27 $containers['temp-thumb'] = $this->getNewTempDirectory();
30 $this->backend
= new FSFileBackend( [
31 'name' => 'localtesting',
32 'wikiId' => WikiMap
::getCurrentWikiId(),
33 'containerPaths' => $containers,
34 'tmpDirectory' => $this->getNewTempDirectory(),
35 'obResetFunc' => static function () {
36 // do nothing, we need the output buffer in tests
39 $this->repo
= new FileRepo( $this->getRepoOptions() );
43 * @return array Argument for FileRepo constructor
45 protected function getRepoOptions() {
48 'url' => 'http://localhost/thumbtest',
49 'backend' => $this->backend
54 * The result of this method will set the file path to use,
55 * as well as the protected member $filePath
57 * @return string Path where files are
59 protected function getFilePath() {
60 return __DIR__
. '/../../data/media/';
64 * Will the test create thumbnails (and thus do we need to set aside
65 * a temporary directory for them?)
67 * Override this method if your test case creates thumbnails
71 protected function createsThumbnails() {
76 * Utility function: Get a new file object for a file on disk but not actually in db.
78 * File must be in the path returned by getFilePath()
79 * @param string $name File name
80 * @param string|false $type MIME type [optional]
81 * @return UnregisteredLocalFile
83 protected function dataFile( $name, $type = false ) {
84 return new UnregisteredLocalFile( false, $this->repo
,
85 "mwstore://localtesting/data/$name", $type );
89 * Get a mock LocalFile with the specified metadata, specified as a
90 * serialized string. The metadata-related methods will return this
91 * metadata. The behaviour of the other methods is undefined.
94 * @param string $metadata
97 protected function getMockFileWithMetadata( $metadata ) {
98 return new class( $metadata ) extends LocalFile
{
99 public function __construct( $metadata ) {
100 $this->loadMetadataFromString( $metadata );
101 $this->dataLoaded
= true;