weekly release 3.0dev
[moodle.git] / lib / tests / behat / behat_permissions.php
CommitLineData
92c79fcc
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 * Steps definitions related with permissions.
19 *
20 * @package core
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__ . '/../../behat/behat_base.php');
29
30use Behat\Mink\Exception\ExpectationException as ExpectationException,
31 Behat\Behat\Context\Step\Given as Given,
32 Behat\Gherkin\Node\TableNode as TableNode;
33
34/**
35 * Steps definitions to set up permissions to capabilities.
36 *
37 * @package core
38 * @category test
39 * @copyright 2013 David MonllaĆ³
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class behat_permissions extends behat_base {
43
44 /**
45 * Set system level permissions to the specified role. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
46 * @Given /^I set the following system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role:$/
47 * @param string $rolename
48 * @param TableNode $table
49 * @return void Executes other steps
50 */
51 public function i_set_the_following_system_permissions_of_role($rolename, $table) {
52
1f631c7c
DM
53 $parentnodes = get_string('administrationsite') . ' > ' .
54 get_string('users', 'admin') . ' > ' .
a542553d 55 get_string('permissions', 'role');
92c79fcc
DM
56 return array(
57 new Given('I am on homepage'),
a542553d 58 new Given('I navigate to "' . get_string('defineroles', 'role') . '" node in "' . $parentnodes . '"'),
92c79fcc
DM
59 new Given('I follow "Edit ' . $this->escape($rolename) . ' role"'),
60 new Given('I fill the capabilities form with the following permissions:', $table),
dedb9738 61 new Given('I press "' . get_string('savechanges') . '"')
92c79fcc
DM
62 );
63 }
64
65 /**
66 * 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.
67 * @Given /^I override the system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
68 * @param string $rolename
69 * @param TableNode $table
70 * @return void Executes other steps
71 */
72 public function i_override_the_system_permissions_of_role_with($rolename, $table) {
73
74 // We don't know the number of overrides so we have to get it to match the option contents.
38976081 75 $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]');
92c79fcc
DM
76
77 return array(
a5254141
DM
78 new Given('I set the field "' . get_string('advancedoverride', 'role') .
79 '" to "' . $this->escape($roleoption->getText()) . '"'),
92c79fcc 80 new Given('I fill the capabilities form with the following permissions:', $table),
dedb9738 81 new Given('I press "' . get_string('savechanges') . '"')
92c79fcc
DM
82 );
83 }
84
85 /**
86 * Fills the advanced permissions form with the provided data. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
87 * @Given /^I fill the capabilities form with the following permissions:$/
88 * @param TableNode $table
89 * @return void
90 */
91 public function i_fill_the_capabilities_form_with_the_following_permissions($table) {
92
93 // Ensure we are using the advanced view.
94 // Wrapped in a try/catch to capture the exception and continue execution, we don't know if advanced mode was already enabled.
95 try {
dedb9738 96 $advancedtoggle = $this->find_button(get_string('showadvanced', 'form'));
92c79fcc 97 if ($advancedtoggle) {
d1e55a47
DM
98 $advancedtoggle->click();
99
100 // Wait for the page to load.
101 $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
92c79fcc
DM
102 }
103 } catch (Exception $e) {
104 // We already are in advanced mode.
105 }
106
107 // Using getRows() as we are not sure if tests writers will add the header.
108 foreach ($table->getRows() as $key => $row) {
109
110 if (count($row) !== 2) {
111 throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession());
112 }
113
114 list($capability, $permission) = $row;
115
116 // Skip the headers row if it was provided
117 if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') {
118 continue;
119 }
120
121 // Checking the permission value.
122 $permissionconstant = 'CAP_'. strtoupper($permission);
123 if (!defined($permissionconstant)) {
124 throw new ExpectationException(
125 'The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited',
126 $this->getSession()
127 );
128 }
129
130 // Converting from permission to constant value.
131 $permissionvalue = constant($permissionconstant);
132
c51c3b55 133 // Here we wait for the element to appear and exception if it does not exist.
92c79fcc
DM
134 $radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]');
135 $radio->click();
136 }
137 }
138
a5254141
DM
139 /**
140 * Checks if the capability has the specified permission. Works in the role definition advanced page.
141 *
142 * @Then /^"(?P<capability_string>(?:[^"]|\\")*)" capability has "(?P<permission_string>Not set|Allow|Prevent|Prohibit)" permission$/
143 * @throws ExpectationException
144 * @param string $capabilityname
145 * @param string $permission
146 * @return void
147 */
148 public function capability_has_permission($capabilityname, $permission) {
149
150 // We already know the name, so we just need the value.
151 $radioxpath = "//table[@class='rolecap']/descendant::input[@type='radio']" .
152 "[@name='" . $capabilityname . "'][@checked]";
153
154 $checkedradio = $this->find('xpath', $radioxpath);
155
156 switch ($permission) {
157 case get_string('notset', 'role'):
158 $perm = CAP_INHERIT;
159 break;
160 case get_string('allow', 'role'):
161 $perm = CAP_ALLOW;
162 break;
163 case get_string('prevent', 'role'):
164 $perm = CAP_PREVENT;
165 break;
166 case get_string('prohibit', 'role'):
167 $perm = CAP_PROHIBIT;
168 break;
169 default:
170 throw new ExpectationException('"' . $permission . '" permission does not exist', $this->getSession());
171 break;
172 }
173
174 if ($checkedradio->getAttribute('value') != $perm) {
175 throw new ExpectationException('"' . $capabilityname . '" permission is not "' . $permission . '"', $this->getSession());
176 }
177 }
178
494cf9b8
AN
179 /**
180 * Set the allowed role assignments for the specified role.
181 *
182 * @Given /^I define the allowed role assignments for the "(?P<rolefullname_string>(?:[^"]|\\")*)" role as:$/
183 * @param string $rolename
184 * @param TableNode $table
185 * @return void Executes other steps
186 */
187 public function i_define_the_allowed_role_assignments_for_a_role_as($rolename, $table) {
188 $parentnodes = get_string('administrationsite') . ' > ' .
189 get_string('users', 'admin') . ' > ' .
190 get_string('permissions', 'role');
191 return array(
192 new Given('I am on homepage'),
193 new Given('I navigate to "' . get_string('defineroles', 'role') . '" node in "' . $parentnodes . '"'),
194 new Given('I follow "Allow role assignments"'),
195 new Given('I fill in the allowed role assignments form for the "' . $rolename . '" role with:', $table),
196 new Given('I press "' . get_string('savechanges') . '"')
197 );
198 }
199
200 /**
201 * Fill in the allowed role assignments form for the specied role.
202 *
203 * Takes a table with two columns. Each row should contain the target
204 * role, and either "Assignable" or "Not assignable".
205 *
206 * @Given /^I fill in the allowed role assignments form for the "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
207 * @param String $sourcerole
208 * @param TableNode $table
209 * @return void
210 */
211 public function i_fill_in_the_allowed_role_assignments_form_for_a_role_with($sourcerole, $table) {
212 foreach ($table->getRows() as $key => $row) {
213 list($targetrole, $allowed) = $row;
214
215 $node = $this->find('xpath', '//input[@title="Allow users with role ' .
216 $sourcerole .
217 ' to assign the role ' .
218 $targetrole . '"]');
219
220 if ($allowed == 'Assignable') {
221 if (!$node->isChecked()) {
222 $node->click();
223 }
224 } else if ($allowed == 'Not assignable') {
225 if ($node->isChecked()) {
226 $node->click();
227 }
228 } else {
229 throw new ExpectationException(
230 'The provided permission value "' . $allowed . '" is not valid. Use Assignable, or Not assignable',
231 $this->getSession()
232 );
233 }
234 }
235 }
92c79fcc 236}