Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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 steps definitions for drag and drop markers.
  19   *
  20   * @package   qtype_ddmarker
  21   * @category  test
  22   * @copyright 2015 The Open University
  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__ . '/../../../../../lib/behat/behat_base.php');
  29  
  30  /**
  31   * Steps definitions related with the drag and drop markers question type.
  32   *
  33   * @copyright 2015 The Open University
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class behat_qtype_ddmarker extends behat_base {
  37  
  38      /**
  39       * Get the xpath for a given drag item.
  40       *
  41       * @param string $marker the text of the item to drag.
  42       * @param bool $iskeyboard is using keyboard or not.
  43       * @return string the xpath expression.
  44       */
  45      protected function marker_xpath($marker, $iskeyboard = false) {
  46          if ($iskeyboard) {
  47              return '//span[contains(@class, "marker") and not(contains(@class, "dragplaceholder")) ' .
  48                      'and span[@class = "markertext" and contains(normalize-space(.), "' .
  49                      $this->escape($marker) . '")]]';
  50          }
  51          return '//span[contains(@class, "marker") and contains(@class, "unneeded") ' .
  52                  'and not(contains(@class, "dragplaceholder")) and span[@class = "markertext" and contains(normalize-space(.), "' .
  53                  $this->escape($marker) . '")]]';
  54      }
  55  
  56      /**
  57       * Drag the drag item with the given text to the given space.
  58       *
  59       * @param string $marker the marker to drag. The label, optionally followed by ,<instance number> (int) if relevant.
  60       * @param string $coordinates the position to drag the marker to, 'x,y'.
  61       *
  62       * @Given /^I drag "(?P<marker>[^"]*)" to "(?P<coordinates>\d+,\d+)" in the drag and drop markers question$/
  63       */
  64      public function i_drag_to_in_the_drag_and_drop_markers_question($marker, $coordinates) {
  65          list($x, $y) = explode(',', $coordinates);
  66  
  67          // This is a bit nasty, but Behat (indeed Selenium) will only drag on
  68          // DOM node so that its centre is over the centre of anothe DOM node.
  69          // Therefore to make it drag to the specified place, we have to add
  70          // a target div.
  71          $this->execute_script("
  72                  (function() {
  73                      if (document.getElementById('target-{$x}-{$y}')) {
  74                          return;
  75                      }
  76                      var image = document.querySelector('.dropbackground');
  77                      var target = document.createElement('div');
  78                      target.setAttribute('id', 'target-{$x}-{$y}');
  79                      var container = document.querySelector('.droparea');
  80                      container.insertBefore(target, image);
  81                      var xadjusted = {$x} + (container.offsetWidth - image.offsetWidth) / 2;
  82                      var yadjusted = {$y} + (container.offsetHeight - image.offsetHeight) / 2;
  83                      target.style.setProperty('position', 'absolute');
  84                      target.style.setProperty('left', xadjusted + 'px');
  85                      target.style.setProperty('top', yadjusted + 'px');
  86                      target.style.setProperty('width', '1px');
  87                      target.style.setProperty('height', '1px');
  88                  }())"
  89          );
  90  
  91          $generalcontext = behat_context_helper::get('behat_general');
  92          $generalcontext->i_drag_and_i_drop_it_in($this->marker_xpath($marker),
  93                  'xpath_element', "#target-{$x}-{$y}", 'css_element');
  94      }
  95  
  96      /**
  97       * Type some characters while focussed on a given drop box.
  98       *
  99       * @param string $direction the direction key to press.
 100       * @param int $
 101       * @param string $marker the marker to drag. The label, optionally followed by ,<instance number> (int) if relevant.
 102       *
 103       * @Given /^I type "(?P<direction>up|down|left|right)" "(?P<repeats>\d+)" times on marker "(?P<marker>[^"]*)" in the drag and drop markers question$/
 104       */
 105      public function i_type_on_marker_in_the_drag_and_drop_markers_question($direction, $repeats, $marker) {
 106          $node = $this->get_selected_node('xpath_element', $this->marker_xpath($marker, true));
 107          $this->ensure_node_is_visible($node);
 108          $node->focus();
 109          for ($i = 0; $i < $repeats; $i++) {
 110              $this->execute('behat_general::i_press_named_key', ['', $direction]);
 111          }
 112      }
 113  }