MDL-81960 core: Move moodle_url tests to correct location

This commit is contained in:
Andrew Nicols 2024-06-10 11:56:02 +08:00
parent b637f8cc4e
commit 097b3fbee3
No known key found for this signature in database
GPG key ID: 6D1E3157C8CFBF14
3 changed files with 1 additions and 150 deletions

View file

@ -175,26 +175,6 @@ final class url_test extends \advanced_testcase {
$this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
}
public function test_out_as_local_url(): void {
global $CFG;
// Test http url.
$url1 = new url('/lib/tests/weblib_test.php');
$this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
// Test https url.
$httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot);
$url2 = new url($httpswwwroot . '/login/profile.php');
$this->assertSame('/login/profile.php', $url2->out_as_local_url());
// Test http url matching wwwroot.
$url3 = new url($CFG->wwwroot);
$this->assertSame('', $url3->out_as_local_url());
// Test http url matching wwwroot ending with slash (/).
$url3 = new url($CFG->wwwroot . '/');
$this->assertSame('/', $url3->out_as_local_url());
}
public function test_out_as_local_url_error(): void {
$url2 = new url('http://www.google.com/lib/tests/weblib_test.php');
$this->expectException(\coding_exception::class);

View file

@ -1199,136 +1199,6 @@ EXPECTED;
$this->assertEquals($expected, get_html_lang_attribute_value($langcode));
}
/**
* Test the coding exceptions when returning URL as relative path from $CFG->wwwroot.
*
* @param moodle_url $url The URL pointing to a web resource.
* @param string $exmessage The expected output URL.
* @throws coding_exception If called on a non-local URL.
* @see \moodle_url::out_as_local_url()
* @covers \moodle_url::out_as_local_url
* @dataProvider out_as_local_url_coding_exception_provider
*/
public function test_out_as_local_url_coding_exception(\moodle_url $url, string $exmessage): void {
$this->expectException(\coding_exception::class);
$this->expectExceptionMessage($exmessage);
$localurl = $url->out_as_local_url();
}
/**
* Data provider for throwing coding exceptions in <u>\moodle_url::out_as_local_url()</u>.
*
* @return array
* @throws moodle_exception On seriously malformed URLs (<u>parse_url</u>).
* @see \moodle_url::out_as_local_url()
* @see parse_url()
*/
public function out_as_local_url_coding_exception_provider() {
return [
'Google Maps CDN (HTTPS)' => [
new \moodle_url('https://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
'Coding error detected, it must be fixed by a programmer: out_as_local_url called on a non-local URL'
],
'Google Maps CDN (HTTP)' => [
new \moodle_url('http://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
'Coding error detected, it must be fixed by a programmer: out_as_local_url called on a non-local URL'
],
];
}
/**
* Test URL as relative path from $CFG->wwwroot.
*
* @param moodle_url $url The URL pointing to a web resource.
* @param string $expected The expected local URL.
* @throws coding_exception If called on a non-local URL.
* @see \moodle_url::out_as_local_url()
* @covers \moodle_url::out_as_local_url
* @dataProvider out_as_local_url_provider
*/
public function test_out_as_local_url(\moodle_url $url, string $expected): void {
$this->assertEquals($expected, $url->out_as_local_url(false));
}
/**
* Data provider for returning local paths via <u>\moodle_url::out_as_local_url()</u>.
*
* @return array
* @throws moodle_exception On seriously malformed URLs (<u>parse_url</u>).
* @see \moodle_url::out_as_local_url()
* @see parse_url()
*/
public function out_as_local_url_provider() {
global $CFG;
$wwwroot = rtrim($CFG->wwwroot, '/');
return [
'Environment XML file' => [
new \moodle_url('/admin/environment.xml'),
'/admin/environment.xml'
],
'H5P JS internal resource' => [
new \moodle_url('/h5p/js/embed.js'),
'/h5p/js/embed.js'
],
'A Moodle JS resource using the full path including the proper JS Handler' => [
new \moodle_url($wwwroot . '/lib/javascript.php/1/lib/editor/tiny/js/tinymce/tinymce.js'),
'/lib/javascript.php/1/lib/editor/tiny/js/tinymce/tinymce.js'
],
];
}
/**
* Test URL as relative path from $CFG->wwwroot.
*
* @param moodle_url $url The URL pointing to a web resource.
* @param bool $expected The expected result.
* @see \moodle_url::is_local_url()
* @covers \moodle_url::is_local_url
* @dataProvider is_local_url_provider
*/
public function test_is_local_url(\moodle_url $url, bool $expected): void {
$this->assertEquals($expected, $url->is_local_url(), "'{$url}' is not a local URL!");
}
/**
* Data provider for testing <u>\moodle_url::is_local_url()</u>.
*
* @return array
* @see \moodle_url::is_local_url()
*/
public function is_local_url_provider() {
global $CFG;
$wwwroot = rtrim($CFG->wwwroot, '/');
return [
'Google Maps CDN (HTTPS)' => [
new \moodle_url('https://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
false
],
'Google Maps CDN (HTTP)' => [
new \moodle_url('http://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
false
],
'wwwroot' => [
new \moodle_url($wwwroot),
true
],
'wwwroot/' => [
new \moodle_url($wwwroot . '/'),
true
],
'Environment XML file' => [
new \moodle_url('/admin/environment.xml'),
true
],
'H5P JS internal resource' => [
new \moodle_url('/h5p/js/embed.js'),
true
],
];
}
/**
* Data provider for strip_querystring tests.
*