This commit is contained in:
Jun Pataleta 2024-03-12 11:38:26 +08:00
commit a0e333fb7c
No known key found for this signature in database
GPG key ID: F83510526D99E2C7
2 changed files with 53 additions and 0 deletions

View file

@ -16,6 +16,8 @@
namespace core;
use GuzzleHttp\Psr7\Uri;
/**
* Tests for moodle_url.
*
@ -414,4 +416,33 @@ class moodle_url_test extends \advanced_testcase {
],
];
}
public function test_from_uri(): void {
global $CFG;
$uri = new Uri('http://www.example.org:447/my/file/is/here.txt?really=1');
$url = \moodle_url::from_uri($uri);
$this->assertSame('http://www.example.org:447/my/file/is/here.txt?really=1', $url->out(false));
$this->assertEquals(1, $url->param('really'));
$uri = new Uri('https://www.example.org/my/file/is/here.txt?really=1');
$url = \moodle_url::from_uri($uri);
$this->assertSame('https://www.example.org/my/file/is/here.txt?really=1', $url->out(false));
$this->assertEquals(1, $url->param('really'));
// Multiple params.
$uri = new Uri('https://www.example.org/my/file/is/here.txt?really=1&another=2&&more=3&moar=4');
$url = \moodle_url::from_uri($uri);
$this->assertSame('https://www.example.org/my/file/is/here.txt?really=1&another=2&more=3&moar=4', $url->out(false));
$this->assertEquals(1, $url->param('really'));
$this->assertEquals(2, $url->param('another'));
$this->assertEquals(3, $url->param('more'));
$this->assertEquals(4, $url->param('moar'));
// Anchors.
$uri = new Uri("{$CFG->wwwroot}/course/view/#section-1");
$url = \moodle_url::from_uri($uri);
$this->assertSame("{$CFG->wwwroot}/course/view/#section-1", $url->out(false));
$this->assertEmpty($url->params());
}
}