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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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 into text.
  19   *
  20   * @package   qtype_ddwtos
  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 into text 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_ddwtos extends behat_base {
  37  
  38      /**
  39       * Get the xpath for a given drag item.
  40       * @param string $dragitem the text of the item to drag.
  41       * @return string the xpath expression.
  42       */
  43      protected function drag_xpath($dragitem) {
  44          return '//div[@class="answercontainer"]//span[contains(@class, "draghome") and contains(., "' .
  45                  $this->escape($dragitem) . '") and not(contains(@class, "dragplaceholder"))]';
  46      }
  47  
  48      /**
  49       * Get the xpath for a given drop box.
  50       * @param string $spacenumber the number of the drop box.
  51       * @return string the xpath expression.
  52       */
  53      protected function drop_xpath($spacenumber) {
  54          return '//span[contains(@class, " drop ") and contains(@class, "place' . $spacenumber . ' ")]';
  55      }
  56  
  57      /**
  58       * Get the xpath for a given drop box contain a placed drag.
  59       * @param string $placeddragnumber the number of the placed drag.
  60       * @return string the xpath expression.
  61       */
  62      protected function inplace_xpath(string $placeddragnumber): string {
  63          return '//span[contains(@class, "inplace' . $placeddragnumber . '")]';
  64      }
  65  
  66      /**
  67       * Drag the drag item with the given text to the given space.
  68       *
  69       * @param string $dragitem the text of the item to drag.
  70       * @param int $spacenumber the number of the gap to drop into.
  71       *
  72       * @Given /^I drag "(?P<drag_item>[^"]*)" to space "(?P<space_number>\d+)" in the drag and drop into text question$/
  73       */
  74      public function i_drag_to_space_in_the_drag_and_drop_into_text_question($dragitem, $spacenumber) {
  75          $generalcontext = behat_context_helper::get('behat_general');
  76          $generalcontext->i_drag_and_i_drop_it_in($this->drag_xpath($dragitem),
  77                  'xpath_element', $this->drop_xpath($spacenumber), 'xpath_element');
  78      }
  79  
  80      /**
  81       * Drag the drag item with the given text to the given placed drag number.
  82       *
  83       * @param string $dragitem the text of the item to drag.
  84       * @param int $placeddragnumber the number of the placed drag to drop into.
  85       *
  86       * @Given /^I drag "(?P<drag_item>[^"]*)" to placed drag "(?P<number>\d+)" in the drag and drop into text question$/
  87       */
  88      public function i_drag_to_placed_drag_number_in_the_drag_and_drop_into_text_question(string $dragitem,
  89          int $placeddragnumber): void {
  90          $generalcontext = behat_context_helper::get('behat_general');
  91          $generalcontext->i_drag_and_i_drop_it_in($this->drag_xpath($dragitem),
  92              'xpath_element', $this->inplace_xpath($placeddragnumber), 'xpath_element');
  93      }
  94  
  95      /**
  96       * Type some characters while focussed on a given space.
  97       *
  98       * @param string $keys the characters to type.
  99       * @param int $spacenumber the number of the space to type into.
 100       *
 101       * @Given /^I type "(?P<keys>[^"]*)" into space "(?P<space_number>\d+)" in the drag and drop onto image question$/
 102       */
 103      public function i_type_into_space_in_the_drag_and_drop_into_text_question($keys, $spacenumber) {
 104          $node = $this->get_selected_node('xpath_element', $this->drop_xpath($spacenumber));
 105          $this->ensure_node_is_visible($node);
 106          $node->focus();
 107          foreach (str_split($keys) as $key) {
 108              behat_base::type_keys($this->getSession(), [$key]);
 109              $this->wait_for_pending_js();
 110          }
 111      }
 112  
 113      /**
 114       * Check that the given drag exist in drag home area
 115       *
 116       * @param string $dragitem the text of the drag item.
 117       *
 118       * @Given /^I should see "(?P<drag_item>[^"]*)" in the home area of drag and drop into text question$/
 119       */
 120      public function i_should_see_drag_in_the_home_area($dragitem) {
 121          $this->ensure_element_exists($this->drag_xpath($dragitem), 'xpath_element');
 122      }
 123  }