Skip to content

Commit

Permalink
MDL-56486 workshop: Deprecate custom methods for handling file types
Browse files Browse the repository at this point in the history
The functionality and the tests are now part of the core element.
  • Loading branch information
mudrd8mz committed Jul 6, 2017
1 parent 9ef238b commit b8b07ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 64 deletions.
16 changes: 16 additions & 0 deletions mod/workshop/locallib.php
Expand Up @@ -424,11 +424,15 @@ public static function timestamp_formats($timestamp) {
* 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();
}
Expand Down Expand Up @@ -464,11 +468,15 @@ public static function normalize_file_extensions($extensions) {
/**
* 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) {
Expand All @@ -483,12 +491,16 @@ public static function clean_file_extensions($extensions) {
*
* 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);

Expand All @@ -506,12 +518,16 @@ public static function invalid_file_extensions($extensions, $whitelist) {
* 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)) {
Expand Down
72 changes: 8 additions & 64 deletions mod/workshop/tests/locallib_test.php
Expand Up @@ -636,26 +636,8 @@ public function test_reset_userdata_submissions() {
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();
}

/**
Expand All @@ -664,13 +646,8 @@ public function test_normalize_file_extensions() {
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);
}

/**
Expand All @@ -679,20 +656,8 @@ public function test_clean_file_extensions() {
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);
}

/**
Expand All @@ -701,29 +666,8 @@ public function test_invalid_file_extensions() {
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);
}

/**
Expand Down

0 comments on commit b8b07ce

Please sign in to comment.