diff --git a/admin/tool/licensemanager/tests/behat/delete_license.feature b/admin/tool/licensemanager/tests/behat/delete_license.feature index 9bab27bf5fd..8648d55242d 100644 --- a/admin/tool/licensemanager/tests/behat/delete_license.feature +++ b/admin/tool/licensemanager/tests/behat/delete_license.feature @@ -10,12 +10,10 @@ Feature: Delete custom licenses And I navigate to "Licence > Licence manager" in site administration And I click on "Create licence" "link" And I set the following fields to these values: - | shortname | MIT | - | fullname | MIT Licence | - | source | https://opensource.org/licenses/MIT | - | version[day] | 1 | - | version[month] | March | - | version[year] | 2019 | + | shortname | MIT | + | fullname | MIT Licence | + | source | https://opensource.org/licenses/MIT | + | Licence version | ##1 March 2019## | And I press "Save changes" And I click on "Delete" "icon" in the "MIT" "table_row" When I click on "Save changes" "button" in the "Delete licence" "dialogue" diff --git a/admin/tool/licensemanager/tests/behat/edit_license.feature b/admin/tool/licensemanager/tests/behat/edit_license.feature index d4d73ff220c..d2a0b5e8ca6 100644 --- a/admin/tool/licensemanager/tests/behat/edit_license.feature +++ b/admin/tool/licensemanager/tests/behat/edit_license.feature @@ -9,12 +9,10 @@ Feature: Custom licences And I navigate to "Licence > Licence manager" in site administration And I click on "Create licence" "link" And I set the following fields to these values: - | shortname | MIT | - | fullname | MIT Licence | - | source | https://opensource.org/licenses/MIT | - | version[day] | 1 | - | version[month] | January | - | version[year] | 2020 | + | shortname | MIT | + | fullname | MIT Licence | + | source | https://opensource.org/licenses/MIT | + | Licence version | ##first day of January 2020## | When I press "Save changes" Then I should see "Licence manager" And I should see "MIT Licence" in the "MIT" "table_row" @@ -25,12 +23,10 @@ Feature: Custom licences And I navigate to "Licence > Licence manager" in site administration And I click on "Create licence" "link" And I set the following fields to these values: - | shortname | MIT | - | fullname | MIT Licence | - | source | opensource.org/licenses/MIT | - | version[day] | 1 | - | version[month] | January | - | version[year] | 2020 | + | shortname | MIT | + | fullname | MIT Licence | + | source | opensource.org/licenses/MIT | + | Licence version | ##2020-01-01## | When I press "Save changes" Then I should see "Invalid source URL" And I set the following fields to these values: @@ -49,12 +45,10 @@ Feature: Custom licences And I navigate to "Licence > Licence manager" in site administration And I click on "Create licence" "link" And I set the following fields to these values: - | shortname | MIT | - | fullname | MIT Licence | - | source | https://opensource.org/licenses/MIT | - | version[day] | 1 | - | version[month] | March | - | version[year] | 2019 | + | shortname | MIT | + | fullname | MIT Licence | + | source | https://opensource.org/licenses/MIT | + | Licence version | ##1 March 2019## | When I press "Save changes" Then I should see "Licence manager" And I should see "2019030100" in the "MIT" "table_row" @@ -65,12 +59,10 @@ Feature: Custom licences And I navigate to "Licence > Licence manager" in site administration And I click on "Create licence" "link" And I set the following fields to these values: - | shortname | MIT | - | fullname | MIT Licence | - | source | https://opensource.org/licenses/MIT | - | version[day] | 1 | - | version[month] | March | - | version[year] | 2019 | + | shortname | MIT | + | fullname | MIT Licence | + | source | https://opensource.org/licenses/MIT | + | Licence version | ##1 Mar 2019## | And I press "Save changes" And I should see "Licence manager" And I should see "MIT Licence" in the "MIT" "table_row" diff --git a/lib/behat/behat_field_manager.php b/lib/behat/behat_field_manager.php index 193d2afaec5..d2d0da7a6c0 100644 --- a/lib/behat/behat_field_manager.php +++ b/lib/behat/behat_field_manager.php @@ -136,6 +136,11 @@ class behat_field_manager { */ public static function guess_field_type(NodeElement $fieldnode, Session $session) { + // If the type is explicitly set on the element pointed to by the label - use it. + if ($fieldtype = $fieldnode->getAttribute('data-fieldtype')) { + return self::normalise_fieldtype($fieldtype); + } + // Textareas are considered text based elements. $tagname = strtolower($fieldnode->getTagName()); if ($tagname == 'textarea') { @@ -151,10 +156,6 @@ class behat_field_manager { $type = $fieldnode->getAttribute('type'); switch ($type) { case 'text': - if ($fieldtype = $fieldnode->getAttribute('data-fieldtype')) { - return self::normalise_fieldtype($fieldtype); - } - return 'text'; case 'password': case 'email': case 'file': diff --git a/lib/behat/classes/partial_named_selector.php b/lib/behat/classes/partial_named_selector.php index 3689733441e..f987b015738 100644 --- a/lib/behat/classes/partial_named_selector.php +++ b/lib/behat/classes/partial_named_selector.php @@ -267,6 +267,9 @@ XPATH .//descendant::span[@data-inplaceeditable][descendant::a[%titleMatch%]] XPATH , + 'date_time' => <<. + +/** + * 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'); + +use Behat\Mink\Exception\ExpectationException; + +/** + * Date form field. + * + * This class will be refactored in case we are interested in + * creating more complex formats to fill date and date-time fields. + * + * @package core_form + * @category test + * @copyright 2013 David Monllaó + * @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); + } else if (is_numeric($value)) { // The value is numeric (unix timestamp). + // 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); + } + } else { // Invalid value. + // Get the name of the field. + $fieldname = $this->field->find('css', 'legend')->getHtml(); + throw new ExpectationException("Invalid value for '{$fieldname}'", $this->session); + } + } + + /** + * Returns the date field identifiers and the values that should be assigned to them. + * + * @param int $timestamp The UNIX timestamp + * @return array + */ + protected function get_mapped_fields(int $timestamp): array { + return [ + 'enabled' => true, + 'day' => date('j', $timestamp), + 'month' => date('n', $timestamp), + 'year' => date('Y', $timestamp), + ]; + } + + /** + * Sets a value to a child element in the date form field. + * + * @param string $childname The name of the child field + * @param string|bool $childvalue The value + */ + private function set_child_field_value(string $childname, $childvalue) { + // Find the given child form element in the date selector field. + $childelement = $this->field->find('css', "*[name$='[{$childname}]']"); + if ($childelement) { + // Get the field instance for the given child form element. + $childinstance = $this->get_field_instance_for_element($childelement); + // Set the value to the child form element. + $childinstance->set_value($childvalue); + } + } +} diff --git a/lib/behat/form_field/behat_form_date_selector.php b/lib/behat/form_field/behat_form_date_selector.php deleted file mode 100644 index 71c477c9596..00000000000 --- a/lib/behat/form_field/behat_form_date_selector.php +++ /dev/null @@ -1,42 +0,0 @@ -. - -/** - * 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'); - -/** - * Date form field. - * - * This class will be refactored in case we are interested in - * creating more complex formats to fill date and date-time fields. - * - * @package core_form - * @category test - * @copyright 2013 David Monllaó - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class behat_form_date_selector extends behat_form_group { -} diff --git a/lib/behat/form_field/behat_form_date_time_selector.php b/lib/behat/form_field/behat_form_date_time.php similarity index 70% rename from lib/behat/form_field/behat_form_date_time_selector.php rename to lib/behat/form_field/behat_form_date_time.php index 19181cf6b40..f1344603adf 100644 --- a/lib/behat/form_field/behat_form_date_time_selector.php +++ b/lib/behat/form_field/behat_form_date_time.php @@ -25,7 +25,7 @@ // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. -require_once(__DIR__ . '/behat_form_date_selector.php'); +require_once(__DIR__ . '/behat_form_date.php'); /** * Date time form field. @@ -38,5 +38,18 @@ require_once(__DIR__ . '/behat_form_date_selector.php'); * @copyright 2013 David Monllaó * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class behat_form_date_time_selector extends behat_form_date_selector { +class behat_form_date_time extends behat_form_date { + + /** + * Returns the date field identifiers and the values that should be assigned to them. + * + * @param int $timestamp The UNIX timestamp + * @return array + */ + protected function get_mapped_fields(int $timestamp): array { + return array_merge(parent::get_mapped_fields($timestamp), [ + 'hour' => date('G', $timestamp), + 'minute' => (int) date('i', $timestamp) + ]); + } } diff --git a/lib/behat/form_field/behat_form_field.php b/lib/behat/form_field/behat_form_field.php index 4da8002404a..7f30d2ccab8 100644 --- a/lib/behat/form_field/behat_form_field.php +++ b/lib/behat/form_field/behat_form_field.php @@ -173,17 +173,28 @@ class behat_form_field implements behat_session_interface { * @return behat_form_field */ private function guess_type() { + return $this->get_field_instance_for_element($this->field); + } + + /** + * Returns the appropriate form field object for a given node element. + * + * @param NodeElement $element The node element + * @return behat_form_field + */ + protected function get_field_instance_for_element(NodeElement $element): behat_form_field { global $CFG; // We default to the text-based field if nothing was detected. - if (!$type = behat_field_manager::guess_field_type($this->field, $this->session)) { + if (!$type = behat_field_manager::guess_field_type($element, $this->session)) { $type = 'text'; } $classname = 'behat_form_' . $type; $classpath = $CFG->dirroot . '/lib/behat/form_field/' . $classname . '.php'; require_once($classpath); - return new $classname($this->session, $this->field); + + return new $classname($this->session, $element); } /** diff --git a/lib/form/templates/element-checkbox-inline.mustache b/lib/form/templates/element-checkbox-inline.mustache index fb90de4750b..a63d92aaf27 100644 --- a/lib/form/templates/element-checkbox-inline.mustache +++ b/lib/form/templates/element-checkbox-inline.mustache @@ -1,4 +1,4 @@ -