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