From 7dc928b5ff1b9ea64148f950d5e2a8638e9c9bb0 Mon Sep 17 00:00:00 2001
From: mudrd8mz <mudrd8mz>
Date: Wed, 29 Jul 2009 16:05:36 +0000
Subject: [PATCH] MDL-19956 $PAGE->set_url(...) accepts already prepared
 instance of moodle_url

---
 lib/pagelib.php                           | 19 ++++++++++++++-----
 lib/simpletest/testpagelib_moodlepage.php | 20 ++++++++++++++++++++
 2 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/lib/pagelib.php b/lib/pagelib.php
index 4d265b7b55..a90578a8e5 100644
--- a/lib/pagelib.php
+++ b/lib/pagelib.php
@@ -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);
         }
     }
 
diff --git a/lib/simpletest/testpagelib_moodlepage.php b/lib/simpletest/testpagelib_moodlepage.php
index ea69b56791..4620fa22ca 100644
--- a/lib/simpletest/testpagelib_moodlepage.php
+++ b/lib/simpletest/testpagelib_moodlepage.php
@@ -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));
-- 
2.39.5