mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
Merge branch 'MDL-80896-main' of https://github.com/andrewnicols/moodle
This commit is contained in:
commit
a0e333fb7c
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,8 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use Psr\Http\Message\UriInterface;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
// Constants.
|
||||
@ -770,6 +772,26 @@ class moodle_url {
|
||||
|
||||
// Static factory methods.
|
||||
|
||||
/**
|
||||
* Create a new moodle_url instance from a UriInterface.
|
||||
*
|
||||
* @param UriInterface $uri
|
||||
* @return self
|
||||
*/
|
||||
public static function from_uri(UriInterface $uri): self {
|
||||
$url = new self(
|
||||
url: $uri->getScheme() . '://' . $uri->getAuthority() . $uri->getPath(),
|
||||
anchor: $uri->getFragment() ?: null,
|
||||
);
|
||||
|
||||
$params = $uri->getQuery();
|
||||
foreach (explode('&', $params) as $param) {
|
||||
$url->param(...explode('=', $param, 2));
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* General moodle file url.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user