]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19956 $PAGE->set_url(...) accepts already prepared instance of moodle_url
authormudrd8mz <mudrd8mz>
Wed, 29 Jul 2009 16:05:36 +0000 (16:05 +0000)
committermudrd8mz <mudrd8mz>
Wed, 29 Jul 2009 16:05:36 +0000 (16:05 +0000)
lib/pagelib.php
lib/simpletest/testpagelib_moodlepage.php

index 4d265b7b555bcfc7c00e9b8bf270034d1382bbc7..a90578a8e5e132ec99e06fceaf4b897cb708f0be 100644 (file)
@@ -720,17 +720,26 @@ class moodle_page {
      * For example, course/view.php does:
      *      $id = optional_param('id', 0, PARAM_INT);
      *      $PAGE->set_url('course/view.php', array('id' => $id));
-     * @param string $url a URL, relative to $CFG->wwwroot.
-     * @param array $params paramters to add ot the URL.
+     * @param mixed $url moodle_url|string URL relative to $CFG->wwwroot or {@link moodle_url} instance
+     * @param array $params paramters to add to the URL (allowed only if $url is string)
      */
     public function set_url($url, $params = array()) {
         global $CFG;
-        $this->_url = new moodle_url($CFG->wwwroot . '/' . $url, $params);
+        if ($url instanceof moodle_url) {
+            $shorturl = str_replace($CFG->wwwroot, '', $url->out(true));
+            $this->_url = clone($url);
+            if (!empty($params)) {
+                throw new coding_exception('Cannot pass params together with moodle_url to moodle_page::set_url().');
+            }
+        } else {
+            $shorturl = $url;
+            $this->_url = new moodle_url($CFG->wwwroot . '/' . $url, $params);
+        }
         if (is_null($this->_pagetype)) {
-            $this->initialise_default_pagetype($url);
+            $this->initialise_default_pagetype($shorturl);
         }
         if (!is_null($this->_legacypageobject)) {
-            $this->_legacypageobject->set_url($url, $params);
+            $this->_legacypageobject->set_url($shorturl, $params);
         }
     }
 
index ea69b5679162a463b518225bdecbcc087ef2527d..4620fa22cac4a55766ddaf41560009b28f834da1 100644 (file)
@@ -300,6 +300,26 @@ class moodle_page_test extends UnitTestCase {
         $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
     }
 
+    public function test_set_url_using_moodle_url() {
+        global $CFG;
+        // Fixture setup
+        $url = new moodle_url($CFG->wwwroot . '/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
+        // Exercise SUT
+        $this->testpage->set_url($url);
+        // Validate
+        $this->assertEqual($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&amp;method=manual', $this->testpage->url->out());
+    }
+
+    public function test_set_url_using_moodle_url_and_params() {
+        global $CFG;
+        // Fixture setup
+        $url = new moodle_url($CFG->wwwroot . '/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
+        // Set expectation
+        $this->expectException('coding_exception');
+        // Exercise SUT
+        $this->testpage->set_url($url, array('notallowed' => 666));
+    }
+
     public function test_set_url_sets_page_type() {
         // Exercise SUT
         $this->testpage->set_url('mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));