From db03f2a58e2ce5970cb3cdf05af5309d40b66115 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 30 Oct 2024 13:08:44 +0000 Subject: [PATCH] MDL-80689 filepicker: better reporting of incorrect file type Before this change, file type errors were reported like system errors. Now, they are reported in a plain dialoue, which is more like how it works for drag-drop upload. Also, before this, after an upload errors, you were left with a blank filepicker dialogue. Now, the upload form is redisplayed. --- repository/filepicker.js | 25 ++++++++++++++++--- .../tests/behat/upload_file_type.feature | 17 +++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 repository/tests/behat/upload_file_type.feature diff --git a/repository/filepicker.js b/repository/filepicker.js index 5b703bade0364..a13248961e5e7 100644 --- a/repository/filepicker.js +++ b/repository/filepicker.js @@ -688,13 +688,30 @@ M.core_filepicker.init = function(Y, options) { } // error checking if (data && data.error) { - Y.use('moodle-core-notification-ajaxexception', function () { - return new M.core.ajaxException(data); - }); - this.fpnode.one('.fp-content').setContent(''); + if (data.errorcode === 'invalidfiletype') { + // File type errors are not really errors, so report them less scarily. + Y.use('moodle-core-notification-alert', function() { + return new M.core.alert({ + title: M.util.get_string('error', 'moodle'), + message: data.error, + }); + }); + } else { + Y.use('moodle-core-notification-ajaxexception', function() { + return new M.core.ajaxException(data); + }); + } + if (args.onerror) { + args.onerror(id, data, p); + } else { + // Don't know what to do, so blank the dialogue to ensure it is not left in an inconsistent state. + // This is not great. The user needs to re-click 'Upload file' to reset the display. + this.fpnode.one('.fp-content').setContent(''); + } return; } else { if (data.msg) { + // As far as I can tell, msg will never be set by any PHP code. -- Tim Oct 2024. scope.print_msg(data.msg, 'info'); } // cache result if applicable diff --git a/repository/tests/behat/upload_file_type.feature b/repository/tests/behat/upload_file_type.feature new file mode 100644 index 0000000000000..46c807ecf5256 --- /dev/null +++ b/repository/tests/behat/upload_file_type.feature @@ -0,0 +1,17 @@ +@core @core_filepicker @_file_upload +Feature: File type can be validated on upload + While uploading files + As a user + I want the file type to be validated to save me from errors + + @javascript + Scenario: File-picker does not break if you upload the wrong file type + Given I am on the "filemanager_hideif_disabledif_form" "core_form > Fixture" page logged in as "admin" + When I click on "Add..." "link" + And I select "Upload a file" repository in file picker + And I set the field "Attachment" to "#dirroot#/lib/form/tests/fixtures/filemanager_hideif_disabledif_form.php" + And I click on "Upload this file" "button" in the "File picker" "dialogue" + Then I should see "Text file filetype cannot be accepted." in the "Error" "dialogue" + And I click on "OK" "button" in the "Error" "dialogue" + And I should see "Attachment" in the "File picker" "dialogue" + And "Upload this file" "button" in the "File picker" "dialogue" should be visible