forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
behat_permissions.php
252 lines (212 loc) · 10.3 KB
/
behat_permissions.php
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?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 related with permissions.
*
* @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__ . '/../../behat/behat_base.php');
use Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Gherkin\Node\TableNode as TableNode;
/**
* Steps definitions to set up permissions to capabilities.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_permissions extends behat_base {
/**
* Set system level permissions to the specified role. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
* @Given /^I set the following system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role:$/
* @param string $rolename
* @param TableNode $table
*/
public function i_set_the_following_system_permissions_of_role($rolename, $table) {
$parentnodes = get_string('users', 'admin') . ' > ' .
get_string('permissions', 'role');
// Go to home page.
$this->execute("behat_general::i_am_on_homepage");
// Navigate to course management page via navigation block.
$this->execute("behat_navigation::i_navigate_to_in_site_administration",
array($parentnodes . ' > ' . get_string('defineroles', 'role'))
);
$this->execute("behat_general::click_link", "Edit " . $this->escape($rolename) . " role");
$this->execute("behat_permissions::i_fill_the_capabilities_form_with_the_following_permissions", $table);
$this->execute('behat_forms::press_button', get_string('savechanges'));
}
/**
* Overrides system capabilities at category, course and module levels. This step begins after clicking 'Permissions' link. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
* @Given /^I override the system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
* @param string $rolename
* @param TableNode $table
*/
public function i_override_the_system_permissions_of_role_with($rolename, $table) {
// We don't know the number of overrides so we have to get it to match the option contents.
$roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]');
$this->execute('behat_forms::i_set_the_field_to',
array(get_string('advancedoverride', 'role'), $this->escape($roleoption->getText()))
);
if (!$this->running_javascript()) {
$this->execute("behat_general::i_click_on_in_the", [get_string('go'), 'button', 'region-main', 'region']);
}
$this->execute("behat_permissions::i_fill_the_capabilities_form_with_the_following_permissions", $table);
$this->execute('behat_forms::press_button', get_string('savechanges'));
}
/**
* Fills the advanced permissions form with the provided data. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
* @Given /^I fill the capabilities form with the following permissions:$/
* @param TableNode $table
* @return void
*/
public function i_fill_the_capabilities_form_with_the_following_permissions($table) {
// Ensure we are using the advanced view.
// Wrapped in a try/catch to capture the exception and continue execution, we don't know if advanced mode was already enabled.
try {
$advancedtoggle = $this->find_button(get_string('showadvanced', 'form'));
if ($advancedtoggle) {
$advancedtoggle->click();
// Wait for the page to load.
$this->getSession()->wait(self::get_timeout() * 1000, self::PAGE_READY_JS);
}
} catch (Exception $e) {
// We already are in advanced mode.
}
// Using getRows() as we are not sure if tests writers will add the header.
foreach ($table->getRows() as $key => $row) {
if (count($row) !== 2) {
throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession());
}
list($capability, $permission) = $row;
// Skip the headers row if it was provided
if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') {
continue;
}
// Checking the permission value.
$permissionconstant = 'CAP_'. strtoupper($permission);
if (!defined($permissionconstant)) {
throw new ExpectationException(
'The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited',
$this->getSession()
);
}
// Converting from permission to constant value.
$permissionvalue = constant($permissionconstant);
// Here we wait for the element to appear and exception if it does not exist.
$radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]');
$field = behat_field_manager::get_field_instance('radio', $radio, $this->getSession());
$field->set_value(1);
}
}
/**
* Checks if the capability has the specified permission. Works in the role definition advanced page.
*
* @Then /^"(?P<capability_string>(?:[^"]|\\")*)" capability has "(?P<permission_string>Not set|Allow|Prevent|Prohibit)" permission$/
* @throws ExpectationException
* @param string $capabilityname
* @param string $permission
* @return void
*/
public function capability_has_permission($capabilityname, $permission) {
// We already know the name, so we just need the value.
$radioxpath = "//table[contains(concat(' ',
normalize-space(@class), ' '), ' rolecap ')]/descendant::input[@type='radio']" .
"[@name='" . $capabilityname . "'][@checked]";
$checkedradio = $this->find('xpath', $radioxpath);
switch ($permission) {
case get_string('notset', 'role'):
$perm = CAP_INHERIT;
break;
case get_string('allow', 'role'):
$perm = CAP_ALLOW;
break;
case get_string('prevent', 'role'):
$perm = CAP_PREVENT;
break;
case get_string('prohibit', 'role'):
$perm = CAP_PROHIBIT;
break;
default:
throw new ExpectationException('"' . $permission . '" permission does not exist', $this->getSession());
break;
}
if ($checkedradio->getAttribute('value') != $perm) {
throw new ExpectationException('"' . $capabilityname . '" permission is not "' . $permission . '"', $this->getSession());
}
}
/**
* Set the allowed role assignments for the specified role.
*
* @Given /^I define the allowed role assignments for the "(?P<rolefullname_string>(?:[^"]|\\")*)" role as:$/
* @param string $rolename
* @param TableNode $table
* @return void Executes other steps
*/
public function i_define_the_allowed_role_assignments_for_a_role_as($rolename, $table) {
$parentnodes = get_string('users', 'admin') . ' > ' .
get_string('permissions', 'role');
// Go to home page.
$this->execute("behat_general::i_am_on_homepage");
// Navigate to Define roles page via site administration menu.
$this->execute("behat_navigation::i_navigate_to_in_site_administration",
$parentnodes .' > '. get_string('defineroles', 'role')
);
$this->execute("behat_general::click_link", "Allow role assignments");
$this->execute("behat_permissions::i_fill_in_the_allowed_role_assignments_form_for_a_role_with",
array($rolename, $table)
);
$this->execute('behat_forms::press_button', get_string('savechanges'));
}
/**
* Fill in the allowed role assignments form for the specied role.
*
* Takes a table with two columns. Each row should contain the target
* role, and either "Assignable" or "Not assignable".
*
* @Given /^I fill in the allowed role assignments form for the "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
* @param String $sourcerole
* @param TableNode $table
* @return void
*/
public function i_fill_in_the_allowed_role_assignments_form_for_a_role_with($sourcerole, $table) {
foreach ($table->getRows() as $key => $row) {
list($targetrole, $allowed) = $row;
$node = $this->find('xpath', '//input[@title="Allow users with role ' .
$sourcerole .
' to assign the role ' .
$targetrole . '"]');
if ($allowed == 'Assignable') {
if (!$node->isChecked()) {
$node->check();
}
} else if ($allowed == 'Not assignable') {
if ($node->isChecked()) {
$node->uncheck();
}
} else {
throw new ExpectationException(
'The provided permission value "' . $allowed . '" is not valid. Use Assignable, or Not assignable',
$this->getSession()
);
}
}
}
}