MimeAnalyzer: Add testcases for mp3 detection
authorDerk-Jan Hartman <hartman.wiki@gmail.com>
Fri, 19 May 2017 11:36:04 +0000 (13:36 +0200)
committerJforrester <jforrester@wikimedia.org>
Thu, 8 Jun 2017 00:27:33 +0000 (00:27 +0000)
This is a follow-up to change Ie1a63aa

Bug: T115170
Change-Id: Iab5e19e1dd26b0d88c72ad7253cd11e865c5e008

.gitattributes
tests/phpunit/data/media/say-test-mpeg1.mp3 [new file with mode: 0644]
tests/phpunit/data/media/say-test-mpeg2.5.mp3 [new file with mode: 0644]
tests/phpunit/data/media/say-test-mpeg2.mp3 [new file with mode: 0644]
tests/phpunit/data/media/say-test-with-id3.mp3 [new file with mode: 0644]
tests/phpunit/includes/libs/mime/MimeAnalyzerTest.php

index 09f86a3..f230c60 100644 (file)
@@ -1,3 +1,4 @@
 *.sh eol=lf
 *.icc binary
 *.webp binary
+*.mp3 binary
\ No newline at end of file
diff --git a/tests/phpunit/data/media/say-test-mpeg1.mp3 b/tests/phpunit/data/media/say-test-mpeg1.mp3
new file mode 100644 (file)
index 0000000..b3a6318
Binary files /dev/null and b/tests/phpunit/data/media/say-test-mpeg1.mp3 differ
diff --git a/tests/phpunit/data/media/say-test-mpeg2.5.mp3 b/tests/phpunit/data/media/say-test-mpeg2.5.mp3
new file mode 100644 (file)
index 0000000..e6743a8
Binary files /dev/null and b/tests/phpunit/data/media/say-test-mpeg2.5.mp3 differ
diff --git a/tests/phpunit/data/media/say-test-mpeg2.mp3 b/tests/phpunit/data/media/say-test-mpeg2.mp3
new file mode 100644 (file)
index 0000000..8b2aaa2
Binary files /dev/null and b/tests/phpunit/data/media/say-test-mpeg2.mp3 differ
diff --git a/tests/phpunit/data/media/say-test-with-id3.mp3 b/tests/phpunit/data/media/say-test-with-id3.mp3
new file mode 100644 (file)
index 0000000..04205d5
Binary files /dev/null and b/tests/phpunit/data/media/say-test-with-id3.mp3 differ
index 787d9a8..bf2a3f6 100644 (file)
@@ -1,4 +1,8 @@
 <?php
+/*
+ * @group Media
+ * @covers MimeAnalyzer
+ */
 class MimeMagicTest extends PHPUnit_Framework_TestCase {
        /** @var MimeAnalyzer */
        private $mimeAnalyzer;
@@ -20,6 +24,13 @@ class MimeMagicTest extends PHPUnit_Framework_TestCase {
                parent::setUp();
        }
 
+       function doGuessMimeType( array $parameters = [] ) {
+               $class = new ReflectionClass( get_class( $this->mimeAnalyzer ) );
+               $method = $class->getMethod( 'doGuessMimeType' );
+               $method->setAccessible( true );
+               return $method->invokeArgs( $this->mimeAnalyzer, $parameters );
+       }
+
        /**
         * @dataProvider providerImproveTypeFromExtension
         * @param string $ext File extension (no leading dot)
@@ -69,4 +80,49 @@ class MimeMagicTest extends PHPUnit_Framework_TestCase {
                $actualType = $this->mimeAnalyzer->getMediaType( $oggFile, 'application/ogg' );
                $this->assertEquals( $actualType, MEDIATYPE_AUDIO );
        }
+
+       /**
+        * Test to make sure that mp3 files are detected as audio type
+        */
+       function testMP3AsAudio() {
+               $file = __DIR__ . '/../../../data/media/say-test-with-id3.mp3';
+               $actualType = $this->mimeAnalyzer->getMediaType( $file );
+               $this->assertEquals( MEDIATYPE_AUDIO, $actualType );
+       }
+
+       /**
+        * Test to make sure that MP3 with id3 tag is recognized
+        */
+       function testMP3WithID3Recognize() {
+               $file = __DIR__ . '/../../../data/media/say-test-with-id3.mp3';
+               $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
+               $this->assertEquals( 'audio/mpeg', $actualType );
+       }
+
+       /**
+        * Test to make sure that MP3 without id3 tag is recognized (MPEG-1 sample rates)
+        */
+       function testMP3NoID3RecognizeMPEG1() {
+               $file = __DIR__ . '/../../../data/media/say-test-mpeg1.mp3';
+               $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
+               $this->assertEquals( 'audio/mpeg', $actualType );
+       }
+
+       /**
+        * Test to make sure that MP3 without id3 tag is recognized (MPEG-2 sample rates)
+        */
+       function testMP3NoID3RecognizeMPEG2() {
+               $file = __DIR__ . '/../../../data/media/say-test-mpeg2.mp3';
+               $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
+               $this->assertEquals( 'audio/mpeg', $actualType );
+       }
+
+       /**
+        * Test to make sure that MP3 without id3 tag is recognized (MPEG-2.5 sample rates)
+        */
+       function testMP3NoID3RecognizeMPEG2_5() {
+               $file = __DIR__ . '/../../../data/media/say-test-mpeg2.5.mp3';
+               $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
+               $this->assertEquals( 'audio/mpeg', $actualType );
+       }
 }