summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/media/MediaWikiMediaTestCase.php
blob: 8f28158dcd40bcafb07c7aed40d0f517fdb34510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
 * Specificly for testing Media handlers. Sets up a FSFile backend
 */
abstract class MediaWikiMediaTestCase extends MediaWikiTestCase {

	/** @var FSRepo */
	protected $repo;
	/** @var FSFileBackend */
	protected $backend;
	/** @var string */
	protected $filePath;


	protected function setUp() {
		parent::setUp();

		$this->filePath = $this->getFilePath();
		$containers = array( 'data' => $this->filePath );
		if ( $this->createsThumbnails() ) {
			// We need a temp directory for the thumbnails
			// the container is named 'temp-thumb' because it is the
			// thumb directory for a FSRepo named "temp".
			$containers['temp-thumb'] = $this->getNewTempDirectory();
		}

		$this->backend = new FSFileBackend( array(
			'name' => 'localtesting',
			'wikiId' => wfWikiId(),
			'containerPaths' => $containers
		) );
		$this->repo = new FSRepo( $this->getRepoOptions() );
	}

	/**
	 * @return array Argument for FSRepo constructor
	 */
	protected function getRepoOptions() {
		return array(
			'name' => 'temp',
			'url' => 'http://localhost/thumbtest',
			'backend' => $this->backend
		);
	}

	/**
	 * The result of this method will set the file path to use,
	 * as well as the protected member $filePath
	 *
	 * @return string Path where files are
	 */
	protected function getFilePath() {
		return __DIR__ . '/../../data/media/';
	}

	/**
	 * Will the test create thumbnails (and thus do we need to set aside
	 * a temporary directory for them?)
	 *
	 * Override this method if your test case creates thumbnails
	 *
	 * @return bool
	 */
	protected function createsThumbnails() {
		return false;
	}

	/**
	 * Utility function: Get a new file object for a file on disk but not actually in db.
	 *
	 * File must be in the path returned by getFilePath()
	 * @param string $name File name
	 * @param string $type MIME type [optional]
	 * @return UnregisteredLocalFile
	 */
	protected function dataFile( $name, $type = null ) {
		if ( !$type ) {
			// Autodetect by file extension for the lazy.
			$magic = MimeMagic::singleton();
			$parts = explode( $name, '.' );
			$type = $magic->guessTypesForExtension( $parts[count( $parts ) - 1] );
		}
		return new UnregisteredLocalFile( false, $this->repo,
			"mwstore://localtesting/data/$name", $type );
	}
}