MDL-51718 weblib: Allow moodle_url scheme to be set

This commit is contained in:
Brendan Heywood 2015-10-12 10:40:50 +11:00
parent 4cef723c22
commit 462065e822
2 changed files with 32 additions and 0 deletions

View File

@ -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);

View File

@ -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.
*