From 947684505ecebfe3a6343648a55f0742f06208a4 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 4 Nov 2024 18:52:12 +0000 Subject: [PATCH] MDL-83610 behat: allow to set date fields in groups --- lib/behat/behat_field_manager.php | 6 ++ lib/behat/classes/partial_named_selector.php | 8 +- lib/form/tests/behat/dates.feature | 76 +++++++++++++++ lib/form/tests/behat/fixtures/dates_form.php | 99 ++++++++++++++++++++ 4 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 lib/form/tests/behat/dates.feature create mode 100644 lib/form/tests/behat/fixtures/dates_form.php diff --git a/lib/behat/behat_field_manager.php b/lib/behat/behat_field_manager.php index 07b87e5b3d8..6136871759a 100644 --- a/lib/behat/behat_field_manager.php +++ b/lib/behat/behat_field_manager.php @@ -337,6 +337,12 @@ class behat_field_manager { if ($fieldtype === 'tags') { return 'autocomplete'; } + if ($fieldtype === 'date_time_selector') { + return 'date_time'; + } + if ($fieldtype === 'date_selector') { + return 'date'; + } return $fieldtype; } diff --git a/lib/behat/classes/partial_named_selector.php b/lib/behat/classes/partial_named_selector.php index 4ca9f59258b..a73150c8074 100644 --- a/lib/behat/classes/partial_named_selector.php +++ b/lib/behat/classes/partial_named_selector.php @@ -328,7 +328,13 @@ XPATH XPATH , 'date_time' => << <<. + +/** + * Test for setting date fields in behat + * + * @package core_form + * @copyright Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +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/dates_form.php'); +$PAGE->add_body_class('limitedwidth'); +require_login(); +$PAGE->set_context(context_system::instance()); + +/** + * Test form class adding all types of date elements + * + * @package core_form + */ +class test_dates_form extends moodleform { + /** + * Define the form. + */ + public function definition() { + $mform = $this->_form; + + $mform->addElement('date_selector', 'simpledateonly', 'Simple only date'); + $mform->addElement('date_selector', 'simpleoptionaldateonly', 'Simple optional only date', ['optional' => true]); + $mform->addElement('date_time_selector', 'simpledatetime', 'Simple date and time'); + $mform->addElement('date_time_selector', 'simpleoptionaldatetime', 'Simple optional date and time', ['optional' => true]); + + $group = []; + $group[] = $mform->createElement('date_selector', 'group1dateonly', 'Group1 only date'); + $group[] = $mform->createElement('date_selector', 'group1optionaldateonly', 'Group1 optional only date', + ['optional' => true]); + $group[] = $mform->createElement('date_time_selector', 'group1datetime', 'Group1 date and time'); + $group[] = $mform->createElement('date_time_selector', 'group1optionaldatetime', 'Group1 optional date and time', + ['optional' => true]); + $mform->addGroup($group, 'dategroup1', 'Date group1', '', false); + + $group = []; + $group[] = $mform->createElement('date_selector', 'group2dateonly', 'Group2 only date'); + $group[] = $mform->createElement('date_selector', 'group2optionaldateonly', 'Group2 optional only date', + ['optional' => true]); + $group[] = $mform->createElement('date_time_selector', 'group2datetime', 'Group2 date and time'); + $group[] = $mform->createElement('date_time_selector', 'group2optionaldatetime', 'Group2 optional date and time', + ['optional' => true]); + $mform->addGroup($group, 'dategroup2', 'Date group2', '', true); + + $this->add_action_buttons(false, 'Send form'); + } +} + +echo $OUTPUT->header(); + +echo "

Quickform integration test

"; + +$form = new test_dates_form(); + +$data = $form->get_data(); +if ($data) { + echo "

Submitted data

"; + echo '
    '; + $data = (array) $data; + foreach ($data as $field => $value) { + if (is_array($value)) { + foreach ($value as $key => $v) { + echo "
  • {$field}[$key]: $v
  • "; + } + } else { + echo "
  • $field: $value
  • "; + } + } + echo '
'; +} +$form->display(); + +echo $OUTPUT->footer();