Index: modules/simpletest/tests/file.test
===================================================================
--- modules/simpletest/tests/file.test	(revision 87)
+++ modules/simpletest/tests/file.test	(working copy)
@@ -542,6 +542,174 @@
 
 
 /**
+ * URL creation related tests.
+ */
+class FileUrlTest extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('File URLs'),
+      'description' => t('Tests the creation of file URLs.'),
+      'group' => t('File'),
+    );
+  }
+
+  /**
+   * This will take a directory and path, and find a valid filepath that is not
+   * taken by another file. Only tests with clean URLs disabled, as url() cannot
+   * switch between clean and unclean URLs dynamically due to a static variable.
+   */
+  function testFileCreateNewFilepathWithUncleanURLs() {
+    global $base_url;
+    $destination = 'misc/xyz.txt';
+    variable_set('clean_url', 0);
+
+    // It seems like file_directory_path is just about the only thing that can
+    // mess up the original Drupal installation if it's not reset properly at
+    // the end of the test.
+    $original_file_directory_path = variable_get('file_directory_path', conf_path() . '/files');
+
+    // This test doesn't require the files directory to be writable, so have
+    // a fixed value instead.
+    $file_directory_path = 'sites/default/files';
+
+    // Private downloads without clean URLs, with an absolute directory path.
+    variable_set('file_downloads', FILE_DOWNLOADS_PRIVATE);
+    variable_set('file_directory_path', dirname(DRUPAL_ROOT) . '/outside-path');
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/?q=system/files/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (private downloads, no clean URLs, absolute directory path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Private downloads without clean URLs, with an absolute directory path
+    // and an absolute file path, too.
+    $path = file_create_url(file_directory_path() . '/' . $destination);
+    $expected_url = $base_url . '/?q=system/files/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (private downloads, no clean URLs, absolute directory path and absolute file path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Private downloads without clean URLs, with a relative directory path.
+    // Should yield the same results as for a relative path.
+    variable_set('file_downloads', FILE_DOWNLOADS_PRIVATE);
+    variable_set('file_directory_path', $file_directory_path);
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/?q=system/files/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (private downloads, no clean URLs, relative directory path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Public downloads without clean URLs, with a relative directory path.
+    variable_set('file_downloads', FILE_DOWNLOADS_PUBLIC);
+    variable_set('file_directory_path', $file_directory_path);
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/' . $file_directory_path . '/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (public downloads, no clean URLs, relative directory path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Public downloads without clean URLs, with an absolute directory path
+    // that is inside the Drupal root directory. file_create_url() should
+    // strip that root directory in order to return a valid URL.
+    variable_set('file_downloads', FILE_DOWNLOADS_PUBLIC);
+    variable_set('file_directory_path', DRUPAL_ROOT . '/' . $file_directory_path);
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/' . $file_directory_path . '/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (public downloads, no clean URLs, absolute directory path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Public downloads without clean URLs, with an absolute directory path
+    // and an absolute file path, too.
+    $path = file_create_url(file_directory_path() . '/' . $destination);
+    $expected_url = $base_url . '/' . $file_directory_path . '/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (public downloads, no clean URLs, absolute directory path and absolute file path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // URLs can't be correctly determined if public downloads are combined with
+    // and absolute directory path outside of the Drupal root directory.
+    // The result might be whatever broken URL there is.
+    variable_set('file_downloads', FILE_DOWNLOADS_PUBLIC);
+    variable_set('file_directory_path', dirname(DRUPAL_ROOT) . '/outside-path');
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/' . $file_directory_path . '/misc/xyz.txt';
+    $this->assertNotEqual($path, $expected_url, t('Created file URL %path does not equal %expected. (public downloads, no clean URLs, absolute directory path outside of the Drupal root directory)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Recover the original file_directory_path, see the top of this function.
+    variable_set('file_directory_path', $original_file_directory_path);
+  }
+
+  /**
+   * This will take a directory and path, and find a valid filepath that is not
+   * taken by another file. Only tests with clean URLs enabled, as url() cannot
+   * switch between clean and unclean URLs dynamically due to a static variable.
+   */
+  /*function testFileCreateNewFilepathWithCleanURLs() {
+    global $base_url;
+    $destination = 'misc/xyz.txt';
+    variable_set('clean_url', 1);
+
+    // It seems like file_directory_path is just about the only thing that can
+    // mess up the original Drupal installation if it's not reset properly at
+    // the end of the test.
+    $original_file_directory_path = variable_get('file_directory_path', conf_path() . '/files');
+
+    // This test doesn't require the files directory to be writable, so have
+    // a fixed value instead.
+    $file_directory_path = 'sites/default/files';
+
+    // Private downloads with clean URLs, with a relative directory path.
+    variable_set('file_downloads', FILE_DOWNLOADS_PRIVATE);
+    variable_set('file_directory_path', $file_directory_path);
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/system/files/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (private downloads, clean URLs, relative directory path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Private downloads with clean URLs, with an absolute directory path.
+    // Should yield exactly the same results as for a relative path.
+    variable_set('file_downloads', FILE_DOWNLOADS_PRIVATE);
+    variable_set('file_directory_path', DRUPAL_ROOT . '/' . $file_directory_path);
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/system/files/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (private downloads, clean URLs, absolute directory path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Private downloads with clean URLs, with an absolute directory path
+    // and an absolute file path, too.
+    $path = file_create_url(file_directory_path() . '/' . $destination);
+    $expected_url = $base_url . '/system/files/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (private downloads, clean URLs, absolute directory path and absolute file path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Private downloads with clean URLs, with an absolute directory path
+    // outside of the Drupal root directory. Should again be the same thing.
+    variable_set('file_downloads', FILE_DOWNLOADS_PRIVATE);
+    variable_set('file_directory_path', dirname(DRUPAL_ROOT) . '/outside-path');
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/system/files/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (public downloads, no clean URLs, absolute directory path outside of the Drupal root directory)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Public downloads with clean URLs, with a relative directory path.
+    // For public downloads, it should not matter whether clean URLs are
+    // enabled or not.
+    variable_set('file_downloads', FILE_DOWNLOADS_PUBLIC);
+    variable_set('file_directory_path', $file_directory_path);
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/' . $file_directory_path . '/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (public downloads, clean URLs, relative directory path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Public downloads with clean URLs, with an absolute directory path
+    // that is inside the Drupal root directory. file_create_url() should
+    // strip that root directory in order to return a valid URL.
+    variable_set('file_downloads', FILE_DOWNLOADS_PUBLIC);
+    variable_set('file_directory_path', DRUPAL_ROOT . '/' . $file_directory_path);
+    $path = file_create_url($destination);
+    $expected_url = $base_url . '/' . $file_directory_path . '/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (public downloads, clean URLs, absolute directory path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Public downloads with clean URLs, with an absolute directory path
+    // and an absolute file path, too.
+    $path = file_create_url(file_directory_path() . '/' . $destination);
+    $expected_url = $base_url . '/' . $file_directory_path . '/misc/xyz.txt';
+    $this->assertEqual($path, $expected_url, t('Created file URL %path equals %expected. (public downloads, clean URLs, absolute directory path and absolute file path)', array('%path' => $path, '%expected' => $expected_url)), 'File');
+
+    // Recover the original file_directory_path, see the top of this function.
+    variable_set('file_directory_path', $original_file_directory_path);
+  }*/
+}
+
+
+/**
  * Deletion related tests.
  */
 class FileUnmanagedDeleteTest extends FileTestCase {
