diff --git a/media.module b/media.module index 849555b..1f65bf7 100644 --- a/media.module +++ b/media.module @@ -1212,6 +1212,30 @@ function media_get_browser_plugin_info() { } /** + * Gets the MIME type mapped to a given extension. + * + * @param string $extension + * A file extension. + * + * @return string + * The MIME type associated with the extension or FALSE if the extension does + * not have an associated MIME type. + * + * @see file_mimetype_mapping() + */ +function media_get_extension_mimetype($extension) { + include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc'; + $mimetype_mappings = file_mimetype_mapping(); + + if ($id = $mimetype_mappings['extensions'][$extension]) { + return $mimetype_mappings['mimetypes'][$id]; + } + else { + return FALSE; + } +} + +/** * Helper function to get a list of local stream wrappers. */ function media_get_local_stream_wrappers() { diff --git a/tests/media.test b/tests/media.test index b72035e..893c949 100644 --- a/tests/media.test +++ b/tests/media.test @@ -610,6 +610,56 @@ class MediaBrowserLibraryTestCase extends MediaFileFieldTestCase { } /** + * Tests the media browser settings. + */ +class MediaBrowserSettingsTestCase extends MediaFileFieldTestCase { + public static function getInfo() { + return array( + 'name' => 'Media browser settings test', + 'description' => 'Tests the media browser settings.', + 'group' => 'Media', + ); + } + + /** + * Tests the media browser settings. + */ + function testBrowserSettings() { + // Perform the tests with all permutations of $scheme, $type and $extension. + foreach (array('public', 'private') as $scheme) { + foreach (array('image', 'document') as $type) { + foreach (array('jpg', 'txt') as $extension) { + $file = $this->createFileEntity(array('scheme' => $scheme, 'uid' => $this->admin_user->uid, 'type' => $type, 'filemime' => media_get_extension_mimetype($extension))); + + $options = array( + 'query' => array( + 'enabledPlugins' => array( + 'media_default--media_browser_1' => 'media_default--media_browser_1', + ), + 'schemes' => array($scheme), + 'types' => array($type), + 'file_extensions' => $extension, + ), + ); + + // Verify that the file is displayed. + $this->drupalGet('media/browser', $options); + $this->assertResponse(200); + $xpath = $this->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array( + ':fid' => $file->fid, + )); + $this->assertFieldByXPath($xpath, TRUE, format_string('File with file ID %fid found.', array('%fid' => $file->fid))); + + // Verify that no other files are also displayed. + $files = $this->xpath('//ul[@class="media-list-thumbnails"]/li/div[@data-fid]'); + $this->assertEqual(count($files), 1, 'There is only one file that matches the current browser configuration.'); + } + } + } + } +} + +/** * Tests the media browser 'My files' tab. */ class MediaBrowserMyFilesTestCase extends MediaFileFieldTestCase {