MediaWikiMediaTestCase.php 2.3 KB

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