MDL-47494 qtype: Fix the behat tests for OU dd question types
[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.
a366c8b9
TH
74 $session = $this->getSession();
75 $session->evaluateScript("
76 (function() {
77 if (document.getElementById('target-{$x}-{$y}')) {
78 return;
79 }
80 var image = document.querySelector('.dropbackground');
81 var target = document.createElement('div');
82 target.setAttribute('id', 'target-{$x}-{$y}');
83 var container = document.querySelector('.droparea');
84 container.style.setProperty('position', 'relative');
85 container.insertBefore(target, image);
86 var xadjusted = {$x} + (container.offsetWidth - image.offsetWidth) / 2
87 target.style.setProperty('position', 'absolute');
88 target.style.setProperty('left', xadjusted + 'px');
89 target.style.setProperty('top', '{$y}px');
031152cc
DW
90 target.style.setProperty('width', '1px');
91 target.style.setProperty('height', '1px');
a366c8b9
TH
92 }())");
93
94 $generalcontext = behat_context_helper::get('behat_general');
95 $generalcontext->i_drag_and_i_drop_it_in($this->marker_xpath($marker, $item),
96 'xpath_element', "#target-{$x}-{$y}", 'css_element');
97 }
98
99 /**
100 * Type some characters while focussed on a given drop box.
101 *
102 * @param string $direction the direction key to press.
103 * @param int $
104 * @param string $marker the marker to drag. The label, optionally followed by ,<instance number> (int) if relevant.
105 *
106 * @Given /^I type "(?P<direction>up|down|left|right)" "(?P<repeats>\d+)" times on marker "(?P<marker>[^"]*)" in the drag and drop markers question$/
107 */
108 public function i_type_on_marker_in_the_drag_and_drop_markers_question($direction, $repeats, $marker) {
109 $keycodes = array(
110 'up' => chr(38),
111 'down' => chr(40),
112 'left' => chr(37),
113 'right' => chr(39),
114 );
a366c8b9 115 list($marker, $item) = $this->parse_marker_name($marker);
e885360f
TH
116 $node = $this->get_selected_node('xpath_element', $this->marker_xpath($marker, $item));
117 $this->ensure_node_is_visible($node);
118 for ($i = 0; $i < $repeats; $i++) {
119 $node->keyDown($keycodes[$direction]);
120 $node->keyPress($keycodes[$direction]);
121 $node->keyUp($keycodes[$direction]);
122 }
a366c8b9
TH
123 }
124}