FolderTest.php 31 KB

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