summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/file.inc40
-rw-r--r--modules/simpletest/drupal_web_test_case.php14
-rw-r--r--modules/simpletest/simpletest.module23
-rw-r--r--modules/simpletest/tests/file.test77
4 files changed, 124 insertions, 30 deletions
diff --git a/includes/file.inc b/includes/file.inc
index 05318add6..6c865eeaf 100644
--- a/includes/file.inc
+++ b/includes/file.inc
@@ -782,6 +782,7 @@ function file_delete($file, $force = FALSE) {
* error.
*
* @see file_delete()
+ * @see file_unmanaged_delete_recursive()
*/
function file_unmanaged_delete($path) {
if (is_dir($path)) {
@@ -797,11 +798,48 @@ function file_unmanaged_delete($path) {
watchdog('file', 'The file %path was not deleted, because it does not exist.', array('%path' => $path), WATCHDOG_NOTICE);
return TRUE;
}
- // Catch all for everything else: sockets, symbolic links, etc.
+ // We cannot handle anything other than files and directories. Log an error
+ // for everything else (sockets, symbolic links, etc).
+ watchdog('file', 'The file %path is not of a recognized type so it was not deleted.', array('%path' => $path), WATCHDOG_ERROR);
return FALSE;
}
/**
+ * Recursively delete all files and directories in the specified filepath.
+ *
+ * If the specified path is a directory then the function will call itself
+ * recursively to process the contents. Once the contents have been removed the
+ * directory will also be removed.
+ *
+ * If the specified path is a file then it will be passed to
+ * file_unmanaged_delete().
+ *
+ * Note that this only deletes visible files with write permission.
+ *
+ * @param $path
+ * A string containing a file or directory path.
+ * @return
+ * TRUE for success or path does not exist, or FALSE in the event of an
+ * error.
+ *
+ * @see file_unmanaged_delete()
+ */
+function file_unmanaged_delete_recursive($path) {
+ if (is_dir($path)) {
+ $dir = dir($path);
+ while (($entry = $dir->read()) !== FALSE) {
+ if ($entry == '.' || $entry == '..') {
+ continue;
+ }
+ $entry_path = $path . '/' . $entry;
+ file_unmanaged_delete_recursive($entry_path);
+ }
+ return rmdir($path);
+ }
+ return file_unmanaged_delete($path);
+}
+
+/**
* Determine total disk space used by a single user or the whole filesystem.
*
* @param $uid
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index 9aad62dd9..cd8b16216 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -902,7 +902,7 @@ class DrupalWebTestCase {
global $db_prefix, $user;
if (preg_match('/simpletest\d+/', $db_prefix)) {
// Delete temporary files directory and reset files directory path.
- simpletest_clean_temporary_directory(file_directory_path());
+ file_unmanaged_delete_recursive(file_directory_path());
variable_set('file_directory_path', $this->originalFileDirectory);
// Remove all prefixed tables (all the tables in the schema).
@@ -1999,7 +1999,7 @@ class DrupalWebTestCase {
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
}
-
+
/**
* TODO write documentation.
* @param $type
@@ -2017,13 +2017,13 @@ class DrupalWebTestCase {
);
$field_definition += $settings;
field_create_field($field_definition);
-
+
$field = field_read_field($field_name);
$this->assertTrue($field, t('Created field @field_name of type @type.', array('@field_name' => $field_name, '@type' => $type)));
-
+
return $field;
}
-
+
/**
* TODO write documentation.
* @param $field_name
@@ -2046,10 +2046,10 @@ class DrupalWebTestCase {
),
);
field_create_instance($instance_definition);
-
+
$instance = field_read_instance($field_name, $bundle);
$this->assertTrue($instance, t('Created instance of field @field_name on bundle @bundle.', array('@field_name' => $field_name, '@bundle' => $bundle)));
-
+
return $instance;
}
}
diff --git a/modules/simpletest/simpletest.module b/modules/simpletest/simpletest.module
index 39c08fecb..5476a9795 100644
--- a/modules/simpletest/simpletest.module
+++ b/modules/simpletest/simpletest.module
@@ -550,7 +550,7 @@ function simpletest_clean_temporary_directories() {
foreach ($files as $file) {
$path = file_directory_path() . '/' . $file;
if (is_dir($path) && preg_match('/^simpletest\d+/', $file)) {
- simpletest_clean_temporary_directory($path);
+ file_unmanaged_delete_recursive($path);
$count++;
}
}
@@ -564,27 +564,6 @@ function simpletest_clean_temporary_directories() {
}
/**
- * Remove all files from specified directory and then remove directory.
- *
- * @param string $path Directory path.
- */
-function simpletest_clean_temporary_directory($path) {
- $files = scandir($path);
- foreach ($files as $file) {
- if ($file != '.' && $file != '..') {
- $file_path = "$path/$file";
- if (is_dir($file_path)) {
- simpletest_clean_temporary_directory($file_path);
- }
- else {
- file_unmanaged_delete($file_path);
- }
- }
- }
- rmdir($path);
-}
-
-/**
* Clear the test results tables.
*/
function simpletest_clean_results_table() {
diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test
index 66fadfba5..c42d69e20 100644
--- a/modules/simpletest/tests/file.test
+++ b/modules/simpletest/tests/file.test
@@ -850,6 +850,83 @@ class FileUnmanagedDeleteTest extends FileTestCase {
/**
+ * Deletion related tests.
+ */
+class FileUnmanagedDeleteRecursiveTest extends FileTestCase {
+ function getInfo() {
+ return array(
+ 'name' => t('Unmanaged recursive file delete'),
+ 'description' => t('Tests the unmanaged file delete recursive function.'),
+ 'group' => t('File'),
+ );
+ }
+
+ /**
+ * Delete a normal file.
+ */
+ function testSingleFile() {
+ // Create a file for testing
+ $filepath = file_directory_path() . '/' . $this->randomName();
+ file_put_contents($filepath, '');
+
+ // Delete the file.
+ $this->assertTrue(file_unmanaged_delete_recursive($filepath), t('Function reported success.'));
+ $this->assertFalse(file_exists($filepath), t('Test file has been deleted.'));
+ }
+
+ /**
+ * Try deleting an empty directory.
+ */
+ function testEmptyDirectory() {
+ // A directory to operate on.
+ $directory = $this->createDirectory();
+
+ // Delete the directory.
+ $this->assertTrue(file_unmanaged_delete_recursive($directory), t('Function reported success.'));
+ $this->assertFalse(file_exists($directory), t('Directory has been deleted.'));
+ }
+
+ /**
+ * Try deleting a directory with some files.
+ */
+ function testDirectory() {
+ // A directory to operate on.
+ $directory = $this->createDirectory();
+ $filepathA = $directory . '/A';
+ $filepathB = $directory . '/B';
+ file_put_contents($filepathA, '');
+ file_put_contents($filepathB, '');
+
+ // Delete the directory.
+ $this->assertTrue(file_unmanaged_delete_recursive($directory), t('Function reported success.'));
+ $this->assertFalse(file_exists($filepathA), t('Test file A has been deleted.'));
+ $this->assertFalse(file_exists($filepathB), t('Test file B has been deleted.'));
+ $this->assertFalse(file_exists($directory), t('Directory has been deleted.'));
+ }
+
+ /**
+ * Try deleting subdirectories with some files.
+ */
+ function testSubDirectory() {
+ // A directory to operate on.
+ $directory = $this->createDirectory();
+ $subdirectory = $this->createDirectory($directory . '/sub');
+ $filepathA = $directory . '/A';
+ $filepathB = $subdirectory . '/B';
+ file_put_contents($filepathA, '');
+ file_put_contents($filepathB, '');
+
+ // Delete the directory.
+ $this->assertTrue(file_unmanaged_delete_recursive($directory), t('Function reported success.'));
+ $this->assertFalse(file_exists($filepathA), t('Test file A has been deleted.'));
+ $this->assertFalse(file_exists($filepathB), t('Test file B has been deleted.'));
+ $this->assertFalse(file_exists($subdirectory), t('Subdirectory has been deleted.'));
+ $this->assertFalse(file_exists($directory), t('Directory has been deleted.'));
+ }
+}
+
+
+/**
* Unmanaged move related tests.
*/
class FileUnmanagedMoveTest extends FileTestCase {