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
/**
18
 * Behat grade related steps definitions.
19
 *
20
 * @package    core_grades
21
 * @category   test
22
 * @copyright  2014 Mark Nelson <markn@moodle.com>
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
use Behat\Gherkin\Node\TableNode as TableNode;
31
 
32
class behat_grade extends behat_base {
33
 
34
    /**
35
     * Enters a grade via the gradebook for a specific grade item and user when viewing the 'Grader report' with editing mode turned on.
36
     *
37
     * @Given /^I give the grade "(?P<grade_number>(?:[^"]|\\")*)" to the user "(?P<username_string>(?:[^"]|\\")*)" for the grade item "(?P<grade_activity_string>(?:[^"]|\\")*)"$/
38
     * @param int $grade
39
     * @param string $userfullname the user's fullname as returned by fullname()
40
     * @param string $itemname
41
     */
42
    public function i_give_the_grade($grade, $userfullname, $itemname) {
43
        $this->execute('behat_navigation::i_close_block_drawer_if_open');
44
        $gradelabel = $userfullname . ' ' . $itemname;
45
        $fieldstr = get_string('useractivitygrade', 'gradereport_grader', $gradelabel);
46
 
47
        $this->execute('behat_forms::i_set_the_field_to', array($this->escape($fieldstr), $grade));
48
    }
49
 
50
    /**
51
     * Changes the settings of a grade item or category or the course.
52
     *
53
     * Teacher must be either on the grade setup page or on the Grader report page with editing mode turned on.
54
     *
55
     * @Given /^I set the following settings for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" of type "([^"]*)" on "([^"]*)" page:$/
56
     * @param string $gradeitem
57
     * @param string $type
58
     * @param string $page
59
     * @param TableNode $data
60
     */
61
    public function i_set_the_following_settings_for_grade_item(string $gradeitem, string $type, string $page, TableNode $data) {
62
        $this->execute("behat_navigation::i_close_block_drawer_if_open");
63
 
64
        if ($this->running_javascript()) {
65
            $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, $type, $page]);
66
        }
67
 
68
        $linktext = $type == 'gradeitem' ? get_string('itemsedit', 'grades') : get_string('categoryedit', 'grades');
69
 
70
        $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $linktext);
71
 
72
        if ($type !== 'gradeitem') {
73
            $this->execute('behat_general::i_click_on_in_the', [get_string('showmore', 'form'),
74
                'link', '.modal-dialog', 'css_element']);
75
        }
76
 
11 efrain 77
 
1 efrain 78
        $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
79
        if ($this->getSession()->getPage()->find('xpath', './/button[@data-action="save"]')) {
80
            $container = $this->get_selected_node("core_grades > gradeitem modal", "form");
11 efrain 81
            $this->execute('behat_general::i_click_on_in_the', [
82
                './/button[@data-action="save"]',
83
                'xpath',
84
                $container,
85
                'NodeElement',
86
            ]);
1 efrain 87
        } else {
88
            $savechanges = get_string('savechanges', 'grades');
89
            $this->execute('behat_forms::press_button', $this->escape($savechanges));
90
        }
91
    }
92
 
93
    /**
94
     * Hids a grade item or category on gradebook setup or grader page.
95
     *
96
     * Teacher must be on the grade setup page.
97
     *
98
     * @Given /^I hide the grade item "(?P<grade_item_string>(?:[^"]|\\")*)" of type "([^"]*)" on "([^"]*)" page$/
99
     * @param string $gradeitem
100
     * @param string $type
101
     * @param string $page
102
     */
103
    public function i_hide_the_grade_item(string $gradeitem, string $type, string $page) {
104
 
105
        $linktext = get_string('hide');
106
 
107
        if ($this->running_javascript()) {
108
            $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, $type, $page]);
109
        }
110
 
111
        $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $linktext);
112
    }
113
 
114
    /**
115
     * Duplicates a grade item or category.
116
     *
117
     * Teacher must be on the grade setup page.
118
     *
119
     * @Given /^I duplicate the grade item "(?P<grade_item_string>(?:[^"]|\\")*)"$/
120
     * @param string $gradeitem
121
     */
