Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  
  28  require_once (__DIR__ . '/../../behat/behat_base.php');
  29  
  30  use Behat\Mink\Exception\ExpectationException as ExpectationException,
  31      Behat\Gherkin\Node\TableNode as TableNode;
  32  
  33  /**
  34   * Steps definitions to set up permissions to capabilities.
  35   *
  36   * @package   core
  37   * @category  test
  38   * @copyright 2013 David MonllaĆ³
  39   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class behat_permissions extends behat_base {
  42  
  43      /**
  44       * Set system level permissions to the specified role. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
  45       * @Given /^I set the following system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role:$/
  46       * @param string $rolename
  47       * @param TableNode $table
  48       */
  49      public function i_set_the_following_system_permissions_of_role($rolename, $table) {
  50  
  51          $parentnodes = get_string('users', 'admin') . ' > ' .
  52              get_string('permissions', 'role');
  53  
  54          // Go to home page.
  55          $this->execute("behat_general::i_am_on_homepage");
  56  
  57          // Navigate to course management page via navigation block.
  58          $this->execute("behat_navigation::i_navigate_to_in_site_administration",
  59              array($parentnodes . ' > ' . get_string('defineroles', 'role'))
  60          );
  61  
  62          $this->execute("behat_general::click_link", "Edit " . $this->escape($rolename) . " role");
  63          $this->execute("behat_permissions::i_fill_the_capabilities_form_with_the_following_permissions", $table);
  64  
  65          $this->execute('behat_forms::press_button', get_string('savechanges'));
  66      }
  67  
  68      /**
  69       * 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.
  70       * @Given /^I override the system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
  71       * @param string $rolename
  72       * @param TableNode $table
  73       */
  74      public function i_override_the_system_permissions_of_role_with($rolename, $table) {
  75  
  76          // We don't know the number of overrides so we have to get it to match the option contents.
  77          $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]');
  78  
  79          $this->execute('behat_forms::i_set_the_field_to',
  80              array(get_string('advancedoverride', 'role'), $this->escape($roleoption->getText()))
  81          );
  82  
  83          if (!$this->running_javascript()) {
  84              $this->execute("behat_general::i_click_on_in_the", [get_string('go'), 'button', 'region-main', 'region']);
  85          }
  86  
  87          $this->execute("behat_permissions::i_fill_the_capabilities_form_with_the_following_permissions", $table);
  88  
  89          $this->execute('behat_forms::press_button', get_string('savechanges'));
  90      }
  91  
  92      /**
  93       * Fills the advanced permissions form with the provided data. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
  94       * @Given /^I fill the capabilities form with the following permissions:$/
  95       * @param TableNode $table
  96       * @return void
  97       */
  98      public function i_fill_the_capabilities_form_with_the_following_permissions($table) {
  99  
 100          // Ensure we are using the advanced view.
 101          // Wrapped in a try/catch to capture the exception and continue execution, we don't know if advanced mode was already enabled.
 102          try {
 103              $advancedtoggle = $this->find_button(get_string('showadvanced', 'form'));
 104              if ($advancedtoggle) {
 105                  $advancedtoggle->click();
 106  
 107                  // Wait for the page to load.
 108                  $this->getSession()->wait(self::get_timeout() * 1000, self::PAGE_READY_JS);
 109              }
 110          } catch (Exception $e) {
 111              // We already are in advanced mode.
 112          }
 113  
 114          // Using getRows() as we are not sure if tests writers will add the header.
 115          foreach ($table->getRows() as $key => $row) {
 116  
 117              if (count($row) !== 2) {
 118                  throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession());
 119              }
 120  
 121              list($capability, $permission) = $row;
 122  
 123              // Skip the headers row if it was provided
 124              if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') {
 125                  continue;
 126              }
 127  
 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()
 134                  );
 135              }
 136  
 137              // Converting from permission to constant value.
 138              $permissionvalue = constant($permissionconstant);
 139  
 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              $field = behat_field_manager::get_field_instance('radio', $radio, $this->getSession());
 143              $field->set_value(1);
 144          }
 145      }
 146  
 147      /**
 148       * Checks if the capability has the specified permission. Works in the role definition advanced page.
 149       *
 150       * @Then /^"(?P<capability_string>(?:[^"]|\\")*)" capability has "(?P<permission_string>Not set|Allow|Prevent|Prohibit)" permission$/
 151       * @throws ExpectationException
 152       * @param string $capabilityname
 153       * @param string $permission
 154       * @return void
 155       */
 156      public function capability_has_permission($capabilityname, $permission) {
 157  
 158          // We already know the name, so we just need the value.
 159          $radioxpath = "//table[contains(concat(' ',
 160   normalize-space(@class), ' '), ' rolecap ')]/descendant::input[@type='radio']" .
 161              "[@name='" . $capabilityname . "'][@checked]";
 162  
 163          $checkedradio = $this->find('xpath', $radioxpath);
 164  
 165          switch ($permission) {
 166              case get_string('notset', 'role'):
 167                  $perm = CAP_INHERIT;
 168                  break;
 169              case get_string('allow', 'role'):
 170                  $perm = CAP_ALLOW;
 171                  break;
 172              case get_string('prevent', 'role'):
 173                  $perm = CAP_PREVENT;
 174                  break;
 175              case get_string('prohibit', 'role'):
 176                  $perm = CAP_PROHIBIT;
 177                  break;
 178              default:
 179                  throw new ExpectationException('"' . $permission . '" permission does not exist', $this->getSession());
 180                  break;
 181          }
 182  
 183          if ($checkedradio->getAttribute('value') != $perm) {
 184              throw new ExpectationException('"' . $capabilityname . '" permission is not "' . $permission . '"', $this->getSession());
 185          }
 186      }
 187  
 188      /**
 189       * Set the allowed role assignments for the specified role.
 190       *
 191       * @Given /^I define the allowed role assignments for the "(?P<rolefullname_string>(?:[^"]|\\")*)" role as:$/
 192       * @param string $rolename
 193       * @param TableNode $table
 194       * @return void Executes other steps
 195       */
 196      public function i_define_the_allowed_role_assignments_for_a_role_as($rolename, $table) {
 197          $parentnodes = get_string('users', 'admin') . ' > ' .
 198              get_string('permissions', 'role');
 199  
 200          // Go to home page.
 201          $this->execute("behat_general::i_am_on_homepage");
 202  
 203          // Navigate to Define roles page via site administration menu.
 204          $this->execute("behat_navigation::i_navigate_to_in_site_administration",
 205                  $parentnodes .' > '. get_string('defineroles', 'role')
 206          );
 207  
 208          $this->execute("behat_general::click_link", "Allow role assignments");
 209          $this->execute("behat_permissions::i_fill_in_the_allowed_role_assignments_form_for_a_role_with",
 210              array($rolename, $table)
 211          );
 212  
 213          $this->execute('behat_forms::press_button', get_string('savechanges'));
 214      }
 215  
 216      /**
 217       * Fill in the allowed role assignments form for the specied role.
 218       *
 219       * Takes a table with two columns. Each row should contain the target
 220       * role, and either "Assignable" or "Not assignable".
 221       *
 222       * @Given /^I fill in the allowed role assignments form for the "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
 223       * @param String $sourcerole
 224       * @param TableNode $table
 225       * @return void
 226       */
 227      public function i_fill_in_the_allowed_role_assignments_form_for_a_role_with($sourcerole, $table) {
 228          foreach ($table->getRows() as $key => $row) {
 229              list($targetrole, $allowed) = $row;
 230  
 231              $node = $this->find('xpath', '//input[@title="Allow users with role ' .
 232                  $sourcerole .
 233                  ' to assign the role ' .
 234                  $targetrole . '"]');
 235  
 236              if ($allowed == 'Assignable') {
 237                  if (!$node->isChecked()) {
 238                      $node->check();
 239                  }
 240              } else if ($allowed == 'Not assignable') {
 241                  if ($node->isChecked()) {
 242                      $node->uncheck();
 243                  }
 244              } else {
 245                  throw new ExpectationException(
 246                      'The provided permission value "' . $allowed . '" is not valid. Use Assignable, or Not assignable',
 247                      $this->getSession()
 248                  );
 249              }
 250          }
 251      }
 252  
 253      /**
 254       * Mark context as frozen.
 255       *
 256       * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" is context frozen$/
 257       * @throws ExpectationException if the context cannot be frozen or found
 258       * @param string $element Element we look on
 259       * @param string $selector The type of where we look (activity, course)
 260       */
 261      public function the_context_is_context_frozen(string $element, string $selector) {
 262  
 263          // Enable context freeze if it is not done yet.
 264          set_config('contextlocking', 1);
 265  
 266          // Find context.
 267          $context = self::get_context($selector, $element);
 268  
 269          // Freeze context.
 270          $context->set_locked(true);
 271      }
 272  
 273      /**
 274       * Unmark context as frozen.
 275       *
 276       * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" is not context frozen$/
 277       * @throws ExpectationException if the context cannot be frozen or found
 278       * @param string $element Element we look on
 279       * @param string $selector The type of where we look (activity, course)
 280       */
 281      public function the_context_is_not_context_frozen(string $element, string $selector) {
 282  
 283          // Enable context freeze if it is not done yet.
 284          set_config('contextlocking', 1);
 285  
 286          // Find context.
 287          $context = self::get_context($selector, $element);
 288  
 289          // Freeze context.
 290          $context->set_locked(false);
 291      }
 292  }