Skip to content

Commit 7a868e5

Browse files
author
Mihail Geshoski
committed
MDL-47410 behat: Support date selection from the date selector element
Adds behat support for selecting a date from the date selector element. The passed values should represent a textual date description wrapped in '##' (e.g. '##first day of January 2020##', '##1 Jan 2020##'). Also, the value 'disabled' is valid and can be used to disable the date selector element.
1 parent c9ecca6 commit 7a868e5

File tree

4 files changed

+105
-43
lines changed

4 files changed

+105
-43
lines changed
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Date form field class.
19+
*
20+
* @package core_form
21+
* @category test
22+
* @copyright 2013 David Monllaó
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27+
28+
require_once(__DIR__ . '/behat_form_group.php');
29+
30+
use Behat\Mink\Exception\ExpectationException;
31+
32+
/**
33+
* Date form field.
34+
*
35+
* This class will be refactored in case we are interested in
36+
* creating more complex formats to fill date and date-time fields.
37+
*
38+
* @package core_form
39+
* @category test
40+
* @copyright 2013 David Monllaó
41+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42+
*/
43+
class behat_form_date extends behat_form_group {
44+
45+
/**
46+
* Sets the value to a date field.
47+
*
48+
* @param string $value The value to be assigned to the date selector field. The string value must be either
49+
* parsable into a UNIX timestamp or equal to 'disabled' (if disabling the date selector).
50+
* @return void
51+
* @throws ExpectationException If the value is invalid.
52+
*/
53+
public function set_value($value) {
54+
55+
if ($value === 'disabled') {
56+
// Disable the given date selector field.
57+
$this->set_child_field_value('enabled', false);
58+
} else if (is_numeric($value)) { // The value is numeric (unix timestamp).
59+
// Assign the mapped values to each form element in the date selector field.
60+
foreach ($this->get_mapped_fields($value) as $childname => $childvalue) {
61+
$this->set_child_field_value($childname, $childvalue);
62+
}
63+
} else { // Invalid value.
64+
// Get the name of the field.
65+
$fieldname = $this->field->find('css', 'legend')->getHtml();
66+
throw new ExpectationException("Invalid value for '{$fieldname}'", $this->session);
67+
}
68+
}
69+
70+
/**
71+
* Returns the date field identifiers and the values that should be assigned to them.
72+
*
73+
* @param int $timestamp The UNIX timestamp
74+
* @return array
75+
*/
76+
protected function get_mapped_fields(int $timestamp): array {
77+
return [
78+
'enabled' => true,
79+
'day' => date('j', $timestamp),
80+
'month' => date('n', $timestamp),
81+
'year' => date('Y', $timestamp),
82+
];
83+
}
84+
85+
/**
86+
* Sets a value to a child element in the date form field.
87+
*
88+
* @param string $childname The name of the child field
89+
* @param string|bool $childvalue The value
90+
*/
91+
private function set_child_field_value(string $childname, $childvalue) {
92+
// Find the given child form element in the date selector field.
93+
$childelement = $this->field->find('css', "*[name$='[{$childname}]']");
94+
if ($childelement) {
95+
// Get the field instance for the given child form element.
96+
$childinstance = $this->get_field_instance_for_element($childelement);
97+
// Set the value to the child form element.
98+
$childinstance->set_value($childvalue);
99+
}
100+
}
101+
}

lib/behat/form_field/behat_form_date_selector.php

-42
This file was deleted.

lib/form/templates/element-date_selector.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{< core_form/element-group }}
22
{{$element}}
3-
<fieldset class="m-0 p-0 border-0" id="{{element.id}}">
3+
<fieldset data-fieldtype="date" class="m-0 p-0 border-0" id="{{element.id}}">
44
<legend class="sr-only">{{label}}</legend>
55
<span class="fdate_selector d-flex align-items-center">
66
{{#element.elements}}

lib/upgrade.txt

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ information provided here is intended especially for developers.
44
=== 3.10.1 ===
55
* New optional parameter $extracontent for print_collapsible_region_start(). This allows developers to add interactive HTML elements
66
(e.g. a help icon) after the collapsible region's toggle link.
7+
* Behat now supports date selection from the date form element. Examples:
8+
- I set the field "<field_string>" to "##15 March 2021##"
9+
- I set the field "<field_string>" to "##first day of January last year##"
710

811
=== 3.10 ===
912
* PHPUnit has been upgraded to 8.5. That comes with a few changes:

0 commit comments

Comments
 (0)