From 462065e822a2337f764aabcbb0cb5e71b77efa7c Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Mon, 12 Oct 2015 10:40:50 +1100 Subject: [PATCH] MDL-51718 weblib: Allow moodle_url scheme to be set --- lib/tests/weblib_test.php | 18 ++++++++++++++++++ lib/weblib.php | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php index f2a60e35bc0..899b0880b7d 100644 --- a/lib/tests/weblib_test.php +++ b/lib/tests/weblib_test.php @@ -246,6 +246,24 @@ class core_weblib_testcase extends advanced_testcase { $this->assertSame($strurl, $url->out(false)); } + /** + * Test set good scheme on Moodle URL objects. + */ + public function test_moodle_url_set_good_scheme() { + $url = new moodle_url('http://moodle.org/foo/bar'); + $url->set_scheme('myscheme'); + $this->assertSame('myscheme://moodle.org/foo/bar', $url->out()); + } + + /** + * Test set bad scheme on Moodle URL objects. + */ + public function test_moodle_url_set_bad_scheme() { + $url = new moodle_url('http://moodle.org/foo/bar'); + $this->setExpectedException('coding_exception'); + $url->set_scheme('not a valid $ scheme'); + } + public function test_moodle_url_round_trip_array_params() { $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2'; $url = new moodle_url($strurl); diff --git a/lib/weblib.php b/lib/weblib.php index b7e67e5dd4e..aed1cb4cd6c 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -672,6 +672,20 @@ class moodle_url { } } + /** + * Sets the scheme for the URI (the bit before ://) + * + * @param string $scheme + */ + public function set_scheme($scheme) { + // See http://www.ietf.org/rfc/rfc3986.txt part 3.1. + if (preg_match('/^[a-zA-Z][a-zA-Z0-9+.-]*$/', $scheme)) { + $this->scheme = $scheme; + } else { + throw new coding_exception('Bad URL scheme.'); + } + } + /** * Sets the url slashargument value. * -- 2.43.0