Merge "Reset scoped session for upload jobs after deferred updates"
[lhc/web/wiklou.git] / tests / phpunit / includes / media / MediaWikiMediaTestCase.php
1 <?php
2 /**
3 * Specificly for testing Media handlers. Sets up a FSFile backend
4 */
5 abstract class MediaWikiMediaTestCase extends MediaWikiTestCase {
6
7 /** @var FSRepo */
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 FSRepo 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 ] );
31 $this->repo = new FSRepo( $this->getRepoOptions() );
32 }
33
34 /**
35 * @return array Argument for FSRepo constructor
36 */
37 protected function getRepoOptions() {
38 return [
39 'name' => 'temp',
40 'url' => 'http://localhost/thumbtest',
41 'backend' => $this->backend
42 ];
43 }
44
45 /**
46 * The result of this method will set the file path to use,
47 * as well as the protected member $filePath
48 *
49 * @return string Path where files are
50 */
51 protected function getFilePath() {
52 return __DIR__ . '/../../data/media/';
53 }
54
55 /**
56 * Will the test create thumbnails (and thus do we need to set aside
57 * a temporary directory for them?)
58 *
59 * Override this method if your test case creates thumbnails
60 *
61 * @return bool
62 */
63 protected function createsThumbnails() {
64 return false;
65 }
66
67 /**
68 * Utility function: Get a new file object for a file on disk but not actually in db.
69 *
70 * File must be in the path returned by getFilePath()
71 * @param string $name File name
72 * @param string $type MIME type [optional]
73 * @return UnregisteredLocalFile
74 */
75 protected function dataFile( $name, $type = null ) {
76 if ( !$type ) {
77 // Autodetect by file extension for the lazy.
78 $magic = MimeMagic::singleton();
79 $parts = explode( $name, '.' );
80 $type = $magic->guessTypesForExtension( $parts[count( $parts ) - 1] );
81 }
82 return new UnregisteredLocalFile( false, $this->repo,
83 "mwstore://localtesting/data/$name", $type );
84 }
85 }