MDL-47494 ddmarker: Fix dd qtype Behat tests to not use the generic type step. #14895
[moodle.git] / question / type / ddmarker / tests / behat / behat_qtype_ddmarker.php
CommitLineData
a366c8b9
TH
1<?php
2// This file is part of Stack - http://stack.bham.ac.uk/
3//
4// Stack 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// Stack 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 Stack. 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
28require_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 */
36class behat_qtype_ddmarker 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 marker_xpath($marker, $item = 0) {
44 return '//span[contains(@class, " dragitem ") and contains(@class, " item' . $item .
45 '") and span[@class = "markertext" and contains(normalize-space(.), "' .
46 $this->escape($marker) . '")]]';
47 }
48
49 protected function parse_marker_name($marker) {
50 $item = 0;
51 if (preg_match('~,(\d+)$~', $marker, $matches)) {
52 $item = $matches[1];
53 $marker = substr($marker, 0, -1 - strlen($item));
54 }
55 return array($marker, $item);
56 }
57
58 /**
59 * Drag the drag item with the given text to the given space.
60 *
61 * @param string $marker the marker to drag. The label, optionally followed by ,<instance number> (int) if relevant.
62 * @param string $coordinates the position to drag the marker to, 'x,y'.
63 *
64 * @Given /^I drag "(?P<marker>[^"]*)" to "(?P<coordinates>\d+,\d+)" in the drag and drop markers question$/
65 */
66 public function i_drag_to_in_the_drag_and_drop_markers_question($marker, $coordinates) {
67 list($marker, $item) = $this->parse_marker_name($marker);
68 list($x, $y) = explode(',', $coordinates);
69
70 // This is a bit nasty, but Behat (indeed Selenium) will only drag on
71 // DOM node so that its centre is over the centre of anothe DOM node.
72 // Therefore to make it drag to the specified place, we have to add
73 // a target div.
74 $style =
75 $session = $this->getSession();
76 $session->evaluateScript("
77 (function() {
78 if (document.getElementById('target-{$x}-{$y}')) {
79 return;
80 }
81 var image = document.querySelector('.dropbackground');
82 var target = document.createElement('div');
83 target.setAttribute('id', 'target-{$x}-{$y}');
84 var container = document.querySelector('.droparea');
85 container.style.setProperty('position', 'relative');
86 container.insertBefore(target, image);
87 var xadjusted = {$x} + (container.offsetWidth - image.offsetWidth) / 2
88 target.style.setProperty('position', 'absolute');
89 target.style.setProperty('left', xadjusted + 'px');
90 target.style.setProperty('top', '{$y}px');
91 target.style.setProperty('width', '0');
92 target.style.setProperty('height', '0');
93 }())");
94
95 $generalcontext = behat_context_helper::get('behat_general');
96 $generalcontext->i_drag_and_i_drop_it_in($this->marker_xpath($marker, $item),
97 'xpath_element', "#target-{$x}-{$y}", 'css_element');
98 }
99
100 /**
101 * Type some characters while focussed on a given drop box.
102 *
103 * @param string $direction the direction key to press.
104 * @param int $
105 * @param string $marker the marker to drag. The label, optionally followed by ,<instance number> (int) if relevant.
106 *
107 * @Given /^I type "(?P<direction>up|down|left|right)" "(?P<repeats>\d+)" times on marker "(?P<marker>[^"]*)" in the drag and drop markers question$/
108 */
109 public function i_type_on_marker_in_the_drag_and_drop_markers_question($direction, $repeats, $marker) {
110 $keycodes = array(
111 'up' => chr(38),
112 'down' => chr(40),
113 'left' => chr(37),
114 'right' => chr(39),
115 );
a366c8b9 116 list($marker, $item) = $this->parse_marker_name($marker);
e885360f
TH
117 $node = $this->get_selected_node('xpath_element', $this->marker_xpath($marker, $item));
118 $this->ensure_node_is_visible($node);
119 for ($i = 0; $i < $repeats; $i++) {
120 $node->keyDown($keycodes[$direction]);
121 $node->keyPress($keycodes[$direction]);
122 $node->keyUp($keycodes[$direction]);
123 }
a366c8b9
TH
124 }
125}