MDL-56486 workshop: Deprecate custom methods for handling file types

The functionality and the tests are now part of the core element.
This commit is contained in:
David Mudrák 2017-07-04 12:07:55 +02:00
parent 9ef238bc84
commit b8b07ce2c4
2 changed files with 24 additions and 64 deletions

View File

@ -424,11 +424,15 @@ class workshop {
* Empty values are not returned. Values are converted to lowercase.
* Duplicates are removed. Glob evaluation is not supported.
*
* @deprecated since Moodle 3.4 MDL-56486 - please use the {@link core_form\filetypes_util}
* @param string|array $extensions list of file extensions
* @return array of strings
*/
public static function normalize_file_extensions($extensions) {
debugging('The method workshop::normalize_file_extensions() is deprecated.
Please use the methods provided by the \core_form\filetypes_util class.', DEBUG_DEVELOPER);
if ($extensions === '') {
return array();
}
@ -464,11 +468,15 @@ class workshop {
/**
* Cleans the user provided list of file extensions.
*
* @deprecated since Moodle 3.4 MDL-56486 - please use the {@link core_form\filetypes_util}
* @param string $extensions
* @return string
*/
public static function clean_file_extensions($extensions) {
debugging('The method workshop::clean_file_extensions() is deprecated.
Please use the methods provided by the \core_form\filetypes_util class.', DEBUG_DEVELOPER);
$extensions = self::normalize_file_extensions($extensions);
foreach ($extensions as $i => $extension) {
@ -483,12 +491,16 @@ class workshop {
*
* Empty whitelist is interpretted as "any extension is valid".
*
* @deprecated since Moodle 3.4 MDL-56486 - please use the {@link core_form\filetypes_util}
* @param string|array $extensions list of file extensions
* @param string|array $whitelist list of valid extensions
* @return array list of invalid extensions not found in the whitelist
*/
public static function invalid_file_extensions($extensions, $whitelist) {
debugging('The method workshop::invalid_file_extensions() is deprecated.
Please use the methods provided by the \core_form\filetypes_util class.', DEBUG_DEVELOPER);
$extensions = self::normalize_file_extensions($extensions);
$whitelist = self::normalize_file_extensions($whitelist);
@ -506,12 +518,16 @@ class workshop {
* Empty whitelist is interpretted as "any file type is allowed" rather
* than "no file can be uploaded".
*
* @deprecated since Moodle 3.4 MDL-56486 - please use the {@link core_form\filetypes_util}
* @param string $filename the file name
* @param string|array $whitelist list of allowed file extensions
* @return false
*/
public static function is_allowed_file_type($filename, $whitelist) {
debugging('The method workshop::is_allowed_file_type() is deprecated.
Please use the methods provided by the \core_form\filetypes_util class.', DEBUG_DEVELOPER);
$whitelist = self::normalize_file_extensions($whitelist);
if (empty($whitelist)) {

View File

@ -636,26 +636,8 @@ class mod_workshop_internal_api_testcase extends advanced_testcase {
public function test_normalize_file_extensions() {
$this->resetAfterTest(true);
$this->assertSame(['.odt'], workshop::normalize_file_extensions('odt'));
$this->assertSame(['.odt'], workshop::normalize_file_extensions('.odt'));
$this->assertSame(['.odt'], workshop::normalize_file_extensions('.ODT'));
$this->assertSame(['.doc', '.jpg', '.mp3'], workshop::normalize_file_extensions('doc, jpg, mp3'));
$this->assertSame(['.doc', '.jpg', '.mp3'], workshop::normalize_file_extensions(['.doc', '.jpg', '.mp3']));
$this->assertSame(['.doc', '.jpg', '.mp3'], workshop::normalize_file_extensions('doc, *.jpg, mp3'));
$this->assertSame(['.doc', '.jpg', '.mp3'], workshop::normalize_file_extensions(['doc ', ' JPG ', '.mp3']));
$this->assertSame(['.rtf', '.pdf', '.docx'], workshop::normalize_file_extensions("RTF,.pdf\n...DocX,,,;\rPDF\trtf ...Rtf"));
$this->assertSame(['.tgz', '.tar.gz'], workshop::normalize_file_extensions('tgz,TAR.GZ tar.gz .tar.gz tgz TGZ'));
$this->assertSame(['.notebook'], workshop::normalize_file_extensions('"Notebook":notebook;NOTEBOOK;,\'NoTeBook\''));
$this->assertSame([], workshop::normalize_file_extensions(''));
$this->assertSame([], workshop::normalize_file_extensions([]));
$this->assertSame(['.0'], workshop::normalize_file_extensions(0));
$this->assertSame(['.0'], workshop::normalize_file_extensions('0'));
$this->assertSame(['.odt'], workshop::normalize_file_extensions('*.odt'));
$this->assertSame([], workshop::normalize_file_extensions('.'));
$this->assertSame(['.foo'], workshop::normalize_file_extensions('. foo'));
$this->assertSame([], workshop::normalize_file_extensions('*'));
$this->assertSame([], workshop::normalize_file_extensions('*~'));
$this->assertSame(['.pdf', '.ps'], workshop::normalize_file_extensions('* pdf *.ps foo* *bar .r??'));
workshop::normalize_file_extensions('');
$this->assertDebuggingCalled();
}
/**
@ -664,13 +646,8 @@ class mod_workshop_internal_api_testcase extends advanced_testcase {
public function test_clean_file_extensions() {
$this->resetAfterTest(true);
$this->assertSame('', workshop::clean_file_extensions(''));
$this->assertSame('', workshop::clean_file_extensions(null));
$this->assertSame('', workshop::clean_file_extensions(' '));
$this->assertSame('0', workshop::clean_file_extensions(0));
$this->assertSame('0', workshop::clean_file_extensions('0'));
$this->assertSame('doc, rtf, pdf', workshop::clean_file_extensions('*.Doc, RTF, PDF, .rtf'.PHP_EOL.'PDF '));
$this->assertSame('doc, rtf, pdf', 'doc, rtf, pdf');
workshop::clean_file_extensions('');
$this->assertDebuggingCalledCount(2);
}
/**
@ -679,20 +656,8 @@ class mod_workshop_internal_api_testcase extends advanced_testcase {
public function test_invalid_file_extensions() {
$this->resetAfterTest(true);
$this->assertSame([], workshop::invalid_file_extensions('', ''));
$this->assertSame([], workshop::invalid_file_extensions('', '.doc'));
$this->assertSame([], workshop::invalid_file_extensions('odt', ''));
$this->assertSame([], workshop::invalid_file_extensions('odt', '*'));
$this->assertSame([], workshop::invalid_file_extensions('odt', 'odt'));
$this->assertSame([], workshop::invalid_file_extensions('doc, odt, pdf', ['pdf', 'doc', 'odt']));
$this->assertSame([], workshop::invalid_file_extensions(['doc', 'odt', 'PDF'], ['.doc', '.pdf', '.odt']));
$this->assertSame([], workshop::invalid_file_extensions('*~ .docx, Odt PDF :doc .pdf', '*.docx *.odt *.pdf *.doc'));
$this->assertSame(['.00001-wtf-is-this'], workshop::invalid_file_extensions('docx tgz .00001-wtf-is-this', 'tgz docx'));
$this->assertSame(['.foobar', '.wtfisthis'], workshop::invalid_file_extensions(['.pdf', '.foobar', 'wtfisthis'], 'pdf'));
$this->assertSame([], workshop::invalid_file_extensions('', ''));
$this->assertSame(['.odt'], workshop::invalid_file_extensions(['.PDF', 'PDF', '.ODT'], 'jpg pdf png gif'));
$this->assertSame(['.odt'], workshop::invalid_file_extensions(['.PDF', 'PDF', '.ODT'], '.jpg,.pdf, .png .gif'));
$this->assertSame(['.exe', '.bat'], workshop::invalid_file_extensions(['.exe', '.odt', '.bat', ''], 'odt'));
workshop::invalid_file_extensions('', '');
$this->assertDebuggingCalledCount(3);
}
/**
@ -701,29 +666,8 @@ class mod_workshop_internal_api_testcase extends advanced_testcase {
public function test_is_allowed_file_type() {
$this->resetAfterTest(true);
$this->assertTrue(workshop::is_allowed_file_type('README.txt', ''));
$this->assertTrue(workshop::is_allowed_file_type('README.txt', ['']));
$this->assertFalse(workshop::is_allowed_file_type('README.txt', '0'));
$this->assertFalse(workshop::is_allowed_file_type('README.txt', 'xt'));
$this->assertFalse(workshop::is_allowed_file_type('README.txt', 'old.txt'));
$this->assertTrue(workshop::is_allowed_file_type('README.txt', 'txt'));
$this->assertTrue(workshop::is_allowed_file_type('README.txt', '.TXT'));
$this->assertTrue(workshop::is_allowed_file_type('README.TXT', 'txt'));
$this->assertTrue(workshop::is_allowed_file_type('README.txt', '.txt .md'));
$this->assertTrue(workshop::is_allowed_file_type('README.txt', 'HTML TXT DOC RTF'));
$this->assertTrue(workshop::is_allowed_file_type('README.txt', ['HTML', '...TXT', 'DOC', 'RTF']));
$this->assertTrue(workshop::is_allowed_file_type('C:\Moodle\course-data.tar.gz', 'gzip zip 7z tar.gz'));
$this->assertFalse(workshop::is_allowed_file_type('C:\Moodle\course-data.tar.gz', 'gzip zip 7z tar'));
$this->assertTrue(workshop::is_allowed_file_type('~/course-data.tar.gz', 'gzip zip 7z gz'));
$this->assertFalse(workshop::is_allowed_file_type('~/course-data.tar.gz', 'gzip zip 7z'));
$this->assertFalse(workshop::is_allowed_file_type('Alice on the beach.jpg.exe', 'png gif jpg bmp'));
$this->assertFalse(workshop::is_allowed_file_type('xfiles.exe.jpg', 'exe com bat sh'));
$this->assertFalse(workshop::is_allowed_file_type('solution.odt~', 'odt, xls'));
$this->assertTrue(workshop::is_allowed_file_type('solution.odt~', 'odt, odt~'));
workshop::is_allowed_file_type('', '');
$this->assertDebuggingCalledCount(2);
}
/**