Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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__ . '/behat_question_base.php');
20
 
21
use Behat\Gherkin\Node\TableNode as TableNode;
22
use Behat\Mink\Exception\ExpectationException as ExpectationException;
23
use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
24
 
25
/**
26
 * Steps definitions related with the question bank management.
27
 *
28
 * @package    core_question
29
 * @category   test
30
 * @copyright  2013 David Monllaó
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class behat_core_question extends behat_question_base {
34
 
35
    /**
36
     * Convert page names to URLs for steps like 'When I am on the "[page name]" page'.
37
     *
38
     * Recognised page names are:
39
     * | None so far!      |                                                              |
40
     *
41
     * @param string $page name of the page, with the component name removed e.g. 'Admin notification'.
42
     * @return moodle_url the corresponding URL.
43
     * @throws Exception with a meaningful error message if the specified page cannot be found.
44
     */
45
    protected function resolve_page_url(string $page): moodle_url {
46
        switch (strtolower($page)) {
47
            default:
48
                throw new Exception('Unrecognised core_question page type "' . $page . '."');
49
        }
50
    }
51
 
52
    /**
53
     * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
54
     *
55
     * Recognised page names are:
56
     * | pagetype               | name meaning               | description                              |
57
     * | course question bank   | Course name                | The question bank for a course           |
58
     * | course question import | Course name                | The import questions screen for a course |
59
     * | course question export | Course name                | The export questions screen for a course |
60
     * | preview                | Question name              | The screen to preview a question         |
61
     * | edit                   | Question name              | The screen to edit a question            |
62
     *
63
     * @param string $type identifies which type of page this is, e.g. 'Preview'.
64
     * @param string $identifier identifies the particular page, e.g. 'My question'.
65
     * @return moodle_url the corresponding URL.
66
     * @throws Exception with a meaningful error message if the specified page cannot be found.
67
     */
68
    protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
69
        switch (strtolower($type)) {
70
            case 'course question bank':
11 efrain 71
                // The question bank does not handle fields at the edge of the viewport well.
72
                // Increase the size to avoid this.
73
                $this->execute('behat_general::i_change_window_size_to', ['window', 'large']);
74
                return new moodle_url('/question/edit.php', [
75
                    'courseid' => $this->get_course_id($identifier),
76
                ]);
1 efrain 77
 
78
            case 'course question categories':
79
                return new moodle_url('/question/bank/managecategories/category.php',
80
                        ['courseid' => $this->get_course_id($identifier)]);
81
 
82
            case 'course question import':
83
                return new moodle_url('/question/bank/importquestions/import.php',
84
                        ['courseid' => $this->get_course_id($identifier)]);
85
 
86
            case 'course question export':
87
                return new moodle_url('/question/bank/exportquestions/export.php',
88
                        ['courseid' => $this->get_course_id($identifier)]);
89
 
90
            case 'preview':
91
                [$questionid, $otheridtype, $otherid] = $this->find_question_by_name($identifier);
92
                return new moodle_url('/question/bank/previewquestion/preview.php',
93
                        ['id' => $questionid, $otheridtype => $otherid]);
94
 
95
            case 'edit':
96
                [$questionid, $otheridtype, $otherid] = $this->find_question_by_name($identifier);
97
                return new moodle_url('/question/bank/editquestion/question.php',
98
                        ['id' => $questionid, $otheridtype => $otherid]);
99
 
100
            default:
101
                throw new Exception('Unrecognised core_question page type "' . $type . '."');
102
        }
103
    }
104
 
105
    /**
106
     * Find a question, and where it is, from the question name.
107
     *
108
     * This is a helper used by resolve_page_instance_url.
109
     *
110
     * @param string $questionname
111
     * @return array with three elemnets, int question id, a string 'cmid' or 'courseid',
112
     *     and int either cmid or courseid as applicable.
113
     */
114
    protected function find_question_by_name(string $questionname): array {
115
        global $DB;
116
        $questionid = $DB->get_field('question', 'id', ['name' => $questionname], MUST_EXIST);
117
        $question = question_bank::load_question_data($questionid);
118
        $context = context_helper::instance_by_id($question->contextid);
119
 
120
        if ($context->contextlevel == CONTEXT_MODULE) {
121
            return [$questionid, 'cmid', $context->instanceid];
122
        } else if ($context->contextlevel == CONTEXT_COURSE) {
123
            return [$questionid, 'courseid', $context->instanceid];
124
        } else {
125
            throw new coding_exception('Unsupported context level ' . $context->contextlevel);
126
        }
127
    }
128
 
