FolderTest.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. <?php
  2. /**
  3. * FolderTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 1.2.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Filesystem;
  18. use Cake\Filesystem\File;
  19. use Cake\Filesystem\Folder;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * FolderTest class
  23. */
  24. class FolderTest extends TestCase
  25. {
  26. /**
  27. * setUp clearstatcache() to flush file descriptors.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. clearstatcache();
  35. }
  36. /**
  37. * Remove TMP/tests directory to its original state.
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. parent::tearDown();
  44. $cleaner = function ($dir) use (&$cleaner) {
  45. $files = array_diff(scandir($dir), ['.', '..']);
  46. foreach ($files as $file) {
  47. $path = $dir . DS . $file;
  48. if (is_dir($path)) {
  49. $cleaner($path);
  50. } else {
  51. unlink($path);
  52. }
  53. }
  54. rmdir($dir);
  55. };
  56. if (file_exists(TMP . 'tests')) {
  57. $cleaner(TMP . 'tests');
  58. }
  59. parent::tearDown();
  60. }
  61. /**
  62. * testBasic method
  63. *
  64. * @return void
  65. */
  66. public function testBasic()
  67. {
  68. $path = __DIR__;
  69. $Folder = new Folder($path);
  70. $result = $Folder->pwd();
  71. $this->assertEquals($path, $result);
  72. $result = Folder::addPathElement($path, 'test');
  73. $expected = $path . DS . 'test';
  74. $this->assertEquals($expected, $result);
  75. $result = $Folder->cd(ROOT);
  76. $expected = ROOT;
  77. $this->assertEquals($expected, $result);
  78. $result = $Folder->cd(ROOT . DS . 'non-existent');
  79. $this->assertFalse($result);
  80. }
  81. /**
  82. * testInPath method
  83. *
  84. * @return void
  85. */
  86. public function testInPath()
  87. {
  88. // "/tests/test_app/"
  89. $basePath = TEST_APP;
  90. $Base = new Folder($basePath);
  91. $result = $Base->pwd();
  92. $this->assertEquals($basePath, $result);
  93. // is "/" in "/tests/test_app/"
  94. $result = $Base->inPath(realpath(DS), true);
  95. $this->assertFalse($result, true);
  96. // is "/tests/test_app/" in "/tests/test_app/"
  97. $result = $Base->inPath($basePath, true);
  98. $this->assertTrue($result);
  99. // is "/tests/test_app" in "/tests/test_app/"
  100. $result = $Base->inPath(mb_substr($basePath, 0, -1), true);
  101. $this->assertTrue($result);
  102. // is "/tests/test_app/sub" in "/tests/test_app/"
  103. $result = $Base->inPath($basePath . 'sub', true);
  104. $this->assertTrue($result);
  105. // is "/tests" in "/tests/test_app/"
  106. $result = $Base->inPath(dirname($basePath), true);
  107. $this->assertFalse($result);
  108. // is "/tests/other/(...)tests/test_app" in "/tests/test_app/"
  109. $result = $Base->inPath(TMP . 'tests' . DS . 'other' . DS . $basePath, true);
  110. $this->assertFalse($result);
  111. // is "/tests/test_app/" in "/"
  112. $result = $Base->inPath(realpath(DS));
  113. $this->assertTrue($result);
  114. // is "/tests/test_app/" in "/tests/test_app/"
  115. $result = $Base->inPath($basePath);
  116. $this->assertTrue($result);
  117. // is "/tests/test_app/" in "/tests/test_app"
  118. $result = $Base->inPath(mb_substr($basePath, 0, -1));
  119. $this->assertTrue($result);
  120. // is "/tests/test_app/" in "/tests"
  121. $result = $Base->inPath(dirname($basePath));
  122. $this->assertTrue($result);
  123. // is "/tests/test_app/" in "/tests/test_app/sub"
  124. $result = $Base->inPath($basePath . 'sub');
  125. $this->assertFalse($result);
  126. // is "/other/tests/test_app/" in "/tests/test_app/"
  127. $VirtualBase = new Folder();
  128. $VirtualBase->path = '/other/tests/test_app';
  129. $result = $VirtualBase->inPath('/tests/test_app/');
  130. $this->assertFalse($result);
  131. }
  132. /**
  133. * Data provider for the testInPathInvalidPathArgument test
  134. *
  135. * @return array
  136. */
  137. public function inPathInvalidPathArgumentDataProvider()
  138. {
  139. return [
  140. [''],
  141. ['relative/path/'],
  142. ['unknown://stream-wrapper']
  143. ];
  144. }
  145. /**
  146. * @dataProvider inPathInvalidPathArgumentDataProvider
  147. * @param string $path
  148. */
  149. public function testInPathInvalidPathArgument($path)
  150. {
  151. $this->expectException(\InvalidArgumentException::class);
  152. $this->expectExceptionMessage('The $path argument is expected to be an absolute path.');
  153. $Folder = new Folder();
  154. $Folder->inPath($path);
  155. }
  156. /**
  157. * test creation of single and multiple paths.
  158. *
  159. * @return void
  160. */
  161. public function testCreation()
  162. {
  163. $Folder = new Folder(TMP . 'tests');
  164. $result = $Folder->create(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
  165. $this->assertTrue($result);
  166. rmdir(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
  167. rmdir(TMP . 'tests' . DS . 'first' . DS . 'second');
  168. rmdir(TMP . 'tests' . DS . 'first');
  169. $Folder = new Folder(TMP . 'tests');
  170. $result = $Folder->create(TMP . 'tests' . DS . 'first');
  171. $this->assertTrue($result);
  172. }
  173. /**
  174. * test that creation of folders with trailing ds works
  175. *
  176. * @return void
  177. */
  178. public function testCreateWithTrailingDs()
  179. {
  180. $Folder = new Folder(TMP . 'tests');
  181. $path = TMP . 'tests' . DS . 'trailing' . DS . 'dir' . DS;
  182. $result = $Folder->create($path);
  183. $this->assertTrue($result);
  184. $this->assertDirectoryExists($path, 'Folder was not made');
  185. $Folder = new Folder(TMP . 'tests' . DS . 'trailing');
  186. $this->assertTrue($Folder->delete());
  187. }
  188. /**
  189. * Test that relative paths to create() are added to cwd.
  190. *
  191. * @return void
  192. */
  193. public function testCreateRelative()
  194. {
  195. $folder = new Folder(TMP);
  196. $path = TMP . 'tests' . DS . 'relative-test';
  197. $result = $folder->create('tests' . DS . 'relative-test');
  198. $this->assertTrue($result, 'should create');
  199. $this->assertDirectoryExists($path, 'Folder was not made');
  200. $folder = new Folder($path);
  201. $folder->delete();
  202. }
  203. /**
  204. * test recursive directory create failure.
  205. *
  206. * @return void
  207. */
  208. public function testRecursiveCreateFailure()
  209. {
  210. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  211. $path = TMP . 'tests/one';
  212. mkdir($path, 0777, true);
  213. chmod($path, '0444');
  214. try {
  215. $Folder = new Folder($path);
  216. $result = $Folder->create($path . DS . 'two/three');
  217. $this->assertFalse($result);
  218. } catch (\Exception $e) {
  219. $this->assertInstanceOf('PHPUnit\Framework\Error\Error', $e);
  220. }
  221. chmod($path, '0777');
  222. rmdir($path);
  223. }
  224. /**
  225. * testOperations method
  226. *
  227. * @return void
  228. */
  229. public function testOperations()
  230. {
  231. $path = CAKE . 'Template';
  232. $Folder = new Folder($path);
  233. $result = $Folder->pwd();
  234. $this->assertSame($path, $result);
  235. $new = TMP . 'tests' . DS . 'test_folder_new';
  236. $result = $Folder->create($new);
  237. $this->assertTrue($result);
  238. $copy = TMP . 'tests' . DS . 'test_folder_copy';
  239. $result = $Folder->copy($copy);
  240. $this->assertTrue($result);
  241. $copy = TMP . 'tests' . DS . 'test_folder_copy';
  242. $result = $Folder->copy($copy);
  243. $this->assertTrue($result);
  244. $copy = TMP . 'tests' . DS . 'test_folder_copy';
  245. $result = $Folder->chmod($copy, 0755, false);
  246. $this->assertTrue($result);
  247. $result = $Folder->cd($copy);
  248. $this->assertTrue((bool)$result);
  249. $mv = TMP . 'tests' . DS . 'test_folder_mv';
  250. $result = $Folder->move($mv);
  251. $this->assertTrue($result);
  252. $mv = TMP . 'tests' . DS . 'test_folder_mv_2';
  253. $result = $Folder->move($mv);
  254. $this->assertTrue($result);
  255. $result = $Folder->delete($new);
  256. $this->assertTrue($result);
  257. $result = $Folder->delete($mv);
  258. $this->assertTrue($result);
  259. $result = $Folder->delete($mv);
  260. $this->assertTrue($result);
  261. $new = CONFIG . 'acl.ini';
  262. $result = $Folder->create($new);
  263. $this->assertFalse($result);
  264. $expected = $new . ' is a file';
  265. $result = $Folder->errors();
  266. $this->assertEquals($expected, $result[0]);
  267. $new = TMP . 'tests' . DS . 'test_folder_new';
  268. $result = $Folder->create($new);
  269. $this->assertTrue($result);
  270. $result = $Folder->cd($new);
  271. $this->assertTrue((bool)$result);
  272. $result = $Folder->delete();
  273. $this->assertTrue($result);
  274. $Folder = new Folder('non-existent');
  275. $result = $Folder->pwd();
  276. $this->assertNull($result);
  277. }
  278. /**
  279. * testChmod method
  280. *
  281. * @return void
  282. */
  283. public function testChmod()
  284. {
  285. $this->skipIf(DS === '\\', 'Folder permissions tests not supported on Windows.');
  286. $path = TMP . 'tests/';
  287. $Folder = new Folder($path);
  288. $subdir = 'test_folder_new';
  289. $new = $path . $subdir;
  290. $this->assertTrue($Folder->create($new));
  291. $this->assertTrue($Folder->create($new . DS . 'test1'));
  292. $this->assertTrue($Folder->create($new . DS . 'test2'));
  293. $filePath = $new . DS . 'test1.php';
  294. $File = new File($filePath);
  295. $this->assertTrue($File->create());
  296. $filePath = $new . DS . 'skip_me.php';
  297. $File = new File($filePath);
  298. $this->assertTrue($File->create());
  299. $this->assertTrue($Folder->chmod($new, 0755, true));
  300. $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
  301. $this->assertEquals('0755', $perms);
  302. $this->assertTrue($Folder->chmod($new, 0744, true, ['skip_me.php', 'test2']));
  303. $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
  304. $this->assertEquals('0755', $perms);
  305. $perms = substr(sprintf('%o', fileperms($new . DS . 'test1')), -4);
  306. $this->assertEquals('0744', $perms);
  307. }
  308. /**
  309. * testRealPathForWebroot method
  310. *
  311. * @return void
  312. */
  313. public function testRealPathForWebroot()
  314. {
  315. $Folder = new Folder('files' . DS);
  316. $this->assertEquals(realpath('files' . DS), $Folder->path);
  317. }
  318. /**
  319. * testZeroAsDirectory method
  320. *
  321. * @return void
  322. */
  323. public function testZeroAsDirectory()
  324. {
  325. $path = TMP . 'tests';
  326. $Folder = new Folder($path, true);
  327. $new = $path . '/0';
  328. $this->assertTrue($Folder->create($new));
  329. $result = $Folder->read(true, true);
  330. $this->assertContains('0', $result[0]);
  331. $result = $Folder->read(true, ['logs']);
  332. $this->assertContains('0', $result[0]);
  333. $result = $Folder->delete($new);
  334. $this->assertTrue($result);
  335. }
  336. /**
  337. * test Adding path elements to a path
  338. *
  339. * @return void
  340. */
  341. public function testAddPathElement()
  342. {
  343. $expected = DS . 'some' . DS . 'dir' . DS . 'another_path';
  344. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path');
  345. $this->assertEquals($expected, $result);
  346. $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path');
  347. $this->assertEquals($expected, $result);
  348. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', ['another_path']);
  349. $this->assertEquals($expected, $result);
  350. $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, ['another_path']);
  351. $this->assertEquals($expected, $result);
  352. $expected = DS . 'some' . DS . 'dir' . DS . 'another_path' . DS . 'and' . DS . 'another';
  353. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', ['another_path', 'and', 'another']);
  354. $this->assertEquals($expected, $result);
  355. }
  356. /**
  357. * testFolderRead method
  358. *
  359. * @return void
  360. */
  361. public function testFolderRead()
  362. {
  363. $Folder = new Folder(CAKE);
  364. $result = $Folder->read(true, true);
  365. $this->assertContains('Core', $result[0]);
  366. $this->assertContains('Cache', $result[0]);
  367. $Folder = new Folder(TMP . 'non-existent');
  368. $expected = [[], []];
  369. $result = $Folder->read(true, true);
  370. $this->assertEquals($expected, $result);
  371. }
  372. /**
  373. * testFolderReadWithHiddenFiles method
  374. *
  375. * @return void
  376. */
  377. public function testFolderReadWithHiddenFiles()
  378. {
  379. $this->skipIf(!is_writable(TMP), 'Cant test Folder::read with hidden files unless the tmp folder is writable.');
  380. $path = TMP . 'tests' . DS;
  381. $Folder = new Folder($path . 'folder_tree_hidden', true, 0777);
  382. mkdir($Folder->path . DS . '.svn');
  383. mkdir($Folder->path . DS . 'some_folder');
  384. touch($Folder->path . DS . 'not_hidden.txt');
  385. touch($Folder->path . DS . '.hidden.txt');
  386. $expected = [
  387. ['some_folder'],
  388. ['not_hidden.txt'],
  389. ];
  390. $result = $Folder->read(true, true);
  391. $this->assertEquals($expected, $result);
  392. $expected = [
  393. [
  394. '.svn',
  395. 'some_folder'
  396. ],
  397. [
  398. '.hidden.txt',
  399. 'not_hidden.txt'
  400. ],
  401. ];
  402. $result = $Folder->read(true);
  403. $this->assertEquals($expected, $result);
  404. }
  405. /**
  406. * testFolderSubdirectories method
  407. *
  408. * @return void
  409. */
  410. public function testFolderSubdirectories()
  411. {
  412. $path = CAKE . 'Network';
  413. $folder = new Folder($path);
  414. $expected = [
  415. $path . DS . 'Exception',
  416. $path . DS . 'Http',
  417. $path . DS . 'Session'
  418. ];
  419. $result = $folder->subdirectories();
  420. $this->assertSame([], array_diff($expected, $result));
  421. $result = $folder->subdirectories($path);
  422. $this->assertSame([], array_diff($expected, $result));
  423. $expected = [
  424. 'Exception',
  425. 'Http',
  426. 'Session'
  427. ];
  428. $result = $folder->subdirectories(null, false);
  429. $this->assertSame([], array_diff($expected, $result));
  430. $result = $folder->subdirectories($path, false);
  431. $this->assertSame([], array_diff($expected, $result));
  432. $expected = [];
  433. $result = $folder->subdirectories('NonExistentPath');
  434. $this->assertSame([], array_diff($expected, $result));
  435. $result = $folder->subdirectories($path . DS . 'Exception');
  436. $this->assertSame([], array_diff($expected, $result));
  437. }
  438. /**
  439. * testFolderTree method
  440. *
  441. * @return void
  442. */
  443. public function testFolderTree()
  444. {
  445. $Folder = new Folder();
  446. $expected = [
  447. [
  448. CORE_PATH . 'config',
  449. ],
  450. [
  451. CORE_PATH . 'config' . DS . 'config.php',
  452. ]
  453. ];
  454. $result = $Folder->tree(CORE_PATH . 'config', false);
  455. $this->assertSame([], array_diff($expected[0], $result[0]));
  456. $this->assertSame([], array_diff($result[0], $expected[0]));
  457. $result = $Folder->tree(CORE_PATH . 'config', false, 'dir');
  458. $this->assertSame([], array_diff($expected[0], $result));
  459. $this->assertSame([], array_diff($expected[0], $result));
  460. $result = $Folder->tree(CORE_PATH . 'config', false, 'files');
  461. $this->assertSame([], array_diff($expected[1], $result));
  462. $this->assertSame([], array_diff($expected[1], $result));
  463. }
  464. /**
  465. * testFolderTreeWithHiddenFiles method
  466. *
  467. * @return void
  468. */
  469. public function testFolderTreeWithHiddenFiles()
  470. {
  471. $this->skipIf(!is_writable(TMP), 'Can\'t test Folder::tree with hidden files unless the tmp folder is writable.');
  472. $path = TMP . 'tests' . DS;
  473. $Folder = new Folder($path . 'folder_tree_hidden', true, 0777);
  474. mkdir($Folder->path . DS . '.svn', 0777, true);
  475. touch($Folder->path . DS . '.svn/InHiddenFolder.php');
  476. mkdir($Folder->path . DS . '.svn/inhiddenfolder');
  477. touch($Folder->path . DS . '.svn/inhiddenfolder/NestedInHiddenFolder.php');
  478. touch($Folder->path . DS . 'not_hidden.txt');
  479. touch($Folder->path . DS . '.hidden.txt');
  480. mkdir($Folder->path . DS . 'visible_folder/.git', 0777, true);
  481. $expected = [
  482. [
  483. $Folder->path,
  484. $Folder->path . DS . 'visible_folder',
  485. ],
  486. [
  487. $Folder->path . DS . 'not_hidden.txt',
  488. ],
  489. ];
  490. $result = $Folder->tree(null, true);
  491. $this->assertEquals($expected, $result);
  492. $result = $Folder->tree(null, ['.']);
  493. $this->assertEquals($expected, $result);
  494. $expected = [
  495. [
  496. $Folder->path,
  497. $Folder->path . DS . 'visible_folder',
  498. $Folder->path . DS . 'visible_folder' . DS . '.git',
  499. $Folder->path . DS . '.svn',
  500. $Folder->path . DS . '.svn' . DS . 'inhiddenfolder',
  501. ],
  502. [
  503. $Folder->path . DS . 'not_hidden.txt',
  504. $Folder->path . DS . '.hidden.txt',
  505. $Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php',
  506. $Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php',
  507. ],
  508. ];
  509. $result = $Folder->tree(null, false);
  510. sort($result[0]);
  511. sort($expected[0]);
  512. sort($result[1]);
  513. sort($expected[1]);
  514. $this->assertEquals($expected, $result);
  515. $Folder->delete();
  516. }
  517. /**
  518. * testWindowsPath method
  519. *
  520. * @return void
  521. */
  522. public function testWindowsPath()
  523. {
  524. $this->assertFalse(Folder::isWindowsPath('0:\\cake\\is\\awesome'));
  525. $this->assertTrue(Folder::isWindowsPath('C:\\cake\\is\\awesome'));
  526. $this->assertTrue(Folder::isWindowsPath('d:\\cake\\is\\awesome'));
  527. $this->assertTrue(Folder::isWindowsPath('\\\\vmware-host\\Shared Folders\\file'));
  528. }
  529. /**
  530. * testIsAbsolute method
  531. *
  532. * @return void
  533. */
  534. public function testIsAbsolute()
  535. {
  536. $this->assertFalse(Folder::isAbsolute('path/to/file'));
  537. $this->assertFalse(Folder::isAbsolute('cake/'));
  538. $this->assertFalse(Folder::isAbsolute('path\\to\\file'));
  539. $this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
  540. $this->assertFalse(Folder::isAbsolute('\\path/to/file'));
  541. $this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
  542. $this->assertFalse(Folder::isAbsolute('notRegisteredStreamWrapper://example'));
  543. $this->assertFalse(Folder::isAbsolute('://example'));
  544. $this->assertTrue(Folder::isAbsolute('/usr/local'));
  545. $this->assertTrue(Folder::isAbsolute('//path/to/file'));
  546. $this->assertTrue(Folder::isAbsolute('C:\\cake'));
  547. $this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
  548. $this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
  549. $this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
  550. $this->assertTrue(Folder::isAbsolute('http://www.example.com'));
  551. }
  552. /**
  553. * testIsSlashTerm method
  554. *
  555. * @return void
  556. */
  557. public function testIsSlashTerm()
  558. {
  559. $this->assertFalse(Folder::isSlashTerm('cake'));
  560. $this->assertTrue(Folder::isSlashTerm('C:\\cake\\'));
  561. $this->assertTrue(Folder::isSlashTerm('/usr/local/'));
  562. }
  563. /**
  564. * testStatic method
  565. *
  566. * @return void
  567. */
  568. public function testSlashTerm()
  569. {
  570. $result = Folder::slashTerm('/path/to/file');
  571. $this->assertEquals('/path/to/file/', $result);
  572. }
  573. /**
  574. * testNormalizePath method
  575. *
  576. * @group deprecated
  577. * @return void
  578. */
  579. public function testNormalizePath()
  580. {
  581. $path = '/path/to/file';
  582. /** @noinspection PhpDeprecationInspection */
  583. $result = Folder::normalizePath($path);
  584. $this->assertEquals('/', $result);
  585. $path = '\\path\\\to\\\file';
  586. /** @noinspection PhpDeprecationInspection */
  587. $result = Folder::normalizePath($path);
  588. $this->assertEquals('/', $result);
  589. $path = 'C:\\path\\to\\file';
  590. /** @noinspection PhpDeprecationInspection */
  591. $result = Folder::normalizePath($path);
  592. $this->assertEquals('\\', $result);
  593. }
  594. /**
  595. * testNormalizeFullPath method
  596. *
  597. * @return void
  598. */
  599. public function testNormalizeFullPath()
  600. {
  601. $path = '/path/to\file';
  602. $expected = '/path/to/file';
  603. $result = Folder::normalizeFullPath($path);
  604. $this->assertEquals($expected, $result);
  605. $path = '\\path\\to\file';
  606. $expected = '/path/to/file';
  607. $result = Folder::normalizeFullPath($path);
  608. $this->assertEquals($expected, $result);
  609. $path = 'C:\\path/to/file';
  610. $expected = 'C:\\path\\to\\file';
  611. $result = Folder::normalizeFullPath($path);
  612. $this->assertEquals($expected, $result);
  613. }
  614. /**
  615. * correctSlashFor method
  616. *
  617. * @return void
  618. */
  619. public function testCorrectSlashFor()
  620. {
  621. $path = '/path/to/file';
  622. $result = Folder::correctSlashFor($path);
  623. $this->assertEquals('/', $result);
  624. $path = '\\path\\to\\file';
  625. $result = Folder::correctSlashFor($path);
  626. $this->assertEquals('/', $result);
  627. $path = 'C:\\path\to\\file';
  628. $result = Folder::correctSlashFor($path);
  629. $this->assertEquals('\\', $result);
  630. }
  631. /**
  632. * testInCakePath method
  633. *
  634. * @group deprecated
  635. * @return void
  636. */
  637. public function testInCakePath()
  638. {
  639. $this->deprecated(function () {
  640. $Folder = new Folder();
  641. $Folder->cd(ROOT);
  642. $path = 'C:\\path\\to\\file';
  643. /** @noinspection PhpDeprecationInspection */
  644. $result = $Folder->inCakePath($path);
  645. $this->assertFalse($result);
  646. $path = ROOT;
  647. $Folder->cd(ROOT);
  648. /** @noinspection PhpDeprecationInspection */
  649. $result = $Folder->inCakePath($path);
  650. $this->assertFalse($result);
  651. $path = DS . 'config';
  652. $Folder->cd(ROOT . DS . 'config');
  653. /** @noinspection PhpDeprecationInspection */
  654. $result = $Folder->inCakePath($path);
  655. $this->assertTrue($result);
  656. });
  657. }
  658. /**
  659. * testFind method
  660. *
  661. * @return void
  662. */
  663. public function testFind()
  664. {
  665. $Folder = new Folder();
  666. $Folder->cd(CORE_PATH . 'config');
  667. $result = $Folder->find();
  668. $expected = ['config.php'];
  669. $this->assertSame(array_diff($expected, $result), []);
  670. $this->assertSame(array_diff($expected, $result), []);
  671. $result = $Folder->find('.*', true);
  672. $expected = ['bootstrap.php', 'cacert.pem', 'config.php'];
  673. $this->assertSame($expected, $result);
  674. $result = $Folder->find('.*\.php');
  675. $expected = ['bootstrap.php', 'config.php'];
  676. $this->assertSame(array_diff($expected, $result), []);
  677. $this->assertSame(array_diff($expected, $result), []);
  678. $result = $Folder->find('.*\.php', true);
  679. $expected = ['bootstrap.php', 'config.php'];
  680. $this->assertSame($expected, $result);
  681. $result = $Folder->find('.*ig\.php');
  682. $expected = ['config.php'];
  683. $this->assertSame($expected, $result);
  684. $result = $Folder->find('config\.php');
  685. $expected = ['config.php'];
  686. $this->assertSame($expected, $result);
  687. $Folder = new Folder(TMP . 'tests/', true);
  688. new File($Folder->pwd() . DS . 'paths.php', true);
  689. $Folder->create($Folder->pwd() . DS . 'testme');
  690. $Folder->cd('testme');
  691. $result = $Folder->find('paths\.php');
  692. $expected = [];
  693. $this->assertSame($expected, $result);
  694. $Folder->cd($Folder->pwd() . '/..');
  695. $result = $Folder->find('paths\.php');
  696. $expected = ['paths.php'];
  697. $this->assertSame($expected, $result);
  698. }
  699. /**
  700. * testFindRecursive method
  701. *
  702. * @return void
  703. */
  704. public function testFindRecursive()
  705. {
  706. $Folder = new Folder(CORE_PATH . 'config');
  707. $result = $Folder->findRecursive('(config|paths)\.php');
  708. $expected = [
  709. CORE_PATH . 'config' . DS . 'config.php'
  710. ];
  711. $this->assertSame([], array_diff($expected, $result));
  712. $result = $Folder->findRecursive('(config|bootstrap)\.php', true);
  713. $expected = [
  714. CORE_PATH . 'config' . DS . 'bootstrap.php',
  715. CORE_PATH . 'config' . DS . 'config.php'
  716. ];
  717. $this->assertSame($expected, $result);
  718. $path = TMP . 'tests' . DS;
  719. $Folder = new Folder($path, true);
  720. $Folder->create($path . 'sessions');
  721. $Folder->create($path . 'testme');
  722. $Folder->cd($path . 'testme');
  723. $File = new File($Folder->pwd() . DS . 'paths.php');
  724. $File->create();
  725. $Folder->cd($path . 'sessions');
  726. $result = $Folder->findRecursive('paths\.php');
  727. $expected = [];
  728. $this->assertSame($expected, $result);
  729. $Folder->cd($path . 'testme');
  730. $File = new File($Folder->pwd() . DS . 'my.php');
  731. $File->create();
  732. $Folder->cd($path);
  733. $result = $Folder->findRecursive('(paths|my)\.php');
  734. $expected = [
  735. $path . 'testme' . DS . 'my.php',
  736. $path . 'testme' . DS . 'paths.php'
  737. ];
  738. $this->assertSame(sort($expected), sort($result));
  739. $result = $Folder->findRecursive('(paths|my)\.php', true);
  740. $expected = [
  741. $path . 'testme' . DS . 'my.php',
  742. $path . 'testme' . DS . 'paths.php'
  743. ];
  744. $this->assertSame($expected, $result);
  745. }
  746. /**
  747. * testConstructWithNonExistentPath method
  748. *
  749. * @return void
  750. */
  751. public function testConstructWithNonExistentPath()
  752. {
  753. $path = TMP . 'tests' . DS;
  754. $Folder = new Folder($path . 'config_non_existent', true);
  755. $this->assertDirectoryExists($path . 'config_non_existent');
  756. $Folder->cd($path);
  757. }
  758. /**
  759. * testDirSize method
  760. *
  761. * @return void
  762. */
  763. public function testDirSize()
  764. {
  765. $path = TMP . 'tests' . DS;
  766. $Folder = new Folder($path . 'config_non_existent', true);
  767. $this->assertEquals(0, $Folder->dirSize());
  768. $File = new File($Folder->pwd() . DS . 'my.php', true, 0777);
  769. $File->create();
  770. $File->write('something here');
  771. $File->close();
  772. $this->assertEquals(14, $Folder->dirSize());
  773. }
  774. /**
  775. * test that errors and messages can be restarted
  776. *
  777. * @return void
  778. */
  779. public function testReset()
  780. {
  781. $path = TMP . 'tests' . DS . 'folder_delete_test';
  782. mkdir($path, 0777, true);
  783. $folder = $path . DS . 'sub';
  784. mkdir($folder);
  785. $file = $folder . DS . 'file';
  786. touch($file);
  787. chmod($folder, 0555);
  788. chmod($file, 0444);
  789. $Folder = new Folder($folder);
  790. $return = $Folder->delete();
  791. $this->assertFalse($return);
  792. $messages = $Folder->messages();
  793. $errors = $Folder->errors();
  794. $expected = [
  795. $file . ' NOT removed',
  796. $folder . ' NOT removed',
  797. ];
  798. sort($expected);
  799. sort($errors);
  800. $this->assertEmpty($messages);
  801. $this->assertEquals($expected, $errors);
  802. chmod($file, 0644);
  803. chmod($folder, 0755);
  804. $return = $Folder->delete();
  805. $this->assertTrue($return);
  806. $messages = $Folder->messages();
  807. $errors = $Folder->errors();
  808. $expected = [
  809. $file . ' removed',
  810. $folder . ' removed',
  811. ];
  812. sort($expected);
  813. sort($messages);
  814. $this->assertEmpty($errors);
  815. $this->assertEquals($expected, $messages);
  816. }
  817. /**
  818. * testDelete method
  819. *
  820. * @return void
  821. */
  822. public function testDelete()
  823. {
  824. $path = TMP . 'tests' . DS . 'folder_delete_test';
  825. mkdir($path, 0777, true);
  826. touch($path . DS . 'file_1');
  827. mkdir($path . DS . 'level_1_1');
  828. touch($path . DS . 'level_1_1/file_1_1');
  829. mkdir($path . DS . 'level_1_1/level_2_1');
  830. touch($path . DS . 'level_1_1/level_2_1/file_2_1');
  831. touch($path . DS . 'level_1_1/level_2_1/file_2_2');
  832. mkdir($path . DS . 'level_1_1/level_2_2');
  833. $Folder = new Folder($path, true);
  834. $return = $Folder->delete();
  835. $this->assertTrue($return);
  836. $messages = $Folder->messages();
  837. $errors = $Folder->errors();
  838. $this->assertEquals([], $errors);
  839. $expected = [
  840. $path . DS . 'file_1 removed',
  841. $path . DS . 'level_1_1' . DS . 'file_1_1 removed',
  842. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1 removed',
  843. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2 removed',
  844. $path . DS . 'level_1_1' . DS . 'level_2_1 removed',
  845. $path . DS . 'level_1_1' . DS . 'level_2_2 removed',
  846. $path . DS . 'level_1_1 removed',
  847. $path . ' removed'
  848. ];
  849. sort($expected);
  850. sort($messages);
  851. $this->assertEquals($expected, $messages);
  852. }
  853. /**
  854. * testCopy method
  855. *
  856. * Verify that subdirectories existing in both destination and source directory
  857. * are merged recursively.
  858. *
  859. * @return void
  860. */
  861. public function testCopy()
  862. {
  863. /** @var string $path
  864. * @var string $folderOne
  865. * @var string $folderOneA
  866. * @var string $folderTwo
  867. * @var string $folderTwoB
  868. * @var string $folderThree
  869. * @var string $fileOne
  870. * @var string $fileTwo
  871. * @var string $fileOneA
  872. * @var string $fileTwoB
  873. */
  874. extract($this->_setupFilesystem());
  875. $Folder = new Folder($folderOne);
  876. $result = $Folder->copy($folderThree);
  877. $this->assertTrue($result);
  878. $this->assertFileExists($folderThree . DS . 'file1.php');
  879. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  880. $Folder = new Folder($folderTwo);
  881. $result = $Folder->copy($folderThree);
  882. $this->assertTrue($result);
  883. $this->assertFileExists($folderThree . DS . 'file1.php');
  884. $this->assertFileExists($folderThree . DS . 'file2.php');
  885. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  886. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  887. $Folder = new Folder($path);
  888. $Folder->delete();
  889. }
  890. /**
  891. * testCopyWithMerge method
  892. *
  893. * Verify that subdirectories existing in both destination and source directory
  894. * are merged recursively.
  895. *
  896. * @return void
  897. */
  898. public function testCopyWithMerge()
  899. {
  900. /** @var string $path
  901. * @var string $folderOne
  902. * @var string $folderOneA
  903. * @var string $folderTwo
  904. * @var string $folderTwoB
  905. * @var string $folderThree
  906. * @var string $fileOne
  907. * @var string $fileTwo
  908. * @var string $fileOneA
  909. * @var string $fileTwoB
  910. */
  911. extract($this->_setupFilesystem());
  912. $Folder = new Folder($folderOne);
  913. $result = $Folder->copy($folderThree);
  914. $this->assertTrue($result);
  915. $this->assertFileExists($folderThree . DS . 'file1.php');
  916. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  917. $Folder = new Folder($folderTwo);
  918. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::MERGE]);
  919. $this->assertTrue($result);
  920. $this->assertFileExists($folderThree . DS . 'file1.php');
  921. $this->assertFileExists($folderThree . DS . 'file2.php');
  922. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  923. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  924. }
  925. /**
  926. * testCopyWithSkip method
  927. *
  928. * Verify that directories and files are copied recursively
  929. * even if the destination directory already exists.
  930. * Subdirectories existing in both destination and source directory
  931. * are skipped and not merged or overwritten.
  932. *
  933. * @return void
  934. */
  935. public function testCopyWithSkip()
  936. {
  937. /** @var string $path
  938. * @var string $folderOne
  939. * @var string $folderOneA
  940. * @var string $folderTwo
  941. * @var string $folderTwoB
  942. * @var string $folderThree
  943. * @var string $fileOne
  944. * @var string $fileTwo
  945. * @var string $fileOneA
  946. * @var string $fileTwoB
  947. */
  948. extract($this->_setupFilesystem());
  949. $Folder = new Folder($folderOne);
  950. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  951. $this->assertTrue($result);
  952. $this->assertFileExists($folderTwo . DS . 'file1.php');
  953. $this->assertFileExists($folderTwo . DS . 'folderA' . DS . 'fileA.php');
  954. $Folder = new Folder($folderTwo);
  955. $Folder->delete();
  956. $Folder = new Folder($folderOne);
  957. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  958. $this->assertTrue($result);
  959. $this->assertFileExists($folderTwo . DS . 'file1.php');
  960. $this->assertFileExists($folderTwo . DS . 'folderA' . DS . 'fileA.php');
  961. $Folder = new Folder($folderTwo);
  962. $Folder->delete();
  963. new Folder($folderTwo, true);
  964. new Folder($folderTwo . DS . 'folderB', true);
  965. file_put_contents($folderTwo . DS . 'file2.php', 'touched');
  966. file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  967. $Folder = new Folder($folderTwo);
  968. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::SKIP]);
  969. $this->assertTrue($result);
  970. $this->assertFileExists($folderThree . DS . 'file2.php');
  971. $this->assertStringEqualsFile($folderThree . DS . 'file2.php', 'touched');
  972. $this->assertStringEqualsFile($folderThree . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  973. }
  974. /**
  975. * Test that SKIP mode skips files too.
  976. *
  977. * @return void
  978. */
  979. public function testCopyWithSkipFileSkipped()
  980. {
  981. $path = TMP . 'folder_test';
  982. $folderOne = $path . DS . 'folder1';
  983. $folderTwo = $path . DS . 'folder2';
  984. new Folder($path, true);
  985. new Folder($folderOne, true);
  986. new Folder($folderTwo, true);
  987. file_put_contents($folderOne . DS . 'fileA.txt', 'Folder One File');
  988. file_put_contents($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  989. $Folder = new Folder($folderOne);
  990. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  991. $this->assertTrue($result);
  992. $this->assertStringEqualsFile($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  993. }
  994. /**
  995. * testCopyWithOverwrite
  996. *
  997. * Verify that subdirectories existing in both destination and source directory
  998. * are overwritten/replaced recursively.
  999. *
  1000. * @return void
  1001. */
  1002. public function testCopyWithOverwrite()
  1003. {
  1004. /** @var string $path
  1005. * @var string $folderOne
  1006. * @var string $folderOneA
  1007. * @var string $folderTwo
  1008. * @var string $folderTwoB
  1009. * @var string $folderThree
  1010. * @var string $fileOne
  1011. * @var string $fileTwo
  1012. * @var string $fileOneA
  1013. * @var string $fileTwoB
  1014. */
  1015. extract($this->_setupFilesystem());
  1016. $Folder = new Folder($folderOne);
  1017. $Folder->copy(['to' => $folderThree, 'scheme' => Folder::OVERWRITE]);
  1018. $this->assertFileExists($folderThree . DS . 'file1.php');
  1019. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  1020. $Folder = new Folder($folderTwo);
  1021. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::OVERWRITE]);
  1022. $this->assertTrue($result);
  1023. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  1024. $Folder = new Folder($folderOne);
  1025. unlink($fileOneA);
  1026. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::OVERWRITE]);
  1027. $this->assertTrue($result);
  1028. $this->assertFileExists($folderThree . DS . 'file1.php');
  1029. $this->assertFileExists($folderThree . DS . 'file2.php');
  1030. $this->assertFileNotExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  1031. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  1032. }
  1033. /**
  1034. * testCopyWithoutRecursive
  1035. *
  1036. * Verify that only the files exist in the target directory.
  1037. *
  1038. * @return void
  1039. */
  1040. public function testCopyWithoutRecursive()
  1041. {
  1042. /** @var string $path
  1043. * @var string $folderOne
  1044. * @var string $folderOneA
  1045. * @var string $folderTwo
  1046. * @var string $folderTwoB
  1047. * @var string $folderThree
  1048. * @var string $fileOne
  1049. * @var string $fileTwo
  1050. * @var string $fileOneA
  1051. * @var string $fileTwoB
  1052. */
  1053. extract($this->_setupFilesystem());
  1054. $Folder = new Folder($folderOne);
  1055. $Folder->copy(['to' => $folderThree, 'recursive' => false]);
  1056. $this->assertFileExists($folderThree . DS . 'file1.php');
  1057. $this->assertDirectoryNotExists($folderThree . DS . 'folderA');
  1058. $this->assertFileNotExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  1059. }
  1060. /**
  1061. * Setup filesystem for copy tests
  1062. * $path: folder_test/
  1063. * - folder1/file1.php
  1064. * - folder1/folderA/fileA.php
  1065. * - folder2/file2.php
  1066. * - folder2/folderB/fileB.php
  1067. * - folder3/
  1068. *
  1069. * @return array Filenames to extract in the test methods
  1070. */
  1071. protected function _setupFilesystem()
  1072. {
  1073. $path = TMP . 'tests';
  1074. $folderOne = $path . DS . 'folder1';
  1075. $folderOneA = $folderOne . DS . 'folderA';
  1076. $folderTwo = $path . DS . 'folder2';
  1077. $folderTwoB = $folderTwo . DS . 'folderB';
  1078. $folderThree = $path . DS . 'folder3';
  1079. $fileOne = $folderOne . DS . 'file1.php';
  1080. $fileTwo = $folderTwo . DS . 'file2.php';
  1081. $fileOneA = $folderOneA . DS . 'fileA.php';
  1082. $fileTwoB = $folderTwoB . DS . 'fileB.php';
  1083. new Folder($path, true);
  1084. new Folder($folderOne, true);
  1085. new Folder($folderOneA, true);
  1086. new Folder($folderTwo, true);
  1087. new Folder($folderTwoB, true);
  1088. new Folder($folderThree, true);
  1089. touch($fileOne);
  1090. touch($fileTwo);
  1091. touch($fileOneA);
  1092. touch($fileTwoB);
  1093. return compact(
  1094. 'path',
  1095. 'folderOne',
  1096. 'folderOneA',
  1097. 'folderTwo',
  1098. 'folderTwoB',
  1099. 'folderThree',
  1100. 'fileOne',
  1101. 'fileOneA',
  1102. 'fileTwo',
  1103. 'fileTwoB'
  1104. );
  1105. }
  1106. /**
  1107. * testMove method
  1108. *
  1109. * Verify that directories and files are moved recursively
  1110. * even if the destination directory already exists.
  1111. * Subdirectories existing in both destination and source directory
  1112. * are merged recursively.
  1113. *
  1114. * @return void
  1115. */
  1116. public function testMove()
  1117. {
  1118. /** @var string $path
  1119. * @var string $folderOne
  1120. * @var string $folderOneA
  1121. * @var string $folderTwo
  1122. * @var string $folderTwoB
  1123. * @var string $folderThree
  1124. * @var string $fileOne
  1125. * @var string $fileTwo
  1126. * @var string $fileOneA
  1127. * @var string $fileTwoB
  1128. */
  1129. extract($this->_setupFilesystem());
  1130. $Folder = new Folder($folderOne);
  1131. $result = $Folder->move($folderTwo);
  1132. $this->assertTrue($result);
  1133. $this->assertFileExists($folderTwo . '/file1.php');
  1134. $this->assertDirectoryExists($folderTwo . '/folderB');
  1135. $this->assertFileExists($folderTwo . '/folderB/fileB.php');
  1136. $this->assertFileNotExists($fileOne);
  1137. $this->assertFileExists($folderTwo . '/folderA');
  1138. $this->assertFileNotExists($folderOneA);
  1139. $this->assertFileNotExists($fileOneA);
  1140. $Folder = new Folder($folderTwo);
  1141. $Folder->delete();
  1142. new Folder($folderOne, true);
  1143. new Folder($folderOneA, true);
  1144. touch($fileOne);
  1145. touch($fileOneA);
  1146. $Folder = new Folder($folderOne);
  1147. $result = $Folder->move($folderTwo);
  1148. $this->assertTrue($result);
  1149. $this->assertFileExists($folderTwo . '/file1.php');
  1150. $this->assertDirectoryExists($folderTwo . '/folderA');
  1151. $this->assertFileExists($folderTwo . '/folderA/fileA.php');
  1152. $this->assertFileNotExists($fileOne);
  1153. $this->assertFileNotExists($folderOneA);
  1154. $this->assertFileNotExists($fileOneA);
  1155. $Folder = new Folder($folderTwo);
  1156. $Folder->delete();
  1157. new Folder($folderOne, true);
  1158. new Folder($folderOneA, true);
  1159. new Folder($folderTwo, true);
  1160. new Folder($folderTwoB, true);
  1161. touch($fileOne);
  1162. touch($fileOneA);
  1163. new Folder($folderOne . '/folderB', true);
  1164. touch($folderOne . '/folderB/fileB.php');
  1165. file_put_contents($folderTwoB . '/fileB.php', 'untouched');
  1166. $Folder = new Folder($folderOne);
  1167. $result = $Folder->move($folderTwo);
  1168. $this->assertTrue($result);
  1169. $this->assertFileExists($folderTwo . '/file1.php');
  1170. $this->assertStringEqualsFile($folderTwoB . '/fileB.php', '');
  1171. $this->assertFileNotExists($fileOne);
  1172. $this->assertFileNotExists($folderOneA);
  1173. $this->assertFileNotExists($fileOneA);
  1174. $Folder = new Folder($path);
  1175. $Folder->delete();
  1176. }
  1177. /**
  1178. * testMoveWithSkip method
  1179. *
  1180. * Verify that directories and files are moved recursively
  1181. * even if the destination directory already exists.
  1182. * Subdirectories existing in both destination and source directory
  1183. * are skipped and not merged or overwritten.
  1184. *
  1185. * @return void
  1186. */
  1187. public function testMoveWithSkip()
  1188. {
  1189. /** @var string $path
  1190. * @var string $folderOne
  1191. * @var string $folderOneA
  1192. * @var string $folderTwo
  1193. * @var string $folderTwoB
  1194. * @var string $folderThree
  1195. * @var string $fileOne
  1196. * @var string $fileTwo
  1197. * @var string $fileOneA
  1198. * @var string $fileTwoB
  1199. */
  1200. extract($this->_setupFilesystem());
  1201. $Folder = new Folder($folderOne);
  1202. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1203. $this->assertTrue($result);
  1204. $this->assertFileExists($folderTwo . '/file1.php');
  1205. $this->assertDirectoryExists($folderTwo . '/folderB');
  1206. $this->assertFileExists($folderTwoB . '/fileB.php');
  1207. $this->assertFileNotExists($fileOne);
  1208. $this->assertFileNotExists($folderOneA);
  1209. $this->assertFileNotExists($fileOneA);
  1210. $Folder = new Folder($folderTwo);
  1211. $Folder->delete();
  1212. new Folder($folderOne, true);
  1213. new Folder($folderOneA, true);
  1214. new Folder($folderTwo, true);
  1215. touch($fileOne);
  1216. touch($fileOneA);
  1217. $Folder = new Folder($folderOne);
  1218. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1219. $this->assertTrue($result);
  1220. $this->assertFileExists($folderTwo . '/file1.php');
  1221. $this->assertDirectoryExists($folderTwo . '/folderA');
  1222. $this->assertFileExists($folderTwo . '/folderA/fileA.php');
  1223. $this->assertFileNotExists($fileOne);
  1224. $this->assertFileNotExists($folderOneA);
  1225. $this->assertFileNotExists($fileOneA);
  1226. $Folder = new Folder($folderTwo);
  1227. $Folder->delete();
  1228. new Folder($folderOne, true);
  1229. new Folder($folderOneA, true);
  1230. new Folder($folderTwo, true);
  1231. new Folder($folderTwoB, true);
  1232. touch($fileOne);
  1233. touch($fileOneA);
  1234. file_put_contents($folderTwoB . '/fileB.php', 'untouched');
  1235. $Folder = new Folder($folderOne);
  1236. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1237. $this->assertTrue($result);
  1238. $this->assertFileExists($folderTwo . '/file1.php');
  1239. $this->assertStringEqualsFile($folderTwoB . '/fileB.php', 'untouched');
  1240. $this->assertFileNotExists($fileOne);
  1241. $this->assertFileNotExists($folderOneA);
  1242. $this->assertFileNotExists($fileOneA);
  1243. $Folder = new Folder($path);
  1244. $Folder->delete();
  1245. }
  1246. public function testMoveWithoutRecursive()
  1247. {
  1248. /** @var string $path
  1249. * @var string $folderOne
  1250. * @var string $folderOneA
  1251. * @var string $folderTwo
  1252. * @var string $folderTwoB
  1253. * @var string $folderThree
  1254. * @var string $fileOne
  1255. * @var string $fileTwo
  1256. * @var string $fileOneA
  1257. * @var string $fileTwoB
  1258. */
  1259. extract($this->_setupFilesystem());
  1260. $Folder = new Folder($folderOne);
  1261. $result = $Folder->move(['to' => $folderTwo, 'recursive' => false]);
  1262. $this->assertTrue($result);
  1263. $this->assertFileExists($folderTwo . '/file1.php');
  1264. $this->assertDirectoryNotExists($folderTwo . '/folderA');
  1265. $this->assertFileNotExists($folderTwo . '/folderA/fileA.php');
  1266. }
  1267. /**
  1268. * testSortByTime method
  1269. *
  1270. * Verify that the order using modified time is correct.
  1271. *
  1272. * @return void
  1273. */
  1274. public function testSortByTime()
  1275. {
  1276. $Folder = new Folder(TMP . 'tests', true);
  1277. $file2 = new File($Folder->pwd() . DS . 'file_2.tmp');
  1278. $file2->create();
  1279. sleep(1);
  1280. $file1 = new File($Folder->pwd() . DS . 'file_1.tmp');
  1281. $file1->create();
  1282. $results = $Folder->find('.*', Folder::SORT_TIME);
  1283. $this->assertSame(['file_2.tmp', 'file_1.tmp'], $results);
  1284. }
  1285. /**
  1286. * Verify that the order using name is correct.
  1287. */
  1288. public function testSortByName()
  1289. {
  1290. $Folder = new Folder(TMP . 'tests', true);
  1291. $fileA = new File($Folder->pwd() . DS . 'a.txt');
  1292. $fileA->create();
  1293. $fileC = new File($Folder->pwd() . DS . 'c.txt');
  1294. $fileC->create();
  1295. sleep(1);
  1296. $fileB = new File($Folder->pwd() . DS . 'b.txt');
  1297. $fileB->create();
  1298. $results = $Folder->find('.*', Folder::SORT_NAME);
  1299. $this->assertSame(['a.txt', 'b.txt', 'c.txt'], $results);
  1300. }
  1301. /**
  1302. * testIsRegisteredStreamWrapper
  1303. *
  1304. * @return void
  1305. */
  1306. public function testIsRegisteredStreamWrapper()
  1307. {
  1308. foreach (stream_get_wrappers() as $wrapper) {
  1309. $this->assertTrue(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1310. $this->assertFalse(Folder::isRegisteredStreamWrapper("bad." . $wrapper . "://path/to/file"));
  1311. }
  1312. $wrapper = 'unit.test1-';
  1313. $this->assertFalse(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1314. stream_wrapper_register($wrapper, self::class);
  1315. $this->assertTrue(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1316. stream_wrapper_unregister($wrapper);
  1317. }
  1318. }