MDL-43236 behat: Deprecate old steps
[moodle.git] / lib / tests / behat / behat_permissions.php
blob37928cfdf82f65518187257318da5e5b0a670d0d
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.
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/>.
17 /**
18 * Steps definitions related with permissions.
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
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../behat/behat_base.php');
30 use Behat\Mink\Exception\ExpectationException as ExpectationException,
31 Behat\Behat\Context\Step\Given as Given,
32 Behat\Gherkin\Node\TableNode as TableNode;
34 /**
35 * Steps definitions to set up permissions to capabilities.
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
42 class behat_permissions extends behat_base {
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
51 public function i_set_the_following_system_permissions_of_role($rolename, $table) {
53 return array(
54 new Given('I am on homepage'),
55 new Given('I collapse "' . get_string('frontpagesettings', 'admin') . '" node'),
56 new Given('I expand "' . get_string('administrationsite') . '" node'),
57 new Given('I expand "' . get_string('users', 'admin') . '" node'),
58 new Given('I expand "' . get_string('permissions', 'role') . '" node'),
59 new Given('I follow "' . get_string('defineroles', 'role') . '"'),
60 new Given('I follow "Edit ' . $this->escape($rolename) . ' role"'),
61 new Given('I fill the capabilities form with the following permissions:', $table),
62 new Given('I press "' . get_string('savechanges') . '"')
66 /**
67 * 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.
68 * @Given /^I override the system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
69 * @param string $rolename
70 * @param TableNode $table
71 * @return void Executes other steps
73 public function i_override_the_system_permissions_of_role_with($rolename, $table) {
75 // We don't know the number of overrides so we have to get it to match the option contents.
76 $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]');
78 return array(
79 new Given('I set the field "' . get_string('advancedoverride', 'role') .
80 '" to "' . $this->escape($roleoption->getText()) . '"'),
81 new Given('I fill the capabilities form with the following permissions:', $table),
82 new Given('I press "' . get_string('savechanges') . '"')
86 /**
87 * Fills the advanced permissions form with the provided data. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
88 * @Given /^I fill the capabilities form with the following permissions:$/
89 * @param TableNode $table
90 * @return void
92 public function i_fill_the_capabilities_form_with_the_following_permissions($table) {
94 // Ensure we are using the advanced view.
95 // Wrapped in a try/catch to capture the exception and continue execution, we don't know if advanced mode was already enabled.
96 try {
97 $advancedtoggle = $this->find_button(get_string('showadvanced', 'form'));
98 if ($advancedtoggle) {
100 // As we are interacting with a moodle form we wait for the editor to be ready
101 // otherwise we may have problems when setting values on it or clicking on elements
102 // as the position of the elements will change once the editor is loaded.
103 $this->ensure_editors_are_loaded();
105 $advancedtoggle->click();
107 // Wait for the page to load.
108 $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
110 } catch (Exception $e) {
111 // We already are in advanced mode.
114 // Using getRows() as we are not sure if tests writers will add the header.
115 foreach ($table->getRows() as $key => $row) {
117 if (count($row) !== 2) {
118 throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession());
121 list($capability, $permission) = $row;
123 // Skip the headers row if it was provided
124 if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') {
125 continue;
128 // Checking the permission value.
129 $permissionconstant = 'CAP_'. strtoupper($permission);
130 if (!defined($permissionconstant)) {
131 throw new ExpectationException(
132 'The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited',
133 $this->getSession()
137 // Converting from permission to constant value.
138 $permissionvalue = constant($permissionconstant);
140 // Here we wait for the element to appear and exception if it does not exist.
141 $radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]');
142 $radio->click();
147 * Checks if the capability has the specified permission. Works in the role definition advanced page.
149 * @Then /^"(?P<capability_string>(?:[^"]|\\")*)" capability has "(?P<permission_string>Not set|Allow|Prevent|Prohibit)" permission$/
150 * @throws ExpectationException
151 * @param string $capabilityname
152 * @param string $permission
153 * @return void
155 public function capability_has_permission($capabilityname, $permission) {
157 // We already know the name, so we just need the value.
158 $radioxpath = "//table[@class='rolecap']/descendant::input[@type='radio']" .
159 "[@name='" . $capabilityname . "'][@checked]";
161 $checkedradio = $this->find('xpath', $radioxpath);
163 switch ($permission) {
164 case get_string('notset', 'role'):
165 $perm = CAP_INHERIT;
166 break;
167 case get_string('allow', 'role'):
168 $perm = CAP_ALLOW;
169 break;
170 case get_string('prevent', 'role'):
171 $perm = CAP_PREVENT;
172 break;
173 case get_string('prohibit', 'role'):
174 $perm = CAP_PROHIBIT;
175 break;
176 default:
177 throw new ExpectationException('"' . $permission . '" permission does not exist', $this->getSession());
178 break;
181 if ($checkedradio->getAttribute('value') != $perm) {
182 throw new ExpectationException('"' . $capabilityname . '" permission is not "' . $permission . '"', $this->getSession());