behat_deprecated.php 7.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Steps definitions that will be deprecated in the next releases.
 *
 * @package    core
 * @category   test
 * @copyright  2013 David Monllaó
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.

require_once(__DIR__ . '/../../../lib/behat/behat_base.php');

30
use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
31
    Behat\Gherkin\Node\TableNode as TableNode,
Andrew Nicols's avatar
Andrew Nicols committed
32
    Behat\Gherkin\Node\PyStringNode as PyStringNode;
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * Deprecated behat step definitions.
 *
 * @package    core
 * @category   test
 * @copyright  2013 David Monllaó
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class behat_deprecated extends behat_base {

    /**
     * Throws an exception if $CFG->behat_usedeprecated is not allowed.
     *
     * @throws Exception
     * @param string|array $alternatives Alternative/s to the requested step
49
     * @param bool $throwexception If set to true we always throw exception, irrespective of behat_usedeprecated setting.
50
51
     * @return void
     */
52
    protected function deprecated_message($alternatives, $throwexception = false) {
53
54
55
        global $CFG;

        // We do nothing if it is enabled.
56
        if (!empty($CFG->behat_usedeprecated) && !$throwexception) {
57
58
59
60
61
62
63
            return;
        }

        if (is_scalar($alternatives)) {
            $alternatives = array($alternatives);
        }

64
65
66
67
68
69
70
71
        // Show an appropriate message based on the throwexception flag.
        if ($throwexception) {
            $message = 'This step has been removed. Rather than using this step you can:';
        } else {
            $message = 'Deprecated step, rather than using this step you can:';
        }

        // Add all alternatives to the message.
72
73
74
        foreach ($alternatives as $alternative) {
            $message .= PHP_EOL . '- ' . $alternative;
        }
75
76
77
78
79
80

        if (!$throwexception) {
            $message .= PHP_EOL . '- Set $CFG->behat_usedeprecated in config.php to allow the use of deprecated steps
                    if you don\'t have any other option';
        }

81
82
83
        throw new Exception($message);
    }

84
85
86
87
88
89
90
91
92
93
94
95
96
97
    /**
     * Clicks link with specified id|title|alt|text in the flat navigation drawer.
     *
     * @When /^I select "(?P<link_string>(?:[^"]|\\")*)" from flat navigation drawer$/
     * @throws ElementNotFoundException Thrown by behat_base::find
     * @param string $link
     * @deprecated Since Moodle 4.0
     */
    public function i_select_from_flat_navigation_drawer(string $link) {
        self::deprecated_message(['i_select_from_primary_navigation', 'i_select_from_secondary_navigation'], true);

        $this->i_open_flat_navigation_drawer();
        $this->execute('behat_general::i_click_on_in_the', [$link, 'link', '#nav-drawer', 'css_element']);
    }
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    /**
     * Hover over a specific day in the calendar.
     *
     * @Given /^I hover over day "(?P<dayofmonth>\d+)" of this month in the calendar$/
     * @param int $day The day of the current month
     *
     * @deprecated since 4.0 MDL-72810. This tested the three-month calendar pseudo block, which has been removed.
     * @todo MDL-73117 This will be deleted in Moodle 4.4.
     */
    public function i_hover_over_day_of_this_month_in_calendar($day) {
        $this->deprecated_message('Check information in the course or timeline calendar blocks or full calendar, as appropriate.');

        $summarytitle = userdate(time(), get_string('strftimemonthyear'));
        // The current month table.
        $currentmonth = "table[descendant::*[self::caption[contains(concat(' ', normalize-space(.), ' '), ' {$summarytitle} ')]]]";

        // Strings for the class cell match.
        $cellclasses  = "contains(concat(' ', normalize-space(@class), ' '), ' day ')";
        $daycontains  = "text()[contains(concat(' ', normalize-space(.), ' '), ' {$day} ')]";
        $daycell      = "td[{$cellclasses}]";
        $dayofmonth   = "a[{$daycontains}]";

        $xpath = '//' . $currentmonth . '/descendant::' . $daycell . '/' . $dayofmonth;
        $this->execute("behat_general::i_hover", [$xpath, "xpath_element"]);
    }

    /**
     * Click a specific day in the calendar.
     *
     * @Given /^I click day "(?P<dayofmonth>\d+)" of this month in the calendar$/
     * @param int $day The day of the current month
     *
     * @deprecated since 4.0 MDL-72810. This tested the three-month calendar pseudo block, which has been removed.
     * @todo MDL-73117 This will be deleted in Moodle 4.4.
     */
    public function i_click_day_of_this_month_in_calendar($day) {
        $this->deprecated_message('Check information in the course or timeline calendar blocks or full calendar, as appropriate.');

        // The current month table.
        $currentmonth = "table[descendant::*[self::caption[contains(concat(' ', normalize-space(.), ' '), ' {$summarytitle} ')]]]";

        // Strings for the class cell match.
        $cellclasses  = "contains(concat(' ', normalize-space(@class), ' '), ' day ')";
        $daycontains  = "text()[contains(concat(' ', normalize-space(.), ' '), ' {$day} ')]";
        $daycell      = "td[{$cellclasses}]";
        $dayofmonth   = "a[{$daycontains}]";

        $xpath = '//' . $currentmonth . '/descendant::' . $daycell . '/' . $dayofmonth;
        $this->execute("behat_general::wait_until_the_page_is_ready");
        $this->execute("behat_general::i_click_on", array($xpath, "xpath_element"));
        $this->execute("behat_general::wait_until_the_page_is_ready");
    }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

    /**
     * Adds the specified enrolment method to the current course filling the form with the provided data.
     *
     * @Given /^I add "(?P<enrolment_method_name_string>(?:[^"]|\\")*)" enrolment method with:$/
     * @param string $enrolmethod
     * @param TableNode $table
     *
     * @deprecated since 4.0 MDL-72090. We now need the course to enrol in. Please use i_add_enrolment_method_for_with()
     * @todo MDL-71733 This will be deleted in Moodle 4.4.
     */
    public function i_add_enrolment_method_with($enrolmethod, TableNode $table) {
        $this->deprecated_message(['i_add_enrolment_method_for_with']);

        // Navigate to enrolment method page.
        $parentnodes = get_string('users', 'admin');
        $this->execute("behat_navigation::i_navigate_to_in_current_page_administration",
            array($parentnodes .' > '. get_string('type_enrol_plural', 'plugin'))
        );

        // Select enrolment method.
        $this->execute('behat_forms::i_select_from_the_singleselect',
            array($this->escape($enrolmethod), get_string('addinstance', 'enrol'))
        );

        // Wait again, for page to reloaded.
        $this->execute('behat_general::i_wait_to_be_redirected');

        // Set form fields.
        $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table);

        // Ensure we get button in focus, before pressing button.
        if ($this->running_javascript()) {
            $this->execute('behat_general::i_press_named_key', ['', 'tab']);
        }

        // Save changes.
        $this->execute("behat_forms::press_button", get_string('addinstance', 'enrol'));
    }
190
}