From 7cd10ab05c0600f8dd859905ac506cc3bbcf04af Mon Sep 17 00:00:00 2001 From: Peter Burnett Date: Mon, 18 Jan 2021 15:47:30 +1000 Subject: [PATCH] Updated DOM element matching to allow for "//" urls in images --- .../maintenance_static_page_io.php | 4 +++- .../maintenance_static_page_test.php | 21 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/classes/local/controllers/maintenance_static_page_io.php b/classes/local/controllers/maintenance_static_page_io.php index eeafb53..6ec6e97 100644 --- a/classes/local/controllers/maintenance_static_page_io.php +++ b/classes/local/controllers/maintenance_static_page_io.php @@ -44,11 +44,13 @@ defined('MOODLE_INTERNAL') || die(); class maintenance_static_page_io { /** * Checks if the given string starts with "http://" or "https://". + * Also checks for "//" at the start of image, which setting_file_url still uses. + * * @param $url * @return bool */ public static function is_url($url) { - return (bool)preg_match('#^http(s)?://#', $url); + return ((bool) preg_match('#^http(s)?://#', $url) || (bool) preg_match('#^//#', $url)); } /** diff --git a/tests/phpunit/local/controllers/maintenance_static_page_test.php b/tests/phpunit/local/controllers/maintenance_static_page_test.php index f632284..2318ff8 100644 --- a/tests/phpunit/local/controllers/maintenance_static_page_test.php +++ b/tests/phpunit/local/controllers/maintenance_static_page_test.php @@ -260,11 +260,22 @@ class maintenance_static_page_test extends auth_outage_base_testcase { self::assertContains('www.example.com/moodle/auth/outage/file.php?file=img.png', $io->get_url_for_file('img.png')); } - public function test_is_url() { - self::assertTrue(maintenance_static_page_io::is_url('http://catalyst.net.nz')); - self::assertTrue(maintenance_static_page_io::is_url('https://www.catalyst-au.net/')); - self::assertFalse(maintenance_static_page_io::is_url('/homepage')); - self::assertFalse(maintenance_static_page_io::is_url('file://homepage')); + public function is_url_dataprovider() { + return [ + [true, 'http://catalyst.net.nz'], + [true, 'https://www.catalyst-au.net/'], + [false, '/homepage'], + [false, 'file://homepage'], + [true, '//catalyst-au.net/img/test.jpg'], + [false, '://www.catalyst-au.net/img/test.jpg'] + ]; + } + + /** + * @dataProvider is_url_dataprovider + */ + public function test_is_url($result, $url) { + self::assertEquals($result, maintenance_static_page_io::is_url($url)); } public function test_file_get_data() {