file_copy_recursive_test.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 ezcBaseFileCopyRecursiveTest extends ezcTestCase
  32. {
  33. protected function setUp()
  34. {
  35. $this->tempDir = $this->createTempDir( __CLASS__ );
  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/0' );
  46. file_put_contents( $this->tempDir . '/dir1/file1.txt', 'test' );
  47. file_put_contents( $this->tempDir . '/dir1/file2.txt', 'test' );
  48. file_put_contents( $this->tempDir . '/dir1/.file3.txt', 'test' );
  49. file_put_contents( $this->tempDir . '/dir2/file1.txt', 'test' );
  50. file_put_contents( $this->tempDir . '/dir2/dir1/file1.txt', 'test' );
  51. file_put_contents( $this->tempDir . '/dir2/dir1/dir1/file1.txt', 'test' );
  52. file_put_contents( $this->tempDir . '/dir2/dir1/dir1/file2.txt', 'test' );
  53. file_put_contents( $this->tempDir . '/dir2/dir2/file1.txt', 'test' );
  54. file_put_contents( $this->tempDir . '/dir4/file1.txt', 'test' );
  55. file_put_contents( $this->tempDir . '/dir4/file2.txt', 'test' );
  56. file_put_contents( $this->tempDir . '/dir5/file1.txt', 'test' );
  57. file_put_contents( $this->tempDir . '/dir5/file2.txt', 'test' );
  58. file_put_contents( $this->tempDir . '/dir6/file1.txt', 'test' );
  59. file_put_contents( $this->tempDir . '/dir6/file2.txt', 'test' );
  60. chmod( $this->tempDir . '/dir4/file1.txt', 0 );
  61. chmod( $this->tempDir . '/dir5', 0 );
  62. chmod( $this->tempDir . '/dir6', 0400 );
  63. }
  64. protected function tearDown()
  65. {
  66. chmod( $this->tempDir . '/dir5', 0700 );
  67. chmod( $this->tempDir . '/dir6', 0700 );
  68. $this->removeTempDir();
  69. }
  70. public function testRecursiveCopyEmptyDir()
  71. {
  72. ezcBaseFile::copyRecursive(
  73. $this->tempDir . '/dir1',
  74. $this->tempDir . '/dest'
  75. );
  76. $this->assertEquals(
  77. count( ezcBaseFile::findRecursive( $this->tempDir . '/dir1' ) ),
  78. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  79. );
  80. $this->assertSame(
  81. 0775,
  82. fileperms( $this->tempDir . '/dest' ) & 0777,
  83. 'Directory mode should equal 0775.'
  84. );
  85. }
  86. public function testRecursiveCopyFile()
  87. {
  88. ezcBaseFile::copyRecursive(
  89. $this->tempDir . '/dir1/file1.txt',
  90. $this->tempDir . '/dest'
  91. );
  92. $this->assertTrue(
  93. is_file( $this->tempDir . '/dest' )
  94. );
  95. $this->assertSame(
  96. 0664,
  97. fileperms( $this->tempDir . '/dest' ) & 0777,
  98. 'File mode should equal 0664.'
  99. );
  100. }
  101. public function testRecursiveCopyEmptyDirMode()
  102. {
  103. ezcBaseFile::copyRecursive(
  104. $this->tempDir . '/dir1',
  105. $this->tempDir . '/dest',
  106. -1,
  107. 0777,
  108. 0777
  109. );
  110. $this->assertEquals(
  111. count( ezcBaseFile::findRecursive( $this->tempDir . '/dir1' ) ),
  112. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  113. );
  114. $this->assertSame(
  115. 0777,
  116. fileperms( $this->tempDir . '/dest' ) & 0777,
  117. 'Directory mode should equal 0777.'
  118. );
  119. }
  120. public function testRecursiveCopyFileMode()
  121. {
  122. ezcBaseFile::copyRecursive(
  123. $this->tempDir . '/dir1/file1.txt',
  124. $this->tempDir . '/dest',
  125. -1,
  126. 0777,
  127. 0777
  128. );
  129. $this->assertTrue(
  130. is_file( $this->tempDir . '/dest' )
  131. );
  132. $this->assertSame(
  133. 0777,
  134. fileperms( $this->tempDir . '/dest' ) & 0777,
  135. 'File mode should equal 0777.'
  136. );
  137. }
  138. public function testRecursiveCopyFullDir()
  139. {
  140. ezcBaseFile::copyRecursive(
  141. $this->tempDir . '/dir2',
  142. $this->tempDir . '/dest'
  143. );
  144. $this->assertEquals(
  145. count( ezcBaseFile::findRecursive( $this->tempDir . '/dir2' ) ),
  146. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  147. );
  148. }
  149. public function testRecursiveCopyFullDirDepthZero()
  150. {
  151. ezcBaseFile::copyRecursive(
  152. $this->tempDir . '/dir2',
  153. $this->tempDir . '/dest',
  154. 0
  155. );
  156. $this->assertEquals(
  157. 0,
  158. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  159. );
  160. $this->assertTrue(
  161. is_dir( $this->tempDir . '/dest' )
  162. );
  163. }
  164. public function testRecursiveCopyFullDirLimitedDepth()
  165. {
  166. ezcBaseFile::copyRecursive(
  167. $this->tempDir . '/dir2',
  168. $this->tempDir . '/dest',
  169. 2
  170. );
  171. $this->assertEquals(
  172. 3,
  173. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  174. );
  175. }
  176. public function testRecursiveCopyFailureNotExisting()
  177. {
  178. try
  179. {
  180. ezcBaseFile::copyRecursive(
  181. $this->tempDir . '/not_existing',
  182. $this->tempDir . '/dest'
  183. );
  184. }
  185. catch ( ezcBaseFileNotFoundException $e )
  186. {
  187. return;
  188. }
  189. $this->fail( 'Expected ezcBaseFileNotFoundException.' );
  190. }
  191. public function testRecursiveCopyFailureNotReadable()
  192. {
  193. ezcBaseFile::copyRecursive(
  194. $this->tempDir . '/dir5',
  195. $this->tempDir . '/dest'
  196. );
  197. $this->assertFalse(
  198. is_dir( $this->tempDir . '/dest' )
  199. );
  200. $this->assertFalse(
  201. is_file( $this->tempDir . '/dest' )
  202. );
  203. }
  204. public function testRecursiveCopyFailureNotWriteable()
  205. {
  206. try
  207. {
  208. ezcBaseFile::copyRecursive(
  209. $this->tempDir . '/dir2',
  210. $this->tempDir . '/dir4'
  211. );
  212. }
  213. catch ( ezcBaseFilePermissionException $e )
  214. {
  215. return;
  216. }
  217. $this->fail( 'Expected ezcBaseFilePermissionException.' );
  218. }
  219. public static function suite()
  220. {
  221. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  222. }
  223. public function testRecursiveCopyDirCalled0()
  224. {
  225. ezcBaseFile::copyRecursive(
  226. $this->tempDir . '/dir7',
  227. $this->tempDir . '/dest'
  228. );
  229. $this->assertEquals(
  230. count( ezcBaseFile::findRecursive( $this->tempDir . '/dir7' ) ),
  231. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  232. );
  233. $this->assertTrue( is_dir( $this->tempDir . '/dest/0' ) );
  234. }
  235. }
  236. ?>