From 097b3fbee31797f636434985d6fccdcde14aa619 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 10 Jun 2024 11:56:02 +0800 Subject: [PATCH] MDL-81960 core: Move moodle_url tests to correct location --- lib/classes/component.php | 1 + lib/tests/url_test.php | 20 ------ lib/tests/weblib_test.php | 130 -------------------------------------- 3 files changed, 1 insertion(+), 150 deletions(-) diff --git a/lib/classes/component.php b/lib/classes/component.php index e7f3eb4b324..0169c7e7f77 100644 --- a/lib/classes/component.php +++ b/lib/classes/component.php @@ -390,6 +390,7 @@ class core_component { $keyclasses = [ \core\exception\moodle_exception::class, \core\output\bootstrap_renderer::class, + \core\url::class, ]; foreach ($keyclasses as $classname) { if (!array_key_exists($classname, $cache['classmap'])) { diff --git a/lib/tests/url_test.php b/lib/tests/url_test.php index fd3cc8a5dbb..069dcc4be41 100644 --- a/lib/tests/url_test.php +++ b/lib/tests/url_test.php @@ -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); diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php index 705f962a6d4..4de970c12d0 100644 --- a/lib/tests/weblib_test.php +++ b/lib/tests/weblib_test.php @@ -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 \moodle_url::out_as_local_url(). - * - * @return array - * @throws moodle_exception On seriously malformed URLs (parse_url). - * @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 \moodle_url::out_as_local_url(). - * - * @return array - * @throws moodle_exception On seriously malformed URLs (parse_url). - * @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 \moodle_url::is_local_url(). - * - * @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. *