FolderTest.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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. $this->deprecated(function () {
  582. $path = '/path/to/file';
  583. $result = Folder::normalizePath($path);
  584. $this->assertEquals('/', $result);
  585. $path = '\\path\\\to\\\file';
  586. $result = Folder::normalizePath($path);
  587. $this->assertEquals('/', $result);
  588. $path = 'C:\\path\\to\\file';
  589. $result = Folder::normalizePath($path);
  590. $this->assertEquals('\\', $result);
  591. });
  592. }
  593. /**
  594. * testNormalizeFullPath method
  595. *
  596. * @return void
  597. */
  598. public function testNormalizeFullPath()
  599. {
  600. $path = '/path/to\file';
  601. $expected = '/path/to/file';
  602. $result = Folder::normalizeFullPath($path);
  603. $this->assertEquals($expected, $result);
  604. $path = '\\path\\to\file';
  605. $expected = '/path/to/file';
  606. $result = Folder::normalizeFullPath($path);
  607. $this->assertEquals($expected, $result);
  608. $path = 'C:\\path/to/file';
  609. $expected = 'C:\\path\\to\\file';
  610. $result = Folder::normalizeFullPath($path);
  611. $this->assertEquals($expected, $result);
  612. }
  613. /**
  614. * correctSlashFor method
  615. *
  616. * @return void
  617. */
  618. public function testCorrectSlashFor()
  619. {
  620. $path = '/path/to/file';
  621. $result = Folder::correctSlashFor($path);
  622. $this->assertEquals('/', $result);
  623. $path = '\\path\\to\\file';
  624. $result = Folder::correctSlashFor($path);
  625. $this->assertEquals('/', $result);
  626. $path = 'C:\\path\to\\file';
  627. $result = Folder::correctSlashFor($path);
  628. $this->assertEquals('\\', $result);
  629. }
  630. /**
  631. * testInCakePath method
  632. *
  633. * @group deprecated
  634. * @return void
  635. */
  636. public function testInCakePath()
  637. {
  638. $this->deprecated(function () {
  639. $Folder = new Folder();
  640. $Folder->cd(ROOT);
  641. $path = 'C:\\path\\to\\file';
  642. $result = $Folder->inCakePath($path);
  643. $this->assertFalse($result);
  644. $path = ROOT;
  645. $Folder->cd(ROOT);
  646. $result = $Folder->inCakePath($path);
  647. $this->assertFalse($result);
  648. $path = DS . 'config';
  649. $Folder->cd(ROOT . DS . 'config');
  650. $result = $Folder->inCakePath($path);
  651. $this->assertTrue($result);
  652. });
  653. }
  654. /**
  655. * testFind method
  656. *
  657. * @return void
  658. */
  659. public function testFind()
  660. {
  661. $Folder = new Folder();
  662. $Folder->cd(CORE_PATH . 'config');
  663. $result = $Folder->find();
  664. $expected = ['config.php'];
  665. $this->assertSame(array_diff($expected, $result), []);
  666. $this->assertSame(array_diff($expected, $result), []);
  667. $result = $Folder->find('.*', true);
  668. $expected = ['bootstrap.php', 'cacert.pem', 'config.php'];
  669. $this->assertSame($expected, $result);
  670. $result = $Folder->find('.*\.php');
  671. $expected = ['bootstrap.php', 'config.php'];
  672. $this->assertSame(array_diff($expected, $result), []);
  673. $this->assertSame(array_diff($expected, $result), []);
  674. $result = $Folder->find('.*\.php', true);
  675. $expected = ['bootstrap.php', 'config.php'];
  676. $this->assertSame($expected, $result);
  677. $result = $Folder->find('.*ig\.php');
  678. $expected = ['config.php'];
  679. $this->assertSame($expected, $result);
  680. $result = $Folder->find('config\.php');
  681. $expected = ['config.php'];
  682. $this->assertSame($expected, $result);
  683. $Folder = new Folder(TMP . 'tests/', true);
  684. new File($Folder->pwd() . DS . 'paths.php', true);
  685. $Folder->create($Folder->pwd() . DS . 'testme');
  686. $Folder->cd('testme');
  687. $result = $Folder->find('paths\.php');
  688. $expected = [];
  689. $this->assertSame($expected, $result);
  690. $Folder->cd($Folder->pwd() . '/..');
  691. $result = $Folder->find('paths\.php');
  692. $expected = ['paths.php'];
  693. $this->assertSame($expected, $result);
  694. }
  695. /**
  696. * testFindRecursive method
  697. *
  698. * @return void
  699. */
  700. public function testFindRecursive()
  701. {
  702. $Folder = new Folder(CORE_PATH . 'config');
  703. $result = $Folder->findRecursive('(config|paths)\.php');
  704. $expected = [
  705. CORE_PATH . 'config' . DS . 'config.php'
  706. ];
  707. $this->assertSame([], array_diff($expected, $result));
  708. $result = $Folder->findRecursive('(config|bootstrap)\.php', true);
  709. $expected = [
  710. CORE_PATH . 'config' . DS . 'bootstrap.php',
  711. CORE_PATH . 'config' . DS . 'config.php'
  712. ];
  713. $this->assertSame($expected, $result);
  714. $path = TMP . 'tests' . DS;
  715. $Folder = new Folder($path, true);
  716. $Folder->create($path . 'sessions');
  717. $Folder->create($path . 'testme');
  718. $Folder->cd($path . 'testme');
  719. $File = new File($Folder->pwd() . DS . 'paths.php');
  720. $File->create();
  721. $Folder->cd($path . 'sessions');
  722. $result = $Folder->findRecursive('paths\.php');
  723. $expected = [];
  724. $this->assertSame($expected, $result);
  725. $Folder->cd($path . 'testme');
  726. $File = new File($Folder->pwd() . DS . 'my.php');
  727. $File->create();
  728. $Folder->cd($path);
  729. $result = $Folder->findRecursive('(paths|my)\.php');
  730. $expected = [
  731. $path . 'testme' . DS . 'my.php',
  732. $path . 'testme' . DS . 'paths.php'
  733. ];
  734. $this->assertSame(sort($expected), sort($result));
  735. $result = $Folder->findRecursive('(paths|my)\.php', true);
  736. $expected = [
  737. $path . 'testme' . DS . 'my.php',
  738. $path . 'testme' . DS . 'paths.php'
  739. ];
  740. $this->assertSame($expected, $result);
  741. }
  742. /**
  743. * testConstructWithNonExistentPath method
  744. *
  745. * @return void
  746. */
  747. public function testConstructWithNonExistentPath()
  748. {
  749. $path = TMP . 'tests' . DS;
  750. $Folder = new Folder($path . 'config_non_existent', true);
  751. $this->assertDirectoryExists($path . 'config_non_existent');
  752. $Folder->cd($path);
  753. }
  754. /**
  755. * testDirSize method
  756. *
  757. * @return void
  758. */
  759. public function testDirSize()
  760. {
  761. $path = TMP . 'tests' . DS;
  762. $Folder = new Folder($path . 'config_non_existent', true);
  763. $this->assertEquals(0, $Folder->dirSize());
  764. $File = new File($Folder->pwd() . DS . 'my.php', true, 0777);
  765. $File->create();
  766. $File->write('something here');
  767. $File->close();
  768. $this->assertEquals(14, $Folder->dirSize());
  769. }
  770. /**
  771. * test that errors and messages can be restarted
  772. *
  773. * @return void
  774. */
  775. public function testReset()
  776. {
  777. $path = TMP . 'tests' . DS . 'folder_delete_test';
  778. mkdir($path, 0777, true);
  779. $folder = $path . DS . 'sub';
  780. mkdir($folder);
  781. $file = $folder . DS . 'file';
  782. touch($file);
  783. chmod($folder, 0555);
  784. chmod($file, 0444);
  785. $Folder = new Folder($folder);
  786. $return = $Folder->delete();
  787. $this->assertFalse($return);
  788. $messages = $Folder->messages();
  789. $errors = $Folder->errors();
  790. $expected = [
  791. $file . ' NOT removed',
  792. $folder . ' NOT removed',
  793. ];
  794. sort($expected);
  795. sort($errors);
  796. $this->assertEmpty($messages);
  797. $this->assertEquals($expected, $errors);
  798. chmod($file, 0644);
  799. chmod($folder, 0755);
  800. $return = $Folder->delete();
  801. $this->assertTrue($return);
  802. $messages = $Folder->messages();
  803. $errors = $Folder->errors();
  804. $expected = [
  805. $file . ' removed',
  806. $folder . ' removed',
  807. ];
  808. sort($expected);
  809. sort($messages);
  810. $this->assertEmpty($errors);
  811. $this->assertEquals($expected, $messages);
  812. }
  813. /**
  814. * testDelete method
  815. *
  816. * @return void
  817. */
  818. public function testDelete()
  819. {
  820. $path = TMP . 'tests' . DS . 'folder_delete_test';
  821. mkdir($path, 0777, true);
  822. touch($path . DS . 'file_1');
  823. mkdir($path . DS . 'level_1_1');
  824. touch($path . DS . 'level_1_1/file_1_1');
  825. mkdir($path . DS . 'level_1_1/level_2_1');
  826. touch($path . DS . 'level_1_1/level_2_1/file_2_1');
  827. touch($path . DS . 'level_1_1/level_2_1/file_2_2');
  828. mkdir($path . DS . 'level_1_1/level_2_2');
  829. $Folder = new Folder($path, true);
  830. $return = $Folder->delete();
  831. $this->assertTrue($return);
  832. $messages = $Folder->messages();
  833. $errors = $Folder->errors();
  834. $this->assertEquals([], $errors);
  835. $expected = [
  836. $path . DS . 'file_1 removed',
  837. $path . DS . 'level_1_1' . DS . 'file_1_1 removed',
  838. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1 removed',
  839. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2 removed',
  840. $path . DS . 'level_1_1' . DS . 'level_2_1 removed',
  841. $path . DS . 'level_1_1' . DS . 'level_2_2 removed',
  842. $path . DS . 'level_1_1 removed',
  843. $path . ' removed'
  844. ];
  845. sort($expected);
  846. sort($messages);
  847. $this->assertEquals($expected, $messages);
  848. }
  849. /**
  850. * testCopy method
  851. *
  852. * Verify that subdirectories existing in both destination and source directory
  853. * are merged recursively.
  854. *
  855. * @return void
  856. */
  857. public function testCopy()
  858. {
  859. /**
  860. * @var string $path
  861. * @var string $folderOne
  862. * @var string $folderOneA
  863. * @var string $folderTwo
  864. * @var string $folderTwoB
  865. * @var string $folderThree
  866. * @var string $fileOne
  867. * @var string $fileTwo
  868. * @var string $fileOneA
  869. * @var string $fileTwoB
  870. */
  871. extract($this->_setupFilesystem());
  872. $Folder = new Folder($folderOne);
  873. $result = $Folder->copy($folderThree);
  874. $this->assertTrue($result);
  875. $this->assertFileExists($folderThree . DS . 'file1.php');
  876. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  877. $Folder = new Folder($folderTwo);
  878. $result = $Folder->copy($folderThree);
  879. $this->assertTrue($result);
  880. $this->assertFileExists($folderThree . DS . 'file1.php');
  881. $this->assertFileExists($folderThree . DS . 'file2.php');
  882. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  883. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  884. $Folder = new Folder($path);
  885. $Folder->delete();
  886. }
  887. /**
  888. * testCopyWithMerge method
  889. *
  890. * Verify that subdirectories existing in both destination and source directory
  891. * are merged recursively.
  892. *
  893. * @return void
  894. */
  895. public function testCopyWithMerge()
  896. {
  897. /**
  898. * @var string $path
  899. * @var string $folderOne
  900. * @var string $folderOneA
  901. * @var string $folderTwo
  902. * @var string $folderTwoB
  903. * @var string $folderThree
  904. * @var string $fileOne
  905. * @var string $fileTwo
  906. * @var string $fileOneA
  907. * @var string $fileTwoB
  908. */
  909. extract($this->_setupFilesystem());
  910. $Folder = new Folder($folderOne);
  911. $result = $Folder->copy($folderThree);
  912. $this->assertTrue($result);
  913. $this->assertFileExists($folderThree . DS . 'file1.php');
  914. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  915. $Folder = new Folder($folderTwo);
  916. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::MERGE]);
  917. $this->assertTrue($result);
  918. $this->assertFileExists($folderThree . DS . 'file1.php');
  919. $this->assertFileExists($folderThree . DS . 'file2.php');
  920. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  921. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  922. }
  923. /**
  924. * testCopyWithSkip method
  925. *
  926. * Verify that directories and files are copied recursively
  927. * even if the destination directory already exists.
  928. * Subdirectories existing in both destination and source directory
  929. * are skipped and not merged or overwritten.
  930. *
  931. * @return void
  932. */
  933. public function testCopyWithSkip()
  934. {
  935. /**
  936. * @var string $path
  937. * @var string $folderOne
  938. * @var string $folderOneA
  939. * @var string $folderTwo
  940. * @var string $folderTwoB
  941. * @var string $folderThree
  942. * @var string $fileOne
  943. * @var string $fileTwo
  944. * @var string $fileOneA
  945. * @var string $fileTwoB
  946. */
  947. extract($this->_setupFilesystem());
  948. $Folder = new Folder($folderOne);
  949. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  950. $this->assertTrue($result);
  951. $this->assertFileExists($folderTwo . DS . 'file1.php');
  952. $this->assertFileExists($folderTwo . DS . 'folderA' . DS . 'fileA.php');
  953. $Folder = new Folder($folderTwo);
  954. $Folder->delete();
  955. $Folder = new Folder($folderOne);
  956. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  957. $this->assertTrue($result);
  958. $this->assertFileExists($folderTwo . DS . 'file1.php');
  959. $this->assertFileExists($folderTwo . DS . 'folderA' . DS . 'fileA.php');
  960. $Folder = new Folder($folderTwo);
  961. $Folder->delete();
  962. new Folder($folderTwo, true);
  963. new Folder($folderTwo . DS . 'folderB', true);
  964. file_put_contents($folderTwo . DS . 'file2.php', 'touched');
  965. file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  966. $Folder = new Folder($folderTwo);
  967. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::SKIP]);
  968. $this->assertTrue($result);
  969. $this->assertFileExists($folderThree . DS . 'file2.php');
  970. $this->assertStringEqualsFile($folderThree . DS . 'file2.php', 'touched');
  971. $this->assertStringEqualsFile($folderThree . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  972. }
  973. /**
  974. * Test that SKIP mode skips files too.
  975. *
  976. * @return void
  977. */
  978. public function testCopyWithSkipFileSkipped()
  979. {
  980. $path = TMP . 'folder_test';
  981. $folderOne = $path . DS . 'folder1';
  982. $folderTwo = $path . DS . 'folder2';
  983. new Folder($path, true);
  984. new Folder($folderOne, true);
  985. new Folder($folderTwo, true);
  986. file_put_contents($folderOne . DS . 'fileA.txt', 'Folder One File');
  987. file_put_contents($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  988. $Folder = new Folder($folderOne);
  989. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  990. $this->assertTrue($result);
  991. $this->assertStringEqualsFile($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  992. }
  993. /**
  994. * testCopyWithOverwrite
  995. *
  996. * Verify that subdirectories existing in both destination and source directory
  997. * are overwritten/replaced recursively.
  998. *
  999. * @return void
  1000. */
  1001. public function testCopyWithOverwrite()
  1002. {
  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. /**
  1043. * @var string $path
  1044. * @var string $folderOne
  1045. * @var string $folderOneA
  1046. * @var string $folderTwo
  1047. * @var string $folderTwoB
  1048. * @var string $folderThree
  1049. * @var string $fileOne
  1050. * @var string $fileTwo
  1051. * @var string $fileOneA
  1052. * @var string $fileTwoB
  1053. */
  1054. extract($this->_setupFilesystem());
  1055. $Folder = new Folder($folderOne);
  1056. $Folder->copy(['to' => $folderThree, 'recursive' => false]);
  1057. $this->assertFileExists($folderThree . DS . 'file1.php');
  1058. $this->assertDirectoryNotExists($folderThree . DS . 'folderA');
  1059. $this->assertFileNotExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  1060. }
  1061. /**
  1062. * Setup filesystem for copy tests
  1063. * $path: folder_test/
  1064. * - folder1/file1.php
  1065. * - folder1/folderA/fileA.php
  1066. * - folder2/file2.php
  1067. * - folder2/folderB/fileB.php
  1068. * - folder3/
  1069. *
  1070. * @return array Filenames to extract in the test methods
  1071. */
  1072. protected function _setupFilesystem()
  1073. {
  1074. $path = TMP . 'tests';
  1075. $folderOne = $path . DS . 'folder1';
  1076. $folderOneA = $folderOne . DS . 'folderA';
  1077. $folderTwo = $path . DS . 'folder2';
  1078. $folderTwoB = $folderTwo . DS . 'folderB';
  1079. $folderThree = $path . DS . 'folder3';
  1080. $fileOne = $folderOne . DS . 'file1.php';
  1081. $fileTwo = $folderTwo . DS . 'file2.php';
  1082. $fileOneA = $folderOneA . DS . 'fileA.php';
  1083. $fileTwoB = $folderTwoB . DS . 'fileB.php';
  1084. new Folder($path, true);
  1085. new Folder($folderOne, true);
  1086. new Folder($folderOneA, true);
  1087. new Folder($folderTwo, true);
  1088. new Folder($folderTwoB, true);
  1089. new Folder($folderThree, true);
  1090. touch($fileOne);
  1091. touch($fileTwo);
  1092. touch($fileOneA);
  1093. touch($fileTwoB);
  1094. return compact(
  1095. 'path',
  1096. 'folderOne',
  1097. 'folderOneA',
  1098. 'folderTwo',
  1099. 'folderTwoB',
  1100. 'folderThree',
  1101. 'fileOne',
  1102. 'fileOneA',
  1103. 'fileTwo',
  1104. 'fileTwoB'
  1105. );
  1106. }
  1107. /**
  1108. * testMove method
  1109. *
  1110. * Verify that directories and files are moved recursively
  1111. * even if the destination directory already exists.
  1112. * Subdirectories existing in both destination and source directory
  1113. * are merged recursively.
  1114. *
  1115. * @return void
  1116. */
  1117. public function testMove()
  1118. {
  1119. /**
  1120. * @var string $path
  1121. * @var string $folderOne
  1122. * @var string $folderOneA
  1123. * @var string $folderTwo
  1124. * @var string $folderTwoB
  1125. * @var string $folderThree
  1126. * @var string $fileOne
  1127. * @var string $fileTwo
  1128. * @var string $fileOneA
  1129. * @var string $fileTwoB
  1130. */
  1131. extract($this->_setupFilesystem());
  1132. $Folder = new Folder($folderOne);
  1133. $result = $Folder->move($folderTwo);
  1134. $this->assertTrue($result);
  1135. $this->assertFileExists($folderTwo . '/file1.php');
  1136. $this->assertDirectoryExists($folderTwo . '/folderB');
  1137. $this->assertFileExists($folderTwo . '/folderB/fileB.php');
  1138. $this->assertFileNotExists($fileOne);
  1139. $this->assertFileExists($folderTwo . '/folderA');
  1140. $this->assertFileNotExists($folderOneA);
  1141. $this->assertFileNotExists($fileOneA);
  1142. $Folder = new Folder($folderTwo);
  1143. $Folder->delete();
  1144. new Folder($folderOne, true);
  1145. new Folder($folderOneA, true);
  1146. touch($fileOne);
  1147. touch($fileOneA);
  1148. $Folder = new Folder($folderOne);
  1149. $result = $Folder->move($folderTwo);
  1150. $this->assertTrue($result);
  1151. $this->assertFileExists($folderTwo . '/file1.php');
  1152. $this->assertDirectoryExists($folderTwo . '/folderA');
  1153. $this->assertFileExists($folderTwo . '/folderA/fileA.php');
  1154. $this->assertFileNotExists($fileOne);
  1155. $this->assertFileNotExists($folderOneA);
  1156. $this->assertFileNotExists($fileOneA);
  1157. $Folder = new Folder($folderTwo);
  1158. $Folder->delete();
  1159. new Folder($folderOne, true);
  1160. new Folder($folderOneA, true);
  1161. new Folder($folderTwo, true);
  1162. new Folder($folderTwoB, true);
  1163. touch($fileOne);
  1164. touch($fileOneA);
  1165. new Folder($folderOne . '/folderB', true);
  1166. touch($folderOne . '/folderB/fileB.php');
  1167. file_put_contents($folderTwoB . '/fileB.php', 'untouched');
  1168. $Folder = new Folder($folderOne);
  1169. $result = $Folder->move($folderTwo);
  1170. $this->assertTrue($result);
  1171. $this->assertFileExists($folderTwo . '/file1.php');
  1172. $this->assertStringEqualsFile($folderTwoB . '/fileB.php', '');
  1173. $this->assertFileNotExists($fileOne);
  1174. $this->assertFileNotExists($folderOneA);
  1175. $this->assertFileNotExists($fileOneA);
  1176. $Folder = new Folder($path);
  1177. $Folder->delete();
  1178. }
  1179. /**
  1180. * testMoveWithSkip method
  1181. *
  1182. * Verify that directories and files are moved recursively
  1183. * even if the destination directory already exists.
  1184. * Subdirectories existing in both destination and source directory
  1185. * are skipped and not merged or overwritten.
  1186. *
  1187. * @return void
  1188. */
  1189. public function testMoveWithSkip()
  1190. {
  1191. /**
  1192. * @var string $path
  1193. * @var string $folderOne
  1194. * @var string $folderOneA
  1195. * @var string $folderTwo
  1196. * @var string $folderTwoB
  1197. * @var string $folderThree
  1198. * @var string $fileOne
  1199. * @var string $fileTwo
  1200. * @var string $fileOneA
  1201. * @var string $fileTwoB
  1202. */
  1203. extract($this->_setupFilesystem());
  1204. $Folder = new Folder($folderOne);
  1205. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1206. $this->assertTrue($result);
  1207. $this->assertFileExists($folderTwo . '/file1.php');
  1208. $this->assertDirectoryExists($folderTwo . '/folderB');
  1209. $this->assertFileExists($folderTwoB . '/fileB.php');
  1210. $this->assertFileNotExists($fileOne);
  1211. $this->assertFileNotExists($folderOneA);
  1212. $this->assertFileNotExists($fileOneA);
  1213. $Folder = new Folder($folderTwo);
  1214. $Folder->delete();
  1215. new Folder($folderOne, true);
  1216. new Folder($folderOneA, true);
  1217. new Folder($folderTwo, true);
  1218. touch($fileOne);
  1219. touch($fileOneA);
  1220. $Folder = new Folder($folderOne);
  1221. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1222. $this->assertTrue($result);
  1223. $this->assertFileExists($folderTwo . '/file1.php');
  1224. $this->assertDirectoryExists($folderTwo . '/folderA');
  1225. $this->assertFileExists($folderTwo . '/folderA/fileA.php');
  1226. $this->assertFileNotExists($fileOne);
  1227. $this->assertFileNotExists($folderOneA);
  1228. $this->assertFileNotExists($fileOneA);
  1229. $Folder = new Folder($folderTwo);
  1230. $Folder->delete();
  1231. new Folder($folderOne, true);
  1232. new Folder($folderOneA, true);
  1233. new Folder($folderTwo, true);
  1234. new Folder($folderTwoB, true);
  1235. touch($fileOne);
  1236. touch($fileOneA);
  1237. file_put_contents($folderTwoB . '/fileB.php', 'untouched');
  1238. $Folder = new Folder($folderOne);
  1239. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1240. $this->assertTrue($result);
  1241. $this->assertFileExists($folderTwo . '/file1.php');
  1242. $this->assertStringEqualsFile($folderTwoB . '/fileB.php', 'untouched');
  1243. $this->assertFileNotExists($fileOne);
  1244. $this->assertFileNotExists($folderOneA);
  1245. $this->assertFileNotExists($fileOneA);
  1246. $Folder = new Folder($path);
  1247. $Folder->delete();
  1248. }
  1249. public function testMoveWithoutRecursive()
  1250. {
  1251. /**
  1252. * @var string $path
  1253. * @var string $folderOne
  1254. * @var string $folderOneA
  1255. * @var string $folderTwo
  1256. * @var string $folderTwoB
  1257. * @var string $folderThree
  1258. * @var string $fileOne
  1259. * @var string $fileTwo
  1260. * @var string $fileOneA
  1261. * @var string $fileTwoB
  1262. */
  1263. extract($this->_setupFilesystem());
  1264. $Folder = new Folder($folderOne);
  1265. $result = $Folder->move(['to' => $folderTwo, 'recursive' => false]);
  1266. $this->assertTrue($result);
  1267. $this->assertFileExists($folderTwo . '/file1.php');
  1268. $this->assertDirectoryNotExists($folderTwo . '/folderA');
  1269. $this->assertFileNotExists($folderTwo . '/folderA/fileA.php');
  1270. }
  1271. /**
  1272. * testSortByTime method
  1273. *
  1274. * Verify that the order using modified time is correct.
  1275. *
  1276. * @return void
  1277. */
  1278. public function testSortByTime()
  1279. {
  1280. $Folder = new Folder(TMP . 'tests', true);
  1281. $file2 = new File($Folder->pwd() . DS . 'file_2.tmp');
  1282. $file2->create();
  1283. sleep(1);
  1284. $file1 = new File($Folder->pwd() . DS . 'file_1.tmp');
  1285. $file1->create();
  1286. $results = $Folder->find('.*', Folder::SORT_TIME);
  1287. $this->assertSame(['file_2.tmp', 'file_1.tmp'], $results);
  1288. }
  1289. /**
  1290. * Verify that the order using name is correct.
  1291. */
  1292. public function testSortByName()
  1293. {
  1294. $Folder = new Folder(TMP . 'tests', true);
  1295. $fileA = new File($Folder->pwd() . DS . 'a.txt');
  1296. $fileA->create();
  1297. $fileC = new File($Folder->pwd() . DS . 'c.txt');
  1298. $fileC->create();
  1299. sleep(1);
  1300. $fileB = new File($Folder->pwd() . DS . 'b.txt');
  1301. $fileB->create();
  1302. $results = $Folder->find('.*', Folder::SORT_NAME);
  1303. $this->assertSame(['a.txt', 'b.txt', 'c.txt'], $results);
  1304. }
  1305. /**
  1306. * testIsRegisteredStreamWrapper
  1307. *
  1308. * @return void
  1309. */
  1310. public function testIsRegisteredStreamWrapper()
  1311. {
  1312. foreach (stream_get_wrappers() as $wrapper) {
  1313. $this->assertTrue(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1314. $this->assertFalse(Folder::isRegisteredStreamWrapper("bad." . $wrapper . "://path/to/file"));
  1315. }
  1316. $wrapper = 'unit.test1-';
  1317. $this->assertFalse(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1318. stream_wrapper_register($wrapper, self::class);
  1319. $this->assertTrue(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1320. stream_wrapper_unregister($wrapper);
  1321. }
  1322. }