]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/media/MediaWikiMediaTestCase.php
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / media / MediaWikiMediaTestCase.php
1 <?php
2 /**
3  * Specificly for testing Media handlers. Sets up a FileRepo backend
4  */
5 abstract class MediaWikiMediaTestCase extends MediaWikiTestCase {
6
7         /** @var FileRepo */
8         protected $repo;
9         /** @var FSFileBackend */
10         protected $backend;
11         /** @var string */
12         protected $filePath;
13
14         protected function setUp() {
15                 parent::setUp();
16
17                 $this->filePath = $this->getFilePath();
18                 $containers = [ 'data' => $this->filePath ];
19                 if ( $this->createsThumbnails() ) {
20                         // We need a temp directory for the thumbnails
21                         // the container is named 'temp-thumb' because it is the
22                         // thumb directory for a repo named "temp".
23                         $containers['temp-thumb'] = $this->getNewTempDirectory();
24                 }
25
26                 $this->backend = new FSFileBackend( [
27                         'name' => 'localtesting',
28                         'wikiId' => wfWikiID(),
29                         'containerPaths' => $containers,
30                         'tmpDirectory' => $this->getNewTempDirectory()
31                 ] );
32                 $this->repo = new FileRepo( $this->getRepoOptions() );
33         }
34
35         /**
36          * @return array Argument for FileRepo constructor
37          */
38         protected function getRepoOptions() {
39                 return [
40                         'name' => 'temp',
41                         'url' => 'http://localhost/thumbtest',
42                         'backend' => $this->backend
43                 ];
44         }
45
46         /**
47          * The result of this method will set the file path to use,
48          * as well as the protected member $filePath
49          *
50          * @return string Path where files are
51          */
52         protected function getFilePath() {
53                 return __DIR__ . '/../../data/media/';
54         }
55
56         /**
57          * Will the test create thumbnails (and thus do we need to set aside
58          * a temporary directory for them?)
59          *
60          * Override this method if your test case creates thumbnails
61          *
62          * @return bool
63          */
64         protected function createsThumbnails() {
65                 return false;
66         }
67
68         /**
69          * Utility function: Get a new file object for a file on disk but not actually in db.
70          *
71          * File must be in the path returned by getFilePath()
72          * @param string $name File name
73          * @param string $type MIME type [optional]
74          * @return UnregisteredLocalFile
75          */
76         protected function dataFile( $name, $type = null ) {
77                 if ( !$type ) {
78                         // Autodetect by file extension for the lazy.
79                         $magic = MimeMagic::singleton();
80                         $parts = explode( $name, '.' );
81                         $type = $magic->guessTypesForExtension( $parts[count( $parts ) - 1] );
82                 }
83                 return new UnregisteredLocalFile( false, $this->repo,
84                         "mwstore://localtesting/data/$name", $type );
85         }
86 }