diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2008-10-09 00:02:29 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2008-10-09 00:02:29 +0000 |
commit | f841d1a764c4aa6aa6d2a58daa401be615f4e873 (patch) | |
tree | eac1b6d3c4325d926f06e036a337746d03edb9fc /modules | |
parent | 72e09d7beb7788a3a1f473c0d7a7a4802a5dc75a (diff) | |
download | brdo-f841d1a764c4aa6aa6d2a58daa401be615f4e873.tar.gz brdo-f841d1a764c4aa6aa6d2a58daa401be615f4e873.tar.bz2 |
#142995 by dopry, drewish, quicksketch, jpetso, and flobruit: Adding hook_file_X(). This is an enabler of lots and lots of goodies. See CHANGELOG.txt for more. Awesome work, guys. :)
Diffstat (limited to 'modules')
-rw-r--r-- | modules/aggregator/aggregator.test | 8 | ||||
-rw-r--r-- | modules/blogapi/blogapi.module | 2 | ||||
-rw-r--r-- | modules/color/color.module | 4 | ||||
-rw-r--r-- | modules/simpletest/simpletest.install | 2 | ||||
-rw-r--r-- | modules/simpletest/simpletest.module | 2 | ||||
-rw-r--r-- | modules/simpletest/tests/file.test | 561 | ||||
-rw-r--r-- | modules/simpletest/tests/file_test.module | 114 | ||||
-rw-r--r-- | modules/system/system.admin.inc | 4 | ||||
-rw-r--r-- | modules/system/system.module | 2 | ||||
-rw-r--r-- | modules/upload/upload.module | 134 | ||||
-rw-r--r-- | modules/upload/upload.test | 32 | ||||
-rw-r--r-- | modules/user/user.module | 6 |
12 files changed, 709 insertions, 162 deletions
diff --git a/modules/aggregator/aggregator.test b/modules/aggregator/aggregator.test index 282f0e868..b56e2cee7 100644 --- a/modules/aggregator/aggregator.test +++ b/modules/aggregator/aggregator.test @@ -151,7 +151,7 @@ class AggregatorTestCase extends DrupalWebTestCase { EOF; $path = file_directory_path() . '/valid-opml.xml'; - return file_save_data($opml, $path); + return file_unmanaged_save_data($opml, $path); } /** @@ -168,7 +168,7 @@ EOF; EOF; $path = file_directory_path() . '/invalid-opml.xml'; - return file_save_data($opml, $path); + return file_unmanaged_save_data($opml, $path); } /** @@ -190,7 +190,7 @@ EOF; EOF; $path = file_directory_path() . '/empty-opml.xml'; - return file_save_data($opml, $path); + return file_unmanaged_save_data($opml, $path); } function getRSS091Sample() { @@ -223,7 +223,7 @@ EOF; EOT; $path = file_directory_path() . '/rss091.xml'; - return file_save_data($feed, $path); + return file_unmanaged_save_data($feed, $path); } } diff --git a/modules/blogapi/blogapi.module b/modules/blogapi/blogapi.module index 9633c3816..99c1bb43e 100644 --- a/modules/blogapi/blogapi.module +++ b/modules/blogapi/blogapi.module @@ -385,7 +385,7 @@ function blogapi_metaweblog_new_media_object($blogid, $username, $password, $fil return blogapi_error(t('No file sent.')); } - if (!$filepath = file_save_data($data, $name)) { + if (!$filepath = file_unmanaged_save_data($data, $name)) { return blogapi_error(t('Error storing file.')); } diff --git a/modules/color/color.module b/modules/color/color.module index 92479c387..e99f907fc 100644 --- a/modules/color/color.module +++ b/modules/color/color.module @@ -308,7 +308,7 @@ function color_scheme_form_submit($form, &$form_state) { foreach ($info['copy'] as $file) { $base = basename($file); $source = $paths['source'] . $file; - $filepath = file_copy($source, $paths['target'] . $base); + $filepath = file_unmanaged_copy($source, $paths['target'] . $base); $paths['map'][$file] = $base; $paths['files'][] = $filepath; } @@ -435,7 +435,7 @@ function _color_rewrite_stylesheet($theme, &$info, &$paths, $palette, $style) { * Save the rewritten stylesheet to disk. */ function _color_save_stylesheet($file, $style, &$paths) { - $filepath = file_save_data($style, $file, FILE_EXISTS_REPLACE); + $filepath = file_unmanaged_save_data($style, $file, FILE_EXISTS_REPLACE); $paths['files'][] = $filepath; // Set standard file permissions for webserver-generated files. diff --git a/modules/simpletest/simpletest.install b/modules/simpletest/simpletest.install index 4d549399b..a1ceeb974 100644 --- a/modules/simpletest/simpletest.install +++ b/modules/simpletest/simpletest.install @@ -33,7 +33,7 @@ function simpletest_install() { $original = drupal_get_path('module', 'simpletest') . '/files'; $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/'); foreach ($files as $file) { - file_copy($file->filename, $path . '/' . $file->basename); + file_unmanaged_copy($file->filename, $path . '/' . $file->basename); } $generated = TRUE; } diff --git a/modules/simpletest/simpletest.module b/modules/simpletest/simpletest.module index f2e547fcd..80f47f54e 100644 --- a/modules/simpletest/simpletest.module +++ b/modules/simpletest/simpletest.module @@ -571,7 +571,7 @@ function simpletest_clean_temporary_directory($path) { simpletest_clean_temporary_directory($file_path); } else { - file_delete($file_path); + file_unmanaged_delete($file_path); } } } diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test index 5805080f1..2943949f3 100644 --- a/modules/simpletest/tests/file.test +++ b/modules/simpletest/tests/file.test @@ -76,7 +76,7 @@ class FileTestCase extends DrupalWebTestCase { $filepath = file_directory_path() . '/' . $this->randomName(); } - file_put_contents($filepath, ''); + file_put_contents($filepath, 'File put contents does not seem to appreciate empty strings so lets put in some data.'); $this->assertTrue(is_file($filepath), t('The test file exists on the disk.')); $file = new stdClass(); @@ -85,7 +85,8 @@ class FileTestCase extends DrupalWebTestCase { $file->filemime = 'text/plain'; $file->uid = 1; $file->timestamp = REQUEST_TIME; - $file->filesize = 0; + $file->filesize = filesize($file->filepath); + $file->status = FILE_STATUS_TEMPORARY; $this->assertNotIdentical(drupal_write_record('files', $file), FALSE, t('The file was added to the database.')); return $file; @@ -93,15 +94,55 @@ class FileTestCase extends DrupalWebTestCase { } /** + * Base class for file tests that use the file_test module to test uploads and + * hooks. + */ +class FileHookTestCase extends FileTestCase { + /** + * Implementation of setUp(). + */ + function setUp() { + // Install file_test module + parent::setUp('file_test'); + // Clear out any hook calls. + file_test_reset(); + } + + /** + * Assert that a hook_file_* hook was called a certain number of times. + * + * @param $hook + * String with the hook name, e.g. 'load', 'save', 'insert', etc. + * @param $expected_count + * Optional integer count. + * @param $message + * Optional translated string message. + */ + function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) { + $actual_count = count(file_test_get_calls($hook)); + + if (is_null($message)) { + if ($actual_count == $expected_count) { + $message = t('hook_file_@name was called correctly.', array('@name' => $hook)); + } + else { + $message = t('hook_file_@name was expected to be called %expected times but was called %actual times.', array('@name' => $hook, '%expected' => $expected_count, '%actual' => $actual_count)); + } + } + $this->assertEqual($actual_count, $expected_count, $message); + } +} + +/** * This will run tests against the file validation functions (file_validate_*). */ -class FileValidateTest extends DrupalWebTestCase { +class FileValidatorTest extends DrupalWebTestCase { /** * Implementation of getInfo(). */ function getInfo() { return array( - 'name' => t('File validation'), + 'name' => t('File validator tests'), 'description' => t('Tests the functions used to validate uploaded files.'), 'group' => t('File'), ); @@ -123,22 +164,6 @@ class FileValidateTest extends DrupalWebTestCase { } /** - * Test the file_validate() function. - */ - function testFileValidate() { - // Empty validators. - $this->assertEqual(file_validate($this->image, array()), array(), t('Validating an empty array works succesfully.')); - - // Use the file_test.module's test validator to ensure that passing tests - // return correctly. - $passing = array('file_test_validator' => array(array())); - $this->assertEqual(file_validate($this->image, $passing), array(), t('Validating passes.')); - - $failing = array('file_test_validator' => array(array('Failed', 'Badly'))); - $this->assertEqual(file_validate($this->image, $failing), array('Failed', 'Badly'), t('Validating returns errors.')); - } - - /** * Test the file_validate_extensions() function. */ function testFileValidateExtensions() { @@ -270,35 +295,37 @@ class FileValidateTest extends DrupalWebTestCase { } } + + /** - * Tests the file_save_data() function. + * Tests the file_unmanaged_save_data() function. */ -class FileSaveDataTest extends FileTestCase { +class FileUnmanagedSaveDataTest extends FileTestCase { /** * Implementation of getInfo(). */ function getInfo() { return array( - 'name' => t('File save'), - 'description' => t('Tests the file save data function.'), + 'name' => t('Unmanaged file save data'), + 'description' => t('Tests the unmanaged file save data function.'), 'group' => t('File'), ); } /** - * Test the file_save_data() function. + * Test the file_unmanaged_save_data() function. */ function testFileSaveData() { $contents = $this->randomName(8); // No filename. - $filepath = file_save_data($contents); + $filepath = file_unmanaged_save_data($contents); $this->assertTrue($filepath, t('Unnamed file saved correctly.')); $this->assertEqual(file_directory_path(), dirname($filepath), t("File was placed in Drupal's files directory.")); $this->assertEqual($contents, file_get_contents(realpath($filepath)), t('Contents of the file are correct.')); // Provide a filename. - $filepath = file_save_data($contents, 'asdf.txt', FILE_EXISTS_REPLACE); + $filepath = file_unmanaged_save_data($contents, 'asdf.txt', FILE_EXISTS_REPLACE); $this->assertTrue($filepath, t('Unnamed file saved correctly.')); $this->assertEqual(file_directory_path(), dirname($filepath), t("File was placed in Drupal's files directory.")); $this->assertEqual('asdf.txt', basename($filepath), t('File was named correctly.')); @@ -310,7 +337,7 @@ class FileSaveDataTest extends FileTestCase { /** * Test the file_save_upload() function. */ -class FileSaveUploadTest extends FileTestCase { +class FileSaveUploadTest extends FileHookTestCase { /** * Implementation of getInfo(). */ @@ -323,19 +350,10 @@ class FileSaveUploadTest extends FileTestCase { } /** - * Implementation of setUp(). - */ - function setUp() { - // Need to enable the test module for an upload target. - parent::setUp('file_test'); - } - - /** * Test the file_save_upload() function. */ function testFileSaveUpload() { $max_fid_before = db_result(db_query('SELECT MAX(fid) AS fid FROM {files}')); - $upload_user = $this->drupalCreateUser(array('access content')); $this->drupalLogin($upload_user); @@ -345,12 +363,13 @@ class FileSaveUploadTest extends FileTestCase { $this->drupalPost('file-test/upload', $edit, t('Submit')); $this->assertResponse(200, t('Received a 200 response for posted test file.')); + // We can't easily check that the hooks were called but since + // file_save_upload() calles file_save() we can rely on file_save()'s + // test to catch problems invoking the hooks. + $max_fid_after = db_result(db_query('SELECT MAX(fid) AS fid FROM {files}')); $this->assertTrue($max_fid_after > $max_fid_before, t('A new file was created.')); - - // FIXME: Replace with file_load() once the hook_file patch gets committed. - $file = db_fetch_object(db_query('SELECT f.* FROM {files} f WHERE f.fid = %d', array($max_fid_after))); - $this->assertTrue($file, t('Loaded the file.')); + $this->assertTrue(file_load($max_fid_after), t('Loaded the file.')); } } @@ -524,14 +543,14 @@ class FileDirectoryTest extends FileTestCase { /** * Deletion related tests. */ -class FileDeleteTest extends FileTestCase { +class FileUnmanagedDeleteTest extends FileTestCase { /** * Implementation of getInfo(). */ function getInfo() { return array( - 'name' => t('File delete'), - 'description' => t('Tests the file delete function.'), + 'name' => t('Unmanaged file delete'), + 'description' => t('Tests the unmanaged file delete function.'), 'group' => t('File'), ); } @@ -544,7 +563,7 @@ class FileDeleteTest extends FileTestCase { $file = $this->createFile(); // Delete a regular file - $this->assertTrue(file_delete($file->filepath), t('Deleted worked.')); + $this->assertTrue(file_unmanaged_delete($file->filepath), t('Deleted worked.')); $this->assertFalse(file_exists($file->filepath), t('Test file has actually been deleted.')); } @@ -553,7 +572,7 @@ class FileDeleteTest extends FileTestCase { */ function testMissing() { // Try to delete a non-existing file - $this->assertTrue(file_delete(file_directory_path() . '/' . $this->randomName()), t('Returns true when deleting a non-existant file.')); + $this->assertTrue(file_unmanaged_delete(file_directory_path() . '/' . $this->randomName()), t('Returns true when deleting a non-existant file.')); } /** @@ -561,26 +580,26 @@ class FileDeleteTest extends FileTestCase { */ function testDirectory() { // A directory to operate on. - $this->dirname = $this->createDirectory(); + $directory = $this->createDirectory(); // Try to delete a directory - $this->assertFalse(file_delete($this->dirname), t('Could not delete the delete directory.')); - $this->assertTrue(file_exists($this->dirname), t('Directory has not been deleted.')); + $this->assertFalse(file_unmanaged_delete($directory), t('Could not delete the delete directory.')); + $this->assertTrue(file_exists($directory), t('Directory has not been deleted.')); } } /** - * Move related tests + * Unmanaged move related tests. */ -class FileMoveTest extends FileTestCase { +class FileUnmanagedMoveTest extends FileTestCase { /** * Implementation of getInfo(). */ function getInfo() { return array( - 'name' => t('File moving'), - 'description' => t('Tests the file move function.'), + 'name' => t('Unmanaged file moving'), + 'description' => t('Tests the unmanaged file move function.'), 'group' => t('File'), ); } @@ -594,7 +613,7 @@ class FileMoveTest extends FileTestCase { // Moving to a new name. $desired_filepath = file_directory_path() . '/' . $this->randomName(); - $new_filepath = file_move($file->filepath, $desired_filepath, FILE_EXISTS_ERROR); + $new_filepath = file_unmanaged_move($file->filepath, $desired_filepath, FILE_EXISTS_ERROR); $this->assertTrue($new_filepath, t('Move was successful.')); $this->assertEqual($new_filepath, $desired_filepath, t('Returned expected filepath.')); $this->assertTrue(file_exists($new_filepath), t('File exists at the new location.')); @@ -605,7 +624,7 @@ class FileMoveTest extends FileTestCase { $desired_filepath = file_directory_path() . '/' . $this->randomName(); $this->assertTrue(file_exists($new_filepath), t('File exists before moving.')); $this->assertTrue(file_put_contents($desired_filepath, ' '), t('Created a file so a rename will have to happen.')); - $newer_filepath = file_move($new_filepath, $desired_filepath, FILE_EXISTS_RENAME); + $newer_filepath = file_unmanaged_move($new_filepath, $desired_filepath, FILE_EXISTS_RENAME); $this->assertTrue($newer_filepath, t('Move was successful.')); $this->assertNotEqual($newer_filepath, $desired_filepath, t('Returned expected filepath.')); $this->assertTrue(file_exists($newer_filepath), t('File exists at the new location.')); @@ -620,7 +639,7 @@ class FileMoveTest extends FileTestCase { */ function testMissing() { // Move non-existant file. - $new_filepath = file_move($this->randomName(), $this->randomName()); + $new_filepath = file_unmanaged_move($this->randomName(), $this->randomName()); $this->assertFalse($new_filepath, t('Moving a missing file fails.')); } @@ -632,12 +651,12 @@ class FileMoveTest extends FileTestCase { $file = $this->createFile(); // Move the file onto itself without renaming shouldn't make changes. - $new_filepath = file_move($file->filepath, $file->filepath, FILE_EXISTS_REPLACE); + $new_filepath = file_unmanaged_move($file->filepath, $file->filepath, FILE_EXISTS_REPLACE); $this->assertFalse($new_filepath, t('Moving onto itself without renaming fails.')); $this->assertTrue(file_exists($file->filepath), t('File exists after moving onto itself.')); // Move the file onto itself with renaming will result in a new filename. - $new_filepath = file_move($file->filepath, $file->filepath, FILE_EXISTS_RENAME); + $new_filepath = file_unmanaged_move($file->filepath, $file->filepath, FILE_EXISTS_RENAME); $this->assertTrue($new_filepath, t('Moving onto itself with renaming works.')); $this->assertFalse(file_exists($file->filepath), t('Original file has been removed.')); $this->assertTrue(file_exists($new_filepath), t('File exists after moving onto itself.')); @@ -646,16 +665,16 @@ class FileMoveTest extends FileTestCase { /** - * Copy related tests. + * Unmanaged copy related tests. */ -class FileCopyTest extends FileTestCase { +class FileUnmanagedCopyTest extends FileTestCase { /** * Implementation of getInfo(). */ function getInfo() { return array( - 'name' => t('File copying'), - 'description' => t('Tests the file copy function.'), + 'name' => t('Unmanaged file copying'), + 'description' => t('Tests the unmanaged file copy function.'), 'group' => t('File'), ); } @@ -665,24 +684,24 @@ class FileCopyTest extends FileTestCase { */ function testNormal() { // Create a file for testing - $this->file = $this->createFile(); + $file = $this->createFile(); // Copying to a new name. $desired_filepath = file_directory_path() . '/' . $this->randomName(); - $new_filepath = file_copy($this->file->filepath, $desired_filepath, FILE_EXISTS_ERROR); + $new_filepath = file_unmanaged_copy($file->filepath, $desired_filepath, FILE_EXISTS_ERROR); $this->assertTrue($new_filepath, t('Copy was successful.')); $this->assertEqual($new_filepath, $desired_filepath, t('Returned expected filepath.')); - $this->assertTrue(file_exists($this->file->filepath), t('Original file remains.')); + $this->assertTrue(file_exists($file->filepath), t('Original file remains.')); $this->assertTrue(file_exists($new_filepath), t('New file exists.')); $this->assertFilePermissions($new_filepath, 0664); // Copying with rename. $desired_filepath = file_directory_path() . '/' . $this->randomName(); $this->assertTrue(file_put_contents($desired_filepath, ' '), t('Created a file so a rename will have to happen.')); - $newer_filepath = file_copy($new_filepath, $desired_filepath, FILE_EXISTS_RENAME); + $newer_filepath = file_unmanaged_copy($new_filepath, $desired_filepath, FILE_EXISTS_RENAME); $this->assertTrue($newer_filepath, t('Copy was successful.')); $this->assertNotEqual($newer_filepath, $desired_filepath, t('Returned expected filepath.')); - $this->assertTrue(file_exists($this->file->filepath), t('Original file remains.')); + $this->assertTrue(file_exists($file->filepath), t('Original file remains.')); $this->assertTrue(file_exists($new_filepath), t('New file exists.')); $this->assertFilePermissions($new_filepath, 0664); @@ -696,7 +715,7 @@ class FileCopyTest extends FileTestCase { // Copy non-existant file $desired_filepath = $this->randomName(); $this->assertFalse(file_exists($desired_filepath), t("Randomly named file doesn't exists.")); - $new_filepath = file_copy($desired_filepath, $this->randomName()); + $new_filepath = file_unmanaged_copy($desired_filepath, $this->randomName()); $this->assertFalse($new_filepath, t('Copying a missing file fails.')); } @@ -705,31 +724,415 @@ class FileCopyTest extends FileTestCase { */ function testOverwriteSelf() { // Create a file for testing - $this->file = $this->createFile(); + $file = $this->createFile(); // Copy the file onto itself with renaming works. - $new_filepath = file_copy($this->file->filepath, $this->file->filepath, FILE_EXISTS_RENAME); + $new_filepath = file_unmanaged_copy($file->filepath, $file->filepath, FILE_EXISTS_RENAME); $this->assertTrue($new_filepath, t('Copying onto itself with renaming works.')); - $this->assertNotEqual($new_filepath, $this->file->filepath, t('Copied file has a new name.')); - $this->assertTrue(file_exists($this->file->filepath), t('Original file exists after copying onto itself.')); + $this->assertNotEqual($new_filepath, $file->filepath, t('Copied file has a new name.')); + $this->assertTrue(file_exists($file->filepath), t('Original file exists after copying onto itself.')); $this->assertTrue(file_exists($new_filepath), t('Copied file exists after copying onto itself.')); // Copy the file onto itself without renaming fails. - $new_filepath = file_copy($this->file->filepath, $this->file->filepath, FILE_EXISTS_ERROR); + $new_filepath = file_unmanaged_copy($file->filepath, $file->filepath, FILE_EXISTS_ERROR); $this->assertFalse($new_filepath, t('Copying onto itself without renaming fails.')); - $this->assertTrue(file_exists($this->file->filepath), t('File exists after copying onto itself.')); + $this->assertTrue(file_exists($file->filepath), t('File exists after copying onto itself.')); // Copy the file into same directory without renaming fails. - $new_filepath = file_copy($this->file->filepath, dirname($this->file->filepath), FILE_EXISTS_ERROR); + $new_filepath = file_unmanaged_copy($file->filepath, dirname($file->filepath), FILE_EXISTS_ERROR); $this->assertFalse($new_filepath, t('Copying onto itself fails.')); - $this->assertTrue(file_exists($this->file->filepath), t('File exists after copying onto itself.')); + $this->assertTrue(file_exists($file->filepath), t('File exists after copying onto itself.')); // Copy the file into same directory with renaming works. - $new_filepath = file_copy($this->file->filepath, dirname($this->file->filepath), FILE_EXISTS_RENAME); + $new_filepath = file_unmanaged_copy($file->filepath, dirname($file->filepath), FILE_EXISTS_RENAME); $this->assertTrue($new_filepath, t('Copying into same directory works.')); - $this->assertNotEqual($new_filepath, $this->file->filepath, t('Copied file has a new name.')); - $this->assertTrue(file_exists($this->file->filepath), t('Original file exists after copying onto itself.')); + $this->assertNotEqual($new_filepath, $file->filepath, t('Copied file has a new name.')); + $this->assertTrue(file_exists($file->filepath), t('Original file exists after copying onto itself.')); $this->assertTrue(file_exists($new_filepath), t('Copied file exists after copying onto itself.')); } } + + +/** + * Deletion related tests. + */ +class FileDeleteTest extends FileHookTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('File delete'), + 'description' => t('Tests the file delete function.'), + 'group' => t('File'), + ); + } + + /** + * Try deleting a normal file (as opposed to a directory, symlink, etc). + */ + function testNormal() { + $file = $this->createFile(); + + // Check that deletion removes the file and database record. + $this->assertTrue(is_file($file->filepath), t("File exists.")); + $this->assertIdentical(file_delete($file), TRUE, t("Delete worked.")); + $this->assertFileHookCalled('references'); + $this->assertFileHookCalled('delete'); + $this->assertFalse(file_exists($file->filepath), t("Test file has actually been deleted.")); + $this->assertFalse(file_load(array('filepath' => $file->filepath)), t("File was removed from the database.")); + + // TODO: implement hook_file_references() in file_test.module and report a + // file in use and test the $force parameter. + } +} + + +/** + * Move related tests + */ +class FileMoveTest extends FileHookTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('File moving'), + 'description' => t('Tests the file move function.'), + 'group' => t('File'), + ); + } + + /** + * Move a normal file. + */ + function testNormal() { + $file = $this->createFile(); + $desired_filepath = file_directory_path() . '/' . $this->randomName(); + + $file = file_move(clone $file, $desired_filepath, FILE_EXISTS_ERROR); + $this->assertTrue($file, t("File moved sucessfully.")); + $this->assertFileHookCalled('move'); + $this->assertFileHookCalled('update'); + $this->assertEqual($file->fid, $file->fid, t("File id $file->fid is unchanged after move.")); + + $loaded_file = file_load($file->fid, TRUE); + $this->assertTrue($loaded_file, t("File can be loaded from the database.")); + $this->assertEqual($file->filename, $loaded_file->filename, t("File name was updated correctly in the database.")); + $this->assertEqual($file->filepath, $loaded_file->filepath, t("File path was updated correctly in the database.")); + } +} + + +/** + * Copy related tests. + */ +class FileCopyTest extends FileHookTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('File copying'), + 'description' => t('Tests the file copy function.'), + 'group' => t('File'), + ); + } + + /** + * Test copying a normal file. + */ + function testNormal() { + $source_file = $this->createFile(); + $desired_filepath = file_directory_path() . '/' . $this->randomName(); + + $file = file_copy(clone $source_file, $desired_filepath, FILE_EXISTS_ERROR); + $this->assertTrue($file, t("File copied sucessfully.")); + $this->assertFileHookCalled('copy'); + $this->assertFileHookCalled('insert'); + $this->assertNotEqual($source_file->fid, $file->fid, t("A new file id was created.")); + $this->assertNotEqual($source_file->filepath, $file->filepath, t("A new filepath was created.")); + $this->assertEqual($file->filepath, $desired_filepath, t('The copied file object has the desired filepath.')); + $this->assertTrue(file_exists($source_file->filepath), t('The original file still exists.')); + $this->assertTrue(file_exists($file->filepath), t('The copied file exists.')); + + // Check that the changes were actually saved to the database. + $loaded_file = file_load($file->fid, TRUE); + $this->assertTrue($loaded_file, t("File can be loaded from the database.")); + $this->assertEqual($file->filename, $loaded_file->filename, t("File name was updated correctly in the database.")); + $this->assertEqual($file->filepath, $loaded_file->filepath, t("File path was updated correctly in the database.")); + } +} + + +/** + * Tests the file_load() function. + */ +class FileLoadTest extends FileHookTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('File loading'), + 'description' => t('Tests the file_load() function.'), + 'group' => t('File'), + ); + } + + /** + * Try to load a non-existant file by fid. + */ + function testLoadMissingFid() { + $this->assertFalse(file_load(-1), t("Try to load an invalid fid fails.")); + $this->assertFileHookCalled('load', 0); + } + + /** + * Try to load a non-existant file by filepath. + */ + function testLoadMissingFilepath() { + $this->assertFalse(file_load(array('filepath' => 'misc/druplicon.png')), t("Try to load a file that doesn't exist in the database fails.")); + $this->assertFileHookCalled('load', 0); + } + + /** + * Try to load a non-existant file by status. + */ + function testLoadInvalidStatus() { + $this->assertFalse(file_load(array('status' => -99)), t("Trying to load a file with an invalid status fails.")); + $this->assertFileHookCalled('load', 0); + } + + /** + * This will test lading file data from the database. + */ + function testFileLoad() { + // Create a new file object. + $file = array( + 'uid' => 1, + 'filename' => 'druplicon.png', + 'filepath' => 'misc/druplicon.png', + 'filemime' => 'image/png', + 'timestamp' => 1, + 'status' => FILE_STATUS_PERMANENT, + ); + $file = file_save($file); + + // Load by path. + file_test_reset(); + $by_path_file = file_load(array('filepath' => $file->filepath)); + $this->assertFileHookCalled('load'); + $this->assertTrue($by_path_file->file_test['loaded'], t('file_test_file_load() was able to modify the file during load.')); + $this->assertEqual($by_path_file->fid, $file->fid, t("Loading by filepath got the correct fid."), 'File'); + + // Load by fid. + file_test_reset(); + $by_fid_file = file_load($file->fid); + $this->assertFileHookCalled('load', 0); + $this->assertTrue($by_fid_file->file_test['loaded'], t('file_test_file_load() was able to modify the file during load.')); + $this->assertEqual($by_fid_file->filepath, $file->filepath, t("Loading by fid got the correct filepath."), 'File'); + + // Load again by fid but use the reset param to reload. + file_test_reset(); + $by_fid_file = file_load($file->fid, TRUE); + $this->assertFileHookCalled('load'); + $this->assertEqual($by_fid_file->filepath, $file->filepath, t("Loading by fid got the correct filepath."), 'File'); + } +} +/** + * Tests the file_save() function. + */ +class FileSaveTest extends FileHookTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('File saving'), + 'description' => t('Tests the file_save() function.'), + 'group' => t('File'), + ); + } + + function testFileSave() { + // Create a new file object. + $file = array( + 'uid' => 1, + 'filename' => 'druplicon.png', + 'filepath' => 'misc/druplicon.png', + 'filemime' => 'image/png', + 'timestamp' => 1, + 'status' => FILE_STATUS_PERMANENT, + ); + $file = (object) $file; + + // Save it, inserting a new record. + $saved_file = file_save($file); + $this->assertFileHookCalled('insert'); + $this->assertNotNull($saved_file, t("Saving the file should give us back a file object."), 'File'); + $this->assertTrue($saved_file->fid > 0, t("A new file ID is set when saving a new file to the database."), 'File'); + $this->assertEqual(db_result(db_query('SELECT COUNT(*) FROM {files} f WHERE f.fid = %d', array($saved_file->fid))), 1, t("Record exists in the database.")); + $this->assertEqual($saved_file->filesize, filesize($file->filepath), t("File size was set correctly."), 'File'); + $this->assertTrue($saved_file->timestamp > 1, t("File size was set correctly."), 'File'); + + // Resave the file, updating the existing record. + file_test_reset(); + $resaved_file = file_save($saved_file); + $this->assertFileHookCalled('update'); + $this->assertEqual($resaved_file->fid, $saved_file->fid, t("The file ID of an existing file is not changed when updating the database."), 'File'); + $this->assertTrue($resaved_file->timestamp >= $saved_file->timestamp, t("Timestamp didn't go backwards."), 'File'); + $count = db_result(db_query('SELECT COUNT(*) FROM {files} f WHERE f.fid = %d', array($saved_file->fid))); + $this->assertEqual($count, 1, t("Record still exists in the database."), 'File'); + } +} + + +/** + * Tests the file_validate() function.. + */ +class FileValidateTest extends FileHookTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('File validate'), + 'description' => t('Tests the file_validate() function.'), + 'group' => t('File'), + ); + } + + /** + * Test that the validators passed into are checked. + */ + function testCallerValidation() { + $file = $this->createFile(); + + // Empty validators. + $this->assertEqual(file_validate($file, array()), array(), t('Validating an empty array works succesfully.')); + $this->assertFileHookCalled('validate', 1); + + // Use the file_test.module's test validator to ensure that passing tests + // return correctly. + file_test_reset(); + $GLOBALS['file_test_hook_return']['validate'] = array(); + $passing = array('file_test_validator' => array(array())); + $this->assertEqual(file_validate($file, $passing), array(), t('Validating passes.')); + $this->assertFileHookCalled('validate', 1); + + // Now test for failures in validators passed in and by hook_validate. + file_test_reset(); + $GLOBALS['file_test_hook_return']['validate'] = array('Epic fail'); + $failing = array('file_test_validator' => array(array('Failed', 'Badly'))); + $this->assertEqual(file_validate($file, $failing), array('Failed', 'Badly', 'Epic fail'), t('Validating returns errors.')); + $this->assertFileHookCalled('validate', 1); + } +} + +/** + * Tests the file_set_status() function. + */ +class FileSetStatusTest extends FileHookTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('File set status'), + 'description' => t('Tests the file set status functions.'), + 'group' => t('File'), + ); + } + + /** + * Test the file_set_status() function. + */ + function testFileSetStatus() { + // Create a new file object. + $file = array( + 'uid' => 1, + 'filename' => 'druplicon.png', + 'filepath' => 'misc/druplicon.png', + 'filemime' => 'image/png', + 'timestamp' => 1, + 'status' => FILE_STATUS_TEMPORARY, + ); + $file = file_save($file); + // Just a couple of sanity checks before we start the real testing. + $this->assertTrue($file->fid, t("Make sure the file saved correctly.")); + $this->assertEqual($file->status, FILE_STATUS_TEMPORARY, t("Status was set during save.")); + + // Change the status and make sure everything works + file_test_reset(); + $returned = file_set_status($file); + $this->assertEqual(count(file_test_get_calls('status')), 1, t('hook_file_status was called.')); + $this->assertNotIdentical($returned, FALSE, t("file_set_status() worked and returned a non-false value.")); + $this->assertEqual($returned->fid, $file->fid, t("Returned the correct file.")); + $this->assertEqual($returned->status, FILE_STATUS_PERMANENT, t("File's status was changed.")); + + // Try it resetting it to the same value. + file_test_reset(); + $returned = file_set_status($file, FILE_STATUS_PERMANENT); + $this->assertEqual(count(file_test_get_calls('status')), 0, t('hook_file_status was not called.')); + $this->assertIdentical($returned, FALSE, t("file_set_status() failed since there was no change.")); + $test_file = file_load($file->fid); + $this->assertEqual($test_file->fid, $file->fid , t("Loaded the correct file.")); + $this->assertEqual($test_file->status, FILE_STATUS_PERMANENT, t("File's status is correct.")); + + // Now switch it. + file_test_reset(); + $returned = file_set_status($file, FILE_STATUS_TEMPORARY); + $this->assertEqual(count(file_test_get_calls('status')), 1, t('hook_file_status was called.')); + $this->assertNotIdentical($returned, FALSE, t("file_set_status() worked and returned a non-false value.")); + $this->assertEqual($returned->fid, $file->fid , t("Returned the correct file.")); + $this->assertEqual($returned->status, FILE_STATUS_TEMPORARY, t("File's status is correct.")); + } +} + +/** + * Tests the file_save_data() function. + */ +class FileSaveDataTest extends FileHookTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('File save data'), + 'description' => t('Tests the file save data function.'), + 'group' => t('File'), + ); + } + + /** + * Test the file_save_data() function. + */ + function testFileSaveData() { + $contents = $this->randomName(8); + + // No filename. + $file = file_save_data($contents); + $this->assertTrue($file, t("Unnamed file saved correctly.")); + $this->assertEqual(file_directory_path(), dirname($file->filepath), t("File was placed in Drupal's files directory.")); + $this->assertEqual($contents, file_get_contents(realpath($file->filepath)), t("Contents of the file are correct.")); + $this->assertEqual($file->filemime, 'application/octet-stream', t("A MIME type was set.")); + $this->assertEqual($file->status, FILE_STATUS_PERMANENT, t("The file's status was set to permanent.")); + + // Try loading the file. + $loaded_file = file_load($file->fid); + $this->assertTrue($loaded_file, t("File loaded from database.")); + + // Provide a filename. + $file = file_save_data($contents, 'asdf.txt', FILE_EXISTS_REPLACE); + $this->assertTrue($file, t("Unnamed file saved correctly.")); + $this->assertEqual(file_directory_path(), dirname($file->filepath), t("File was placed in Drupal's files directory.")); + $this->assertEqual('asdf.txt', basename($file->filepath), t("File was named correctly.")); + $this->assertEqual($contents, file_get_contents(realpath($file->filepath)), t("Contents of the file are correct.")); + + // Check the overwrite error. + $file = file_save_data($contents, 'asdf.txt', FILE_EXISTS_ERROR); + $this->assertFalse($file, t("Overwriting a file fails when FILE_EXISTS_ERROR is specified.")); + } +}
\ No newline at end of file diff --git a/modules/simpletest/tests/file_test.module b/modules/simpletest/tests/file_test.module index b48129547..fd2109485 100644 --- a/modules/simpletest/tests/file_test.module +++ b/modules/simpletest/tests/file_test.module @@ -50,3 +50,117 @@ function _file_test_form_submit(&$form, &$form_state) { drupal_set_message(t('Epic upload FAIL!'), 'error'); } } + + +/** + * Reset/initialize the history of calls to the file_* hooks. + */ +function file_test_reset() { + // Keep track of calls to these hooks + $GLOBALS['file_test_results'] = array( + 'load' => array(), + 'validate' => array(), + 'download' => array(), + 'references' => array(), + 'status' => array(), + 'insert' => array(), + 'update' => array(), + 'copy' => array(), + 'move' => array(), + 'delete' => array(), + ); + + // These hooks will return these values. + $GLOBALS['file_test_hook_return'] = array( + 'validate' => NULL, + 'download' => NULL, + 'references' => NULL, + ); +} + +/** + * Get the values passed to a the hook calls for a given operation. + * + * @param $op One of the hook_file_* operations. + * @returns Array of the parameters passed to each call. + */ +function file_test_get_calls($op) { + return $GLOBALS['file_test_results'][$op]; +} + +/** + * Implementation of hook_file_load(). + */ +function file_test_file_load(&$file) { + $GLOBALS['file_test_results']['load'][] = func_get_args(); + // Assign a value on the object so that we can test that the $file is passed + // by reference. + $file->file_test['loaded'] = TRUE; +} + +/** + * Implementation of hook_file_validate(). + */ +function file_test_file_validate(&$file) { + $GLOBALS['file_test_results']['validate'][] = func_get_args(); + return $GLOBALS['file_test_hook_return']['validate']; +} + +/** + * Implementation of hook_file_status(). + */ +function file_test_file_status(&$file) { + $GLOBALS['file_test_results']['status'][] = func_get_args(); +} + +/** + * Implementation of hook_file_download(). + */ +function file_test_file_download(&$file) { + $GLOBALS['file_test_results']['download'][] = func_get_args(); + return $GLOBALS['file_test_hook_return']['download']; +} + +/** + * Implementation of hook_file_references(). + */ +function file_test_file_references(&$file) { + $GLOBALS['file_test_results']['references'][] = func_get_args(); + return $GLOBALS['file_test_hook_return']['references']; +} + +/** + * Implementation of hook_file_insert(). + */ +function file_test_file_insert(&$file) { + $GLOBALS['file_test_results']['insert'][] = func_get_args(); +} + +/** + * Implementation of hook_file_update(). + */ +function file_test_file_update(&$file) { + $GLOBALS['file_test_results']['update'][] = func_get_args(); +} + +/** + * Implemenation of hook_file_copy(). + */ +function file_test_file_copy(&$file, &$source) { + $GLOBALS['file_test_results']['copy'][] = func_get_args(); +} + +/** + * Implemenation of hook_file_move(). + */ +function file_test_file_move(&$file, &$source) { + $GLOBALS['file_test_results']['move'][] = func_get_args(); +} + +/** + * Implementation of hook_file_delete(). + */ +function file_test_file_delete(&$file) { + $GLOBALS['file_test_results']['delete'][] = func_get_args(); +} + diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc index fead5c2fc..9ef4fcf45 100644 --- a/modules/system/system.admin.inc +++ b/modules/system/system.admin.inc @@ -338,7 +338,7 @@ function system_theme_settings(&$form_state, $key = '') { // The image was saved using file_save_upload() and was added to the // files table as a temporary file. We'll make a copy and let the garbage // collector delete the original upload. - if ($filepath = file_copy($file->filepath, $filename, FILE_EXISTS_REPLACE)) { + if ($filepath = file_unmanaged_copy($file->filepath, $filename, FILE_EXISTS_REPLACE)) { $_POST['default_logo'] = 0; $_POST['logo_path'] = $filepath; $_POST['toggle_logo'] = 1; @@ -353,7 +353,7 @@ function system_theme_settings(&$form_state, $key = '') { // The image was saved using file_save_upload() and was added to the // files table as a temporary file. We'll make a copy and let the garbage // collector delete the original upload. - if ($filepath = file_copy($file->filepath, $filename, FILE_EXISTS_REPLACE)) { + if ($filepath = file_unmanaged_copy($file->filepath, $filename, FILE_EXISTS_REPLACE)) { $_POST['default_favicon'] = 0; $_POST['favicon_path'] = $filepath; $_POST['toggle_favicon'] = 1; diff --git a/modules/system/system.module b/modules/system/system.module index e4d9abea0..8f533b20f 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -1405,7 +1405,7 @@ function system_cron() { if (file_exists($file->filepath)) { // If files that exist cannot be deleted, continue so the database remains // consistent. - if (!file_delete($file->filepath)) { + if (!file_delete($file)) { watchdog('file system', 'Could not delete temporary file "%path" during garbage collection', array('%path' => $file->filepath), WATCHDOG_ERROR); continue; } diff --git a/modules/upload/upload.module b/modules/upload/upload.module index 7f989bfb9..3e4049f63 100644 --- a/modules/upload/upload.module +++ b/modules/upload/upload.module @@ -263,6 +263,38 @@ function upload_form_alter(&$form, $form_state, $form_id) { } /** + * Implementation of hook_file_load(). + */ +function upload_file_load(&$file) { + // Add the upload specific data into the file object. + $values = db_query('SELECT * FROM {upload} u WHERE u.fid = :fid', array(':fid' => $file->fid))->fetch(PDO::FETCH_ASSOC); + foreach ((array)$values as $key => $value) { + $file->{$key} = $value; + } +} + +/** + * Implementation of hook_file_references(). + */ +function upload_file_references(&$file) { + // If upload.module is still using a file, do not let other modules delete it. + $count = db_query('SELECT COUNT(*) FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid))->fetchField(); + if ($count) { + // Return the name of the module and how many references it has to the file. + return array('upload' => $count); + } +} + +/** + * Implementation of hook_file_delete(). + */ +function upload_file_delete(&$file) { + // Delete all information associated with the file. + db_delete('upload')->condition('fid', $file->fid)->execute(); +} + + +/** * Implementation of hook_nodeapi_load(). */ function upload_nodeapi_load(&$node, $teaser) { @@ -289,7 +321,7 @@ function upload_nodeapi_view(&$node, $teaser) { } } } - + /** * Implementation of hook_nodeapi_prepare(). */ @@ -325,23 +357,35 @@ function upload_nodeapi_update(&$node, $teaser) { * Implementation of hook_nodeapi_delete(). */ function upload_nodeapi_delete(&$node, $teaser) { - upload_delete($node); + db_delete('upload')->condition('nid', $node->nid)->execute(); + if (!is_array($node->files)) { + return; + } + foreach($node->files as $file) { + file_delete($file); + } } /** * Implementation of hook_nodeapi_delete_revision(). */ function upload_nodeapi_delete_revision(&$node, $teaser) { - upload_delete_revision($node); + db_delete('upload')->condition('vid', $node->vid)->execute(); + if (!is_array($node->files)) { + return; + } + foreach ($node->files as $file) { + file_delete($file); + } } - + /** * Implementation of hook_nodeapi_search_result(). */ function upload_nodeapi_search_result(&$node, $teaser) { return isset($node->files) && is_array($node->files) ? format_plural(count($node->files), '1 attachment', '@count attachments') : NULL; } - + /** * Implementation of hook_nodeapi_rss_item(). */ @@ -370,7 +414,7 @@ function upload_nodeapi_rss_item(&$node, $teaser) { } return array(); } - + /** * Displays file attachments in table * @@ -426,15 +470,10 @@ function upload_save(&$node) { // Remove file. Process removals first since no further processing // will be required. if (!empty($file->remove)) { - db_query('DELETE FROM {upload} WHERE fid = %d AND vid = %d', $fid, $node->vid); - - // If the file isn't used by any other revisions delete it. - $count = db_result(db_query('SELECT COUNT(fid) FROM {upload} WHERE fid = %d', $fid)); - if ($count < 1) { - file_delete($file->filepath); - db_query('DELETE FROM {files} WHERE fid = %d', $fid); - } - + // Remove the reference from this revision. + db_delete('upload')->condition('fid', $file->fid)->condition('vid', $node->vid)->execute(); + // Try a soft delete, if the file isn't used elsewhere it'll be deleted. + file_delete($file); // Remove it from the session in the case of new uploads, // that you want to disassociate before node submission. unset($_SESSION['upload_files'][$fid]); @@ -457,41 +496,6 @@ function upload_save(&$node) { unset($_SESSION['upload_files']); } -function upload_delete($node) { - $files = array(); - $result = db_query('SELECT DISTINCT f.* FROM {upload} u INNER JOIN {files} f ON u.fid = f.fid WHERE u.nid = %d', $node->nid); - while ($file = db_fetch_object($result)) { - $files[$file->fid] = $file; - } - - foreach ($files as $fid => $file) { - // Delete all files associated with the node - db_query('DELETE FROM {files} WHERE fid = %d', $fid); - file_delete($file->filepath); - } - - // Delete all file revision information associated with the node - db_query('DELETE FROM {upload} WHERE nid = %d', $node->nid); -} - -function upload_delete_revision($node) { - if (is_array($node->files)) { - foreach ($node->files as $file) { - // Check if the file will be used after this revision is deleted - $count = db_result(db_query('SELECT COUNT(fid) FROM {upload} WHERE fid = %d', $file->fid)); - - // if the file won't be used, delete it - if ($count < 2) { - db_query('DELETE FROM {files} WHERE fid = %d', $file->fid); - file_delete($file->filepath); - } - } - } - - // delete the revision - db_query('DELETE FROM {upload} WHERE vid = %d', $node->vid); -} - function _upload_form($node) { global $user; @@ -503,11 +507,11 @@ function _upload_form($node) { if (!empty($node->files) && is_array($node->files)) { $form['files']['#theme'] = 'upload_form_current'; $form['files']['#tree'] = TRUE; - foreach ($node->files as $key => $file) { + foreach ($node->files as $file) { $file = (object)$file; - $description = file_create_url($file->filepath); - $description = "<small>" . check_plain($description) . "</small>"; - $form['files'][$key]['description'] = array('#type' => 'textfield', '#default_value' => !empty($file->description) ? $file->description : $file->filename, '#maxlength' => 256, '#description' => $description ); + $key = $file->fid; + + $form['files'][$key]['description'] = array('#type' => 'textfield', '#default_value' => !empty($file->description) ? $file->description : $file->filename, '#maxlength' => 256, '#description' => '<small>' . file_create_url($file->filepath) . '</small>'); $form['files'][$key]['size'] = array('#markup' => format_size($file->filesize)); $form['files'][$key]['remove'] = array('#type' => 'checkbox', '#default_value' => !empty($file->remove)); $form['files'][$key]['list'] = array('#type' => 'checkbox', '#default_value' => $file->list); @@ -522,12 +526,26 @@ function _upload_form($node) { if (user_access('upload files')) { $limits = _upload_file_limits($user); + + $limit_description = t('The maximum size of file uploads is %filesize. ', array('%filesize' => format_size($limits['file_size']))); + if (!empty($limits['resolution'])) { + if (image_get_toolkit()) { + $limit_description .= t('Images larger than %resolution will be resized. ', array('%resolution' => $limits['resolution'])); + } + else { + $limit_description .= t('Images may not be larger than %resolution. ', array('%resolution' => $limits['resolution'])); + } + } + if ($user->uid != 1) { + $limit_description .= t('Only files with the following extensions may be uploaded: %extensions. ', array('%extensions' => $limits['extensions'])); + } + $form['new']['#weight'] = 10; $form['new']['upload'] = array( '#type' => 'file', '#title' => t('Attach new file'), '#size' => 40, - '#description' => ($limits['resolution'] ? t('Images are larger than %resolution will be resized. ', array('%resolution' => $limits['resolution'])) : '') . t('The maximum upload size is %filesize. Only files with the following extensions may be uploaded: %extensions. ', array('%extensions' => $limits['extensions'], '%filesize' => format_size($limits['file_size']))), + '#description' => $limit_description, ); $form['new']['attach'] = array( '#type' => 'submit', @@ -589,9 +607,9 @@ function upload_load($node) { $files = array(); if ($node->vid) { - $result = db_query('SELECT * FROM {files} f INNER JOIN {upload} r ON f.fid = r.fid WHERE r.vid = %d ORDER BY r.weight, f.fid', $node->vid); - while ($file = db_fetch_object($result)) { - $files[$file->fid] = $file; + $result = db_query('SELECT u.fid FROM {upload} u WHERE u.vid = :vid ORDER BY u.weight, u.fid', array(':vid' => $node->vid)); + foreach ($result as $file) { + $files[$file->fid] = file_load($file->fid); } } diff --git a/modules/upload/upload.test b/modules/upload/upload.test index 842883b54..1b1400021 100644 --- a/modules/upload/upload.test +++ b/modules/upload/upload.test @@ -100,16 +100,25 @@ class UploadTestCase extends DrupalWebTestCase { $this->drupalLogin($web_user); $node = $this->drupalCreateNode(); - $text_files = $this->drupalGetTestFiles('text'); - $html_files = $this->drupalGetTestFiles('html'); - $files = array(current($text_files)->filename, current($html_files)->filename); - - // Attempt to upload .txt file when .test is only extension allowed. - $this->uploadFile($node, $files[0], FALSE); - $this->assertRaw(t('The specified file %name could not be uploaded. Only files with the following extensions are allowed: %files-allowed.', array('%name' => basename($files[0]), '%files-allowed' => $settings['upload_extensions'])), 'File '. $files[0] . ' was not allowed to be uploaded'); - // Attempt to upload .test file when .test is only extension allowed. - $this->uploadFile($node, $files[1]); + // Attempt to upload .txt file when .html is only extension allowed. + $text_files = array_values($this->drupalGetTestFiles('text')); + // Select a file that's less than the 1MB upload limit so we only test one + // limit at a time. + $text_file = $text_files[2]->filename; + $this->uploadFile($node, $text_file, FALSE); + // Test the error message in two steps in case there are additional errors + // that change the error message's format. + $this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => basename($text_file))), t('File %filename was not allowed to be uploaded', array('%filename' => $text_file))); + $this->assertRaw(t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $settings['upload_extensions'])), t('File extension cited as reason for failure')); + + // Attempt to upload .html file when .html is only extension allowed. + $html_files = array_values($this->drupalGetTestFiles('html')); + // Use the HTML file with the .html extension, $html_files[0] has a .txt + // extension. + $html_file = $html_files[1]->filename; + $this->uploadFile($node, $html_file); + $this->assertNoRaw(t('The specified file %name could not be uploaded.', array('%name' => basename($html_file))), t('File '. $html_file . ' was allowed to be uploaded')); } /** @@ -143,7 +152,10 @@ class UploadTestCase extends DrupalWebTestCase { $filename = basename($file); $filesize = format_size($info['size']); $maxsize = format_size(parse_size(($settings['upload_uploadsize'] * 1024) . 'KB')); // Won't parse decimals. - $this->assertRaw(t('The specified file %name could not be uploaded. The file is %filesize exceeding the maximum file size of %maxsize.', array('%name' => $filename, '%filesize' => $filesize, '%maxsize' => $maxsize)), t('File upload was blocked since it was larger than maxsize.')); + // Test the error message in two steps in case there are additional errors + // that change the error message's format. + $this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => $filename)), t('File upload was blocked')); + $this->assertRaw(t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => $filesize, '%maxsize' => $maxsize)), t('File size cited as problem with upload')); } function setUploadSettings($settings, $rid = NULL) { diff --git a/modules/user/user.module b/modules/user/user.module index 4138fd72e..e8d956d0f 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -407,7 +407,7 @@ function user_validate_picture(&$form, &$form_state) { if ($file = file_save_upload('picture_upload', $validators)) { // Remove the old picture. if (isset($form_state['values']['_account']->picture) && file_exists($form_state['values']['_account']->picture)) { - file_delete($form_state['values']['_account']->picture); + file_unmanaged_delete($form_state['values']['_account']->picture); } // The image was saved using file_save_upload() and was added to the @@ -415,7 +415,7 @@ function user_validate_picture(&$form, &$form_state) { // collector delete the original upload. $info = image_get_info($file->filepath); $destination = file_create_path(variable_get('user_picture_path', 'pictures') . '/picture-' . $form['#uid'] . '.' . $info['extension']); - if ($filepath = file_copy($file->filepath, $destination, FILE_EXISTS_REPLACE)) { + if ($filepath = file_unmanaged_copy($file->filepath, $destination, FILE_EXISTS_REPLACE)) { $form_state['values']['picture'] = $filepath; } else { @@ -1558,7 +1558,7 @@ function _user_edit_submit($uid, &$edit) { // Delete picture if requested, and if no replacement picture was given. if (!empty($edit['picture_delete'])) { if ($user->picture && file_exists($user->picture)) { - file_delete($user->picture); + file_unmanaged_delete($user->picture); } $edit['picture'] = ''; } |