MDL-37958 behat: Defintions for permissions management
[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
53 return array(
54 new Given('I am on homepage'),
55 new Given('I expand "Front page settings" node'),
56 new Given('I expand "Site administration" node'),
57 new Given('I expand "Users" node'),
58 new Given('I expand "Permissions" node'),
59 new Given('I follow "Define roles"'),
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 "Save changes"')
63 );
64 }
65
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
72 */
73 public function i_override_the_system_permissions_of_role_with($rolename, $table) {
74
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(text(),"' . $this->escape($rolename) . '")]');
77
78 return array(
79 new Given('I select "' . $this->escape($roleoption->getText()) . '" from "Advanced role override"'),
80 new Given('I fill the capabilities form with the following permissions:', $table),
81 new Given('I press "Save changes"')
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 {
96 $advancedtoggle = $this->find_button('Show advanced');
97 if ($advancedtoggle) {
98 $this->getSession()->getPage()->pressButton('Show advanced');
99 }
100 } catch (Exception $e) {
101 // We already are in advanced mode.
102 }
103
104 // Using getRows() as we are not sure if tests writers will add the header.
105 foreach ($table->getRows() as $key => $row) {
106
107 if (count($row) !== 2) {
108 throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession());
109 }
110
111 list($capability, $permission) = $row;
112
113 // Skip the headers row if it was provided
114 if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') {
115 continue;
116 }
117
118 // Checking the permission value.
119 $permissionconstant = 'CAP_'. strtoupper($permission);
120 if (!defined($permissionconstant)) {
121 throw new ExpectationException(
122 'The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited',
123 $this->getSession()
124 );
125 }
126
127 // Converting from permission to constant value.
128 $permissionvalue = constant($permissionconstant);
129
130 // Here we wait for the element to appear and exception if it does not exists.
131 $radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]');
132 $radio->click();
133 }
134 }
135
136}