file_remove_recursive_test.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package Base
  25. * @subpackage Tests
  26. */
  27. /**
  28. * @package Base
  29. * @subpackage Tests
  30. */
  31. class ezcBaseFileRemoveRecursiveTest extends ezcTestCase
  32. {
  33. protected function setUp()
  34. {
  35. $this->tempDir = $this->createTempDir( 'ezcBaseFileRemoveFileRecursiveTest' );
  36. mkdir( $this->tempDir . '/dir1' );
  37. mkdir( $this->tempDir . '/dir2' );
  38. mkdir( $this->tempDir . '/dir2/dir1' );
  39. mkdir( $this->tempDir . '/dir2/dir1/dir1' );
  40. mkdir( $this->tempDir . '/dir2/dir2' );
  41. mkdir( $this->tempDir . '/dir4' );
  42. mkdir( $this->tempDir . '/dir5' );
  43. mkdir( $this->tempDir . '/dir6' );
  44. mkdir( $this->tempDir . '/dir7' );
  45. mkdir( $this->tempDir . '/dir7/dir1' );
  46. mkdir( $this->tempDir . '/dir8' );
  47. mkdir( $this->tempDir . '/dir8/dir1' );
  48. mkdir( $this->tempDir . '/dir8/dir1/dir1' );
  49. file_put_contents( $this->tempDir . '/dir1/file1.txt', 'test' );
  50. file_put_contents( $this->tempDir . '/dir1/file2.txt', 'test' );
  51. file_put_contents( $this->tempDir . '/dir1/.file3.txt', 'test' );
  52. file_put_contents( $this->tempDir . '/dir2/file1.txt', 'test' );
  53. file_put_contents( $this->tempDir . '/dir2/dir1/file1.txt', 'test' );
  54. file_put_contents( $this->tempDir . '/dir2/dir1/dir1/file1.txt', 'test' );
  55. file_put_contents( $this->tempDir . '/dir2/dir1/dir1/file2.txt', 'test' );
  56. file_put_contents( $this->tempDir . '/dir2/dir2/file1.txt', 'test' );
  57. file_put_contents( $this->tempDir . '/dir4/file1.txt', 'test' );
  58. file_put_contents( $this->tempDir . '/dir4/file2.txt', 'test' );
  59. file_put_contents( $this->tempDir . '/dir5/file1.txt', 'test' );
  60. file_put_contents( $this->tempDir . '/dir5/file2.txt', 'test' );
  61. file_put_contents( $this->tempDir . '/dir6/file1.txt', 'test' );
  62. file_put_contents( $this->tempDir . '/dir6/file2.txt', 'test' );
  63. file_put_contents( $this->tempDir . '/dir7/dir1/file1.txt', 'test' );
  64. file_put_contents( $this->tempDir . '/dir8/dir1/file1.txt', 'test' );
  65. file_put_contents( $this->tempDir . '/dir8/dir1/dir1/file1.txt', 'test' );
  66. chmod( $this->tempDir . '/dir4/file1.txt', 0 );
  67. chmod( $this->tempDir . '/dir5', 0 );
  68. chmod( $this->tempDir . '/dir6', 0400 );
  69. chmod( $this->tempDir . '/dir7', 0500 );
  70. chmod( $this->tempDir . '/dir8/dir1', 0500 );
  71. }
  72. protected function tearDown()
  73. {
  74. chmod( $this->tempDir . '/dir5', 0700 );
  75. chmod( $this->tempDir . '/dir6', 0700 );
  76. chmod( $this->tempDir . '/dir7', 0700 );
  77. chmod( $this->tempDir . '/dir8/dir1', 0700 );
  78. $this->removeTempDir();
  79. }
  80. public function testRecursive1()
  81. {
  82. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  83. ezcBaseFile::removeRecursive( $this->tempDir . '/dir1' );
  84. self::assertEquals( 12, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  85. ezcBaseFile::removeRecursive( $this->tempDir . '/dir2' );
  86. self::assertEquals( 7, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  87. }
  88. public function testRecursive2()
  89. {
  90. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  91. try
  92. {
  93. ezcBaseFile::removeRecursive( $this->tempDir . '/dir3' );
  94. }
  95. catch ( ezcBaseFileNotFoundException $e )
  96. {
  97. self::assertEquals( "The directory file '{$this->tempDir}/dir3' could not be found.", $e->getMessage() );
  98. }
  99. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  100. }
  101. public function testRecursive3()
  102. {
  103. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  104. try
  105. {
  106. ezcBaseFile::removeRecursive( $this->tempDir . '/dir4' );
  107. }
  108. catch ( ezcBaseFilePermissionException $e )
  109. {
  110. self::assertEquals( "The file '{$this->tempDir}/dir5' can not be opened for reading.", $e->getMessage() );
  111. }
  112. self::assertEquals( 13, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  113. }
  114. public function testRecursive4()
  115. {
  116. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  117. try
  118. {
  119. ezcBaseFile::removeRecursive( $this->tempDir . '/dir5' );
  120. }
  121. catch ( ezcBaseFilePermissionException $e )
  122. {
  123. self::assertEquals( "The file '{$this->tempDir}/dir5' can not be opened for reading.", $e->getMessage() );
  124. }
  125. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  126. }
  127. public function testRecursive5()
  128. {
  129. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  130. try
  131. {
  132. ezcBaseFile::removeRecursive( $this->tempDir . '/dir6' );
  133. }
  134. catch ( ezcBaseFilePermissionException $e )
  135. {
  136. // Make no asumption on which file is tried to be removed first
  137. self::assertEquals(
  138. 1,
  139. preg_match(
  140. "(The file '{$this->tempDir}/dir6/file[12].txt' can not be removed.)",
  141. $e->getMessage()
  142. )
  143. );
  144. }
  145. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  146. }
  147. public function testRecursiveNotWritableParent()
  148. {
  149. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  150. try
  151. {
  152. ezcBaseFile::removeRecursive( $this->tempDir . '/dir7/dir1' );
  153. }
  154. catch ( ezcBaseFilePermissionException $e )
  155. {
  156. self::assertEquals( "The file '{$this->tempDir}/dir7' can not be opened for writing.", $e->getMessage() );
  157. }
  158. self::assertEquals( 15, count( ezcBaseFile::findRecursive( $this->tempDir ) ) );
  159. }
  160. public static function suite()
  161. {
  162. return new PHPUnit_Framework_TestSuite( "ezcBaseFileRemoveRecursiveTest" );
  163. }
  164. }
  165. ?>