From 7806534af25810b107b7c35ae9edd79776d9199a Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 31 Mar 2022 10:21:13 +0200 Subject: [PATCH] MDL-72446 behat: Ensure that the day reset happens on enabled element Before this change we were setting the month day to 1 unconditionally and that was working ok for enabled by default elements. But in a number or cases we were doing that against disabled elements so the day reset was not happening. Now we wait for the element to be enabled and only then proceed with the day reset. Backed with tests using disabled form elements, so we have now both enabled and disabled fields covered. --- lib/behat/form_field/behat_form_date.php | 18 +++++++------- lib/tests/behat/datetime_any.feature | 30 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/behat/form_field/behat_form_date.php b/lib/behat/form_field/behat_form_date.php index 030d8525992e1..903c15bdcdad5 100644 --- a/lib/behat/form_field/behat_form_date.php +++ b/lib/behat/form_field/behat_form_date.php @@ -56,17 +56,19 @@ public function set_value($value) { // Disable the given date selector field. $this->set_child_field_value('enabled', false); } else if (is_numeric($value)) { // The value is numeric (unix timestamp). - // First, reset the day always to an existing one (1st). Without that - // undesired modifications (JS) happens when changing of month and day if - // the interim combination doesn't exists (for example, 31 March => 01 April). - // Note that instead of always setting the day to 1, this could be a little more - // clever, for example only changing when the day > 28, or only when the - // months (current or changed) have less days that the other. But that would - // require more complex calculations than the simpler line below. - $this->set_child_field_value('day', 1); // Assign the mapped values to each form element in the date selector field. foreach ($this->get_mapped_fields($value) as $childname => $childvalue) { $this->set_child_field_value($childname, $childvalue); + if ($childname === 'enabled') { + // As soon as the form is enabled, reset the day to an existing one (1st). Without that + // undesired modifications (JS) happens when changing of month and day if + // the interim combination doesn't exists (for example, 31 March => 01 April). + // Note that instead of always setting the day to 1, this could be a little more + // clever, for example only changing when the day > 28, or only when the + // months (current or changed) have less days that the other. But that would + // require more complex calculations than the simpler line below. + $this->set_child_field_value('day', 1); + } } } else { // Invalid value. // Get the name of the field. diff --git a/lib/tests/behat/datetime_any.feature b/lib/tests/behat/datetime_any.feature index 5e8c236c4f673..8c78e02073e39 100644 --- a/lib/tests/behat/datetime_any.feature +++ b/lib/tests/behat/datetime_any.feature @@ -5,7 +5,7 @@ Feature: Any day / month / year combination in date form elements works ok. Any day / month / year combination must work ok @javascript - Scenario Outline: Verify that setting any date / datetime is possible + Scenario Outline: Verify that setting any date / datetime is possible with enabled fields Given the following "courses" exist: | fullname | shortname | format | | Course 1 | C1 | topics | @@ -30,3 +30,31 @@ Feature: Any day / month / year combination in date form elements works ok. | 1709186400 | 1740808800 | Saturday, 1 March 2025, 2:00 | | | 1577858400 | 1577772000 | Tuesday, 31 December 2019, 2:00 | change of year, back and forth | | 1577772000 | 1577858400 | Wednesday, 1 January 2020, 2:00 | | + + @javascript + Scenario Outline: Verify that setting any date / datetime is possible with disabled fields + Given the following "courses" exist: + | fullname | shortname | format | + | Course 1 | C1 | topics | + And the following "activity" exist: + | activity | name | intro | course | idnumber | + | assign | Assignment 01 | Assign activity to test some dates | C1 | assign01 | + And I am on the "Assignment 01" "assign activity editing" page logged in as admin + And I expand all fieldsets + And I set the field "Due date" to "" + And I set the field "Due date" to "disabled" + And I set the field "Due date" to "" + When I press "Save and display" + Then the activity date in "Assignment 01" should contain "Due:" + And the activity date in "Assignment 01" should contain "" + + Examples: + | initial_date | final_date | date_result | case_explanation (times Australia/Perth) | + | ##today## | ##tomorrow noon## | ##tomorrow noon##%A, %d %B %Y, %I:%M## | change of day, any day, back and forth | + | ##tomorrow## | ##today noon## | ##today noon##%A, %d %B %Y, %I:%M## | | + | 1617256800 | 1617170400 | Wednesday, 31 March 2021, 2:00 | change of month, back and forth | + | 1617170400 | 1617256800 | Thursday, 1 April 2021, 2:00 | | + | 1740808800 | 1709186400 | Thursday, 29 February 2024, 2:00 | change of month, leap year, back and forth | + | 1709186400 | 1740808800 | Saturday, 1 March 2025, 2:00 | | + | 1577858400 | 1577772000 | Tuesday, 31 December 2019, 2:00 | change of year, back and forth | + | 1577772000 | 1577858400 | Wednesday, 1 January 2020, 2:00 | |