|
| 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 | +} |
0 commit comments