]> git.mjollnir.org Git - moodle.git/commitdiff
moodle_page: MDL-12212 new implementation of user_allowed_editing
authortjhunt <tjhunt>
Wed, 6 May 2009 09:01:42 +0000 (09:01 +0000)
committertjhunt <tjhunt>
Wed, 6 May 2009 09:01:42 +0000 (09:01 +0000)
lib/pagelib.php
lib/simpletest/testpagelib_moodlepage.php

index f1159dc87ab05ce9049ae3141db06ec4eb3495ca..a0b185d401a8cdc3b54733bf003a1e334203ccb7 100644 (file)
@@ -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.
@@ -227,7 +231,7 @@ class moodle_page {
      * @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 =============================================================
@@ -382,6 +386,30 @@ class moodle_page {
         }
     }
 
+    /**
+     * 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.
 
@@ -531,6 +559,12 @@ class moodle_page {
         return $class;
     }
 
+    protected function all_editing_caps() {
+        $caps = $this->_othereditingcaps;
+        $caps[] = $this->_blockseditingcap;
+        return $caps;
+    }
+
 /// Deprecated fields and methods for backwards compatibility ==================
 
     /**
index 0240d0a4d9e049de7258311edbeb3bed3f896c97..60f42cc371ee76d540a123494eefb4ff36f7d449 100644 (file)
@@ -44,6 +44,9 @@ class testable_moodle_page extends moodle_page {
     public function url_to_class_name($url) {
         return parent::url_to_class_name($url);
     }
+    public function all_editing_caps() {
+        return parent::all_editing_caps();
+    }
 }
 
 /**
@@ -464,6 +467,7 @@ class moodle_page_editing_test extends UnitTestCase {
         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() {
@@ -472,6 +476,8 @@ class moodle_page_editing_test extends UnitTestCase {
         $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;
@@ -480,6 +486,8 @@ class moodle_page_editing_test extends UnitTestCase {
         $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;
@@ -487,5 +495,35 @@ class moodle_page_editing_test extends UnitTestCase {
         // 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);
+    }
 }
 ?>