PageRenderTime 28ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/FolderTest.php

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 1230 lines | 1031 code | 78 blank | 121 comment | 7 complexity | dd454fc7cdecee8e431e5bfd8675ff10 MD5 | raw file
  1. <?php
  2. /**
  3. * FolderTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Utility
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Folder', 'Utility');
  19. App::uses('File', 'Utility');
  20. /**
  21. * FolderTest class
  22. *
  23. * @package Cake.Test.Case.Utility
  24. */
  25. class FolderTest extends CakeTestCase {
  26. protected static $_tmp = array();
  27. /**
  28. * Save the directory names in TMP and make sure default directories exist
  29. *
  30. * @return void
  31. */
  32. public static function setUpBeforeClass() {
  33. $dirs = array('cache', 'logs', 'sessions', 'tests');
  34. foreach ($dirs as $dir) {
  35. new Folder(TMP . $dir, true);
  36. }
  37. foreach (scandir(TMP) as $file) {
  38. if (is_dir(TMP . $file) && !in_array($file, array('.', '..'))) {
  39. self::$_tmp[] = $file;
  40. }
  41. }
  42. }
  43. /**
  44. * setUp clearstatcache() to flush file descriptors.
  45. *
  46. * @return void
  47. */
  48. public function setUp() {
  49. parent::setUp();
  50. clearstatcache();
  51. }
  52. /**
  53. * Restore the TMP directory to its original state.
  54. *
  55. * @return void
  56. */
  57. public function tearDown() {
  58. $exclude = array_merge(self::$_tmp, array('.', '..'));
  59. foreach (scandir(TMP) as $dir) {
  60. if (is_dir(TMP . $dir) && !in_array($dir, $exclude)) {
  61. $iterator = new RecursiveDirectoryIterator(TMP . $dir);
  62. foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
  63. if ($file->isFile() || $file->isLink()) {
  64. unlink($file->getPathname());
  65. } elseif ($file->isDir() && !in_array($file->getFilename(), array('.', '..'))) {
  66. rmdir($file->getPathname());
  67. }
  68. }
  69. rmdir(TMP . $dir);
  70. }
  71. }
  72. parent::tearDown();
  73. }
  74. /**
  75. * testBasic method
  76. *
  77. * @return void
  78. */
  79. public function testBasic() {
  80. $path = dirname(__FILE__);
  81. $Folder = new Folder($path);
  82. $result = $Folder->pwd();
  83. $this->assertEquals($path, $result);
  84. $result = Folder::addPathElement($path, 'test');
  85. $expected = $path . DS . 'test';
  86. $this->assertEquals($expected, $result);
  87. $result = $Folder->cd(ROOT);
  88. $expected = ROOT;
  89. $this->assertEquals($expected, $result);
  90. $result = $Folder->cd(ROOT . DS . 'non-existent');
  91. $this->assertFalse($result);
  92. }
  93. /**
  94. * testInPath method
  95. *
  96. * @return void
  97. */
  98. public function testInPath() {
  99. $path = dirname(dirname(__FILE__));
  100. $inside = dirname($path) . DS;
  101. $Folder = new Folder($path);
  102. $result = $Folder->pwd();
  103. $this->assertEquals($path, $result);
  104. $result = Folder::isSlashTerm($inside);
  105. $this->assertTrue($result);
  106. $result = $Folder->realpath('Test/');
  107. $this->assertEquals($path . DS . 'Test' . DS, $result);
  108. $result = $Folder->inPath('Test' . DS);
  109. $this->assertTrue($result);
  110. $result = $Folder->inPath(DS . 'non-existing' . $inside);
  111. $this->assertFalse($result);
  112. $result = $Folder->inPath($path . DS . 'Model', true);
  113. $this->assertTrue($result);
  114. }
  115. /**
  116. * test creation of single and multiple paths.
  117. *
  118. * @return void
  119. */
  120. public function testCreation() {
  121. $Folder = new Folder(TMP . 'tests');
  122. $result = $Folder->create(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
  123. $this->assertTrue($result);
  124. rmdir(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
  125. rmdir(TMP . 'tests' . DS . 'first' . DS . 'second');
  126. rmdir(TMP . 'tests' . DS . 'first');
  127. $Folder = new Folder(TMP . 'tests');
  128. $result = $Folder->create(TMP . 'tests' . DS . 'first');
  129. $this->assertTrue($result);
  130. rmdir(TMP . 'tests' . DS . 'first');
  131. }
  132. /**
  133. * test that creation of folders with trailing ds works
  134. *
  135. * @return void
  136. */
  137. public function testCreateWithTrailingDs() {
  138. $Folder = new Folder(TMP);
  139. $path = TMP . 'tests' . DS . 'trailing' . DS . 'dir' . DS;
  140. $result = $Folder->create($path);
  141. $this->assertTrue($result);
  142. $this->assertTrue(is_dir($path), 'Folder was not made');
  143. $Folder = new Folder(TMP . 'tests' . DS . 'trailing');
  144. $this->assertTrue($Folder->delete());
  145. }
  146. /**
  147. * Test that relative paths to create() are added to cwd.
  148. *
  149. * @return void
  150. */
  151. public function testCreateRelative() {
  152. $folder = new Folder(TMP);
  153. $path = TMP . 'tests' . DS . 'relative-test';
  154. $result = $folder->create('tests' . DS . 'relative-test');
  155. $this->assertTrue($result, 'should create');
  156. $this->assertTrue(is_dir($path), 'Folder was not made');
  157. $folder = new Folder($path);
  158. $folder->delete();
  159. }
  160. /**
  161. * test recursive directory create failure.
  162. *
  163. * @return void
  164. */
  165. public function testRecursiveCreateFailure() {
  166. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Cant perform operations using permissions on windows.');
  167. $path = TMP . 'tests' . DS . 'one';
  168. mkdir($path);
  169. chmod($path, '0444');
  170. try {
  171. $Folder = new Folder($path);
  172. $result = $Folder->create($path . DS . 'two' . DS . 'three');
  173. $this->assertFalse($result);
  174. } catch (PHPUnit_Framework_Error $e) {
  175. $this->assertTrue(true);
  176. }
  177. chmod($path, '0777');
  178. rmdir($path);
  179. }
  180. /**
  181. * testOperations method
  182. *
  183. * @return void
  184. */
  185. public function testOperations() {
  186. $path = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  187. $Folder = new Folder($path);
  188. $result = is_dir($Folder->pwd());
  189. $this->assertTrue($result);
  190. $new = TMP . 'test_folder_new';
  191. $result = $Folder->create($new);
  192. $this->assertTrue($result);
  193. $copy = TMP . 'test_folder_copy';
  194. $result = $Folder->copy($copy);
  195. $this->assertTrue($result);
  196. $copy = TMP . 'test_folder_copy';
  197. $result = $Folder->copy($copy);
  198. $this->assertTrue($result);
  199. $copy = TMP . 'test_folder_copy';
  200. $result = $Folder->chmod($copy, 0755, false);
  201. $this->assertTrue($result);
  202. $result = $Folder->cd($copy);
  203. $this->assertTrue((bool)$result);
  204. $mv = TMP . 'test_folder_mv';
  205. $result = $Folder->move($mv);
  206. $this->assertTrue($result);
  207. $mv = TMP . 'test_folder_mv_2';
  208. $result = $Folder->move($mv);
  209. $this->assertTrue($result);
  210. $result = $Folder->delete($new);
  211. $this->assertTrue($result);
  212. $result = $Folder->delete($mv);
  213. $this->assertTrue($result);
  214. $result = $Folder->delete($mv);
  215. $this->assertTrue($result);
  216. $new = APP . 'index.php';
  217. $result = $Folder->create($new);
  218. $this->assertFalse($result);
  219. $expected = $new . ' is a file';
  220. $result = $Folder->errors();
  221. $this->assertEquals($expected, $result[0]);
  222. $new = TMP . 'test_folder_new';
  223. $result = $Folder->create($new);
  224. $this->assertTrue($result);
  225. $result = $Folder->cd($new);
  226. $this->assertTrue((bool)$result);
  227. $result = $Folder->delete();
  228. $this->assertTrue($result);
  229. $Folder = new Folder('non-existent');
  230. $result = $Folder->pwd();
  231. $this->assertNull($result);
  232. }
  233. /**
  234. * testChmod method
  235. *
  236. * @return void
  237. */
  238. public function testChmod() {
  239. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Folder permissions tests not supported on Windows.');
  240. $path = TMP;
  241. $Folder = new Folder($path);
  242. $subdir = 'test_folder_new';
  243. $new = TMP . $subdir;
  244. $this->assertTrue($Folder->create($new));
  245. $this->assertTrue($Folder->create($new . DS . 'test1'));
  246. $this->assertTrue($Folder->create($new . DS . 'test2'));
  247. $filePath = $new . DS . 'test1.php';
  248. $File = new File($filePath);
  249. $this->assertTrue($File->create());
  250. $filePath = $new . DS . 'skip_me.php';
  251. $File = new File($filePath);
  252. $this->assertTrue($File->create());
  253. $this->assertTrue($Folder->chmod($new, 0755, true));
  254. $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
  255. $this->assertEquals('0755', $perms);
  256. $this->assertTrue($Folder->chmod($new, 0744, true, array('skip_me.php', 'test2')));
  257. $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
  258. $this->assertEquals('0755', $perms);
  259. $perms = substr(sprintf('%o', fileperms($new . DS . 'test1')), -4);
  260. $this->assertEquals('0744', $perms);
  261. $Folder->delete($new);
  262. }
  263. /**
  264. * testRealPathForWebroot method
  265. *
  266. * @return void
  267. */
  268. public function testRealPathForWebroot() {
  269. $Folder = new Folder('files/');
  270. $this->assertEquals(realpath('files/'), $Folder->path);
  271. }
  272. /**
  273. * testZeroAsDirectory method
  274. *
  275. * @return void
  276. */
  277. public function testZeroAsDirectory() {
  278. $Folder = new Folder(TMP);
  279. $new = TMP . '0';
  280. $this->assertTrue($Folder->create($new));
  281. $result = $Folder->read(true, true);
  282. $expected = array('0', 'cache', 'logs', 'sessions', 'tests');
  283. $this->assertEquals($expected, $result[0]);
  284. $result = $Folder->read(true, array('logs'));
  285. $expected = array('0', 'cache', 'sessions', 'tests');
  286. $this->assertEquals($expected, $result[0]);
  287. $result = $Folder->delete($new);
  288. $this->assertTrue($result);
  289. }
  290. /**
  291. * test Adding path elements to a path
  292. *
  293. * @return void
  294. */
  295. public function testAddPathElement() {
  296. $expected = DS . 'some' . DS . 'dir' . DS . 'another_path';
  297. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path');
  298. $this->assertEquals($expected, $result);
  299. $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path');
  300. $this->assertEquals($expected, $result);
  301. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', array('another_path'));
  302. $this->assertEquals($expected, $result);
  303. $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, array('another_path'));
  304. $this->assertEquals($expected, $result);
  305. $expected = DS . 'some' . DS . 'dir' . DS . 'another_path' . DS . 'and' . DS . 'another';
  306. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', array('another_path', 'and', 'another'));
  307. $this->assertEquals($expected, $result);
  308. }
  309. /**
  310. * testFolderRead method
  311. *
  312. * @return void
  313. */
  314. public function testFolderRead() {
  315. $Folder = new Folder(TMP);
  316. $expected = array('cache', 'logs', 'sessions', 'tests');
  317. $result = $Folder->read(true, true);
  318. $this->assertEquals($expected, $result[0]);
  319. $Folder->path = TMP . 'non-existent';
  320. $expected = array(array(), array());
  321. $result = $Folder->read(true, true);
  322. $this->assertEquals($expected, $result);
  323. }
  324. /**
  325. * testFolderReadWithHiddenFiles method
  326. *
  327. * @return void
  328. */
  329. public function testFolderReadWithHiddenFiles() {
  330. $this->skipIf(!is_writable(TMP), 'Cant test Folder::read with hidden files unless the tmp folder is writable.');
  331. $Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
  332. mkdir($Folder->path . DS . '.svn');
  333. mkdir($Folder->path . DS . 'some_folder');
  334. touch($Folder->path . DS . 'not_hidden.txt');
  335. touch($Folder->path . DS . '.hidden.txt');
  336. $expected = array(
  337. array('some_folder'),
  338. array('not_hidden.txt'),
  339. );
  340. $result = $Folder->read(true, true);
  341. $this->assertEquals($expected, $result);
  342. $expected = array(
  343. array(
  344. '.svn',
  345. 'some_folder'
  346. ),
  347. array(
  348. '.hidden.txt',
  349. 'not_hidden.txt'
  350. ),
  351. );
  352. $result = $Folder->read(true);
  353. $this->assertEquals($expected, $result);
  354. }
  355. /**
  356. * testFolderTree method
  357. *
  358. * @return void
  359. */
  360. public function testFolderTree() {
  361. $Folder = new Folder();
  362. $expected = array(
  363. array(
  364. CAKE . 'Config',
  365. CAKE . 'Config' . DS . 'unicode',
  366. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding'
  367. ),
  368. array(
  369. CAKE . 'Config' . DS . 'config.php',
  370. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0080_00ff.php',
  371. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0100_017f.php',
  372. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0180_024F.php',
  373. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0250_02af.php',
  374. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0370_03ff.php',
  375. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0400_04ff.php',
  376. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0500_052f.php',
  377. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0530_058f.php',
  378. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '1e00_1eff.php',
  379. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '1f00_1fff.php',
  380. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2100_214f.php',
  381. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2150_218f.php',
  382. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2460_24ff.php',
  383. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c00_2c5f.php',
  384. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c60_2c7f.php',
  385. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c80_2cff.php',
  386. CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . 'ff00_ffef.php'
  387. )
  388. );
  389. $result = $Folder->tree(CAKE . 'Config', false);
  390. $this->assertSame(array(), array_diff($expected[0], $result[0]));
  391. $this->assertSame(array(), array_diff($result[0], $expected[0]));
  392. $result = $Folder->tree(CAKE . 'Config', false, 'dir');
  393. $this->assertSame(array(), array_diff($expected[0], $result));
  394. $this->assertSame(array(), array_diff($expected[0], $result));
  395. $result = $Folder->tree(CAKE . 'Config', false, 'files');
  396. $this->assertSame(array(), array_diff($expected[1], $result));
  397. $this->assertSame(array(), array_diff($expected[1], $result));
  398. }
  399. /**
  400. * testFolderTreeWithHiddenFiles method
  401. *
  402. * @return void
  403. */
  404. public function testFolderTreeWithHiddenFiles() {
  405. $this->skipIf(!is_writable(TMP), 'Can\'t test Folder::tree with hidden files unless the tmp folder is writable.');
  406. $Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
  407. mkdir($Folder->path . DS . '.svn', 0777, true);
  408. touch($Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php');
  409. mkdir($Folder->path . DS . '.svn' . DS . 'inhiddenfolder');
  410. touch($Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php');
  411. touch($Folder->path . DS . 'not_hidden.txt');
  412. touch($Folder->path . DS . '.hidden.txt');
  413. mkdir($Folder->path . DS . 'visible_folder' . DS . '.git', 0777, true);
  414. $expected = array(
  415. array(
  416. $Folder->path,
  417. $Folder->path . DS . 'visible_folder',
  418. ),
  419. array(
  420. $Folder->path . DS . 'not_hidden.txt',
  421. ),
  422. );
  423. $result = $Folder->tree(null, true);
  424. $this->assertEquals($expected, $result);
  425. $result = $Folder->tree(null, array('.'));
  426. $this->assertEquals($expected, $result);
  427. $expected = array(
  428. array(
  429. $Folder->path,
  430. $Folder->path . DS . 'visible_folder',
  431. $Folder->path . DS . 'visible_folder' . DS . '.git',
  432. $Folder->path . DS . '.svn',
  433. $Folder->path . DS . '.svn' . DS . 'inhiddenfolder',
  434. ),
  435. array(
  436. $Folder->path . DS . 'not_hidden.txt',
  437. $Folder->path . DS . '.hidden.txt',
  438. $Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php',
  439. $Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php',
  440. ),
  441. );
  442. $result = $Folder->tree(null, false);
  443. sort($result[0]);
  444. sort($expected[0]);
  445. sort($result[1]);
  446. sort($expected[1]);
  447. $this->assertEquals($expected, $result);
  448. $Folder->delete();
  449. }
  450. /**
  451. * testWindowsPath method
  452. *
  453. * @return void
  454. */
  455. public function testWindowsPath() {
  456. $this->assertFalse(Folder::isWindowsPath('0:\\cake\\is\\awesome'));
  457. $this->assertTrue(Folder::isWindowsPath('C:\\cake\\is\\awesome'));
  458. $this->assertTrue(Folder::isWindowsPath('d:\\cake\\is\\awesome'));
  459. $this->assertTrue(Folder::isWindowsPath('\\\\vmware-host\\Shared Folders\\file'));
  460. }
  461. /**
  462. * testIsAbsolute method
  463. *
  464. * @return void
  465. */
  466. public function testIsAbsolute() {
  467. $this->assertFalse(Folder::isAbsolute('path/to/file'));
  468. $this->assertFalse(Folder::isAbsolute('cake/'));
  469. $this->assertFalse(Folder::isAbsolute('path\\to\\file'));
  470. $this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
  471. $this->assertFalse(Folder::isAbsolute('\\path/to/file'));
  472. $this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
  473. $this->assertFalse(Folder::isAbsolute('notRegisteredStreamWrapper://example'));
  474. $this->assertFalse(Folder::isAbsolute('://example'));
  475. $this->assertTrue(Folder::isAbsolute('/usr/local'));
  476. $this->assertTrue(Folder::isAbsolute('//path/to/file'));
  477. $this->assertTrue(Folder::isAbsolute('C:\\cake'));
  478. $this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
  479. $this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
  480. $this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
  481. $this->assertTrue(Folder::isAbsolute('http://www.example.com'));
  482. }
  483. /**
  484. * testIsSlashTerm method
  485. *
  486. * @return void
  487. */
  488. public function testIsSlashTerm() {
  489. $this->assertFalse(Folder::isSlashTerm('cake'));
  490. $this->assertTrue(Folder::isSlashTerm('C:\\cake\\'));
  491. $this->assertTrue(Folder::isSlashTerm('/usr/local/'));
  492. }
  493. /**
  494. * testStatic method
  495. *
  496. * @return void
  497. */
  498. public function testSlashTerm() {
  499. $result = Folder::slashTerm('/path/to/file');
  500. $this->assertEquals('/path/to/file/', $result);
  501. }
  502. /**
  503. * testNormalizePath method
  504. *
  505. * @return void
  506. */
  507. public function testNormalizePath() {
  508. $path = '/path/to/file';
  509. $result = Folder::normalizePath($path);
  510. $this->assertEquals('/', $result);
  511. $path = '\\path\\\to\\\file';
  512. $result = Folder::normalizePath($path);
  513. $this->assertEquals('/', $result);
  514. $path = 'C:\\path\\to\\file';
  515. $result = Folder::normalizePath($path);
  516. $this->assertEquals('\\', $result);
  517. }
  518. /**
  519. * correctSlashFor method
  520. *
  521. * @return void
  522. */
  523. public function testCorrectSlashFor() {
  524. $path = '/path/to/file';
  525. $result = Folder::correctSlashFor($path);
  526. $this->assertEquals('/', $result);
  527. $path = '\\path\\to\\file';
  528. $result = Folder::correctSlashFor($path);
  529. $this->assertEquals('/', $result);
  530. $path = 'C:\\path\to\\file';
  531. $result = Folder::correctSlashFor($path);
  532. $this->assertEquals('\\', $result);
  533. }
  534. /**
  535. * testInCakePath method
  536. *
  537. * @return void
  538. */
  539. public function testInCakePath() {
  540. $Folder = new Folder();
  541. $Folder->cd(ROOT);
  542. $path = 'C:\\path\\to\\file';
  543. $result = $Folder->inCakePath($path);
  544. $this->assertFalse($result);
  545. $path = ROOT;
  546. $Folder->cd(ROOT);
  547. $result = $Folder->inCakePath($path);
  548. $this->assertFalse($result);
  549. $path = DS . 'lib' . DS . 'Cake' . DS . 'Config';
  550. $Folder->cd(ROOT . DS . 'lib' . DS . 'Cake' . DS . 'Config');
  551. $result = $Folder->inCakePath($path);
  552. $this->assertTrue($result);
  553. }
  554. /**
  555. * testFind method
  556. *
  557. * @return void
  558. */
  559. public function testFind() {
  560. $Folder = new Folder();
  561. $Folder->cd(CAKE . 'Config');
  562. $result = $Folder->find();
  563. $expected = array('config.php');
  564. $this->assertSame(array_diff($expected, $result), array());
  565. $this->assertSame(array_diff($expected, $result), array());
  566. $result = $Folder->find('.*', true);
  567. $expected = array('cacert.pem', 'config.php', 'routes.php');
  568. $this->assertSame($expected, $result);
  569. $result = $Folder->find('.*\.php');
  570. $expected = array('config.php');
  571. $this->assertSame(array_diff($expected, $result), array());
  572. $this->assertSame(array_diff($expected, $result), array());
  573. $result = $Folder->find('.*\.php', true);
  574. $expected = array('config.php', 'routes.php');
  575. $this->assertSame($expected, $result);
  576. $result = $Folder->find('.*ig\.php');
  577. $expected = array('config.php');
  578. $this->assertSame($expected, $result);
  579. $result = $Folder->find('config\.php');
  580. $expected = array('config.php');
  581. $this->assertSame($expected, $result);
  582. $Folder->cd(TMP);
  583. $File = new File($Folder->pwd() . DS . 'paths.php', true);
  584. $Folder->create($Folder->pwd() . DS . 'testme');
  585. $Folder->cd('testme');
  586. $result = $Folder->find('paths\.php');
  587. $expected = array();
  588. $this->assertSame($expected, $result);
  589. $Folder->cd($Folder->pwd() . '/..');
  590. $result = $Folder->find('paths\.php');
  591. $expected = array('paths.php');
  592. $this->assertSame($expected, $result);
  593. $Folder->cd(TMP);
  594. $Folder->delete($Folder->pwd() . DS . 'testme');
  595. $File->delete();
  596. }
  597. /**
  598. * testFindRecursive method
  599. *
  600. * @return void
  601. */
  602. public function testFindRecursive() {
  603. $Folder = new Folder();
  604. $Folder->cd(CAKE);
  605. $result = $Folder->findRecursive('(config|paths)\.php');
  606. $expected = array(
  607. CAKE . 'Config' . DS . 'config.php'
  608. );
  609. $this->assertSame(array_diff($expected, $result), array());
  610. $this->assertSame(array_diff($expected, $result), array());
  611. $result = $Folder->findRecursive('(config|paths)\.php', true);
  612. $expected = array(
  613. CAKE . 'Config' . DS . 'config.php'
  614. );
  615. $this->assertSame($expected, $result);
  616. $Folder->cd(TMP);
  617. $Folder->create($Folder->pwd() . DS . 'testme');
  618. $Folder->cd('testme');
  619. $File = new File($Folder->pwd() . DS . 'paths.php');
  620. $File->create();
  621. $Folder->cd(TMP . 'sessions');
  622. $result = $Folder->findRecursive('paths\.php');
  623. $expected = array();
  624. $this->assertSame($expected, $result);
  625. $Folder->cd(TMP . 'testme');
  626. $File = new File($Folder->pwd() . DS . 'my.php');
  627. $File->create();
  628. $Folder->cd($Folder->pwd() . '/../..');
  629. $result = $Folder->findRecursive('(paths|my)\.php');
  630. $expected = array(
  631. TMP . 'testme' . DS . 'my.php',
  632. TMP . 'testme' . DS . 'paths.php'
  633. );
  634. $this->assertSame(array_diff($expected, $result), array());
  635. $this->assertSame(array_diff($expected, $result), array());
  636. $result = $Folder->findRecursive('(paths|my)\.php', true);
  637. $expected = array(
  638. TMP . 'testme' . DS . 'my.php',
  639. TMP . 'testme' . DS . 'paths.php'
  640. );
  641. $this->assertSame($expected, $result);
  642. $Folder->cd(CAKE . 'Config');
  643. $Folder->cd(TMP);
  644. $Folder->delete($Folder->pwd() . DS . 'testme');
  645. $File->delete();
  646. }
  647. /**
  648. * testConstructWithNonExistentPath method
  649. *
  650. * @return void
  651. */
  652. public function testConstructWithNonExistentPath() {
  653. $Folder = new Folder(TMP . 'config_non_existent', true);
  654. $this->assertTrue(is_dir(TMP . 'config_non_existent'));
  655. $Folder->cd(TMP);
  656. $Folder->delete($Folder->pwd() . 'config_non_existent');
  657. }
  658. /**
  659. * testDirSize method
  660. *
  661. * @return void
  662. */
  663. public function testDirSize() {
  664. $Folder = new Folder(TMP . 'config_non_existent', true);
  665. $this->assertEquals(0, $Folder->dirSize());
  666. $File = new File($Folder->pwd() . DS . 'my.php', true, 0777);
  667. $File->create();
  668. $File->write('something here');
  669. $File->close();
  670. $this->assertEquals(14, $Folder->dirSize());
  671. $Folder->cd(TMP);
  672. $Folder->delete($Folder->pwd() . 'config_non_existent');
  673. }
  674. /**
  675. * test that errors and messages can be resetted
  676. *
  677. * @return void
  678. */
  679. public function testReset() {
  680. $path = TMP . 'folder_delete_test';
  681. mkdir($path);
  682. $folder = $path . DS . 'sub';
  683. mkdir($folder);
  684. $file = $folder . DS . 'file';
  685. touch($file);
  686. chmod($folder, 0555);
  687. chmod($file, 0444);
  688. $Folder = new Folder($folder);
  689. $return = $Folder->delete();
  690. $this->assertFalse($return);
  691. $messages = $Folder->messages();
  692. $errors = $Folder->errors();
  693. $expected = array(
  694. $file . ' NOT removed',
  695. $folder . ' NOT removed',
  696. );
  697. sort($expected);
  698. sort($errors);
  699. $this->assertEmpty($messages);
  700. $this->assertEquals($expected, $errors);
  701. chmod($file, 0644);
  702. chmod($folder, 0755);
  703. $return = $Folder->delete();
  704. $this->assertTrue($return);
  705. $messages = $Folder->messages();
  706. $errors = $Folder->errors();
  707. $expected = array(
  708. $file . ' removed',
  709. $folder . ' removed',
  710. );
  711. sort($expected);
  712. sort($messages);
  713. $this->assertEmpty($errors);
  714. $this->assertEquals($expected, $messages);
  715. }
  716. /**
  717. * testDelete method
  718. *
  719. * @return void
  720. */
  721. public function testDelete() {
  722. $path = TMP . 'folder_delete_test';
  723. mkdir($path);
  724. touch($path . DS . 'file_1');
  725. mkdir($path . DS . 'level_1_1');
  726. touch($path . DS . 'level_1_1' . DS . 'file_1_1');
  727. mkdir($path . DS . 'level_1_1' . DS . 'level_2_1');
  728. touch($path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1');
  729. touch($path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2');
  730. mkdir($path . DS . 'level_1_1' . DS . 'level_2_2');
  731. $Folder = new Folder($path, true);
  732. $return = $Folder->delete();
  733. $this->assertTrue($return);
  734. $messages = $Folder->messages();
  735. $errors = $Folder->errors();
  736. $this->assertEquals(array(), $errors);
  737. $expected = array(
  738. $path . DS . 'file_1 removed',
  739. $path . DS . 'level_1_1' . DS . 'file_1_1 removed',
  740. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1 removed',
  741. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2 removed',
  742. $path . DS . 'level_1_1' . DS . 'level_2_1 removed',
  743. $path . DS . 'level_1_1' . DS . 'level_2_2 removed',
  744. $path . DS . 'level_1_1 removed',
  745. $path . ' removed'
  746. );
  747. sort($expected);
  748. sort($messages);
  749. $this->assertEquals($expected, $messages);
  750. }
  751. /**
  752. * testCopy method
  753. *
  754. * Verify that subdirectories existing in both destination and source directory
  755. * are merged recursively.
  756. *
  757. * @return void
  758. */
  759. public function testCopy() {
  760. extract($this->_setupFilesystem());
  761. $Folder = new Folder($folderOne);
  762. $result = $Folder->copy($folderThree);
  763. $this->assertTrue($result);
  764. $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
  765. $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
  766. $Folder = new Folder($folderTwo);
  767. $result = $Folder->copy($folderThree);
  768. $this->assertTrue($result);
  769. $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
  770. $this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
  771. $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
  772. $this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
  773. $Folder = new Folder($path);
  774. $Folder->delete();
  775. }
  776. /**
  777. * testCopyWithMerge method
  778. *
  779. * Verify that subdirectories existing in both destination and source directory
  780. * are merged recursively.
  781. *
  782. * @return void
  783. */
  784. public function testCopyWithMerge() {
  785. extract($this->_setupFilesystem());
  786. $Folder = new Folder($folderOne);
  787. $result = $Folder->copy($folderThree);
  788. $this->assertTrue($result);
  789. $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
  790. $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
  791. $Folder = new Folder($folderTwo);
  792. $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::MERGE));
  793. $this->assertTrue($result);
  794. $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
  795. $this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
  796. $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
  797. $this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
  798. $Folder = new Folder($path);
  799. $Folder->delete();
  800. }
  801. /**
  802. * testCopyWithSkip method
  803. *
  804. * Verify that directories and files are copied recursively
  805. * even if the destination directory already exists.
  806. * Subdirectories existing in both destination and source directory
  807. * are skipped and not merged or overwritten.
  808. *
  809. * @return void
  810. */
  811. public function testCopyWithSkip() {
  812. extract($this->_setupFilesystem());
  813. $Folder = new Folder($folderOne);
  814. $result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
  815. $this->assertTrue($result);
  816. $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
  817. $this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
  818. $Folder = new Folder($folderTwo);
  819. $Folder->delete();
  820. $Folder = new Folder($folderOne);
  821. $result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
  822. $this->assertTrue($result);
  823. $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
  824. $this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
  825. $Folder = new Folder($folderTwo);
  826. $Folder->delete();
  827. new Folder($folderTwo, true);
  828. new Folder($folderTwo . DS . 'folderB', true);
  829. file_put_contents($folderTwo . DS . 'file2.php', 'touched');
  830. file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  831. $Folder = new Folder($folderTwo);
  832. $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::SKIP));
  833. $this->assertTrue($result);
  834. $this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
  835. $this->assertEquals('touched', file_get_contents($folderThree . DS . 'file2.php'));
  836. $this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folderB' . DS . 'fileB.php'));
  837. $Folder = new Folder($path);
  838. $Folder->delete();
  839. }
  840. /**
  841. * Test that SKIP mode skips files too.
  842. *
  843. * @return void
  844. */
  845. public function testCopyWithSkipFileSkipped() {
  846. $path = TMP . 'folder_test';
  847. $folderOne = $path . DS . 'folder1';
  848. $folderTwo = $path . DS . 'folder2';
  849. new Folder($path, true);
  850. new Folder($folderOne, true);
  851. new Folder($folderTwo, true);
  852. file_put_contents($folderOne . DS . 'fileA.txt', 'Folder One File');
  853. file_put_contents($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  854. $Folder = new Folder($folderOne);
  855. $result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
  856. $this->assertTrue($result);
  857. $this->assertEquals('Folder Two File', file_get_contents($folderTwo . DS . 'fileA.txt'));
  858. }
  859. /**
  860. * testCopyWithOverwrite
  861. *
  862. * Verify that subdirectories existing in both destination and source directory
  863. * are overwritten/replaced recursively.
  864. *
  865. * @return void
  866. */
  867. public function testCopyWithOverwrite() {
  868. extract($this->_setupFilesystem());
  869. $Folder = new Folder($folderOne);
  870. $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
  871. $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
  872. $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
  873. $Folder = new Folder($folderTwo);
  874. $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
  875. $this->assertTrue($result);
  876. $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
  877. $Folder = new Folder($folderOne);
  878. unlink($fileOneA);
  879. $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
  880. $this->assertTrue($result);
  881. $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
  882. $this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
  883. $this->assertTrue(!file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
  884. $this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
  885. $Folder = new Folder($path);
  886. $Folder->delete();
  887. }
  888. /**
  889. * Setup filesystem for copy tests
  890. * $path: folder_test/
  891. * - folder1/file1.php
  892. * - folder1/folderA/fileA.php
  893. * - folder2/file2.php
  894. * - folder2/folderB/fileB.php
  895. * - folder3/
  896. *
  897. * @return array Filenames to extract in the test methods
  898. */
  899. protected function _setupFilesystem() {
  900. $path = TMP . 'folder_test';
  901. $folderOne = $path . DS . 'folder1';
  902. $folderOneA = $folderOne . DS . 'folderA';
  903. $folderTwo = $path . DS . 'folder2';
  904. $folderTwoB = $folderTwo . DS . 'folderB';
  905. $folderThree = $path . DS . 'folder3';
  906. $fileOne = $folderOne . DS . 'file1.php';
  907. $fileTwo = $folderTwo . DS . 'file2.php';
  908. $fileOneA = $folderOneA . DS . 'fileA.php';
  909. $fileTwoB = $folderTwoB . DS . 'fileB.php';
  910. new Folder($path, true);
  911. new Folder($folderOne, true);
  912. new Folder($folderOneA, true);
  913. new Folder($folderTwo, true);
  914. new Folder($folderTwoB, true);
  915. new Folder($folderThree, true);
  916. touch($fileOne);
  917. touch($fileTwo);
  918. touch($fileOneA);
  919. touch($fileTwoB);
  920. return compact(
  921. 'path',
  922. 'folderOne', 'folderOneA', 'folderTwo', 'folderTwoB', 'folderThree',
  923. 'fileOne', 'fileOneA', 'fileTwo', 'fileTwoB');
  924. }
  925. /**
  926. * testMove method
  927. *
  928. * Verify that directories and files are moved recursively
  929. * even if the destination directory already exists.
  930. * Subdirectories existing in both destination and source directory
  931. * are merged recursively.
  932. *
  933. * @return void
  934. */
  935. public function testMove() {
  936. extract($this->_setupFilesystem());
  937. $Folder = new Folder($folderOne);
  938. $result = $Folder->move($folderTwo);
  939. $this->assertTrue($result);
  940. $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
  941. $this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
  942. $this->assertTrue(file_exists($folderTwo . DS . 'folderB' . DS . 'fileB.php'));
  943. $this->assertFalse(file_exists($fileOne));
  944. $this->assertTrue(file_exists($folderTwo . DS . 'folderA'));
  945. $this->assertFalse(file_exists($folderOneA));
  946. $this->assertFalse(file_exists($fileOneA));
  947. $Folder = new Folder($folderTwo);
  948. $Folder->delete();
  949. new Folder($folderOne, true);
  950. new Folder($folderOneA, true);
  951. touch($fileOne);
  952. touch($fileOneA);
  953. $Folder = new Folder($folderOne);
  954. $result = $Folder->move($folderTwo);
  955. $this->assertTrue($result);
  956. $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
  957. $this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
  958. $this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
  959. $this->assertFalse(file_exists($fileOne));
  960. $this->assertFalse(file_exists($folderOneA));
  961. $this->assertFalse(file_exists($fileOneA));
  962. $Folder = new Folder($folderTwo);
  963. $Folder->delete();
  964. new Folder($folderOne, true);
  965. new Folder($folderOneA, true);
  966. new Folder($folderTwo, true);
  967. new Folder($folderTwoB, true);
  968. touch($fileOne);
  969. touch($fileOneA);
  970. new Folder($folderOne . DS . 'folderB', true);
  971. touch($folderOne . DS . 'folderB' . DS . 'fileB.php');
  972. file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');
  973. $Folder = new Folder($folderOne);
  974. $result = $Folder->move($folderTwo);
  975. $this->assertTrue($result);
  976. $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
  977. $this->assertEquals('', file_get_contents($folderTwoB . DS . 'fileB.php'));
  978. $this->assertFalse(file_exists($fileOne));
  979. $this->assertFalse(file_exists($folderOneA));
  980. $this->assertFalse(file_exists($fileOneA));
  981. $Folder = new Folder($path);
  982. $Folder->delete();
  983. }
  984. /**
  985. * testMoveWithSkip method
  986. *
  987. * Verify that directories and files are moved recursively
  988. * even if the destination directory already exists.
  989. * Subdirectories existing in both destination and source directory
  990. * are skipped and not merged or overwritten.
  991. *
  992. * @return void
  993. */
  994. public function testMoveWithSkip() {
  995. extract($this->_setupFilesystem());
  996. $Folder = new Folder($folderOne);
  997. $result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
  998. $this->assertTrue($result);
  999. $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
  1000. $this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
  1001. $this->assertTrue(file_exists($folderTwoB . DS . 'fileB.php'));
  1002. $this->assertFalse(file_exists($fileOne));
  1003. $this->assertFalse(file_exists($folderOneA));
  1004. $this->assertFalse(file_exists($fileOneA));
  1005. $Folder = new Folder($folderTwo);
  1006. $Folder->delete();
  1007. new Folder($folderOne, true);
  1008. new Folder($folderOneA, true);
  1009. new Folder($folderTwo, true);
  1010. touch($fileOne);
  1011. touch($fileOneA);
  1012. $Folder = new Folder($folderOne);
  1013. $result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
  1014. $this->assertTrue($result);
  1015. $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
  1016. $this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
  1017. $this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
  1018. $this->assertFalse(file_exists($fileOne));
  1019. $this->assertFalse(file_exists($folderOneA));
  1020. $this->assertFalse(file_exists($fileOneA));
  1021. $Folder = new Folder($folderTwo);
  1022. $Folder->delete();
  1023. new Folder($folderOne, true);
  1024. new Folder($folderOneA, true);
  1025. new Folder($folderTwo, true);
  1026. new Folder($folderTwoB, true);
  1027. touch($fileOne);
  1028. touch($fileOneA);
  1029. file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');
  1030. $Folder = new Folder($folderOne);
  1031. $result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
  1032. $this->assertTrue($result);
  1033. $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
  1034. $this->assertEquals('untouched', file_get_contents($folderTwoB . DS . 'fileB.php'));
  1035. $this->assertFalse(file_exists($fileOne));
  1036. $this->assertFalse(file_exists($folderOneA));
  1037. $this->assertFalse(file_exists($fileOneA));
  1038. $Folder = new Folder($path);
  1039. $Folder->delete();
  1040. }
  1041. }