From 5e19ce7907d132cf9ba669a18c0e7fad2ee0581b Mon Sep 17 00:00:00 2001 From: David Woloszyn Date: Wed, 5 Jun 2024 09:34:21 +1000 Subject: [PATCH] MDL-81750 form: Add selector to match filemanager in group --- lib/form/form.js | 2 + lib/form/tests/behat/disabledif.feature | 16 ++++ .../filemanager_hideif_disabledif_form.php | 78 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 lib/form/tests/behat/disabledif.feature create mode 100644 lib/form/tests/behat/fixtures/filemanager_hideif_disabledif_form.php diff --git a/lib/form/form.js b/lib/form/form.js index 1c6dacc4cc0..d1a49667afe 100644 --- a/lib/form/form.js +++ b/lib/form/form.js @@ -339,6 +339,8 @@ if (typeof M.form.dependencyManager === 'undefined') { if (!this._fileinputs) { var fileinputs = {}; var selector = '.fitem [data-fieldtype="filepicker"] input,.fitem [data-fieldtype="filemanager"] input'; + // Include a selector where the filemanager input is nested in a group. + selector += ',.fitem [data-fieldtype="group"] input[id*="filemanager"]'; var els = this.get('form').all(selector); els.each(function(node) { fileinputs[node.getAttribute('name')] = true; diff --git a/lib/form/tests/behat/disabledif.feature b/lib/form/tests/behat/disabledif.feature new file mode 100644 index 00000000000..1cf4bcee4c6 --- /dev/null +++ b/lib/form/tests/behat/disabledif.feature @@ -0,0 +1,16 @@ +@core @javascript @core_form +Feature: disabledIf functionality in forms + For forms including disabledIf functions + As a user + If I trigger the disabledIf condition then the form elements will be disabled + + Background: + Given I log in as "admin" + + Scenario: The file manager is disabled when disabledIf conditions are met + Given I am on fixture page "/lib/form/tests/behat/fixtures/filemanager_hideif_disabledif_form.php" + When I click on "Disable" "radio" + # Test standard file manager. + Then the "disabled" attribute of "input#id_some_filemanager" "css_element" should contain "true" + # Test file manager in a group. + And the "disabled" attribute of "input#id_filemanager_group_some_filemanager_group" "css_element" should contain "true" diff --git a/lib/form/tests/behat/fixtures/filemanager_hideif_disabledif_form.php b/lib/form/tests/behat/fixtures/filemanager_hideif_disabledif_form.php new file mode 100644 index 00000000000..fd4ab05f884 --- /dev/null +++ b/lib/form/tests/behat/fixtures/filemanager_hideif_disabledif_form.php @@ -0,0 +1,78 @@ +. + +require_once(__DIR__ . '/../../../../../config.php'); + +defined('BEHAT_SITE_RUNNING') || die(); + +global $CFG, $PAGE, $OUTPUT; +require_once($CFG->libdir . '/formslib.php'); +$PAGE->set_url('/lib/form/tests/behat/fixtures/filemanager_hideif_disabledif_form.php'); +$PAGE->add_body_class('limitedwidth'); +require_login(); +$PAGE->set_context(core\context\system::instance()); + +/** + * Test class for disabling and hiding a filemanager element. + * + * @copyright 2024 David Woloszyn + * @package core_form + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class test_filemanager_hideif_disabledif_form extends moodleform { + + /** + * Form definition. + */ + public function definition(): void { + $mform = $this->_form; + + $attributes = []; + $attributes['maxbytes'] = '1024'; + $attributes['accepted_types'] = array_map('trim', explode(',', '.odt, .pdf')); + $attributes['subdirs'] = false; + $attributes['maxfiles'] = 3; + + // Radio buttons. + $radiogroup = [ + $mform->createElement('radio', 'some_radios', '', 'Enable', '1'), + $mform->createElement('radio', 'some_radios', '', 'Disable', '2'), + $mform->createElement('radio', 'some_radios', '', 'Hide', '3'), + ]; + + $mform->addGroup($radiogroup, 'some_radios_group', 'Enable/Disable/Hide', ' ', false); + $mform->setDefault('some_radios', 1); + + // Standard file manager. + $mform->addElement('filemanager', 'some_filemanager', 'Standard filemanager', '', $attributes); + $mform->disabledIf('some_filemanager', 'some_radios', 'eq', '2'); + $mform->hideIf('some_filemanager', 'some_radios', 'eq', '3'); + + // File manager nested in group. + $filemanagergroup = []; + $filemanagergroup[] = $mform->createElement('filemanager', 'some_filemanager_group', '', null, $attributes); + $mform->addGroup($filemanagergroup, 'filemanager_group', 'Group filemanager'); + $mform->disabledIf('filemanager_group', 'some_radios', 'eq', '2'); + + $this->add_action_buttons(); + } +} + +$form = new test_filemanager_hideif_disabledif_form(); + +echo $OUTPUT->header(); +$form->display(); +echo $OUTPUT->footer();