diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php index 8ba4a4d5255..4ddffd6c149 100644 --- a/lib/tests/weblib_test.php +++ b/lib/tests/weblib_test.php @@ -147,6 +147,35 @@ class web_testcase extends advanced_testcase { $this->assertEquals(wikify_links('this is a link'), 'this is a link [ http://someaddress.com/query ]'); } + /** + * Test basic moodle_url construction. + */ + function test_moodle_url_constructor() { + global $CFG; + + $url = new moodle_url('/index.php'); + $this->assertEquals($CFG->wwwroot.'/index.php', $url->out()); + + $url = new moodle_url('/index.php', array()); + $this->assertEquals($CFG->wwwroot.'/index.php', $url->out()); + + $url = new moodle_url('/index.php', array('id' => 2)); + $this->assertEquals($CFG->wwwroot.'/index.php?id=2', $url->out()); + + $url = new moodle_url('/index.php', array('id' => 'two')); + $this->assertEquals($CFG->wwwroot.'/index.php?id=two', $url->out()); + + $url = new moodle_url('/index.php', array('id' => 1, 'cid' => '2')); + $this->assertEquals($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out()); + $this->assertEquals($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out(false)); + + $url = new moodle_url('/index.php', null, 'test'); + $this->assertEquals($CFG->wwwroot.'/index.php#test', $url->out()); + + $url = new moodle_url('/index.php', array('id' => 2), 'test'); + $this->assertEquals($CFG->wwwroot.'/index.php?id=2#test', $url->out()); + } + /** * Tests moodle_url::get_path(). */ diff --git a/lib/weblib.php b/lib/weblib.php index f6b646886e5..24f5a935ff7 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -298,8 +298,9 @@ class moodle_url { * $params instead. For admin URLs, just use /admin/script.php, this * class takes care of the $CFG->admin issue. * @param array $params these params override current params or add new + * @param string $anchor The anchor to use as part of the URL if there is one. */ - public function __construct($url, array $params = null) { + public function __construct($url, array $params = null, $anchor = null) { global $CFG; if ($url instanceof moodle_url) { @@ -361,6 +362,9 @@ class moodle_url { } $this->params($params); + if ($anchor !== null) { + $this->anchor = (string)$anchor; + } } /**