129
    /**
130
     * Creates a question in the current course questions bank with the provided data.
131
     * This step can only be used when creating question types composed by a single form.
132
     *
133
     * @Given /^I add a "(?P<question_type_name_string>(?:[^"]|\\")*)" question filling the form with:$/
134
     * @param string $questiontypename The question type name
135
     * @param TableNode $questiondata The data to fill the question type form.
136
     */
137
    public function i_add_a_question_filling_the_form_with($questiontypename, TableNode $questiondata) {
138
        // Click on create question.
139
        $this->execute('behat_forms::press_button', get_string('createnewquestion', 'question'));
140
 
141
        // Add question.
142
        $this->finish_adding_question($questiontypename, $questiondata);
143
    }
144
 
145
    /**
146
     * Checks the state of the specified question.
147
     *
148
     * @Then /^the state of "(?P<question_description_string>(?:[^"]|\\")*)" question is shown as "(?P<state_string>(?:[^"]|\\")*)"$/
149
     * @throws ExpectationException
150
     * @throws ElementNotFoundException
151
     * @param string $questiondescription
152
     * @param string $state
153
     */
154
    public function the_state_of_question_is_shown_as($questiondescription, $state) {
155
 
156
        // Using xpath literal to avoid quotes problems.
157
        $questiondescriptionliteral = behat_context_helper::escape($questiondescription);
158
        $stateliteral = behat_context_helper::escape($state);
159
 
160
        // Split in two checkings to give more feedback in case of exception.
161
        $exception = new ElementNotFoundException($this->getSession(), 'Question "' . $questiondescription . '" ');
162
        $questionxpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]" .
163
                "[contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')]," .
164
                "{$questiondescriptionliteral})]";
165
        $this->find('xpath', $questionxpath, $exception);
166
 
167
        $exception = new ExpectationException('Question "' . $questiondescription .
168
                '" state is not "' . $state . '"', $this->getSession());
169
        $xpath = $questionxpath . "/div[@class='info']/div[@class='state' and contains(., {$stateliteral})]";
170
        $this->find('xpath', $xpath, $exception);
171
    }
172
 
173
    /**
174
     * Activates a particular action on a particular question in the question bank UI.
175
     *
176
     * @When I choose :action action for :questionname in the question bank
177
     * @param string $action the label for the action you want to activate.
178
     * @param string $questionname the question name.
179
     */
180
    public function i_action_the_question($action, $questionname) {
181
        if ($this->running_javascript()) {
182
            // This method isn't allowed unless Javascript is running.
183
            $this->execute('behat_action_menu::i_open_the_action_menu_in', [
184
                $questionname,
185
                'table_row',
186
            ]);
187
            $this->execute('behat_action_menu::i_choose_in_the_open_action_menu', [
188
                $action
189
            ]);
190
        } else {
191
            // This method doesn't open the menu correctly when Javascript is running.
192
            $this->execute('behat_action_menu::i_choose_in_the_named_menu_in_container', [
193
                $action,
194
                get_string('edit', 'core'),
195
                $questionname,
196
                'table_row',
197
            ]);
198
        }
199
    }
200
 
201
    /**
202
     * Checks that action does exist for a question.
203
     *
204
     * @Then the :action action should exist for the :questionname question in the question bank
205
     * @param string $action the label for the action you want to activate.
206
     * @param string $questionname the question name.
207
     */
208
    public function action_exists($action, $questionname) {
209
        $this->execute('behat_action_menu::item_should_exist_in_the', [
210
            $action,
211
            get_string('edit', 'core'),
212
            $questionname,
213
            'table_row',
214
        ]);
215
    }
216
 
217
    /**
218
     * Checks that action does not exist for a question.
219
     *
220
     * @Then the :action action should not exist for the :questionname question in the question bank
221
     * @param string $action the label for the action you want to activate.
222
     * @param string $questionname the question name.
223
     */
224
    public function action_not_exists($action, $questionname) {
225
        $this->execute('behat_action_menu::item_should_not_exist_in_the', [
226
            $action,
227
            get_string('edit', 'core'),
228
            $questionname,
229
            'table_row',
230
        ]);
231
    }
232
 
233
    /**
234
     * A particular bulk action is visible in the question bank UI.
235
     *
236
     * @When I should see question bulk action :action
237
     * @param string $action the value of the input for the action.
238
     */
239
    public function i_should_see_question_bulk_action($action) {
240
        // Check if its visible.
241
        $this->execute("behat_general::should_be_visible",
242
            ["#bulkactionsui-container input[name='$action']", "css_element"]);
243
    }
244
 