122
    public function i_duplicate_the_grade_item(string $gradeitem) {
123
        $linktext = get_string('duplicate');
124
 
125
        if ($this->running_javascript()) {
126
            $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, 'gradeitem', 'setup']);
127
        }
128
 
129
        $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $linktext);
130
    }
131
 
132
    /**
133
     * Sets a calculated manual grade item. Needs a table with item name - idnumber relation.
134
     * The step requires you to be in the 'Gradebook setup' page.
135
     *
136
     * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
137
     * @param string $calculation The calculation.
138
     * @param string $gradeitem The grade item name.
139
     * @param TableNode $TableNode The grade item name - idnumbers relation.
140
     */
141
    public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
142
 
143
        $edit = get_string('editcalculation', 'grades');
144
 
145
        if ($this->running_javascript()) {
146
            $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, 'gradeitem', 'setup']);
147
        }
148
        $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $edit);
149
 
150
        $savechanges = get_string('savechanges', 'grades');
151
 
152
        // Mapping names to idnumbers.
153
        $datahash = $data->getRowsHash();
154
        foreach ($datahash as $gradeitem => $idnumber) {
155
            // This xpath looks for course, categories and items with the provided name.
156
            // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
157
            $inputxpath = "//input[@class='idnumber'][" .
158
                    "parent::li[@class='item'][text()='" . $gradeitem . "']" .
159
                    " or " .
160
                    "parent::li[@class='categoryitem' or @class='courseitem']" .
161
                    "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
162
                    "]";
163
            $this->execute('behat_forms::i_set_the_field_with_xpath_to', [$inputxpath, $idnumber]);
164
        }
165
 
166
        $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
167
        $this->execute('behat_forms::i_set_the_field_to', [get_string('calculation', 'grades'), $calculation]);
168
        $this->execute('behat_forms::press_button', $savechanges);
169
 
170
    }
171
 
172
    /**
173
     * Sets a calculated manual grade category total. Needs a table with item name - idnumber relation.
174
     * The step requires you to be in the 'Gradebook setup' page.
175
     *
176
     * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade category "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
177
     * @param string $calculation The calculation.
178
     * @param string $gradeitem The grade item name.
179
     * @param TableNode $data The grade item name - idnumbers relation.
180
     */
181
    public function i_set_calculation_for_grade_category_with_idnumbers(string $calculation, string $gradeitem, TableNode $data) {
182
 
183
        $edit = get_string('editcalculation', 'grades');
184
 
185
        if ($this->running_javascript()) {
186
            $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, 'gradeitem', 'setup']);
187
        }
188
        $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $edit);
189
 
190
        $savechanges = get_string('savechanges', 'grades');
191
 
192
        // Mapping names to idnumbers.
193
        $datahash = $data->getRowsHash();
194
        foreach ($datahash as $gradeitem => $idnumber) {
195
            // This xpath looks for course, categories and items with the provided name.
196
            // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
197
            $inputxpath = "//input[@class='idnumber'][" .
198
                    "parent::li[@class='item'][text()='" . $gradeitem . "']" .
199
                    " | " .
200
                    "parent::li[@class='categoryitem' or @class='courseitem']" .
201
                    "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
202
                    "]";
203
            $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
204
        }
205
 
206
        $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
207
 
208
        $this->execute('behat_forms::i_set_the_field_to', [get_string('calculation', 'grades'), $calculation]);
209
        $this->execute('behat_forms::press_button', $savechanges);
210
    }
211
 
212
    /**
213
     * Resets the weights for the grade category
214
     *
215
     * Teacher must be on the grade setup page.
216
     *
217
     * @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
218
     * @param string $gradeitem
219
     */
220
    public function i_reset_weights_for_grade_category(string $gradeitem) {
221
 
222
        if ($this->running_javascript()) {
223
            $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, 'category', 'setup']);
224
        }
225
        $linktext = get_string('resetweightsshort', 'grades');
226
        $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", [$this->escape($linktext), "link"]);
227
    }
228
 
229
    /**
230
     * Step allowing to test before-the-fix behaviour of the gradebook
231
     *
232
     * @Given /^gradebook calculations for the course "(?P<coursename_string>(?:[^"]|\\")*)" are frozen at version "(?P<version_string>(?:[^"]|\\")*)"$/
233
     * @param string $coursename
234
     * @param string $version
235
     */
