1 |
efrain |
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 |
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
|
|
|
18 |
|
|
|
19 |
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Behat steps definitions for the ordering question type.
|
|
|
23 |
*
|
|
|
24 |
* @package qtype_ordering
|
|
|
25 |
* @category test
|
|
|
26 |
* @copyright 2018 The Open University
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class behat_qtype_ordering extends behat_base {
|
|
|
30 |
/**
|
|
|
31 |
* Get the xpath for a given item by label.
|
|
|
32 |
* @param string $label the text of the item to drag.
|
|
|
33 |
* @return string the xpath expression.
|
|
|
34 |
*/
|
|
|
35 |
protected function item_xpath_by_label(string $label): string {
|
|
|
36 |
return "//li[contains(concat(' ', normalize-space(@class), ' '), 'sortableitem') and contains(normalize-space(.), '" .
|
|
|
37 |
$this->escape($label) . "')]";
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Get the xpath for a given drop box.
|
|
|
42 |
* @param string $position the number of place to drop it.
|
|
|
43 |
* @return string the xpath expression.
|
|
|
44 |
*/
|
|
|
45 |
protected function item_xpath_by_position(string $position): string {
|
|
|
46 |
return "//li[contains(concat(' ', normalize-space(@class), ' '), 'sortableitem')][" . $position . "]";
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Drag the drag item with the given text to the given space.
|
|
|
51 |
*
|
|
|
52 |
* Warning, only works if the question is using a behaviour like Immediate
|
|
|
53 |
* feedback that has a check button.
|
|
|
54 |
*
|
|
|
55 |
* Also, do not use this to drag an item to the last place. Just drag all
|
|
|
56 |
* the other non-last items to their place.
|
|
|
57 |
*
|
|
|
58 |
* @param string $label the text of the item to drag.
|
|
|
59 |
* @param int $position the number of the position to drop it at.
|
|
|
60 |
*
|
|
|
61 |
* @Given /^I drag "(?P<label>[^"]*)" to space "(?P<position>\d+)" in the ordering question$/
|
|
|
62 |
*/
|
|
|
63 |
public function i_drag_to_space_in_the_drag_and_drop_into_text_question(string $label, int $position): void {
|
|
|
64 |
|
|
|
65 |
$testingpos = $position - 1; // 0-based index.
|
|
|
66 |
|
|
|
67 |
$generalcontext = behat_context_helper::get('behat_general');
|
|
|
68 |
|
|
|
69 |
$this->execute_script("
|
|
|
70 |
(function() {
|
|
|
71 |
var droptarget = document.createElement('li');
|
|
|
72 |
droptarget.setAttribute('class', 'dtb');
|
|
|
73 |
var items = document.querySelector('.sortablelist');
|
|
|
74 |
items.insertBefore(droptarget, items.children[$testingpos]);
|
|
|
75 |
}())"
|
|
|
76 |
);
|
|
|
77 |
|
11 |
efrain |
78 |
// Ensure the script has run and the item is there.
|
|
|
79 |
$generalcontext->wait_until_exists(
|
|
|
80 |
'.dtb',
|
|
|
81 |
'css_element'
|
|
|
82 |
);
|
|
|
83 |
|
1 |
efrain |
84 |
$generalcontext->i_drag_and_i_drop_it_in(
|
|
|
85 |
$this->item_xpath_by_label($label),
|
|
|
86 |
'xpath_element',
|
|
|
87 |
"//li[contains(concat(' ', normalize-space(@class), ' '), 'dtb')]",
|
|
|
88 |
'xpath_element'
|
|
|
89 |
);
|
|
|
90 |
|
|
|
91 |
$this->execute_script("
|
|
|
92 |
(function() {
|
|
|
93 |
var item = document.querySelector('.dtb');
|
|
|
94 |
item.parentNode.removeChild(item);
|
|
|
95 |
}())"
|
|
|
96 |
);
|
11 |
efrain |
97 |
|
|
|
98 |
// Ensure the script has run and the item is gone.
|
|
|
99 |
$generalcontext->wait_until_does_not_exists(
|
|
|
100 |
'.dtb',
|
|
|
101 |
'css_element'
|
|
|
102 |
);
|
1 |
efrain |
103 |
}
|
|
|
104 |
}
|