[ Index ]

PHP Cross Reference of moodle-2.8

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\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   */
  42  class 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          $parentnodes = get_string('administrationsite') . ' > ' .
  54              get_string('users', 'admin') . ' > ' .
  55              get_string('permissions', 'role');
  56          return array(
  57              new Given('I am on homepage'),
  58              new Given('I navigate to "' . get_string('defineroles', 'role') . '" node in "' . $parentnodes . '"'),
  59              new Given('I follow "Edit ' . $this->escape($rolename) . ' role"'),
  60              new Given('I fill the capabilities form with the following permissions:', $table),
  61              new Given('I press "' . get_string('savechanges') . '"')
  62          );
  63      }
  64  
  65      /**
  66       * 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.
  67       * @Given /^I override the system permissions of "(?P<rolefullname_string>(?:[^"]|\\")*)" role with:$/
  68       * @param string $rolename
  69       * @param TableNode $table
  70       * @return void Executes other steps
  71       */
  72      public function i_override_the_system_permissions_of_role_with($rolename, $table) {
  73  
  74          // We don't know the number of overrides so we have to get it to match the option contents.
  75          $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]');
  76  
  77          return array(
  78              new Given('I set the field "' . get_string('advancedoverride', 'role') .
  79                  '" to "' . $this->escape($roleoption->getText()) . '"'),
  80              new Given('I fill the capabilities form with the following permissions:', $table),
  81              new Given('I press "' . get_string('savechanges') . '"')
  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(get_string('showadvanced', 'form'));
  97              if ($advancedtoggle) {
  98                  $advancedtoggle->click();
  99  
 100                  // Wait for the page to load.
 101                  $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
 102              }
 103          } catch (Exception $e) {
 104              // We already are in advanced mode.
 105          }
 106  
 107          // Using getRows() as we are not sure if tests writers will add the header.
 108          foreach ($table->getRows() as $key => $row) {
 109  
 110              if (count($row) !== 2) {
 111                  throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession());
 112              }
 113  
 114              list($capability, $permission) = $row;
 115  
 116              // Skip the headers row if it was provided
 117              if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') {
 118                  continue;
 119              }
 120  
 121              // Checking the permission value.
 122              $permissionconstant = 'CAP_'. strtoupper($permission);
 123              if (!defined($permissionconstant)) {
 124                  throw new ExpectationException(
 125                      'The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited',
 126                      $this->getSession()
 127                  );
 128              }
 129  
 130              // Converting from permission to constant value.
 131              $permissionvalue = constant($permissionconstant);
 132  
 133              // Here we wait for the element to appear and exception if it does not exist.
 134              $radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]');
 135              $radio->click();
 136          }
 137      }
 138  
 139      /**
 140       * Checks if the capability has the specified permission. Works in the role definition advanced page.
 141       *
 142       * @Then /^"(?P<capability_string>(?:[^"]|\\")*)" capability has "(?P<permission_string>Not set|Allow|Prevent|Prohibit)" permission$/
 143       * @throws ExpectationException
 144       * @param string $capabilityname
 145       * @param string $permission
 146       * @return void
 147       */
 148      public function capability_has_permission($capabilityname, $permission) {
 149  
 150          // We already know the name, so we just need the value.
 151          $radioxpath = "//table[@class='rolecap']/descendant::input[@type='radio']" .
 152              "[@name='" . $capabilityname . "'][@checked]";
 153  
 154          $checkedradio = $this->find('xpath', $radioxpath);
 155  
 156          switch ($permission) {
 157              case get_string('notset', 'role'):
 158                  $perm = CAP_INHERIT;
 159                  break;
 160              case get_string('allow', 'role'):
 161                  $perm = CAP_ALLOW;
 162                  break;
 163              case get_string('prevent', 'role'):
 164                  $perm = CAP_PREVENT;
 165                  break;
 166              case get_string('prohibit', 'role'):
 167                  $perm = CAP_PROHIBIT;
 168                  break;
 169              default:
 170                  throw new ExpectationException('"' . $permission . '" permission does not exist', $this->getSession());
 171                  break;
 172          }
 173  
 174          if ($checkedradio->getAttribute('value') != $perm) {
 175              throw new ExpectationException('"' . $capabilityname . '" permission is not "' . $permission . '"', $this->getSession());
 176          }
 177      }
 178  
 179  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1