236
    public function gradebook_calculations_for_the_course_are_frozen_at_version($coursename, $version) {
237
        global $DB;
238
        $courseid = $DB->get_field('course', 'id', array('shortname' => $coursename), MUST_EXIST);
239
        set_config('gradebook_calculations_freeze_' . $courseid, $version);
240
    }
241
 
242
    /**
243
     * Select the tab in the gradebook. We must be on one of the gradebook pages already.
244
     *
245
     * @deprecated since 4.0 - use behat_forms::i_set_the_field_to() instead.
246
     * @param string $gradepath examples: "View > User report", "Letters > View", "Scales"
247
     */
248
    protected function select_in_gradebook_tabs($gradepath) {
249
        debugging('The function select_in_gradebook_tabs() is deprecated, please use ' .
250
            'behat_forms::i_set_the_field_to() instead.', DEBUG_DEVELOPER);
251
 
252
        $gradepath = preg_split('/\s*>\s*/', trim($gradepath));
253
        if (count($gradepath) > 2) {
254
            throw new coding_exception('Grade path is too long (must have no more than two items separated with ">")');
255
        }
256
 
257
        $xpath = '//div[contains(@class,\'grade-navigation\')]';
258
 
259
        // If the first row of the grade-navigation tabs does not have $gradepath[0] as active tab, click on it.
260
        $link = '\'' . $this->escape($gradepath[0]) . '\'';
261
        $xpathrow1 = $xpath . '//ul[1]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
262
        if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow1)) {
263
            $this->find('xpath', $xpath . '//ul[1]/li/a[text()=' . $link . ']')->click();
264
            $this->wait_for_pending_js();
265
        }
266
 
267
        if (isset($gradepath[1])) {
268
            // If the second row of the grade-navigation tabs does not have $gradepath[1] as active tab, click on it.
269
            $link = '\'' . $this->escape($gradepath[1]) . '\'';
270
            $xpathrow2 = $xpath . '//ul[2]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
271
            if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow2)) {
272
                $this->find('xpath', $xpath . '//ul[2]/li/a[text()=' . $link . ']')->click();
273
                $this->wait_for_pending_js();
274
            }
275
        }
276
    }
277
 
278
    /**
279
     * Navigates to the course gradebook and selects the specified item from the general grade navigation selector.
280
     *
281
     * Examples:
282
     * - I navigate to "Setup > Gradebook setup" in the course gradebook
283
     * - I navigate to "Scales" in the course gradebook
284
     * - I navigate to "More > Grade letters" in the course gradebook
285
     *
286
     * @Given /^I navigate to "(?P<gradepath_string>(?:[^"]|\\")*)" in the course gradebook$/
287
     * @param string $gradepath The path string. If the path has two items (ex. "More > Grade letters"), the first item
288
     *                          ("More") will be used to identify an option group in the navigation selector, while the
289
     *                          second ("Grade letters") will be used to identify an option within that option group.
290
     *                          Otherwise, a single item in a path (ex. "Scales") will be used to identify an option in
291
     *                          the navigation selector regardless of the option group.
292
     */
293
    public function i_navigate_to_in_the_course_gradebook($gradepath) {
294
        // If we are not on one of the gradebook pages already, follow "Grades" link in the navigation drawer.
295
        $xpath = '//div[contains(@class,\'grade-navigation\')]';
296
        if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
297
            $this->execute('behat_navigation::i_select_from_secondary_navigation', get_string('grades'));
298
        }
299
 
300
        $this->execute('behat_forms::i_set_the_field_to', [get_string('gradebooknavigationmenu', 'grades'), $gradepath]);
301
    }
302
 
303
    /**
304
     * Navigates to the imports page in the course gradebook and selects the specified import type from the grade
305
     * imports navigation selector.
306
     *
307
     * Examples:
308
     * - I navigate to "CSV file" import page in the course gradebook
309
     *
310
     * @Given /^I navigate to "(?P<importoption_string>(?:[^"]|\\")*)" import page in the course gradebook$/
311
     * @param string $gradeimportoption The name of an existing grade import option.
312
     */
