Commit 91fed57a authored by sam marshall's avatar sam marshall
Browse files

MDL-42616 filelib: Use config value for custom MIME types (1/2)

Uses a new optional config variable to define custom MIME types
in JSON format. Includes new API for accessing and modifying
this data.
parent 8f06fffb
......@@ -517,6 +517,23 @@ $CFG->admin = 'admin';
// on a shared file system that supports locking.
// $CFG->lock_file_root = $CFG->dataroot . '/lock';
//
// Moodle 2.9 allows administrators to customise the list of supported file types.
// To add a new filetype or override the definition of an existing one, set the
// customfiletypes variable like this:
//
// $CFG->customfiletypes = array(
// (object)array(
// 'extension' => 'frog',
// 'icon' => 'archive',
// 'type' => 'application/frog',
// 'customdescription' => 'Amphibian-related file archive'
// )
// );
//
// The extension, icon, and type fields are required. The icon field can refer to
// any icon inside the pix/f folder. You can also set the customdescription field
// (shown above) and (for advanced use) the groups, string, and defaulticon fields.
//
//=========================================================================
// 7. SETTINGS FOR DEVELOPMENT SERVERS - not intended for production use!!!
//=========================================================================
......
This diff is collapsed.
......@@ -1386,206 +1386,8 @@ function download_file_content($url, $headers=null, $postdata=null, $fullrespons
* Unknown types should use the 'xxx' entry which includes defaults.
*/
function &get_mimetypes_array() {
static $mimearray = array (
'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown'),
'3gp' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
'7z' => array ('type'=>'application/x-7z-compressed', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'aac' => array ('type'=>'audio/aac', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'accdb' => array ('type'=>'application/msaccess', 'icon'=>'base'),
'ai' => array ('type'=>'application/postscript', 'icon'=>'eps', 'groups'=>array('image'), 'string'=>'image'),
'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'applescript' => array ('type'=>'text/plain', 'icon'=>'text'),
'asc' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'asm' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'au' => array ('type'=>'audio/au', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi', 'groups'=>array('video','web_video'), 'string'=>'video'),
'bmp' => array ('type'=>'image/bmp', 'icon'=>'bmp', 'groups'=>array('image'), 'string'=>'image'),
'c' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash'),
'cpp' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'cs' => array ('type'=>'application/x-csh', 'icon'=>'sourcecode'),
'css' => array ('type'=>'text/css', 'icon'=>'text', 'groups'=>array('web_file')),
'csv' => array ('type'=>'text/csv', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
'dv' => array ('type'=>'video/x-dv', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'unknown'),
'doc' => array ('type'=>'application/msword', 'icon'=>'document', 'groups'=>array('document')),
'bdoc' => array ('type'=>'application/x-digidoc', 'icon'=>'document', 'groups'=>array('archive')),
'cdoc' => array ('type'=>'application/x-digidoc', 'icon'=>'document', 'groups'=>array('archive')),
'ddoc' => array ('type'=>'application/x-digidoc', 'icon'=>'document', 'groups'=>array('archive')),
'docx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'icon'=>'document', 'groups'=>array('document')),
'docm' => array ('type'=>'application/vnd.ms-word.document.macroEnabled.12', 'icon'=>'document'),
'dotx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'icon'=>'document'),
'dotm' => array ('type'=>'application/vnd.ms-word.template.macroEnabled.12', 'icon'=>'document'),
'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
'dif' => array ('type'=>'video/x-dv', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
'dir' => array ('type'=>'application/x-director', 'icon'=>'flash'),
'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
'eps' => array ('type'=>'application/postscript', 'icon'=>'eps'),
'epub' => array ('type'=>'application/epub+zip', 'icon'=>'epub', 'groups'=>array('document')),
'fdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
'flv' => array ('type'=>'video/x-flv', 'icon'=>'flash', 'groups'=>array('video','web_video'), 'string'=>'video'),
'f4v' => array ('type'=>'video/mp4', 'icon'=>'flash', 'groups'=>array('video','web_video'), 'string'=>'video'),
'gallery' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
'galleryitem' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
'gallerycollection' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
'gif' => array ('type'=>'image/gif', 'icon'=>'gif', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'tgz' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'gz' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'gzip' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'h' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'hpp' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'htc' => array ('type'=>'text/x-component', 'icon'=>'markup'),
'html' => array ('type'=>'text/html', 'icon'=>'html', 'groups'=>array('web_file')),
'xhtml'=> array ('type'=>'application/xhtml+xml', 'icon'=>'html', 'groups'=>array('web_file')),
'htm' => array ('type'=>'text/html', 'icon'=>'html', 'groups'=>array('web_file')),
'ico' => array ('type'=>'image/vnd.microsoft.icon', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
'ics' => array ('type'=>'text/calendar', 'icon'=>'text'),
'isf' => array ('type'=>'application/inspiration', 'icon'=>'isf'),
'ist' => array ('type'=>'application/inspiration.template', 'icon'=>'isf'),
'java' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'jar' => array ('type'=>'application/java-archive', 'icon' => 'archive'),
'jcb' => array ('type'=>'text/xml', 'icon'=>'markup'),
'jcl' => array ('type'=>'text/xml', 'icon'=>'markup'),
'jcw' => array ('type'=>'text/xml', 'icon'=>'markup'),
'jmt' => array ('type'=>'text/xml', 'icon'=>'markup'),
'jmx' => array ('type'=>'text/xml', 'icon'=>'markup'),
'jnlp' => array ('type'=>'application/x-java-jnlp-file', 'icon'=>'markup'),
'jpe' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
'jpg' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
'jqz' => array ('type'=>'text/xml', 'icon'=>'markup'),
'js' => array ('type'=>'application/x-javascript', 'icon'=>'text', 'groups'=>array('web_file')),
'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text'),
'm' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'mbz' => array ('type'=>'application/vnd.moodle.backup', 'icon'=>'moodle'),
'mdb' => array ('type'=>'application/x-msaccess', 'icon'=>'base'),
'mht' => array ('type'=>'message/rfc822', 'icon'=>'archive'),
'mhtml'=> array ('type'=>'message/rfc822', 'icon'=>'archive'),
'mov' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video','web_video'), 'string'=>'video'),
'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
'mw' => array ('type'=>'application/maple', 'icon'=>'math'),
'mws' => array ('type'=>'application/maple', 'icon'=>'math'),
'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'mp3', 'groups'=>array('audio'), 'string'=>'audio'),
'mp3' => array ('type'=>'audio/mp3', 'icon'=>'mp3', 'groups'=>array('audio','web_audio'), 'string'=>'audio'),
'mp4' => array ('type'=>'video/mp4', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
'm4v' => array ('type'=>'video/mp4', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
'm4a' => array ('type'=>'audio/mp4', 'icon'=>'mp3', 'groups'=>array('audio'), 'string'=>'audio'),
'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
'mpe' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
'mpg' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
'mpr' => array ('type'=>'application/vnd.moodle.profiling', 'icon'=>'moodle'),
'nbk' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
'notebook' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'writer', 'groups'=>array('document')),
'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'writer', 'groups'=>array('document')),
'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'oth', 'groups'=>array('document')),
'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'writer'),
'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'draw'),
'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'draw'),
'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'impress'),
'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'impress'),
'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'calc', 'groups'=>array('spreadsheet')),
'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'calc', 'groups'=>array('spreadsheet')),
'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'chart'),
'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'math'),
'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'base'),
'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'draw'),
'oga' => array ('type'=>'audio/ogg', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'ogg' => array ('type'=>'audio/ogg', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'ogv' => array ('type'=>'video/ogg', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
'pct' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
'php' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
'pic' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
'pict' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
'png' => array ('type'=>'image/png', 'icon'=>'png', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint', 'groups'=>array('presentation')),
'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint', 'groups'=>array('presentation')),
'pptx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'icon'=>'powerpoint'),
'pptm' => array ('type'=>'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'icon'=>'powerpoint'),
'potx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.template', 'icon'=>'powerpoint'),
'potm' => array ('type'=>'application/vnd.ms-powerpoint.template.macroEnabled.12', 'icon'=>'powerpoint'),
'ppam' => array ('type'=>'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'icon'=>'powerpoint'),
'ppsx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'icon'=>'powerpoint'),
'ppsm' => array ('type'=>'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'icon'=>'powerpoint'),
'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf'),
'pub' => array ('type'=>'application/x-mspublisher', 'icon'=>'publisher', 'groups'=>array('presentation')),
'qt' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video','web_video'), 'string'=>'video'),
'ra' => array ('type'=>'audio/x-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio','web_audio'), 'string'=>'audio'),
'ram' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'rar' => array ('type'=>'application/x-rar-compressed', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'rhb' => array ('type'=>'text/xml', 'icon'=>'markup'),
'rm' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'rmvb' => array ('type'=>'application/vnd.rn-realmedia-vbr', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
'rtf' => array ('type'=>'text/rtf', 'icon'=>'text', 'groups'=>array('document')),
'rtx' => array ('type'=>'text/richtext', 'icon'=>'text'),
'rv' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('video'), 'string'=>'video'),
'sh' => array ('type'=>'application/x-sh', 'icon'=>'sourcecode'),
'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'smi' => array ('type'=>'application/smil', 'icon'=>'text'),
'smil' => array ('type'=>'application/smil', 'icon'=>'text'),
'sqt' => array ('type'=>'text/xml', 'icon'=>'markup'),
'svg' => array ('type'=>'image/svg+xml', 'icon'=>'image', 'groups'=>array('image','web_image'), 'string'=>'image'),
'svgz' => array ('type'=>'image/svg+xml', 'icon'=>'image', 'groups'=>array('image','web_image'), 'string'=>'image'),
'swa' => array ('type'=>'application/x-director', 'icon'=>'flash'),
'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash', 'groups'=>array('video','web_video')),
'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash', 'groups'=>array('video','web_video')),
'sxw' => array ('type'=>'application/vnd.sun.xml.writer', 'icon'=>'writer'),
'stw' => array ('type'=>'application/vnd.sun.xml.writer.template', 'icon'=>'writer'),
'sxc' => array ('type'=>'application/vnd.sun.xml.calc', 'icon'=>'calc'),
'stc' => array ('type'=>'application/vnd.sun.xml.calc.template', 'icon'=>'calc'),
'sxd' => array ('type'=>'application/vnd.sun.xml.draw', 'icon'=>'draw'),
'std' => array ('type'=>'application/vnd.sun.xml.draw.template', 'icon'=>'draw'),
'sxi' => array ('type'=>'application/vnd.sun.xml.impress', 'icon'=>'impress'),
'sti' => array ('type'=>'application/vnd.sun.xml.impress.template', 'icon'=>'impress'),
'sxg' => array ('type'=>'application/vnd.sun.xml.writer.global', 'icon'=>'writer'),
'sxm' => array ('type'=>'application/vnd.sun.xml.math', 'icon'=>'math'),
'tar' => array ('type'=>'application/x-tar', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
'tif' => array ('type'=>'image/tiff', 'icon'=>'tiff', 'groups'=>array('image'), 'string'=>'image'),
'tiff' => array ('type'=>'image/tiff', 'icon'=>'tiff', 'groups'=>array('image'), 'string'=>'image'),
'tex' => array ('type'=>'application/x-tex', 'icon'=>'text'),
'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text'),
'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text'),
'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text'),
'txt' => array ('type'=>'text/plain', 'icon'=>'text', 'defaulticon'=>true),
'wav' => array ('type'=>'audio/wav', 'icon'=>'wav', 'groups'=>array('audio'), 'string'=>'audio'),
'webm' => array ('type'=>'video/webm', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'wmv', 'groups'=>array('video'), 'string'=>'video'),
'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'wmv', 'groups'=>array('video'), 'string'=>'video'),
'wma' => array ('type'=>'audio/x-ms-wma', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
'xbk' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
'xdp' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
'xfd' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
'xfdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
'xlsx' => array ('type'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'icon'=>'spreadsheet'),
'xlsm' => array ('type'=>'application/vnd.ms-excel.sheet.macroEnabled.12', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
'xltx' => array ('type'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'icon'=>'spreadsheet'),
'xltm' => array ('type'=>'application/vnd.ms-excel.template.macroEnabled.12', 'icon'=>'spreadsheet'),
'xlsb' => array ('type'=>'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'icon'=>'spreadsheet'),
'xlam' => array ('type'=>'application/vnd.ms-excel.addin.macroEnabled.12', 'icon'=>'spreadsheet'),
'xml' => array ('type'=>'application/xml', 'icon'=>'markup'),
'xsl' => array ('type'=>'text/xml', 'icon'=>'markup'),
'zip' => array ('type'=>'application/zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive')
);
return $mimearray;
// Get types from the core_filetypes function, which includes caching.
return core_filetypes::get_types();
}
/**
......@@ -1862,7 +1664,12 @@ function get_mimetype_description($obj, $capitalise=false) {
// MIME types may include + symbol but this is not permitted in string ids.
$safemimetype = str_replace('+', '_', $mimetype);
$safemimetypestr = str_replace('+', '_', $mimetypestr);
if (get_string_manager()->string_exists($safemimetype, 'mimetypes')) {
$customdescription = mimeinfo('customdescription', $filename);
if ($customdescription) {
// Call format_string on the custom description so that multilang
// filter can be used (if enabled).
$result = format_string($customdescription);
} else if (get_string_manager()->string_exists($safemimetype, 'mimetypes')) {
$result = get_string($safemimetype, 'mimetypes', (object)$a);
} else if (get_string_manager()->string_exists($safemimetypestr, 'mimetypes')) {
$result = get_string($safemimetypestr, 'mimetypes', (object)$a);
......
......@@ -205,6 +205,8 @@ class phpunit_util extends testing_util {
core_text::reset_caches();
get_message_processors(false, true);
filter_manager::reset_caches();
core_filetypes::reset_caches();
// Reset internal users.
core_user::reset_internal_users();
......
......@@ -785,4 +785,58 @@ EOF;
// Test it does nothing to the 'plain' data.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
}
/**
* Tests the get_mimetype_description function.
*/
public function test_get_mimetype_description() {
$this->resetAfterTest();
// Test example type (.doc).
$this->assertEquals(get_string('application/msword', 'mimetypes'),
get_mimetype_description(array('filename' => 'test.doc')));
// Test an unknown file type.
$this->assertEquals(get_string('document/unknown', 'mimetypes'),
get_mimetype_description(array('filename' => 'test.frog')));
// Test a custom filetype with no lang string specified.
core_filetypes::add_type('frog', 'application/x-frog', 'document');
$this->assertEquals('application/x-frog',
get_mimetype_description(array('filename' => 'test.frog')));
// Test custom description.
core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document',
array(), '', 'Froggy file');
$this->assertEquals('Froggy file',
get_mimetype_description(array('filename' => 'test.frog')));
// Test custom description using multilang filter.
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', true);
core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document',
array(), '', '<span lang="en" class="multilang">Green amphibian</span>' .
'<span lang="fr" class="multilang">Amphibian vert</span>');
$this->assertEquals('Green amphibian',
get_mimetype_description(array('filename' => 'test.frog')));
}
/**
* Tests the get_mimetypes_array function.
*/
public function test_get_mimetypes_array() {
$mimeinfo = get_mimetypes_array();
// Test example MIME type (doc).
$this->assertEquals('application/msword', $mimeinfo['doc']['type']);
$this->assertEquals('document', $mimeinfo['doc']['icon']);
$this->assertEquals(array('document'), $mimeinfo['doc']['groups']);
$this->assertFalse(isset($mimeinfo['doc']['string']));
$this->assertFalse(isset($mimeinfo['doc']['defaulticon']));
$this->assertFalse(isset($mimeinfo['doc']['customdescription']));
// Check the less common fields using other examples.
$this->assertEquals('image', $mimeinfo['png']['string']);
$this->assertEquals(true, $mimeinfo['txt']['defaulticon']);
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/classes/filetypes.php.
*
* @package core
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/filelib.php');
/**
* Unit tests for /lib/classes/filetypes.php.
*
* @package core
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_filetypes_testcase extends advanced_testcase {
public function test_add_type() {
$this->resetAfterTest();
// Check the filetypes to be added do not exist yet (basically this
// ensures we're testing the cache clear).
$types = get_mimetypes_array();
$this->assertArrayNotHasKey('frog', $types);
$this->assertArrayNotHasKey('zombie', $types);
// Add two filetypes (minimal, then all options).
core_filetypes::add_type('frog', 'application/x-frog', 'document');
core_filetypes::add_type('zombie', 'application/x-zombie', 'document',
array('document', 'image'), 'image', 'A zombie', true);
// Check they now exist, and check data.
$types = get_mimetypes_array();
$this->assertEquals('application/x-frog', $types['frog']['type']);
$this->assertEquals('document', $types['frog']['icon']);
$this->assertEquals(array('document', 'image'), $types['zombie']['groups']);
$this->assertEquals('image', $types['zombie']['string']);
$this->assertEquals(true, $types['zombie']['defaulticon']);
$this->assertEquals('A zombie', $types['zombie']['customdescription']);
// Test adding again causes exception.
try {
core_filetypes::add_type('frog', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('already exists', $e->getMessage());
$this->assertContains('frog', $e->getMessage());
}
// Test bogus extension causes exception.
try {
core_filetypes::add_type('.frog', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('Invalid extension', $e->getMessage());
$this->assertContains('..frog', $e->getMessage());
}
try {
core_filetypes::add_type('', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('Invalid extension', $e->getMessage());
}
// Test there is an exception if you add something with defaulticon when
// there is already a type that has it.
try {
core_filetypes::add_type('gecko', 'text/plain', 'document',
array(), '', '', true);
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('default icon set', $e->getMessage());
$this->assertContains('text/plain', $e->getMessage());
}
}
public function test_update_type() {
$this->resetAfterTest();
// Check previous value for the MIME type of Word documents.
$types = get_mimetypes_array();
$this->assertEquals('application/msword', $types['doc']['type']);
// Change it.
core_filetypes::update_type('doc', 'doc', 'application/x-frog', 'document');
// Check the MIME type is now set and also the other (not specified)
// options, like groups, were removed.
$types = get_mimetypes_array();
$this->assertEquals('application/x-frog', $types['doc']['type']);
$this->assertArrayNotHasKey('groups', $types['doc']);
// This time change the extension.
core_filetypes::update_type('doc', 'docccc', 'application/x-frog', 'document');
$types = get_mimetypes_array();
$this->assertEquals('application/x-frog', $types['docccc']['type']);
$this->assertArrayNotHasKey('doc', $types);
// Test unknown extension.
try {
core_filetypes::update_type('doc', 'doc', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('not found', $e->getMessage());
$this->assertContains('doc', $e->getMessage());
}
// Test bogus extension causes exception.
try {
core_filetypes::update_type('docccc', '.frog', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('Invalid extension', $e->getMessage());
$this->assertContains('.frog', $e->getMessage());
}
try {
core_filetypes::update_type('docccc', '', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('Invalid extension', $e->getMessage());
}
// Test defaulticon changes.
try {
core_filetypes::update_type('docccc', 'docccc', 'text/plain', 'document',
array(), '', '', true);
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('default icon set', $e->getMessage());
$this->assertContains('text/plain', $e->getMessage());
}
}
public function test_delete_type() {
$this->resetAfterTest();
// Filetype exists.
$types = get_mimetypes_array();
$this->assertArrayHasKey('doc', $types);
// Remove it.
core_filetypes::delete_type('doc');
$types = get_mimetypes_array();
$this->assertArrayNotHasKey('doc', $types);
// Test removing one that doesn't exist causes exception.
try {
core_filetypes::delete_type('doc');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('not found', $e->getMessage());
$this->assertContains('doc', $e->getMessage());
}
// Try a custom type (slightly different).
core_filetypes::add_type('frog', 'application/x-frog', 'document');
$types = get_mimetypes_array();
$this->assertArrayHasKey('frog', $types);
core_filetypes::delete_type('frog');
$types = get_mimetypes_array();
$this->assertArrayNotHasKey('frog', $types);
}
public function test_revert_type_to_default() {
$this->resetAfterTest();
// Delete and then revert.
core_filetypes::delete_type('doc');
$this->assertArrayNotHasKey('doc', get_mimetypes_array());
core_filetypes::revert_type_to_default('doc');
$this->assertArrayHasKey('doc', get_mimetypes_array());
// Update and then revert.
core_filetypes::update_type('asm', 'asm', 'text/plain', 'sourcecode', array(), '', 'An asm file');
$types = get_mimetypes_array();
$this->assertEquals('An asm file', $types['asm']['customdescription']);
core_filetypes::revert_type_to_default('asm');
$types = get_mimetypes_array();
$this->assertArrayNotHasKey('customdescription', $types['asm']);
// Test reverting a non-default type causes exception.
try {
core_filetypes::revert_type_to_default('frog');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('not a default type', $e->getMessage());
$this->assertContains('frog', $e->getMessage());
}
}
/**
* Check that the logic cleans up the variable by deleting parts that are
* no longer needed.
*/
public function test_cleanup() {
global $CFG;
$this->resetAfterTest();
// The custom filetypes setting is empty to start with.
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
// Add a custom filetype, then delete it.
core_filetypes::add_type('frog', 'application/x-frog', 'document');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::delete_type('frog');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
// Change a standard filetype, then change it back.
core_filetypes::update_type('asm', 'asm', 'text/plain', 'document');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::update_type('asm', 'asm', 'text/plain', 'sourcecode');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
// Delete a standard filetype, then add it back (the same).
core_filetypes::delete_type('asm');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::add_type('asm', 'text/plain', 'sourcecode');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
// Revert a changed type.
core_filetypes::update_type('asm', 'asm', 'text/plain', 'document');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::revert_type_to_default('asm');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
// Revert a deleted type.
core_filetypes::delete_type('asm');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::revert_type_to_default('asm');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
}
}