summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/file_test.module
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-08-29 06:57:27 +0000
committerDries Buytaert <dries@buytaert.net>2009-08-29 06:57:27 +0000
commitfeb4845493121078ca6c3eb511f4ea69b0cb1809 (patch)
treefa8c6f9292e6b921e2339ea991754b4b8da31aec /modules/simpletest/tests/file_test.module
parent0597c9e48a895603f97632741231946cf6ba24e3 (diff)
downloadbrdo-feb4845493121078ca6c3eb511f4ea69b0cb1809.tar.gz
brdo-feb4845493121078ca6c3eb511f4ea69b0cb1809.tar.bz2
- Patch #499156 by Wim Leers: add hook_file_alter() so we can integrate with CDNs.
Diffstat (limited to 'modules/simpletest/tests/file_test.module')
-rw-r--r--modules/simpletest/tests/file_test.module50
1 files changed, 50 insertions, 0 deletions
diff --git a/modules/simpletest/tests/file_test.module b/modules/simpletest/tests/file_test.module
index 635576506..8f087af77 100644
--- a/modules/simpletest/tests/file_test.module
+++ b/modules/simpletest/tests/file_test.module
@@ -9,6 +9,11 @@
* calling file_test_get_calls() or file_test_set_return().
*/
+
+define('FILE_URL_TEST_CDN_1', 'http://cdn1.example.com');
+define('FILE_URL_TEST_CDN_2', 'http://cdn2.example.com');
+
+
/**
* Implement hook_menu().
*/
@@ -265,6 +270,51 @@ function file_test_file_delete($file) {
}
/**
+ * Implement hook_file_url_alter().
+ */
+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)) {
+ 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);
+ }
+
+ // 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;
+ }
+ }
+}
+
+/**
* Helper class for testing the stream wrapper registry.
*
* Dummy stream wrapper implementation (dummy://).