PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/tests/cases/libs/folder.test.php

http://github.com/Datawalke/Coordino
PHP | 794 lines | 643 code | 47 blank | 104 comment | 2 complexity | b7a6649ec5a1e48b05789b33ba187260 MD5 | raw file
  1. <?php
  2. /**
  3. * FolderTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. App::import('Core', 'File');
  21. /**
  22. * FolderTest class
  23. *
  24. * @package cake
  25. * @subpackage cake.tests.cases.libs
  26. */
  27. class FolderTest extends CakeTestCase {
  28. /**
  29. * testBasic method
  30. *
  31. * @access public
  32. * @return void
  33. */
  34. function testBasic() {
  35. $path = dirname(__FILE__);
  36. $Folder =& new Folder($path);
  37. $result = $Folder->pwd();
  38. $this->assertEqual($result, $path);
  39. $result = Folder::addPathElement($path, 'test');
  40. $expected = $path . DS . 'test';
  41. $this->assertEqual($result, $expected);
  42. $result = $Folder->cd(ROOT);
  43. $expected = ROOT;
  44. $this->assertEqual($result, $expected);
  45. $result = $Folder->cd(ROOT . DS . 'non-existent');
  46. $this->assertFalse($result);
  47. }
  48. /**
  49. * testInPath method
  50. *
  51. * @access public
  52. * @return void
  53. */
  54. function testInPath() {
  55. $path = dirname(dirname(__FILE__));
  56. $inside = dirname($path) . DS;
  57. $Folder =& new Folder($path);
  58. $result = $Folder->pwd();
  59. $this->assertEqual($result, $path);
  60. $result = Folder::isSlashTerm($inside);
  61. $this->assertTrue($result);
  62. $result = $Folder->realpath('tests/');
  63. $this->assertEqual($result, $path . DS .'tests' . DS);
  64. $result = $Folder->inPath('tests' . DS);
  65. $this->assertTrue($result);
  66. $result = $Folder->inPath(DS . 'non-existing' . $inside);
  67. $this->assertFalse($result);
  68. }
  69. /**
  70. * test creation of single and mulitple paths.
  71. *
  72. * @return void
  73. */
  74. function testCreation() {
  75. $folder =& new Folder(TMP . 'tests');
  76. $result = $folder->create(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
  77. $this->assertTrue($result);
  78. rmdir(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
  79. rmdir(TMP . 'tests' . DS . 'first' . DS . 'second');
  80. rmdir(TMP . 'tests' . DS . 'first');
  81. $folder =& new Folder(TMP . 'tests');
  82. $result = $folder->create(TMP . 'tests' . DS . 'first');
  83. $this->assertTrue($result);
  84. rmdir(TMP . 'tests' . DS . 'first');
  85. }
  86. /**
  87. * test that creation of folders with trailing ds works
  88. *
  89. * @return void
  90. */
  91. function testCreateWithTrailingDs() {
  92. $folder =& new Folder(TMP);
  93. $path = TMP . 'tests' . DS . 'trailing' . DS . 'dir' . DS;
  94. $result = $folder->create($path);
  95. $this->assertTrue($result);
  96. $this->assertTrue(is_dir($path), 'Folder was not made');
  97. $folder =& new Folder(TMP . 'tests' . DS . 'trailing');
  98. $this->assertTrue($folder->delete());
  99. }
  100. /**
  101. * test recurisve directory create failure.
  102. *
  103. * @return void
  104. */
  105. function testRecursiveCreateFailure() {
  106. if ($this->skipIf(DS == '\\', 'Cant perform operations using permissions on windows. %s')) {
  107. return;
  108. }
  109. $path = TMP . 'tests' . DS . 'one';
  110. mkdir($path);
  111. chmod($path, '0444');
  112. $this->expectError();
  113. $folder =& new Folder($path);
  114. $result = $folder->create($path . DS . 'two' . DS . 'three');
  115. $this->assertFalse($result);
  116. chmod($path, '0777');
  117. rmdir($path);
  118. }
  119. /**
  120. * testOperations method
  121. *
  122. * @access public
  123. * @return void
  124. */
  125. function testOperations() {
  126. $path = TEST_CAKE_CORE_INCLUDE_PATH . 'console' . DS . 'templates' . DS . 'skel';
  127. $Folder =& new Folder($path);
  128. $result = is_dir($Folder->pwd());
  129. $this->assertTrue($result);
  130. $new = TMP . 'test_folder_new';
  131. $result = $Folder->create($new);
  132. $this->assertTrue($result);
  133. $copy = TMP . 'test_folder_copy';
  134. $result = $Folder->copy($copy);
  135. $this->assertTrue($result);
  136. $copy = TMP . 'test_folder_copy';
  137. $result = $Folder->copy($copy);
  138. $this->assertTrue($result);
  139. $copy = TMP . 'test_folder_copy';
  140. $result = $Folder->chmod($copy, 0755, false);
  141. $this->assertTrue($result);
  142. $result = $Folder->cd($copy);
  143. $this->assertTrue($result);
  144. $mv = TMP . 'test_folder_mv';
  145. $result = $Folder->move($mv);
  146. $this->assertTrue($result);
  147. $mv = TMP . 'test_folder_mv_2';
  148. $result = $Folder->move($mv);
  149. $this->assertTrue($result);
  150. $result = $Folder->delete($new);
  151. $this->assertTrue($result);
  152. $result = $Folder->delete($mv);
  153. $this->assertTrue($result);
  154. $result = $Folder->delete($mv);
  155. $this->assertTrue($result);
  156. $new = APP . 'index.php';
  157. $result = $Folder->create($new);
  158. $this->assertFalse($result);
  159. $expected = $new . ' is a file';
  160. $result = array_pop($Folder->errors());
  161. $this->assertEqual($result, $expected);
  162. $new = TMP . 'test_folder_new';
  163. $result = $Folder->create($new);
  164. $this->assertTrue($result);
  165. $result = $Folder->cd($new);
  166. $this->assertTrue($result);
  167. $result = $Folder->delete();
  168. $this->assertTrue($result);
  169. $Folder =& new Folder('non-existent');
  170. $result = $Folder->pwd();
  171. $this->assertNull($result);
  172. }
  173. /**
  174. * testChmod method
  175. *
  176. * @return void
  177. * @access public
  178. */
  179. function testChmod() {
  180. $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s Folder permissions tests not supported on Windows');
  181. $path = TEST_CAKE_CORE_INCLUDE_PATH . 'console' . DS . 'templates' . DS . 'skel';
  182. $Folder =& new Folder($path);
  183. $subdir = 'test_folder_new';
  184. $new = TMP . $subdir;
  185. $this->assertTrue($Folder->create($new));
  186. $this->assertTrue($Folder->create($new . DS . 'test1'));
  187. $this->assertTrue($Folder->create($new . DS . 'test2'));
  188. $filePath = $new . DS . 'test1.php';
  189. $File =& new File($filePath);
  190. $this->assertTrue($File->create());
  191. $copy = TMP . 'test_folder_copy';
  192. $this->assertTrue($Folder->chmod($new, 0777, true));
  193. $this->assertEqual($File->perms(), '0777');
  194. $Folder->delete($new);
  195. }
  196. /**
  197. * testRealPathForWebroot method
  198. *
  199. * @access public
  200. * @return void
  201. */
  202. function testRealPathForWebroot() {
  203. $Folder = new Folder('files/');
  204. $this->assertEqual(realpath('files/'), $Folder->path);
  205. }
  206. /**
  207. * testZeroAsDirectory method
  208. *
  209. * @access public
  210. * @return void
  211. */
  212. function testZeroAsDirectory() {
  213. $Folder =& new Folder(TMP);
  214. $new = TMP . '0';
  215. $this->assertTrue($Folder->create($new));
  216. $result = $Folder->read(true, true);
  217. $expected = array('0', 'cache', 'logs', 'sessions', 'tests');
  218. $this->assertEqual($expected, $result[0]);
  219. $result = $Folder->read(true, array('.', '..', 'logs', '.svn'));
  220. $expected = array('0', 'cache', 'sessions', 'tests');
  221. $this->assertEqual($expected, $result[0]);
  222. $result = $Folder->delete($new);
  223. $this->assertTrue($result);
  224. }
  225. /**
  226. * test Adding path elements to a path
  227. *
  228. * @return void
  229. */
  230. function testAddPathElement() {
  231. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path');
  232. $this->assertEqual($result, DS . 'some' . DS . 'dir' . DS . 'another_path');
  233. $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path');
  234. $this->assertEqual($result, DS . 'some' . DS . 'dir' . DS . 'another_path');
  235. }
  236. /**
  237. * testFolderRead method
  238. *
  239. * @access public
  240. * @return void
  241. */
  242. function testFolderRead() {
  243. $Folder =& new Folder(TMP);
  244. $expected = array('cache', 'logs', 'sessions', 'tests');
  245. $result = $Folder->read(true, true);
  246. $this->assertEqual($result[0], $expected);
  247. $Folder->path = TMP . DS . 'non-existent';
  248. $expected = array(array(), array());
  249. $result = $Folder->read(true, true);
  250. $this->assertEqual($result, $expected);
  251. }
  252. /**
  253. * testFolderTree method
  254. *
  255. * @access public
  256. * @return void
  257. */
  258. function testFolderTree() {
  259. $Folder =& new Folder();
  260. $expected = array(
  261. array(
  262. TEST_CAKE_CORE_INCLUDE_PATH . 'config',
  263. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode',
  264. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding'
  265. ),
  266. array(
  267. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php',
  268. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'paths.php',
  269. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0080_00ff.php',
  270. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0100_017f.php',
  271. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0180_024F.php',
  272. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0250_02af.php',
  273. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0370_03ff.php',
  274. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0400_04ff.php',
  275. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0500_052f.php',
  276. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0530_058f.php',
  277. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '1e00_1eff.php',
  278. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '1f00_1fff.php',
  279. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2100_214f.php',
  280. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2150_218f.php',
  281. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2460_24ff.php',
  282. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c00_2c5f.php',
  283. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c60_2c7f.php',
  284. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c80_2cff.php',
  285. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . 'ff00_ffef.php'
  286. )
  287. );
  288. $result = $Folder->tree(TEST_CAKE_CORE_INCLUDE_PATH . 'config', false);
  289. $this->assertIdentical(array_diff($expected[0], $result[0]), array());
  290. $this->assertIdentical(array_diff($result[0], $expected[0]), array());
  291. $result = $Folder->tree(TEST_CAKE_CORE_INCLUDE_PATH . 'config', false, 'dir');
  292. $this->assertIdentical(array_diff($expected[0], $result), array());
  293. $this->assertIdentical(array_diff($result, $expected[0]), array());
  294. $result = $Folder->tree(TEST_CAKE_CORE_INCLUDE_PATH . 'config', false, 'files');
  295. $this->assertIdentical(array_diff($expected[1], $result), array());
  296. $this->assertIdentical(array_diff($result, $expected[1]), array());
  297. }
  298. /**
  299. * testWindowsPath method
  300. *
  301. * @access public
  302. * @return void
  303. */
  304. function testWindowsPath() {
  305. $this->assertFalse(Folder::isWindowsPath('0:\\cake\\is\\awesome'));
  306. $this->assertTrue(Folder::isWindowsPath('C:\\cake\\is\\awesome'));
  307. $this->assertTrue(Folder::isWindowsPath('d:\\cake\\is\\awesome'));
  308. $this->assertTrue(Folder::isWindowsPath('\\\\vmware-host\\Shared Folders\\file'));
  309. }
  310. /**
  311. * testIsAbsolute method
  312. *
  313. * @access public
  314. * @return void
  315. */
  316. function testIsAbsolute() {
  317. $this->assertFalse(Folder::isAbsolute('path/to/file'));
  318. $this->assertFalse(Folder::isAbsolute('cake/'));
  319. $this->assertFalse(Folder::isAbsolute('path\\to\\file'));
  320. $this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
  321. $this->assertFalse(Folder::isAbsolute('\\path/to/file'));
  322. $this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
  323. $this->assertTrue(Folder::isAbsolute('/usr/local'));
  324. $this->assertTrue(Folder::isAbsolute('//path/to/file'));
  325. $this->assertTrue(Folder::isAbsolute('C:\\cake'));
  326. $this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
  327. $this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
  328. $this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
  329. }
  330. /**
  331. * testIsSlashTerm method
  332. *
  333. * @access public
  334. * @return void
  335. */
  336. function testIsSlashTerm() {
  337. $this->assertFalse(Folder::isSlashTerm('cake'));
  338. $this->assertTrue(Folder::isSlashTerm('C:\\cake\\'));
  339. $this->assertTrue(Folder::isSlashTerm('/usr/local/'));
  340. }
  341. /**
  342. * testStatic method
  343. *
  344. * @access public
  345. * @return void
  346. */
  347. function testSlashTerm() {
  348. $result = Folder::slashTerm('/path/to/file');
  349. $this->assertEqual($result, '/path/to/file/');
  350. }
  351. /**
  352. * testNormalizePath method
  353. *
  354. * @access public
  355. * @return void
  356. */
  357. function testNormalizePath() {
  358. $path = '/path/to/file';
  359. $result = Folder::normalizePath($path);
  360. $this->assertEqual($result, '/');
  361. $path = '\\path\\\to\\\file';
  362. $result = Folder::normalizePath($path);
  363. $this->assertEqual($result, '/');
  364. $path = 'C:\\path\\to\\file';
  365. $result = Folder::normalizePath($path);
  366. $this->assertEqual($result, '\\');
  367. }
  368. /**
  369. * correctSlashFor method
  370. *
  371. * @access public
  372. * @return void
  373. */
  374. function testCorrectSlashFor() {
  375. $path = '/path/to/file';
  376. $result = Folder::correctSlashFor($path);
  377. $this->assertEqual($result, '/');
  378. $path = '\\path\\to\\file';
  379. $result = Folder::correctSlashFor($path);
  380. $this->assertEqual($result, '/');
  381. $path = 'C:\\path\to\\file';
  382. $result = Folder::correctSlashFor($path);
  383. $this->assertEqual($result, '\\');
  384. }
  385. /**
  386. * testInCakePath method
  387. *
  388. * @access public
  389. * @return void
  390. */
  391. function testInCakePath() {
  392. $Folder =& new Folder();
  393. $Folder->cd(ROOT);
  394. $path = 'C:\\path\\to\\file';
  395. $result = $Folder->inCakePath($path);
  396. $this->assertFalse($result);
  397. $path = ROOT;
  398. $Folder->cd(ROOT);
  399. $result = $Folder->inCakePath($path);
  400. $this->assertFalse($result);
  401. // WHY DOES THIS FAIL ??
  402. $path = DS . 'cake' . DS . 'config';
  403. $Folder->cd(ROOT . DS . 'cake' . DS . 'config');
  404. $result = $Folder->inCakePath($path);
  405. $this->assertTrue($result);
  406. }
  407. /**
  408. * testFind method
  409. *
  410. * @access public
  411. * @return void
  412. */
  413. function testFind() {
  414. $Folder =& new Folder();
  415. $Folder->cd(TEST_CAKE_CORE_INCLUDE_PATH . 'config');
  416. $result = $Folder->find();
  417. $expected = array('config.php', 'paths.php');
  418. $this->assertIdentical(array_diff($expected, $result), array());
  419. $this->assertIdentical(array_diff($result, $expected), array());
  420. $result = $Folder->find('.*', true);
  421. $expected = array('config.php', 'paths.php');
  422. $this->assertIdentical($result, $expected);
  423. $result = $Folder->find('.*\.php');
  424. $expected = array('config.php', 'paths.php');
  425. $this->assertIdentical(array_diff($expected, $result), array());
  426. $this->assertIdentical(array_diff($result, $expected), array());
  427. $result = $Folder->find('.*\.php', true);
  428. $expected = array('config.php', 'paths.php');
  429. $this->assertIdentical($result, $expected);
  430. $result = $Folder->find('.*ig\.php');
  431. $expected = array('config.php');
  432. $this->assertIdentical($result, $expected);
  433. $result = $Folder->find('paths\.php');
  434. $expected = array('paths.php');
  435. $this->assertIdentical($result, $expected);
  436. $Folder->cd(TMP);
  437. $file = new File($Folder->pwd() . DS . 'paths.php', true);
  438. $Folder->create($Folder->pwd() . DS . 'testme');
  439. $Folder->cd('testme');
  440. $result = $Folder->find('paths\.php');
  441. $expected = array();
  442. $this->assertIdentical($result, $expected);
  443. $Folder->cd($Folder->pwd() . '/..');
  444. $result = $Folder->find('paths\.php');
  445. $expected = array('paths.php');
  446. $this->assertIdentical($result, $expected);
  447. $Folder->cd(TMP);
  448. $Folder->delete($Folder->pwd() . DS . 'testme');
  449. $file->delete();
  450. }
  451. /**
  452. * testFindRecursive method
  453. *
  454. * @access public
  455. * @return void
  456. */
  457. function testFindRecursive() {
  458. $Folder =& new Folder();
  459. $Folder->cd(TEST_CAKE_CORE_INCLUDE_PATH);
  460. $result = $Folder->findRecursive('(config|paths)\.php');
  461. $expected = array(
  462. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php',
  463. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'paths.php'
  464. );
  465. $this->assertIdentical(array_diff($expected, $result), array());
  466. $this->assertIdentical(array_diff($result, $expected), array());
  467. $result = $Folder->findRecursive('(config|paths)\.php', true);
  468. $expected = array(
  469. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php',
  470. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'paths.php'
  471. );
  472. $this->assertIdentical($result, $expected);
  473. $Folder->cd(TMP);
  474. $Folder->create($Folder->pwd() . DS . 'testme');
  475. $Folder->cd('testme');
  476. $File =& new File($Folder->pwd() . DS . 'paths.php');
  477. $File->create();
  478. $Folder->cd(TMP . 'sessions');
  479. $result = $Folder->findRecursive('paths\.php');
  480. $expected = array();
  481. $this->assertIdentical($result, $expected);
  482. $Folder->cd(TMP . 'testme');
  483. $File =& new File($Folder->pwd() . DS . 'my.php');
  484. $File->create();
  485. $Folder->cd($Folder->pwd() . '/../..');
  486. $result = $Folder->findRecursive('(paths|my)\.php');
  487. $expected = array(
  488. TMP . 'testme' . DS . 'my.php',
  489. TMP . 'testme' . DS . 'paths.php'
  490. );
  491. $this->assertIdentical(array_diff($expected, $result), array());
  492. $this->assertIdentical(array_diff($result, $expected), array());
  493. $result = $Folder->findRecursive('(paths|my)\.php', true);
  494. $expected = array(
  495. TMP . 'testme' . DS . 'my.php',
  496. TMP . 'testme' . DS . 'paths.php'
  497. );
  498. $this->assertIdentical($result, $expected);
  499. $Folder->cd(TEST_CAKE_CORE_INCLUDE_PATH . 'config');
  500. $Folder->cd(TMP);
  501. $Folder->delete($Folder->pwd() . DS . 'testme');
  502. $File->delete();
  503. }
  504. /**
  505. * testConstructWithNonExistantPath method
  506. *
  507. * @access public
  508. * @return void
  509. */
  510. function testConstructWithNonExistantPath() {
  511. $Folder =& new Folder(TMP . 'config_non_existant', true);
  512. $this->assertTrue(is_dir(TMP . 'config_non_existant'));
  513. $Folder->cd(TMP);
  514. $Folder->delete($Folder->pwd() . 'config_non_existant');
  515. }
  516. /**
  517. * testDirSize method
  518. *
  519. * @access public
  520. * @return void
  521. */
  522. function testDirSize() {
  523. $Folder =& new Folder(TMP . 'config_non_existant', true);
  524. $this->assertEqual($Folder->dirSize(), 0);
  525. $File =& new File($Folder->pwd() . DS . 'my.php', true, 0777);
  526. $File->create();
  527. $File->write('something here');
  528. $File->close();
  529. $this->assertEqual($Folder->dirSize(), 14);
  530. $Folder->cd(TMP);
  531. $Folder->delete($Folder->pwd() . 'config_non_existant');
  532. }
  533. /**
  534. * testDelete method
  535. *
  536. * @access public
  537. * @return void
  538. */
  539. function testDelete() {
  540. $path = TMP . 'folder_delete_test';
  541. $Folder =& new Folder($path, true);
  542. touch(TMP . 'folder_delete_test' . DS . 'file1');
  543. touch(TMP . 'folder_delete_test' . DS . 'file2');
  544. $return = $Folder->delete();
  545. $this->assertTrue($return);
  546. $messages = $Folder->messages();
  547. $errors = $Folder->errors();
  548. $this->assertEqual($errors, array());
  549. $expected = array(
  550. $path . ' created',
  551. $path . DS . 'file1 removed',
  552. $path . DS . 'file2 removed',
  553. $path . ' removed'
  554. );
  555. $this->assertEqual($expected, $messages);
  556. }
  557. /**
  558. * testCopy method
  559. *
  560. * Verify that directories and files are copied recursively
  561. * even if the destination directory already exists.
  562. * Subdirectories existing in both destination and source directory
  563. * are skipped and not merged or overwritten.
  564. *
  565. * @return void
  566. * @access public
  567. * @link https://trac.cakephp.org/ticket/6259
  568. */
  569. function testCopy() {
  570. $path = TMP . 'folder_test';
  571. $folder1 = $path . DS . 'folder1';
  572. $folder2 = $folder1 . DS . 'folder2';
  573. $folder3 = $path . DS . 'folder3';
  574. $file1 = $folder1 . DS . 'file1.php';
  575. $file2 = $folder2 . DS . 'file2.php';
  576. new Folder($path, true);
  577. new Folder($folder1, true);
  578. new Folder($folder2, true);
  579. new Folder($folder3, true);
  580. touch($file1);
  581. touch($file2);
  582. $Folder =& new Folder($folder1);
  583. $result = $Folder->copy($folder3);
  584. $this->assertTrue($result);
  585. $this->assertTrue(file_exists($folder3 . DS . 'file1.php'));
  586. $this->assertTrue(file_exists($folder3 . DS . 'folder2' . DS . 'file2.php'));
  587. $Folder =& new Folder($folder3);
  588. $Folder->delete();
  589. $Folder =& new Folder($folder1);
  590. $result = $Folder->copy($folder3);
  591. $this->assertTrue($result);
  592. $this->assertTrue(file_exists($folder3 . DS . 'file1.php'));
  593. $this->assertTrue(file_exists($folder3 . DS . 'folder2' . DS . 'file2.php'));
  594. $Folder =& new Folder($folder3);
  595. $Folder->delete();
  596. new Folder($folder3, true);
  597. new Folder($folder3 . DS . 'folder2', true);
  598. file_put_contents($folder3 . DS . 'folder2' . DS . 'file2.php', 'untouched');
  599. $Folder =& new Folder($folder1);
  600. $result = $Folder->copy($folder3);
  601. $this->assertTrue($result);
  602. $this->assertTrue(file_exists($folder3 . DS . 'file1.php'));
  603. $this->assertEqual(file_get_contents($folder3 . DS . 'folder2' . DS . 'file2.php'), 'untouched');
  604. $Folder =& new Folder($path);
  605. $Folder->delete();
  606. }
  607. /**
  608. * testMove method
  609. *
  610. * Verify that directories and files are moved recursively
  611. * even if the destination directory already exists.
  612. * Subdirectories existing in both destination and source directory
  613. * are skipped and not merged or overwritten.
  614. *
  615. * @return void
  616. * @access public
  617. * @link https://trac.cakephp.org/ticket/6259
  618. */
  619. function testMove() {
  620. $path = TMP . 'folder_test';
  621. $folder1 = $path . DS . 'folder1';
  622. $folder2 = $folder1 . DS . 'folder2';
  623. $folder3 = $path . DS . 'folder3';
  624. $file1 = $folder1 . DS . 'file1.php';
  625. $file2 = $folder2 . DS . 'file2.php';
  626. new Folder($path, true);
  627. new Folder($folder1, true);
  628. new Folder($folder2, true);
  629. new Folder($folder3, true);
  630. touch($file1);
  631. touch($file2);
  632. $Folder =& new Folder($folder1);
  633. $result = $Folder->move($folder3);
  634. $this->assertTrue($result);
  635. $this->assertTrue(file_exists($folder3 . DS . 'file1.php'));
  636. $this->assertTrue(is_dir($folder3 . DS . 'folder2'));
  637. $this->assertTrue(file_exists($folder3 . DS . 'folder2' . DS . 'file2.php'));
  638. $this->assertFalse(file_exists($file1));
  639. $this->assertFalse(file_exists($folder2));
  640. $this->assertFalse(file_exists($file2));
  641. $Folder =& new Folder($folder3);
  642. $Folder->delete();
  643. new Folder($folder1, true);
  644. new Folder($folder2, true);
  645. touch($file1);
  646. touch($file2);
  647. $Folder =& new Folder($folder1);
  648. $result = $Folder->move($folder3);
  649. $this->assertTrue($result);
  650. $this->assertTrue(file_exists($folder3 . DS . 'file1.php'));
  651. $this->assertTrue(is_dir($folder3 . DS . 'folder2'));
  652. $this->assertTrue(file_exists($folder3 . DS . 'folder2' . DS . 'file2.php'));
  653. $this->assertFalse(file_exists($file1));
  654. $this->assertFalse(file_exists($folder2));
  655. $this->assertFalse(file_exists($file2));
  656. $Folder =& new Folder($folder3);
  657. $Folder->delete();
  658. new Folder($folder1, true);
  659. new Folder($folder2, true);
  660. new Folder($folder3, true);
  661. new Folder($folder3 . DS . 'folder2', true);
  662. touch($file1);
  663. touch($file2);
  664. file_put_contents($folder3 . DS . 'folder2' . DS . 'file2.php', 'untouched');
  665. $Folder =& new Folder($folder1);
  666. $result = $Folder->move($folder3);
  667. $this->assertTrue($result);
  668. $this->assertTrue(file_exists($folder3 . DS . 'file1.php'));
  669. $this->assertEqual(file_get_contents($folder3 . DS . 'folder2' . DS . 'file2.php'), 'untouched');
  670. $this->assertFalse(file_exists($file1));
  671. $this->assertFalse(file_exists($folder2));
  672. $this->assertFalse(file_exists($file2));
  673. $Folder =& new Folder($path);
  674. $Folder->delete();
  675. }
  676. }