MimeTypeTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Drupal\KernelTests\Core\File;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. /**
  5. * Tests filename mimetype detection.
  6. *
  7. * @group File
  8. */
  9. class MimeTypeTest extends FileTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['file_test'];
  16. /**
  17. * Test mapping of mimetypes from filenames.
  18. */
  19. public function testFileMimeTypeDetection() {
  20. $prefixes = ['public://', 'private://', 'temporary://', 'dummy-remote://'];
  21. $test_case = [
  22. 'test.jar' => 'application/java-archive',
  23. 'test.jpeg' => 'image/jpeg',
  24. 'test.JPEG' => 'image/jpeg',
  25. 'test.jpg' => 'image/jpeg',
  26. 'test.jar.jpg' => 'image/jpeg',
  27. 'test.jpg.jar' => 'application/java-archive',
  28. 'test.pcf.Z' => 'application/x-font',
  29. 'pcf.z' => 'application/octet-stream',
  30. 'jar' => 'application/octet-stream',
  31. 'some.junk' => 'application/octet-stream',
  32. 'foo.file_test_1' => 'madeup/file_test_1',
  33. 'foo.file_test_2' => 'madeup/file_test_2',
  34. 'foo.doc' => 'madeup/doc',
  35. 'test.ogg' => 'audio/ogg',
  36. ];
  37. $guesser = $this->container->get('file.mime_type.guesser');
  38. // Test using default mappings.
  39. foreach ($test_case as $input => $expected) {
  40. // Test stream [URI].
  41. foreach ($prefixes as $prefix) {
  42. $output = $guesser->guess($prefix . $input);
  43. $this->assertIdentical($output, $expected, new FormattableMarkup('Mimetype for %input is %output (expected: %expected).', ['%input' => $prefix . $input, '%output' => $output, '%expected' => $expected]));
  44. }
  45. // Test normal path equivalent
  46. $output = $guesser->guess($input);
  47. $this->assertIdentical($output, $expected, new FormattableMarkup('Mimetype (using default mappings) for %input is %output (expected: %expected).', ['%input' => $input, '%output' => $output, '%expected' => $expected]));
  48. }
  49. // Now test the extension guesser by passing in a custom mapping.
  50. $mapping = [
  51. 'mimetypes' => [
  52. 0 => 'application/java-archive',
  53. 1 => 'image/jpeg',
  54. ],
  55. 'extensions' => [
  56. 'jar' => 0,
  57. 'jpg' => 1,
  58. ],
  59. ];
  60. $test_case = [
  61. 'test.jar' => 'application/java-archive',
  62. 'test.jpeg' => 'application/octet-stream',
  63. 'test.jpg' => 'image/jpeg',
  64. 'test.jar.jpg' => 'image/jpeg',
  65. 'test.jpg.jar' => 'application/java-archive',
  66. 'test.pcf.z' => 'application/octet-stream',
  67. 'pcf.z' => 'application/octet-stream',
  68. 'jar' => 'application/octet-stream',
  69. 'some.junk' => 'application/octet-stream',
  70. 'foo.file_test_1' => 'application/octet-stream',
  71. 'foo.file_test_2' => 'application/octet-stream',
  72. 'foo.doc' => 'application/octet-stream',
  73. 'test.ogg' => 'application/octet-stream',
  74. ];
  75. $extension_guesser = $this->container->get('file.mime_type.guesser.extension');
  76. $extension_guesser->setMapping($mapping);
  77. foreach ($test_case as $input => $expected) {
  78. $output = $extension_guesser->guess($input);
  79. $this->assertIdentical($output, $expected, new FormattableMarkup('Mimetype (using passed-in mappings) for %input is %output (expected: %expected).', ['%input' => $input, '%output' => $output, '%expected' => $expected]));
  80. }
  81. }
  82. }