Skip to content

Commit

Permalink
moodle_page: MDL-12212 new implementation of user_allowed_editing
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhunt committed May 6, 2009
1 parent 60dfb02 commit 934524d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class moodle_page {

protected $_blocks = null;

protected $_blockseditingcap = 'moodle/site:manageblocks';

protected $_othereditingcaps = array();

/// Getter methods =============================================================
/// Due to the __get magic below, you normally do not call these as $PAGE->get_x
/// methods, but instead use the $PAGE->x syntax.
Expand Down Expand Up @@ -227,7 +231,7 @@ public function user_is_editing() {
* @return boolean does the user have permission to see this page in editing mode.
*/
public function user_allowed_editing() {
return true; // TODO
return has_any_capability($this->all_editing_caps(), $this->_context);
}

/// Setter methods =============================================================
Expand Down Expand Up @@ -382,6 +386,30 @@ public function set_url($url, $params = array()) {
}
}

/**
* Set the capability that allows users to edit blocks on this page. Normally
* the default of 'moodle/site:manageblocks' is used, but a few pages like
* the My Moodle page need to use a different capability like 'moodle/my:manageblocks'.
* @param string $capability a capability.
*/
public function set_blocks_editing_capability($capability) {
$this->_blockseditingcap = $capability;
}

/**
* Some pages let you turn editing on for reasons other than editing blocks.
* If that is the case, you can pass other capabilitise that let the user
* edit this page here.
* @param string|array $capability either a capability, or an array of capabilities.
*/
public function set_other_editing_capability($capability) {
if (is_array($capability)) {
$this->_othereditingcaps = array_unique($this->_othereditingcaps + $capability);
} else {
$this->_othereditingcaps[] = $capability;
}
}

/// Initialisation methods =====================================================
/// These set various things up in a default way.

Expand Down Expand Up @@ -531,6 +559,12 @@ protected function url_to_class_name($url) {
return $class;
}

protected function all_editing_caps() {
$caps = $this->_othereditingcaps;
$caps[] = $this->_blockseditingcap;
return $caps;
}

/// Deprecated fields and methods for backwards compatibility ==================

/**
Expand Down
38 changes: 38 additions & 0 deletions lib/simpletest/testpagelib_moodlepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function initialise_default_pagetype($script = null) {
public function url_to_class_name($url) {
return parent::url_to_class_name($url);
}
public function all_editing_caps() {
return parent::all_editing_caps();
}
}

/**
Expand Down Expand Up @@ -464,6 +467,7 @@ public function setUp() {
global $USER;
$this->originaluserediting = !empty($USER->editing);
$this->testpage = new testable_moodle_page();
$this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
}

public function tearDown() {
Expand All @@ -472,6 +476,8 @@ public function tearDown() {
$USER->editing = $this->originaluserediting;
}

// We are relying on the fact that unit tests are alwyas run by admin, to
// ensure the user_allows_editing call returns true.
public function test_user_is_editing_on() {
// Setup fixture
global $USER;
Expand All @@ -480,12 +486,44 @@ public function test_user_is_editing_on() {
$this->assertTrue($this->testpage->user_is_editing());
}

// We are relying on the fact that unit tests are alwyas run by admin, to
// ensure the user_allows_editing call returns true.
public function test_user_is_editing_off() {
// Setup fixture
global $USER;
$USER->editing = false;
// Validate
$this->assertFalse($this->testpage->user_is_editing());
}

public function test_default_editing_capabilities() {
// Validate
$this->assertEqual(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps());
}

public function test_other_block_editing_cap() {
// Exercise SUT
$this->testpage->set_blocks_editing_capability('moodle/my:manageblocks');
// Validate
$this->assertEqual(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps());
}

public function test_other_editing_cap() {
// Exercise SUT
$this->testpage->set_other_editing_capability('moodle/course:manageactivities');
// Validate
$actualcaps = $this->testpage->all_editing_caps();
$expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks');
$this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps);
}

public function test_other_editing_caps() {
// Exercise SUT
$this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
// Validate
$actualcaps = $this->testpage->all_editing_caps();
$expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks');
$this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps);
}
}
?>

0 comments on commit 934524d

Please sign in to comment.