245
    /**
246
     * A particular bulk action should not be visible in the question bank UI.
247
     *
248
     * @When I should not see question bulk action :action
249
     * @param string $action the value of the input for the action.
250
     */
251
    public function i_should_not_see_question_bulk_action($action) {
252
        // Check if its visible.
253
        $this->execute("behat_general::should_not_be_visible",
254
            ["#bulkactionsui-container input[name='$action']", "css_element"]);
255
    }
256
 
257
    /**
258
     * A click on a particular bulk action in the question bank UI.
259
     *
260
     * @When I click on question bulk action :action
261
     * @param string $action the value of the input for the action.
262
     */
263
    public function i_click_on_question_bulk_action($action) {
264
        // Click the bulk action.
265
        $this->execute("behat_general::i_click_on",
266
            ["#bulkactionsui-container input[name='$action']", "css_element"]);
267
    }
268
 
269
    /**
270
     * Change the question type of the give question to a type that does not exist.
271
     *
272
     * This is useful for testing robustness of the code when a question type
273
     * has been uninstalled, even though there are still questions of that type
274
     * or attempts at them.
275
     *
276
     * In order to set things up, you probably need to start by generating
277
     * questions of a valid type, then using this to change the type once the
278
     * data is created.
279
     *
280
     * @Given question :questionname is changed to simulate being of an uninstalled type
281
     * @param string $questionname the question name.
282
     */
283
    public function change_question_to_nonexistant_type($questionname) {
284
        global $DB;
285
        [$id] = $this->find_question_by_name($questionname);
286
 
287
        // Check our assumption.
288
        $nonexistanttype = 'invalidqtype';
289
        if (question_bank::is_qtype_installed($nonexistanttype)) {
290
            throw new coding_exception('This code assumes that the qtype_' . $nonexistanttype .
291
                    ' is not a valid plugin name, but that plugin now seems to exist!');
292
        }
293
 
294
        $DB->set_field('question', 'qtype', $nonexistanttype, ['id' => $id]);
295
        question_bank::notify_question_edited($id);
296
    }
297
 
298
    /**
299
     * Forcibly delete a question from the database.
300
     *
301
     * This is useful for testing robustness of the code when a question
302
     * record is no longer in the database, even though it is referred to.
303
     * Obviously, this should never happen, but it has been known to in the past
304
     * and so we sometimes need to be able to test the code can handle this situation.
305
     *
306
     * In order to set things up, you probably need to start by generating
307
     * a valid questions, then using this to remove it once the data is created.
308
     *
309
     * @Given question :questionname no longer exists in the database
310
     * @param string $questionname the question name.
311
     */
312
    public function remove_question_from_db($questionname) {
313
        global $DB;
314
        [$id] = $this->find_question_by_name($questionname);
315
        $DB->delete_records('question', ['id' => $id]);
316
        question_bank::notify_question_edited($id);
317
    }
318
 
319
    /**
320
     * Add a question bank filter
321
     *
322
     * This will add the filter if it does not exist, but leave the value empty.
323
     *
324
     * @When I add question bank filter :filtertype
325
     * @param string $filtertype The filter we are adding
326
     */
327
    public function i_add_question_bank_filter(string $filtertype) {
328
        $filter = $this->getSession()->getPage()->find('css',
329
                '[data-filterregion=filter] [data-field-title="' . $filtertype . '"]');
330
        if ($filter === null) {
331
            $this->execute('behat_forms::press_button', [get_string('addcondition')]);
332
            $this->execute('behat_forms::i_set_the_field_in_container_to', [
333
                    "type",
334
                    "[data-filterregion=filter]:last-child fieldset",
335
                    "css_element",
336
                    $filtertype
337
            ]);
338
        }
339
    }
340
 
341
    /**
342
     * Apply question bank filter.
343
     *
344
     * This will change the existing value of the specified filter, or add the filter and set its value if it doesn't already
345
     * exist.
346
     *
347
     * @When I apply question bank filter :filtertype with value :value
348
     * @param string $filtertype The filter to apply. This should match the get_title() return value from the
349
     *        filter's condition class.
350
     * @param string $value The value to set for the condition.
351
     */
352
    public function i_apply_question_bank_filter(string $filtertype, string $value) {
353
        // Add the filter if needed.
354
        $this->execute('behat_core_question::i_add_question_bank_filter', [
355
            $filtertype,
356
        ]);
357
 
358
        // Set the filter value.
359
        $this->execute('behat_forms::i_set_the_field_to', [
360
            $filtertype,
361
            $value
362
        ]);
363
 
364
        // Apply filters.
365
        $this->execute("behat_forms::press_button", [get_string('applyfilters')]);
366
    }
367
}