MDL-54672 behat: Fix i_enrol_user_as() step in non-js sessions
[moodle.git] / enrol / tests / behat / behat_enrol.php
CommitLineData
f0e4cf5e
DM
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 * Enrolment steps definitions.
19 *
20 * @package core_enrol
21 * @category test
22 * @copyright 2013 David Monllaó
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
eb9ca848 30use Behat\Gherkin\Node\TableNode as TableNode;
f0e4cf5e
DM
31
32/**
33 * Steps definitions for general enrolment actions.
34 *
35 * @package core_enrol
36 * @category test
37 * @copyright 2013 David Monllaó
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class behat_enrol extends behat_base {
41
42 /**
43 * Adds the specified enrolment method to the current course filling the form with the provided data.
44 *
45 * @Given /^I add "(?P<enrolment_method_name_string>(?:[^"]|\\")*)" enrolment method with:$/
46 * @param string $enrolmethod
47 * @param TableNode $table
48 */
49 public function i_add_enrolment_method_with($enrolmethod, TableNode $table) {
eb9ca848
RT
50 // Navigate to enrolment method page.
51 $parentnodes = get_string('courseadministration') . ' > ' . get_string('users', 'admin');
52 $this->execute("behat_navigation::i_navigate_to_node_in",
53 array(get_string('type_enrol_plural', 'plugin'), $parentnodes)
f0e4cf5e 54 );
eb9ca848
RT
55
56 // Select enrolment method.
57 $this->execute('behat_forms::i_select_from_the_singleselect',
58 array($this->escape($enrolmethod), get_string('addinstance', 'enrol'))
59 );
60
61 // Set form fields.
62 $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table);
63
64 // Save changes.
65 $this->execute("behat_forms::press_button", get_string('addinstance', 'enrol'));
66
f0e4cf5e
DM
67 }
68
d46979ec
DM
69 /**
70 * Enrols the specified user in the current course without options.
71 *
72 * This is a simple step, to set enrolment options would be better to
73 * create a separate step as a TableNode will be required.
74 *
75 * @Given /^I enrol "(?P<user_fullname_string>(?:[^"]|\\")*)" user as "(?P<rolename_string>(?:[^"]|\\")*)"$/
76 * @param string $userfullname
77 * @param string $rolename
d46979ec
DM
78 */
79 public function i_enrol_user_as($userfullname, $rolename) {
80
eb9ca848
RT
81 // Navigate to enrolment page.
82 $parentnodes = get_string('courseadministration') . ' > ' . get_string('users', 'admin');
83 $this->execute("behat_navigation::i_navigate_to_node_in",
84 array(get_string('enrolledusers', 'enrol'), $parentnodes)
d46979ec
DM
85 );
86
eb9ca848
RT
87 $this->execute("behat_forms::press_button", get_string('enrolusers', 'enrol'));
88
d46979ec 89 if ($this->running_javascript()) {
ac5dad46 90 $this->execute('behat_forms::i_set_the_field_to', array(get_string('assignroles', 'role'), $rolename));
d46979ec
DM
91
92 // We have a div here, not a tr.
921faad9 93 $userliteral = behat_context_helper::escape($userfullname);
d46979ec
DM
94 $userrowxpath = "//div[contains(concat(' ',normalize-space(@class),' '),' user ')][contains(., $userliteral)]";
95
eb9ca848
RT
96 $this->execute('behat_general::i_click_on_in_the',
97 array(get_string('enrol', 'enrol'), "button", $userrowxpath, "xpath_element")
98 );
99 $this->execute("behat_forms::press_button", get_string('finishenrollingusers', 'enrol'));
d46979ec
DM
100
101 } else {
ac5dad46 102 $this->execute('behat_forms::i_set_the_field_to', array(get_string('assignrole', 'role'), $rolename));
eb9ca848
RT
103 $this->execute('behat_forms::i_set_the_field_to', array("addselect", $userfullname));
104 $this->execute("behat_forms::press_button", "add");
d46979ec 105 }
d46979ec
DM
106 }
107
f0e4cf5e 108}