From 92c79fcc007eb759a8bb1fff8a81dc8455010582 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 15 Feb 2013 08:45:28 +0800 Subject: [PATCH] MDL-37958 behat: Defintions for permissions management --- lib/tests/behat/behat_permissions.php | 136 ++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 lib/tests/behat/behat_permissions.php diff --git a/lib/tests/behat/behat_permissions.php b/lib/tests/behat/behat_permissions.php new file mode 100644 index 00000000000..15981ff0e0f --- /dev/null +++ b/lib/tests/behat/behat_permissions.php @@ -0,0 +1,136 @@ +. + +/** + * Steps definitions related with permissions. + * + * @package core + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +require_once(__DIR__ . '/../../behat/behat_base.php'); + +use Behat\Mink\Exception\ExpectationException as ExpectationException, + Behat\Behat\Context\Step\Given as Given, + Behat\Gherkin\Node\TableNode as TableNode; + +/** + * Steps definitions to set up permissions to capabilities. + * + * @package core + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_permissions extends behat_base { + + /** + * Set system level permissions to the specified role. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns. + * @Given /^I set the following system permissions of "(?P(?:[^"]|\\")*)" role:$/ + * @param string $rolename + * @param TableNode $table + * @return void Executes other steps + */ + public function i_set_the_following_system_permissions_of_role($rolename, $table) { + + return array( + new Given('I am on homepage'), + new Given('I expand "Front page settings" node'), + new Given('I expand "Site administration" node'), + new Given('I expand "Users" node'), + new Given('I expand "Permissions" node'), + new Given('I follow "Define roles"'), + new Given('I follow "Edit ' . $this->escape($rolename) . ' role"'), + new Given('I fill the capabilities form with the following permissions:', $table), + new Given('I press "Save changes"') + ); + } + + /** + * 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. + * @Given /^I override the system permissions of "(?P(?:[^"]|\\")*)" role with:$/ + * @param string $rolename + * @param TableNode $table + * @return void Executes other steps + */ + public function i_override_the_system_permissions_of_role_with($rolename, $table) { + + // We don't know the number of overrides so we have to get it to match the option contents. + $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(text(),"' . $this->escape($rolename) . '")]'); + + return array( + new Given('I select "' . $this->escape($roleoption->getText()) . '" from "Advanced role override"'), + new Given('I fill the capabilities form with the following permissions:', $table), + new Given('I press "Save changes"') + ); + } + + /** + * Fills the advanced permissions form with the provided data. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns. + * @Given /^I fill the capabilities form with the following permissions:$/ + * @param TableNode $table + * @return void + */ + public function i_fill_the_capabilities_form_with_the_following_permissions($table) { + + // Ensure we are using the advanced view. + // Wrapped in a try/catch to capture the exception and continue execution, we don't know if advanced mode was already enabled. + try { + $advancedtoggle = $this->find_button('Show advanced'); + if ($advancedtoggle) { + $this->getSession()->getPage()->pressButton('Show advanced'); + } + } catch (Exception $e) { + // We already are in advanced mode. + } + + // Using getRows() as we are not sure if tests writers will add the header. + foreach ($table->getRows() as $key => $row) { + + if (count($row) !== 2) { + throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession()); + } + + list($capability, $permission) = $row; + + // Skip the headers row if it was provided + if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') { + continue; + } + + // Checking the permission value. + $permissionconstant = 'CAP_'. strtoupper($permission); + if (!defined($permissionconstant)) { + throw new ExpectationException( + 'The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited', + $this->getSession() + ); + } + + // Converting from permission to constant value. + $permissionvalue = constant($permissionconstant); + + // Here we wait for the element to appear and exception if it does not exists. + $radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]'); + $radio->click(); + } + } + +}