Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Behat calendar-related steps definitions.
  19   *
  20   * @package    core_calendar
  21   * @category   test
  22   * @copyright  2013 Mark Nelson <markn@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL used, this file may be required by behat before including /config.php.
  27  require_once (__DIR__ . '/../../../lib/behat/behat_base.php');
  28  
  29  use Behat\Gherkin\Node\TableNode as TableNode;
  30  
  31  /**
  32   * Contains functions used by behat to test functionality.
  33   *
  34   * @package    core_calendar
  35   * @category   test
  36   * @copyright  2013 Mark Nelson <markn@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class behat_calendar extends behat_base {
  40  
  41      /**
  42       * Create event when starting on the front page.
  43       *
  44       * @Given /^I create a calendar event with form data:$/
  45       * @param TableNode $data
  46       */
  47      public function i_create_a_calendar_event_with_form_data($data) {
  48          // Go to current month page.
  49          $this->execute("behat_general::click_link", get_string('monththis', 'calendar'));
  50  
  51          // Create event.
  52          $this->i_create_a_calendar_event($data);
  53      }
  54  
  55      /**
  56       * Create event.
  57       *
  58       * @Given /^I create a calendar event:$/
  59       * @param TableNode $data
  60       */
  61      public function i_create_a_calendar_event($data) {
  62          // Get the event name.
  63          $eventname = $data->getRow(1);
  64          $eventname = $eventname[1];
  65  
  66          $this->execute("behat_general::wait_until_the_page_is_ready");
  67  
  68          if ($this->running_javascript()) {
  69              // Click to create new event.
  70              $this->execute("behat_general::i_click_on", array(get_string('newevent', 'calendar'), "button"));
  71  
  72              // Set form fields.
  73              $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
  74  
  75              // Save event.
  76              $this->execute("behat_forms::press_button", get_string('save'));
  77          }
  78      }
  79  
  80      /**
  81       * Click a specific day in the calendar.
  82       *
  83       * @Given /^I click day "(?P<dayofmonth>\d+)" of this month in the calendar$/
  84       * @param int $day The day of the current month
  85       */
  86      public function i_click_day_of_this_month_in_calendar($day) {
  87          $summarytitle = userdate(time(), get_string('strftimemonthyear'));
  88          // The current month table.
  89          $currentmonth = "table[descendant::*[self::caption[contains(concat(' ', normalize-space(.), ' '), ' {$summarytitle} ')]]]";
  90  
  91          // Strings for the class cell match.
  92          $cellclasses  = "contains(concat(' ', normalize-space(@class), ' '), ' day ')";
  93          $daycontains  = "text()[contains(concat(' ', normalize-space(.), ' '), ' {$day} ')]";
  94          $daycell      = "td[{$cellclasses}]";
  95          $dayofmonth   = "a[{$daycontains}]";
  96  
  97          $xpath = '//' . $currentmonth . '/descendant::' . $daycell . '/' . $dayofmonth;
  98          $this->execute("behat_general::wait_until_the_page_is_ready");
  99          $this->execute("behat_general::i_click_on", array($xpath, "xpath_element"));
 100          $this->execute("behat_general::wait_until_the_page_is_ready");
 101  
 102      }
 103  
 104      /**
 105       * Hover over a specific day in the calendar.
 106       *
 107       * @Given /^I hover over day "(?P<dayofmonth>\d+)" of this month in the calendar$/
 108       * @param int $day The day of the current month
 109       */
 110      public function i_hover_over_day_of_this_month_in_calendar($day) {
 111          $summarytitle = userdate(time(), get_string('strftimemonthyear'));
 112          // The current month table.
 113          $currentmonth = "table[descendant::*[self::caption[contains(concat(' ', normalize-space(.), ' '), ' {$summarytitle} ')]]]";
 114  
 115          // Strings for the class cell match.
 116          $cellclasses  = "contains(concat(' ', normalize-space(@class), ' '), ' day ')";
 117          $daycontains  = "text()[contains(concat(' ', normalize-space(.), ' '), ' {$day} ')]";
 118          $daycell      = "td[{$cellclasses}]";
 119          $dayofmonth   = "a[{$daycontains}]";
 120  
 121          $xpath = '//' . $currentmonth . '/descendant::' . $daycell . '/' . $dayofmonth;
 122          $this->execute("behat_general::wait_until_the_page_is_ready");
 123          $this->execute("behat_general::i_hover", [$xpath, "xpath_element"]);
 124      }
 125  
 126      /**
 127       * Hover over today in the calendar.
 128       *
 129       * @Given /^I hover over today in the calendar$/
 130       */
 131      public function i_hover_over_today_in_the_calendar() {
 132          // For window's compatibility, using %d and not %e.
 133          $todaysday = trim(strftime('%d'));
 134          $todaysday = ltrim($todaysday, '0');
 135          return $this->i_hover_over_day_of_this_month_in_calendar($todaysday);
 136      }
 137  
 138      /**
 139       * Navigate to a specific date in the calendar.
 140       *
 141       * @Given /^I view the calendar for "(?P<month>\d+)" "(?P<year>\d+)"$/
 142       * @param int $month the month selected as a number
 143       * @param int $year the four digit year
 144       */
 145      public function i_view_the_calendar_for($month, $year) {
 146          $time = make_timestamp($year, $month, 1);
 147          $this->execute('behat_general::i_visit', ['/calendar/view.php?view=month&course=1&time='.$time]);
 148  
 149      }
 150  
 151      /**
 152       * Navigate to site calendar.
 153       *
 154       * @Given /^I am viewing site calendar$/
 155       * @throws coding_exception
 156       * @return void
 157       */
 158      public function i_am_viewing_site_calendar() {
 159          $url = new moodle_url('/calendar/view.php', ['view' => 'month']);
 160          $this->execute('behat_general::i_visit', [$url]);
 161      }
 162  }