Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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                  if ($childname === 'enabled') {
  63                      // As soon as the form is enabled, reset the day to an existing one (1st). Without that
  64                      // undesired modifications (JS) happens when changing of month and day if
  65                      // the interim combination doesn't exists (for example, 31 March => 01 April).
  66                      // Note that instead of always setting the day to 1, this could be a little more
  67                      // clever, for example only changing when the day > 28, or only when the
  68                      // months (current or changed) have less days that the other. But that would
  69                      // require more complex calculations than the simpler line below.
  70                      $this->set_child_field_value('day', 1);
  71                  }
  72              }
  73          } else { // Invalid value.
  74              // Get the name of the field.
  75              $fieldname = $this->field->find('css', 'legend')->getHtml();
  76              throw new ExpectationException("Invalid value for '{$fieldname}'", $this->session);
  77          }
  78      }
  79  
  80      /**
  81       * Returns the date field identifiers and the values that should be assigned to them.
  82       *
  83       * @param int $timestamp The UNIX timestamp
  84       * @return array
  85       */
  86      protected function get_mapped_fields(int $timestamp): array {
  87          // Order is important, first enable, and then year -> month -> day
  88          // (other order can lead to some transitions not working as expected,
  89          // for example, changing from 15 June to 31 August, Behat ends with
  90          // date being 1 August if the modification order is day, then month).
  91          // Note that the behaviour described above is 100% reproducible
  92          // manually, with the form (JS) auto-fixing things in the middle and
  93          // leading to undesired final dates.
  94          return [
  95              'enabled' => true,
  96              'year' => date('Y', $timestamp),
  97              'month' => date('n', $timestamp),
  98              'day' => date('j', $timestamp),
  99          ];
 100      }
 101  
 102      /**
 103       * Sets a value to a child element in the date form field.
 104       *
 105       * @param string $childname The name of the child field
 106       * @param string|bool $childvalue The value
 107       */
 108      private function set_child_field_value(string $childname, $childvalue) {
 109          // Find the given child form element in the date selector field.
 110          $childelement = $this->field->find('css', "*[name$='[{$childname}]']");
 111          if ($childelement) {
 112              // Get the field instance for the given child form element.
 113              $childinstance = $this->get_field_instance_for_element($childelement);
 114              // Set the value to the child form element.
 115              $childinstance->set_value($childvalue);
 116          }
 117      }
 118  }