313
    public function i_navigate_to_import_page_in_the_course_gradebook($gradeimportoption) {
314
        $this->i_navigate_to_in_the_course_gradebook("More > Import");
315
        $this->execute('behat_forms::i_set_the_field_to', [get_string('importas', 'grades'), $gradeimportoption]);
316
    }
317
 
318
    /**
319
     * Navigates to the exports page in the course gradebook and selects the specified export type from the grade
320
     * exports navigation selector.
321
     *
322
     * Examples:
323
     * - I navigate to "XML file" export page in the course gradebook
324
     *
325
     * @Given /^I navigate to "(?P<exportoption_string>(?:[^"]|\\")*)" export page in the course gradebook$/
326
     * @param string $gradeexportoption The name of an existing grade export option.
327
     */
328
    public function i_navigate_to_export_page_in_the_course_gradebook($gradeexportoption) {
329
        $this->i_navigate_to_in_the_course_gradebook("More > Export");
330
        $this->execute('behat_forms::i_set_the_field_to', [get_string('exportas', 'grades'), $gradeexportoption]);
331
    }
332
 
333
    /**
334
     * Select a given option from a navigation URL selector in the gradebook. We must be on one of the gradebook pages
335
     * already.
336
     *
337
     * @deprecated since 4.1 - use behat_forms::i_set_the_field_to() instead.
338
     * @param string $path The string path that is used to identify an item within the navigation selector. If the path
339
     *                     has two items (ex. "More > Grade letters"), the first item ("More") will be used to identify
340
     *                     an option group in the navigation selector, while the second ("Grade letters") will be used to
341
     *                     identify an option within that option group. Otherwise, a single item in a path (ex. "Scales")
342
     *                     will be used to identify an option in the navigation selector regardless of the option group.
343
     * @param string $formid The ID of the form element which contains the navigation URL selector element.
344
     */
345
    protected function select_in_gradebook_navigation_selector(string $path, string $formid) {
346
        debugging('The function select_in_gradebook_navigation_selector() is deprecated, please use ' .
347
            'behat_forms::i_set_the_field_to() instead.', DEBUG_DEVELOPER);
348
 
349
        // Split the path string by ">".
350
        $path = preg_split('/\s*>\s*/', trim($path));
351
 
352
        // Make sure that the path does not have more than two items separated with ">".
353
        if (count($path) > 2) {
354
            throw new coding_exception('The path is too long (must have no more than two items separated with ">")');
355
        }
356
 
357
        // Get the select element.
358
        $selectxpath = "//form[contains(@id,'{$formid}')]//select";
359
        $select = $this->find('xpath', $selectxpath);
360
 
361
        // Define the xpath to the option element depending on the provided path.
362
        // If two items are provided in the path, the first item will be considered as an identifier of an existing
363
        // option group in the select select element, while the second item will identify an existing option within
364
        // that option group.
365
        // If one item is provided in the path, this item will identify any existing option in the select element
366
        // regardless of the option group. Also, this is useful when option elements are not a part of an option group
367
        // which is possible.
368
        if (count($path) === 2) {
369
            $optionxpath = $selectxpath . '/optgroup[@label="' . $this->escape($path[0]) . '"]' .
370
                '/option[contains(.,"' . $this->escape($path[1]) . '")]';
371
        } else {
372
            $optionxpath = $selectxpath . '//option[contains(.,"' . $this->escape($path[0]) . '")]';
373
        }
374
 
375
        // Get the option element that we are looking to select.
376
        $option = $this->find('xpath', $optionxpath);
377
 
378
        // Select the given option in the select element.
379
        $field = behat_field_manager::get_field_instance('select', $select, $this->getSession());
380
        $field->set_value($this->escape($option->getValue()));
381
 
382
        if (!$this->running_javascript()) {
383
            $this->execute('behat_general::i_click_on_in_the', [get_string('go'), 'button',
384
                "#{$formid}", 'css_element']);
385
        }
386
    }
387
 
388
    /**
389
     * We tend to use this series of steps a bit so define em once.
390
     *
391
     * @param string $haystack What are we searching within?
392
     * @param string $needle What are we looking for?
393
     * @param bool $fieldset Do we want to set the search field at the same time?
394
     * @return string
395
     * @throws coding_exception
396
     */
