From 52c17c6d3ec99a872436ad09377401c25ff0bb55 Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Fri, 13 Feb 2009 00:39:01 +0000 Subject: #373502 by drewish and sun: Add function to delete unmanaged files recurisevely (with tests). --- modules/simpletest/tests/file.test | 77 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'modules/simpletest/tests') 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 @@ -849,6 +849,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. */ -- cgit v1.2.3