FolderTest.php 46 KB

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