From 494cf9b81c1d3cc1185bbbcd88db0759a71177f2 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 18 Nov 2014 11:07:54 +0800 Subject: [PATCH] MDL-23839 navigation: Check for assignable course category roles --- .../behat/category_role_assignment.feature | 42 ++++++++++++++ lib/navigationlib.php | 2 +- lib/tests/behat/behat_permissions.php | 57 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 course/tests/behat/category_role_assignment.feature diff --git a/course/tests/behat/category_role_assignment.feature b/course/tests/behat/category_role_assignment.feature new file mode 100644 index 00000000000..9d9dc77350a --- /dev/null +++ b/course/tests/behat/category_role_assignment.feature @@ -0,0 +1,42 @@ +@core @core_course +Feature: Role assignments can be made at the category level + In order to grant a user different capabilities + As a user + I can assign roles in categories + + Background: + Given the following "users" exist: + | username | firstname | lastname | + | manager | Manager | Manager | + And the following "categories" exist: + | name | category | idnumber | + | Cat 1 | 0 | CAT1 | + And the following "role assigns" exist: + | user | role | contextlevel | reference | + | manager | manager | Category | CAT1 | + And I log in as "admin" + + + @javascript + Scenario: A user with a category role can assign roles + Given I define the allowed role assignments for the "Manager" role as: + | Teacher | Assignable | + And I log out + And I log in as "manager" + And I follow "Courses" + When I follow "Cat 1" + Then I should see "Assign roles" + + @javascript + Scenario: A user with a category role cannot assign roles if there are no roles to assign + Given I define the allowed role assignments for the "Manager" role as: + | Manager | Not assignable | + | Course creator | Not assignable | + | Teacher | Not assignable | + | Non-editing teacher | Not assignable | + | Student | Not assignable | + And I log out + And I log in as "manager" + And I follow "Courses" + When I follow "Cat 1" + Then I should not see "Assign roles" diff --git a/lib/navigationlib.php b/lib/navigationlib.php index c0c33c2fec6..0f431d45d2b 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -4419,7 +4419,7 @@ class settings_navigation extends navigation_node { } // Assign local roles - if (has_capability('moodle/role:assign', $catcontext)) { + if (!empty(get_assignable_roles($catcontext))) { $assignurl = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid' => $catcontext->id)); $categorynode->add(get_string('assignroles', 'role'), $assignurl, self::TYPE_SETTING, null, 'roles', new pix_icon('i/assignroles', '')); } diff --git a/lib/tests/behat/behat_permissions.php b/lib/tests/behat/behat_permissions.php index fcb1646bf82..cc1ef38d419 100644 --- a/lib/tests/behat/behat_permissions.php +++ b/lib/tests/behat/behat_permissions.php @@ -176,4 +176,61 @@ class behat_permissions extends behat_base { } } + /** + * Set the allowed role assignments for the specified role. + * + * @Given /^I define the allowed role assignments for the "(?P(?:[^"]|\\")*)" role as:$/ + * @param string $rolename + * @param TableNode $table + * @return void Executes other steps + */ + public function i_define_the_allowed_role_assignments_for_a_role_as($rolename, $table) { + $parentnodes = get_string('administrationsite') . ' > ' . + get_string('users', 'admin') . ' > ' . + get_string('permissions', 'role'); + return array( + new Given('I am on homepage'), + new Given('I navigate to "' . get_string('defineroles', 'role') . '" node in "' . $parentnodes . '"'), + new Given('I follow "Allow role assignments"'), + new Given('I fill in the allowed role assignments form for the "' . $rolename . '" role with:', $table), + new Given('I press "' . get_string('savechanges') . '"') + ); + } + + /** + * Fill in the allowed role assignments form for the specied role. + * + * Takes a table with two columns. Each row should contain the target + * role, and either "Assignable" or "Not assignable". + * + * @Given /^I fill in the allowed role assignments form for the "(?P(?:[^"]|\\")*)" role with:$/ + * @param String $sourcerole + * @param TableNode $table + * @return void + */ + public function i_fill_in_the_allowed_role_assignments_form_for_a_role_with($sourcerole, $table) { + foreach ($table->getRows() as $key => $row) { + list($targetrole, $allowed) = $row; + + $node = $this->find('xpath', '//input[@title="Allow users with role ' . + $sourcerole . + ' to assign the role ' . + $targetrole . '"]'); + + if ($allowed == 'Assignable') { + if (!$node->isChecked()) { + $node->click(); + } + } else if ($allowed == 'Not assignable') { + if ($node->isChecked()) { + $node->click(); + } + } else { + throw new ExpectationException( + 'The provided permission value "' . $allowed . '" is not valid. Use Assignable, or Not assignable', + $this->getSession() + ); + } + } + } }