From dc8d401481ebc1e63f83f40a3d6bcb964c64e6a2 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 21 Oct 2025 10:27:56 +0100 Subject: [PATCH] MDL-86255 behat: implement methods to assert date/time field value. --- lib/behat/form_field/behat_form_date.php | 45 ++++++++++++++----- lib/behat/form_field/behat_form_date_time.php | 23 ++++++---- lib/form/tests/behat/dates.feature | 30 ++++++++++++- 3 files changed, 75 insertions(+), 23 deletions(-) diff --git a/lib/behat/form_field/behat_form_date.php b/lib/behat/form_field/behat_form_date.php index 903c15bdcda..cfdbb347380 100644 --- a/lib/behat/form_field/behat_form_date.php +++ b/lib/behat/form_field/behat_form_date.php @@ -14,15 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Date form field class. - * - * @package core_form - * @category test - * @copyright 2013 David Monllaó - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. require_once(__DIR__ . '/behat_form_group.php'); @@ -41,17 +32,14 @@ use Behat\Mink\Exception\ExpectationException; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class behat_form_date extends behat_form_group { - /** * Sets the value to a date field. * * @param string $value The value to be assigned to the date selector field. The string value must be either * parsable into a UNIX timestamp or equal to 'disabled' (if disabling the date selector). - * @return void * @throws ExpectationException If the value is invalid. */ public function set_value($value) { - if ($value === 'disabled') { // Disable the given date selector field. $this->set_child_field_value('enabled', false); @@ -77,6 +65,29 @@ class behat_form_date extends behat_form_group { } } + /** + * Returns the current value of the field + * + * @return int + */ + public function get_value() { + return make_timestamp( + $this->get_child_field_value('year'), + $this->get_child_field_value('month'), + $this->get_child_field_value('day'), + ); + } + + /** + * Matches the provided value against the current field value + * + * @param mixed $expectedvalue + * @return bool + */ + public function matches($expectedvalue) { + return (int) $expectedvalue === $this->get_value(); + } + /** * Returns the date field identifiers and the values that should be assigned to them. * @@ -115,4 +126,14 @@ class behat_form_date extends behat_form_group { $childinstance->set_value($childvalue); } } + + /** + * Gets a value of a child element in the date form field + * + * @param string $childname + * @return string + */ + protected function get_child_field_value(string $childname): string { + return $this->field->find('css', "*[name$='[{$childname}]']")->getValue(); + } } diff --git a/lib/behat/form_field/behat_form_date_time.php b/lib/behat/form_field/behat_form_date_time.php index f1344603adf..8c078d26055 100644 --- a/lib/behat/form_field/behat_form_date_time.php +++ b/lib/behat/form_field/behat_form_date_time.php @@ -14,15 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Date time form field class. - * - * @package core_form - * @category test - * @copyright 2013 David Monllaó - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. require_once(__DIR__ . '/behat_form_date.php'); @@ -39,6 +30,20 @@ require_once(__DIR__ . '/behat_form_date.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class behat_form_date_time extends behat_form_date { + /** + * Returns the current value of the field + * + * @return string + */ + public function get_value() { + return make_timestamp( + $this->get_child_field_value('year'), + $this->get_child_field_value('month'), + $this->get_child_field_value('day'), + $this->get_child_field_value('hour'), + $this->get_child_field_value('minute'), + ); + } /** * Returns the date field identifiers and the values that should be assigned to them. diff --git a/lib/form/tests/behat/dates.feature b/lib/form/tests/behat/dates.feature index ecb9bd7bb9e..e052f840318 100644 --- a/lib/form/tests/behat/dates.feature +++ b/lib/form/tests/behat/dates.feature @@ -27,7 +27,20 @@ Feature: Setting and validating date fields | dategroup2[group2optionaldatetime][enabled] | 1 | | Group2 optional date and time | ## 2023-08-31 14:45 ## | When I press "Send form" - Then I should see "simpledateonly: 1690732800" + Then the following fields match these values: + | Simple only date | ## 2023-07-31 ## | + | Simple optional only date | ## 2023-08-31 ## | + | Simple date and time | ## 2023-07-31 11:15 ## | + | Simple optional date and time | ## 2023-08-31 14:45 ## | + | Group1 only date | ## 2023-07-31 ## | + | Group1 optional only date | ## 2023-08-31 ## | + | Group1 date and time | ## 2023-07-31 11:15 ## | + | Group1 optional date and time | ## 2023-08-31 14:45 ## | + | Group2 only date | ## 2023-07-31 ## | + | Group2 optional only date | ## 2023-08-31 ## | + | Group2 date and time | ## 2023-07-31 11:15 ## | + | Group2 optional date and time | ## 2023-08-31 14:45 ## | + And I should see "simpledateonly: 1690732800" And I should see "simpleoptionaldateonly: 1693411200" And I should see "simpledatetime: 1690773300" And I should see "simpleoptionaldatetime: 1693464300" @@ -62,7 +75,20 @@ Feature: Setting and validating date fields | dategroup2[group2optionaldatetime][enabled] | 1 | | dategroup2[group2optionaldatetime] | ## 2023-08-31 14:45 ## | When I press "Send form" - Then I should see "simpledateonly: 1690732800" + Then the following fields match these values: + | simpledateonly | ## 2023-07-31 ## | + | simpleoptionaldateonly | ## 2023-08-31 ## | + | simpledatetime | ## 2023-07-31 11:15 ## | + | simpleoptionaldatetime | ## 2023-08-31 14:45 ## | + | group1dateonly | ## 2023-07-31 ## | + | group1optionaldateonly | ## 2023-08-31 ## | + | group1datetime | ## 2023-07-31 11:15 ## | + | group1optionaldatetime | ## 2023-08-31 14:45 ## | + | dategroup2[group2dateonly] | ## 2023-07-31 ## | + | dategroup2[group2optionaldateonly] | ## 2023-08-31 ## | + | dategroup2[group2datetime] | ## 2023-07-31 11:15 ## | + | dategroup2[group2optionaldatetime] | ## 2023-08-31 14:45 ## | + And I should see "simpledateonly: 1690732800" And I should see "simpleoptionaldateonly: 1693411200" And I should see "simpledatetime: 1690773300" And I should see "simpleoptionaldatetime: 1693464300"