[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/tests/behat/ -> behat_permissions.php (source)

   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('administrationsite') . ' > ' .
  52              get_string('users', 'admin') . ' > ' .
  53              get_string('permissions', 'role');
  54  
  55          // Go to home page.
  56          $this->execute("behat_general::i_am_on_homepage");
  57  
  58          // Navigate to course management page via navigation block.
  59          $this->execute("behat_navigation::i_navigate_to_node_in",
  60              array(get_string('defineroles', 'role'), $parentnodes)
  61          );
  62  
  63          $this->execute("behat_general::click_link", "Edit " . $this->escape($rolename) . " role");
  64          $this->execute("behat_permissions::i_fill_the_capabilities_form_with_the_following_permissions", $table);
  65  
  66          $this->execute('behat_forms::press_button', get_string('savechanges'));
  67      }
  68  
  69      /**
  70       * 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.
  71       * @Given /^I override the system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
  72       * @param string $rolename
  73       * @param TableNode $table
  74       */
  75      public function i_override_the_system_permissions_of_role_with($rolename, $table) {
  76  
  77          // We don't know the number of overrides so we have to get it to match the option contents.
  78          $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]');
  79  
  80          $this->execute('behat_forms::i_set_the_field_to',
  81              array(get_string('advancedoverride', 'role'), $this->escape($roleoption->getText()))
  82          );
  83  
  84          if (!$this->running_javascript()) {
  85              $this->execute("behat_forms::press_button", get_string('go'));
  86          }
  87  
  88          $this->execute("behat_permissions::i_fill_the_capabilities_form_with_the_following_permissions", $table);
  89  
  90          $this->execute('behat_forms::press_button', get_string('savechanges'));
  91      }
  92  
  93      /**
  94       * Fills the advanced permissions form with the provided data. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns.
  95       * @Given /^I fill the capabilities form with the following permissions:$/
  96       * @param TableNode $table
  97       * @return void
  98       */
  99      public function i_fill_the_capabilities_form_with_the_following_permissions($table) {
 100  
 101          // Ensure we are using the advanced view.
 102          // Wrapped in a try/catch to capture the exception and continue execution, we don't know if advanced mode was already enabled.
 103          try {
 104              $advancedtoggle = $this->find_button(get_string('showadvanced', 'form'));
 105              if ($advancedtoggle) {
 106                  $advancedtoggle->click();
 107  
 108                  // Wait for the page to load.
 109                  $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
 110              }
 111          } catch (Exception $e) {
 112              // We already are in advanced mode.
 113          }
 114  
 115          // Using getRows() as we are not sure if tests writers will add the header.
 116          foreach ($table->getRows() as $key => $row) {
 117  
 118              if (count($row) !== 2) {
 119                  throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession());
 120              }
 121  
 122              list($capability, $permission) = $row;
 123  
 124              // Skip the headers row if it was provided
 125              if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') {
 126                  continue;
 127              }
 128  
 129              // Checking the permission value.
 130              $permissionconstant = 'CAP_'. strtoupper($permission);
 131              if (!defined($permissionconstant)) {
 132                  throw new ExpectationException(
 133                      'The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited',
 134                      $this->getSession()
 135                  );
 136              }
 137  
 138              // Converting from permission to constant value.
 139              $permissionvalue = constant($permissionconstant);
 140  
 141              // Here we wait for the element to appear and exception if it does not exist.
 142              $radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]');
 143              $field = behat_field_manager::get_field_instance('radio', $radio, $this->getSession());
 144              $field->set_value(1);
 145          }
 146      }
 147  
 148      /**
 149       * Checks if the capability has the specified permission. Works in the role definition advanced page.
 150       *
 151       * @Then /^"(?P<capability_string>(?:[^"]|\\")*)" capability has "(?P<permission_string>Not set|Allow|Prevent|Prohibit)" permission$/
 152       * @throws ExpectationException
 153       * @param string $capabilityname
 154       * @param string $permission
 155       * @return void
 156       */
 157      public function capability_has_permission($capabilityname, $permission) {
 158  
 159          // We already know the name, so we just need the value.
 160          $radioxpath = "//table[@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('administrationsite') . ' > ' .
 198              get_string('users', 'admin') . ' > ' .
 199              get_string('permissions', 'role');
 200  
 201          // Go to home page.
 202          $this->execute("behat_general::i_am_on_homepage");
 203  
 204          // Navigate to course management page via navigation block.
 205          $this->execute("behat_navigation::i_navigate_to_node_in",
 206              array(get_string('defineroles', 'role'), $parentnodes)
 207          );
 208  
 209          $this->execute("behat_general::click_link", "Allow role assignments");
 210          $this->execute("behat_permissions::i_fill_in_the_allowed_role_assignments_form_for_a_role_with",
 211              array($rolename, $table)
 212          );
 213  
 214          $this->execute('behat_forms::press_button', get_string('savechanges'));
 215      }
 216  
 217      /**
 218       * Fill in the allowed role assignments form for the specied role.
 219       *
 220       * Takes a table with two columns. Each row should contain the target
 221       * role, and either "Assignable" or "Not assignable".
 222       *
 223       * @Given /^I fill in the allowed role assignments form for the "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
 224       * @param String $sourcerole
 225       * @param TableNode $table
 226       * @return void
 227       */
 228      public function i_fill_in_the_allowed_role_assignments_form_for_a_role_with($sourcerole, $table) {
 229          foreach ($table->getRows() as $key => $row) {
 230              list($targetrole, $allowed) = $row;
 231  
 232              $node = $this->find('xpath', '//input[@title="Allow users with role ' .
 233                  $sourcerole .
 234                  ' to assign the role ' .
 235                  $targetrole . '"]');
 236  
 237              if ($allowed == 'Assignable') {
 238                  if (!$node->isChecked()) {
 239                      $node->click();
 240                  }
 241              } else if ($allowed == 'Not assignable') {
 242                  if ($node->isChecked()) {
 243                      $node->click();
 244                  }
 245              } else {
 246                  throw new ExpectationException(
 247                      'The provided permission value "' . $allowed . '" is not valid. Use Assignable, or Not assignable',
 248                      $this->getSession()
 249                  );
 250              }
 251          }
 252      }
 253  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1