rmdirr.lib.test.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Testing the function rmdirr() for recursive directory deletion.
  4. * This test is an adaptation of a published sample test.
  5. * @link http://aidanlister.com/2004/04/recursively-deleting-a-folder-in-php/
  6. * @author Aidan Lister, April, 2004
  7. * @author Ivan Tcholakov, September, 2009 - adaptation for the Dokeos LMS.
  8. */
  9. class Test_RmDirRFunction extends UnitTestCase {
  10. function Test_RmDirRFunction() {
  11. $this->UnitTestCase('Testing the function rmdirr() for recursive directory deletion');
  12. }
  13. public function test_rmdirr() {
  14. $current_dir = dirname(__FILE__).'/';
  15. $test_dir = $current_dir.'../../../../archive/'; // Write-access is needed for this directory.
  16. $test_dir = realpath($test_dir).'/';
  17. // Let us not clean backwars slashes on Windows, intentionally.
  18. //$test_dir = str_replace('\\', '/', $test_dir);
  19. // Create a directory and file tree
  20. mkdir($test_dir.'testdelete');
  21. mkdir($test_dir.'testdelete/one-a');
  22. touch($test_dir.'testdelete/one-a/testfile');
  23. mkdir($test_dir.'testdelete/one-b');
  24. // Add some hidden files for good measure
  25. touch($test_dir.'testdelete/one-b/.hiddenfile');
  26. mkdir($test_dir.'testdelete/one-c');
  27. touch($test_dir.'testdelete/one-c/.hiddenfile');
  28. // Add some more depth
  29. mkdir($test_dir.'testdelete/one-c/two-a');
  30. touch($test_dir.'testdelete/one-c/two-a/testfile');
  31. mkdir($test_dir.'testdelete/one-d/');
  32. // Test that symlinks are not followed
  33. // The function symlink() does not work on some Windows OS versions. For these cases this part of the test is skipped.
  34. $function_symlink_exists = function_exists('symlink');
  35. if ($function_symlink_exists) {
  36. mkdir($test_dir.'testlink');
  37. touch($test_dir.'testlink/testfile');
  38. symlink($test_dir.'testlink/testfile', 'testdelete/one-d/my-symlink');
  39. symlink($test_dir.'testlink', 'testdelete/one-d/my-symlink-dir');
  40. }
  41. // Run the actual delete
  42. $status = rmdirr($test_dir.'testdelete');
  43. // Check if we passed the test
  44. if ($status === true && !file_exists($test_dir.'testdelete') && ($function_symlink_exists ? file_exists($test_dir.'testlink/testfile') : true)) {
  45. //echo 'TEST PASSED';
  46. $res = true;
  47. } else {
  48. //echo 'TEST FAILED';
  49. $res = false;
  50. }
  51. if ($function_symlink_exists) {
  52. @rmdirr($test_dir.'testlink');
  53. }
  54. // Pass the result of this test
  55. $this->assertTrue($res === true);
  56. //var_dump($test_dir);
  57. }
  58. }