MDL-43738 behat: Normalization and major refactoring of getters and setters
[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'),
dedb9738
DM
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') . '"'),
92c79fcc
DM
60 new Given('I follow "Edit ' . $this->escape($rolename) . ' role"'),
61 new Given('I fill the capabilities form with the following permissions:', $table),
dedb9738 62 new Given('I press "' . get_string('savechanges') . '"')
92c79fcc
DM
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.
38976081 76 $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]');
92c79fcc
DM
77
78 return array(
dedb9738 79 new Given('I select "' . $this->escape($roleoption->getText()) . '" from "' . get_string('advancedoverride', 'role') . '"'),
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
99 // As we are interacting with a moodle form we wait for the editor to be ready
100 // otherwise we may have problems when setting values on it or clicking on elements
101 // as the position of the elements will change once the editor is loaded.
102 $this->ensure_editors_are_loaded();
103
104 $advancedtoggle->click();
105
106 // Wait for the page to load.
107 $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
92c79fcc
DM
108 }
109 } catch (Exception $e) {
110 // We already are in advanced mode.
111 }
112
113 // Using getRows() as we are not sure if tests writers will add the header.
114 foreach ($table->getRows() as $key => $row) {
115
116 if (count($row) !== 2) {
117 throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession());
118 }
119
120 list($capability, $permission) = $row;
121
122 // Skip the headers row if it was provided
123 if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') {
124 continue;
125 }
126
127 // Checking the permission value.
128 $permissionconstant = 'CAP_'. strtoupper($permission);
129 if (!defined($permissionconstant)) {
130 throw new ExpectationException(
131 'The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited',
132 $this->getSession()
133 );
134 }
135
136 // Converting from permission to constant value.
137 $permissionvalue = constant($permissionconstant);
138
139 // Here we wait for the element to appear and exception if it does not exists.
140 $radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]');
141 $radio->click();
142 }
143 }
144
145}