From a4954415212b9c4a74db46bf6f55df5f0f3077c2 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Tue, 11 May 2010 10:56:04 +0000 Subject: - Patch #777830 by Wim Leers: file_create_url() does not support protocol-relative nor root-relative file URLs. --- modules/simpletest/tests/file.test | 43 ++++++++++-- modules/simpletest/tests/file_test.module | 111 +++++++++++++++++++++--------- 2 files changed, 119 insertions(+), 35 deletions(-) (limited to 'modules') diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test index 939430d72..b8afcd167 100644 --- a/modules/simpletest/tests/file.test +++ b/modules/simpletest/tests/file.test @@ -2022,7 +2022,6 @@ class FileURLRewritingTest extends FileTestCase { function setUp() { parent::setUp('file_test'); - variable_set('file_test_hook_file_url_alter', TRUE); } /** @@ -2031,12 +2030,33 @@ class FileURLRewritingTest extends FileTestCase { function testShippedFileURL() { // Test generating an URL to a shipped file (i.e. a file that is part of // Drupal core, a module or a theme, for example a JavaScript file). + + // Test alteration of file URLs to use a CDN. + variable_set('file_test_hook_file_url_alter', 'cdn'); + $filepath = 'misc/jquery.js'; + $url = file_create_url($filepath); + $this->assertEqual(FILE_URL_TEST_CDN_1 . '/' . $filepath, $url, t('Correctly generated a CDN URL for a shipped file.')); + $filepath = 'misc/favicon.ico'; + $url = file_create_url($filepath); + $this->assertEqual(FILE_URL_TEST_CDN_2 . '/' . $filepath, $url, t('Correctly generated a CDN URL for a shipped file.')); + + // Test alteration of file URLs to use root-relative URLs. + variable_set('file_test_hook_file_url_alter', 'root-relative'); + $filepath = 'misc/jquery.js'; + $url = file_create_url($filepath); + $this->assertEqual(base_path() . '/' . $filepath, $url, t('Correctly generated a root-relative URL for a shipped file.')); + $filepath = 'misc/favicon.ico'; + $url = file_create_url($filepath); + $this->assertEqual(base_path() . '/' . $filepath, $url, t('Correctly generated a root-relative URL for a shipped file.')); + + // Test alteration of file URLs to use protocol-relative URLs. + variable_set('file_test_hook_file_url_alter', 'protocol-relative'); $filepath = 'misc/jquery.js'; $url = file_create_url($filepath); - $this->assertEqual(FILE_URL_TEST_CDN_1 . '/' . $filepath, $url, t('Correctly generated a URL for a shipped file.')); + $this->assertEqual('/' . base_path() . '/' . $filepath, $url, t('Correctly generated a protocol-relative URL for a shipped file.')); $filepath = 'misc/favicon.ico'; $url = file_create_url($filepath); - $this->assertEqual(FILE_URL_TEST_CDN_2 . '/' . $filepath, $url, t('Correctly generated a URL for a shipped file.')); + $this->assertEqual('/' . base_path() . '/' . $filepath, $url, t('Correctly generated a protocol-relative URL for a shipped file.')); } /** @@ -2044,9 +2064,24 @@ class FileURLRewritingTest extends FileTestCase { */ function testPublicCreatedFileURL() { // Test generating an URL to a created file. + + // Test alteration of file URLs to use a CDN. + variable_set('file_test_hook_file_url_alter', 'cdn'); + $file = $this->createFile(); + $url = file_create_url($file->uri); + $this->assertEqual(FILE_URL_TEST_CDN_2 . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a CDN URL for a created file.')); + + // Test alteration of file URLs to use root-relative URLs. + variable_set('file_test_hook_file_url_alter', 'root-relative'); + $file = $this->createFile(); + $url = file_create_url($file->uri); + $this->assertEqual(base_path() . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a root-relative URL for a created file.')); + + // Test alteration of file URLs to use a protocol-relative URLs. + variable_set('file_test_hook_file_url_alter', 'protocol-relative'); $file = $this->createFile(); $url = file_create_url($file->uri); - $this->assertEqual(FILE_URL_TEST_CDN_2 . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a URL for a created file.')); + $this->assertEqual('/' . base_path() . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a protocol-relative URL for a created file.')); } } diff --git a/modules/simpletest/tests/file_test.module b/modules/simpletest/tests/file_test.module index b14dd6ef4..6d4d1f8c5 100644 --- a/modules/simpletest/tests/file_test.module +++ b/modules/simpletest/tests/file_test.module @@ -286,41 +286,90 @@ function file_test_file_delete($file) { function file_test_file_url_alter(&$uri) { // Only run this hook when this variable is set. Otherwise, we'd have to add // another hidden test module just for this hook. - if (!variable_get('file_test_hook_file_url_alter', FALSE)) { + $alter_mode = variable_get('file_test_hook_file_url_alter', FALSE); + if (!$alter_mode) { return; } - - $cdn_extensions = array('css', 'js', 'gif', 'jpg', 'jpeg', 'png'); - - // Most CDNs don't support private file transfers without a lot of hassle, - // so don't support this in the common case. - $schemes = array('public'); - - $scheme = file_uri_scheme($uri); - - // Only serve shipped files and public created files from the CDN. - if (!$scheme || in_array($scheme, $schemes)) { - // Shipped files. - if (!$scheme) { - $path = $uri; - } - // Public created files. - else { - $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme); - $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri); + // Test alteration of file URLs to use a CDN. + elseif ($alter_mode == 'cdn') { + $cdn_extensions = array('css', 'js', 'gif', 'jpg', 'jpeg', 'png'); + + // Most CDNs don't support private file transfers without a lot of hassle, + // so don't support this in the common case. + $schemes = array('public'); + + $scheme = file_uri_scheme($uri); + + // Only serve shipped files and public created files from the CDN. + if (!$scheme || in_array($scheme, $schemes)) { + // Shipped files. + if (!$scheme) { + $path = $uri; + } + // Public created files. + else { + $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme); + $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri); + } + + // Clean up Windows paths. + $path = str_replace('\\', '/', $path); + + // Serve files with one of the CDN extensions from CDN 1, all others from + // CDN 2. + $pathinfo = pathinfo($path); + if (array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $cdn_extensions)) { + $uri = FILE_URL_TEST_CDN_1 . '/' . $path; + } + else { + $uri = FILE_URL_TEST_CDN_2 . '/' . $path; + } } - - // Clean up Windows paths. - $path = str_replace('\\', '/', $path); - - // Serve files with one of the CDN extensions from CDN 1, all others from - // CDN 2. - $pathinfo = pathinfo($path); - if (array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $cdn_extensions)) { - $uri = FILE_URL_TEST_CDN_1 . '/' . $path; + } + // Test alteration of file URLs to use root-relative URLs. + elseif ($alter_mode == 'root-relative') { + // Only serve shipped files and public created files with root-relative + // URLs. + $scheme = file_uri_scheme($uri); + if (!$scheme || $scheme == 'public') { + // Shipped files. + if (!$scheme) { + $path = $uri; + } + // Public created files. + else { + $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme); + $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri); + } + + // Clean up Windows paths. + $path = str_replace('\\', '/', $path); + + // Generate a root-relative URL. + $uri = base_path() . '/' . $path; } - else { - $uri = FILE_URL_TEST_CDN_2 . '/' . $path; + } + // Test alteration of file URLs to use protocol-relative URLs. + elseif ($alter_mode == 'protocol-relative') { + // Only serve shipped files and public created files with protocol-relative + // URLs. + $scheme = file_uri_scheme($uri); + if (!$scheme || $scheme == 'public') { + // Shipped files. + if (!$scheme) { + $path = $uri; + } + // Public created files. + else { + $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme); + $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri); + } + + // Clean up Windows paths. + $path = str_replace('\\', '/', $path); + + // Generate a protocol-relative URL. + $uri = '/' . base_path() . '/' . $path; } } } -- cgit v1.2.3