From a6a14f2155a4ddcdd2a4f273e7e6baff34da502d Mon Sep 17 00:00:00 2001 From: sam marshall Date: Tue, 14 Jan 2014 13:44:17 +0000 Subject: [PATCH] MDL-43619 Roles: reset_role_capabilities unit test, cache fix --- lib/accesslib.php | 12 +++++++++--- lib/tests/accesslib_test.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 5001cb6aa1d..dca8351568a 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2615,8 +2615,11 @@ function get_default_role_archetype_allows($type, $archetype) { * Reset role capabilities to default according to selected role archetype. * If no archetype selected, removes all capabilities. * - * @param int $roleid - * @return void + * This applies to capabilities that are assigned to the role (that you could + * edit in the 'define roles' interface), and not to any capability overrides + * in different locations. + * + * @param int $roleid ID of role to reset capabilities for */ function reset_role_capabilities($roleid) { global $DB; @@ -2627,11 +2630,14 @@ function reset_role_capabilities($roleid) { $systemcontext = context_system::instance(); $DB->delete_records('role_capabilities', - array('roleid'=>$roleid, 'contextid' => $systemcontext->id)); + array('roleid' => $roleid, 'contextid' => $systemcontext->id)); foreach($defaultcaps as $cap=>$permission) { assign_capability($cap, $permission, $roleid, $systemcontext->id); } + + // Mark the system context dirty. + context_system::instance()->mark_dirty(); } /** diff --git a/lib/tests/accesslib_test.php b/lib/tests/accesslib_test.php index 80df165c0b3..6a9f8d688fc 100644 --- a/lib/tests/accesslib_test.php +++ b/lib/tests/accesslib_test.php @@ -2799,6 +2799,41 @@ class core_accesslib_testcase extends advanced_testcase { } $this->assertEquals($perms1, $perms2); } + + /** + * Tests reset_role_capabilities function. + */ + public function test_reset_role_capabilities() { + global $DB; + $this->resetAfterTest(true); + $generator = $this->getDataGenerator(); + + // Create test course and user, enrol one in the other. + $course = $generator->create_course(); + $user = $generator->create_user(); + $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'), MUST_EXIST); + $generator->enrol_user($user->id, $course->id, $roleid); + + // Change student role so it DOES have 'mod/forum:addinstance'. + $systemcontext = context_system::instance(); + assign_capability('mod/forum:addinstance', CAP_ALLOW, $roleid, $systemcontext->id); + + // Override course so it does NOT allow students 'mod/forum:viewdiscussion'. + $coursecontext = context_course::instance($course->id); + assign_capability('mod/forum:viewdiscussion', CAP_PREVENT, $roleid, $coursecontext->id); + + // Check expected capabilities so far. + $this->assertTrue(has_capability('mod/forum:addinstance', $coursecontext, $user)); + $this->assertFalse(has_capability('mod/forum:viewdiscussion', $coursecontext, $user)); + + // Oops, allowing student to add forums was a mistake, let's reset the role. + reset_role_capabilities($roleid); + + // Check new expected capabilities - role capabilities should have been reset, + // while the override at course level should remain. + $this->assertFalse(has_capability('mod/forum:addinstance', $coursecontext, $user)); + $this->assertFalse(has_capability('mod/forum:viewdiscussion', $coursecontext, $user)); + } } /**