summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests
diff options
context:
space:
mode:
Diffstat (limited to 'modules/simpletest/tests')
-rw-r--r--modules/simpletest/tests/file.test62
-rw-r--r--modules/simpletest/tests/file_test.module50
2 files changed, 112 insertions, 0 deletions
diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test
index 2830e7b7b..837d1da36 100644
--- a/modules/simpletest/tests/file.test
+++ b/modules/simpletest/tests/file.test
@@ -1879,6 +1879,26 @@ class FileDownloadTest extends FileTestCase {
}
/**
+ * Test the public file transfer system.
+ */
+ function testPublicFileTransfer() {
+ // Test generating an URL to a created file.
+ $file = $this->createFile();
+ $url = file_create_url($file->uri);
+ $this->assertEqual($GLOBALS['base_url'] . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a URL for a created file.'));
+ $this->drupalHead($url);
+ $this->assertResponse(200, t('Confirmed that the generated URL is correct by downloading the created file.'));
+
+ // 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).
+ $filepath = 'misc/jquery.js';
+ $url = file_create_url($filepath);
+ $this->assertEqual($GLOBALS['base_url'] . '/' . $filepath, $url, t('Correctly generated a URL for a shipped file.'));
+ $this->drupalHead($url);
+ $this->assertResponse(200, t('Confirmed that the generated URL is correct by downloading the shipped file.'));
+ }
+
+ /**
* Test the private file transfer system.
*/
function testPrivateFileTransfer() {
@@ -1908,6 +1928,48 @@ class FileDownloadTest extends FileTestCase {
}
/**
+ * Tests for file URL rewriting.
+ */
+class FileURLRewritingTest extends FileTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => t('File URL rewriting'),
+ 'description' => t('Tests for file URL rewriting.'),
+ 'group' => t('File'),
+ );
+ }
+
+ function setUp() {
+ parent::setUp('file_test');
+ variable_set('file_test_hook_file_url_alter', TRUE);
+ }
+
+ /**
+ * Test the generating of rewritten shipped file URLs.
+ */
+ 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).
+ $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.'));
+ $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.'));
+ }
+
+ /**
+ * Test the generating of rewritten public created file URLs.
+ */
+ function testPublicCreatedFileURL() {
+ // Test generating an URL to a created file.
+ $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.'));
+ }
+}
+
+/**
* Tests for file_munge_filename() and file_unmunge_filename().
*/
class FileNameMungingTest extends FileTestCase {
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://).