* 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);
}
}
$this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&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&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));