397
    private function get_dropdown_selector(string $haystack, string $needle, bool $fieldset = true): string {
398
        $this->execute("behat_general::wait_until_the_page_is_ready");
399
 
400
        // Set the default field to search and handle any special preamble.
401
        $string = get_string('searchusers', 'core');
402
        $selector = '.usersearchdropdown';
403
        if (strtolower($haystack) === 'group') {
404
            $string = get_string('searchgroups', 'core');
405
            $selector = '.groupsearchdropdown';
406
            $trigger = ".groupsearchwidget";
407
            $node = $this->find("css_element", $selector);
408
            if (!$node->isVisible()) {
409
                $this->execute("behat_general::i_click_on", [$trigger, "css_element"]);
410
            }
411
        } else if (strtolower($haystack) === 'grade') {
412
            $string = get_string('searchitems', 'core');
413
            $selector = '.gradesearchdropdown';
414
            $trigger = ".gradesearchwidget";
415
            $node = $this->find("css_element", $selector);
416
            if (!$node->isVisible()) {
417
                $this->execute("behat_general::i_click_on", [$trigger, "css_element"]);
418
            }
419
        }
420
 
421
        if ($fieldset) {
422
            $this->execute("behat_forms::set_field_value", [$string, $needle]);
423
            $this->execute("behat_general::wait_until_exists", [$needle, "list_item"]);
424
        }
425
        return $selector;
426
    }
427
 
428
    /**
429
     * Confirm if a value is within the search widget within the gradebook.
430
     *
431
     * Examples:
432
     * - I confirm "User" in "user" search within the gradebook widget exists
433
     * - I confirm "Group" in "group" search within the gradebook widget exists
434
     * - I confirm "Grade item" in "grade" search within the gradebook widget exists
435
     *
436
     * @Given /^I confirm "(?P<needle>(?:[^"]|\\")*)" in "(?P<haystack>(?:[^"]|\\")*)" search within the gradebook widget exists$/
437
     * @param string $needle The value to search for.
438
     * @param string $haystack The type of the search widget.
439
     */
440
    public function i_confirm_in_search_within_the_gradebook_widget_exists($needle, $haystack) {
441
        $this->execute("behat_general::assert_element_contains_text",
442
            [$needle, $this->get_dropdown_selector($haystack, $needle, false), "css_element"]);
443
    }
444
 
445
    /**
446
     * Confirm if a value is not within the search widget within the gradebook.
447
     *
448
     * Examples:
449
     * - I confirm "User" in "user" search within the gradebook widget does not exist
450
     * - I confirm "Group" in "group" search within the gradebook widget does not exist
451
     * - I confirm "Grade item" in "grade" search within the gradebook widget does not exist
452
     *
453
     * @Given /^I confirm "(?P<needle>(?:[^"]|\\")*)" in "(?P<haystack>(?:[^"]|\\")*)" search within the gradebook widget does not exist$/
454
     * @param string $needle The value to search for.
455
     * @param string $haystack The type of the search widget.
456
     */
457
    public function i_confirm_in_search_within_the_gradebook_widget_does_not_exist($needle, $haystack) {
458
        $this->execute("behat_general::assert_element_not_contains_text",
459
            [$needle, $this->get_dropdown_selector($haystack, $needle, false), "css_element"]);
460
    }
461
 
462
    /**
463
     * Clicks on an option from the specified search widget in the current gradebook page.
464
     *
465
     * Examples:
466
     * - I click on "Student" in the "user" search widget
467
     * - I click on "Group" in the "group" search widget
468
     * - I click on "Grade item" in the "grade" search widget
469
     *
470
     * @Given /^I click on "(?P<needle>(?:[^"]|\\")*)" in the "(?P<haystack>(?:[^"]|\\")*)" search widget$/
471
     * @param string $needle The value to search for.
472
     * @param string $haystack The type of the search widget.
473
     */
474
    public function i_click_on_in_search_widget(string $needle, string $haystack) {
475
        $selector = $this->get_dropdown_selector($haystack, $needle);
476
        $this->execute('behat_general::i_click_on_in_the', [
477
            $needle, "list_item",
478
            $selector, "css_element"
479
        ]);
480
        $this->execute("behat_general::i_wait_to_be_redirected");
481
    }
482
}