/crashfix_webapp/protected/vendors/ezcomponents/Base/tests/file_copy_recursive_test.php

https://gitlab.com/quyse/crashfix · PHP · 251 lines · 207 code · 32 blank · 12 comment · 0 complexity · 6dbd65bef22f7c2a0814a332738ffb8c MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  4. * @license http://ez.no/licenses/new_bsd New BSD License
  5. * @version 1.8
  6. * @filesource
  7. * @package Base
  8. * @subpackage Tests
  9. */
  10. /**
  11. * @package Base
  12. * @subpackage Tests
  13. */
  14. class ezcBaseFileCopyRecursiveTest extends ezcTestCase
  15. {
  16. protected function setUp()
  17. {
  18. $this->tempDir = $this->createTempDir( __CLASS__ );
  19. mkdir( $this->tempDir . '/dir1' );
  20. mkdir( $this->tempDir . '/dir2' );
  21. mkdir( $this->tempDir . '/dir2/dir1' );
  22. mkdir( $this->tempDir . '/dir2/dir1/dir1' );
  23. mkdir( $this->tempDir . '/dir2/dir2' );
  24. mkdir( $this->tempDir . '/dir4' );
  25. mkdir( $this->tempDir . '/dir5' );
  26. mkdir( $this->tempDir . '/dir6' );
  27. mkdir( $this->tempDir . '/dir7' );
  28. mkdir( $this->tempDir . '/dir7/0' );
  29. file_put_contents( $this->tempDir . '/dir1/file1.txt', 'test' );
  30. file_put_contents( $this->tempDir . '/dir1/file2.txt', 'test' );
  31. file_put_contents( $this->tempDir . '/dir1/.file3.txt', 'test' );
  32. file_put_contents( $this->tempDir . '/dir2/file1.txt', 'test' );
  33. file_put_contents( $this->tempDir . '/dir2/dir1/file1.txt', 'test' );
  34. file_put_contents( $this->tempDir . '/dir2/dir1/dir1/file1.txt', 'test' );
  35. file_put_contents( $this->tempDir . '/dir2/dir1/dir1/file2.txt', 'test' );
  36. file_put_contents( $this->tempDir . '/dir2/dir2/file1.txt', 'test' );
  37. file_put_contents( $this->tempDir . '/dir4/file1.txt', 'test' );
  38. file_put_contents( $this->tempDir . '/dir4/file2.txt', 'test' );
  39. file_put_contents( $this->tempDir . '/dir5/file1.txt', 'test' );
  40. file_put_contents( $this->tempDir . '/dir5/file2.txt', 'test' );
  41. file_put_contents( $this->tempDir . '/dir6/file1.txt', 'test' );
  42. file_put_contents( $this->tempDir . '/dir6/file2.txt', 'test' );
  43. chmod( $this->tempDir . '/dir4/file1.txt', 0 );
  44. chmod( $this->tempDir . '/dir5', 0 );
  45. chmod( $this->tempDir . '/dir6', 0400 );
  46. }
  47. protected function tearDown()
  48. {
  49. chmod( $this->tempDir . '/dir5', 0700 );
  50. chmod( $this->tempDir . '/dir6', 0700 );
  51. $this->removeTempDir();
  52. }
  53. public function testRecursiveCopyEmptyDir()
  54. {
  55. ezcBaseFile::copyRecursive(
  56. $this->tempDir . '/dir1',
  57. $this->tempDir . '/dest'
  58. );
  59. $this->assertEquals(
  60. count( ezcBaseFile::findRecursive( $this->tempDir . '/dir1' ) ),
  61. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  62. );
  63. $this->assertSame(
  64. 0775,
  65. fileperms( $this->tempDir . '/dest' ) & 0777,
  66. 'Directory mode should equal 0775.'
  67. );
  68. }
  69. public function testRecursiveCopyFile()
  70. {
  71. ezcBaseFile::copyRecursive(
  72. $this->tempDir . '/dir1/file1.txt',
  73. $this->tempDir . '/dest'
  74. );
  75. $this->assertTrue(
  76. is_file( $this->tempDir . '/dest' )
  77. );
  78. $this->assertSame(
  79. 0664,
  80. fileperms( $this->tempDir . '/dest' ) & 0777,
  81. 'File mode should equal 0664.'
  82. );
  83. }
  84. public function testRecursiveCopyEmptyDirMode()
  85. {
  86. ezcBaseFile::copyRecursive(
  87. $this->tempDir . '/dir1',
  88. $this->tempDir . '/dest',
  89. -1,
  90. 0777,
  91. 0777
  92. );
  93. $this->assertEquals(
  94. count( ezcBaseFile::findRecursive( $this->tempDir . '/dir1' ) ),
  95. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  96. );
  97. $this->assertSame(
  98. 0777,
  99. fileperms( $this->tempDir . '/dest' ) & 0777,
  100. 'Directory mode should equal 0777.'
  101. );
  102. }
  103. public function testRecursiveCopyFileMode()
  104. {
  105. ezcBaseFile::copyRecursive(
  106. $this->tempDir . '/dir1/file1.txt',
  107. $this->tempDir . '/dest',
  108. -1,
  109. 0777,
  110. 0777
  111. );
  112. $this->assertTrue(
  113. is_file( $this->tempDir . '/dest' )
  114. );
  115. $this->assertSame(
  116. 0777,
  117. fileperms( $this->tempDir . '/dest' ) & 0777,
  118. 'File mode should equal 0777.'
  119. );
  120. }
  121. public function testRecursiveCopyFullDir()
  122. {
  123. ezcBaseFile::copyRecursive(
  124. $this->tempDir . '/dir2',
  125. $this->tempDir . '/dest'
  126. );
  127. $this->assertEquals(
  128. count( ezcBaseFile::findRecursive( $this->tempDir . '/dir2' ) ),
  129. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  130. );
  131. }
  132. public function testRecursiveCopyFullDirDepthZero()
  133. {
  134. ezcBaseFile::copyRecursive(
  135. $this->tempDir . '/dir2',
  136. $this->tempDir . '/dest',
  137. 0
  138. );
  139. $this->assertEquals(
  140. 0,
  141. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  142. );
  143. $this->assertTrue(
  144. is_dir( $this->tempDir . '/dest' )
  145. );
  146. }
  147. public function testRecursiveCopyFullDirLimitedDepth()
  148. {
  149. ezcBaseFile::copyRecursive(
  150. $this->tempDir . '/dir2',
  151. $this->tempDir . '/dest',
  152. 2
  153. );
  154. $this->assertEquals(
  155. 3,
  156. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  157. );
  158. }
  159. public function testRecursiveCopyFailureNotExisting()
  160. {
  161. try
  162. {
  163. ezcBaseFile::copyRecursive(
  164. $this->tempDir . '/not_existing',
  165. $this->tempDir . '/dest'
  166. );
  167. }
  168. catch ( ezcBaseFileNotFoundException $e )
  169. {
  170. return;
  171. }
  172. $this->fail( 'Expected ezcBaseFileNotFoundException.' );
  173. }
  174. public function testRecursiveCopyFailureNotReadable()
  175. {
  176. ezcBaseFile::copyRecursive(
  177. $this->tempDir . '/dir5',
  178. $this->tempDir . '/dest'
  179. );
  180. $this->assertFalse(
  181. is_dir( $this->tempDir . '/dest' )
  182. );
  183. $this->assertFalse(
  184. is_file( $this->tempDir . '/dest' )
  185. );
  186. }
  187. public function testRecursiveCopyFailureNotWriteable()
  188. {
  189. try
  190. {
  191. ezcBaseFile::copyRecursive(
  192. $this->tempDir . '/dir2',
  193. $this->tempDir . '/dir4'
  194. );
  195. }
  196. catch ( ezcBaseFilePermissionException $e )
  197. {
  198. return;
  199. }
  200. $this->fail( 'Expected ezcBaseFilePermissionException.' );
  201. }
  202. public static function suite()
  203. {
  204. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  205. }
  206. public function testRecursiveCopyDirCalled0()
  207. {
  208. ezcBaseFile::copyRecursive(
  209. $this->tempDir . '/dir7',
  210. $this->tempDir . '/dest'
  211. );
  212. $this->assertEquals(
  213. count( ezcBaseFile::findRecursive( $this->tempDir . '/dir7' ) ),
  214. count( ezcBaseFile::findRecursive( $this->tempDir . '/dest' ) )
  215. );
  216. $this->assertTrue( is_dir( $this->tempDir . '/dest/0' ) );
  217. }
  218. }
  219. ?>