FolderTest.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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 . 'Http';
  413. $folder = new Folder($path);
  414. $expected = [
  415. $path . DS . 'Client',
  416. $path . DS . 'Cookie',
  417. $path . DS . 'Exception',
  418. $path . DS . 'Middleware',
  419. $path . DS . 'Session',
  420. ];
  421. $result = $folder->subdirectories();
  422. $this->assertSame([], array_diff($expected, $result));
  423. $result = $folder->subdirectories($path);
  424. $this->assertSame([], array_diff($expected, $result));
  425. $expected = [
  426. 'Client',
  427. 'Cookie',
  428. 'Exception',
  429. 'Middleware',
  430. 'Session',
  431. ];
  432. $result = $folder->subdirectories(null, false);
  433. $this->assertSame([], array_diff($expected, $result));
  434. $result = $folder->subdirectories($path, false);
  435. $this->assertSame([], array_diff($expected, $result));
  436. $expected = [];
  437. $result = $folder->subdirectories('NonExistentPath');
  438. $this->assertSame([], array_diff($expected, $result));
  439. $result = $folder->subdirectories($path . DS . 'Exception');
  440. $this->assertSame([], array_diff($expected, $result));
  441. }
  442. /**
  443. * testFolderTree method
  444. *
  445. * @return void
  446. */
  447. public function testFolderTree()
  448. {
  449. $Folder = new Folder();
  450. $expected = [
  451. [
  452. CORE_PATH . 'config',
  453. ],
  454. [
  455. CORE_PATH . 'config' . DS . 'config.php',
  456. ]
  457. ];
  458. $result = $Folder->tree(CORE_PATH . 'config', false);
  459. $this->assertSame([], array_diff($expected[0], $result[0]));
  460. $this->assertSame([], array_diff($result[0], $expected[0]));
  461. $result = $Folder->tree(CORE_PATH . 'config', false, 'dir');
  462. $this->assertSame([], array_diff($expected[0], $result));
  463. $this->assertSame([], array_diff($expected[0], $result));
  464. $result = $Folder->tree(CORE_PATH . 'config', false, 'files');
  465. $this->assertSame([], array_diff($expected[1], $result));
  466. $this->assertSame([], array_diff($expected[1], $result));
  467. }
  468. /**
  469. * testFolderTreeWithHiddenFiles method
  470. *
  471. * @return void
  472. */
  473. public function testFolderTreeWithHiddenFiles()
  474. {
  475. $this->skipIf(!is_writable(TMP), 'Can\'t test Folder::tree with hidden files unless the tmp folder is writable.');
  476. $path = TMP . 'tests' . DS;
  477. $Folder = new Folder($path . 'folder_tree_hidden', true, 0777);
  478. mkdir($Folder->path . DS . '.svn', 0777, true);
  479. touch($Folder->path . DS . '.svn/InHiddenFolder.php');
  480. mkdir($Folder->path . DS . '.svn/inhiddenfolder');
  481. touch($Folder->path . DS . '.svn/inhiddenfolder/NestedInHiddenFolder.php');
  482. touch($Folder->path . DS . 'not_hidden.txt');
  483. touch($Folder->path . DS . '.hidden.txt');
  484. mkdir($Folder->path . DS . 'visible_folder/.git', 0777, true);
  485. $expected = [
  486. [
  487. $Folder->path,
  488. $Folder->path . DS . 'visible_folder',
  489. ],
  490. [
  491. $Folder->path . DS . 'not_hidden.txt',
  492. ],
  493. ];
  494. $result = $Folder->tree(null, true);
  495. $this->assertEquals($expected, $result);
  496. $result = $Folder->tree(null, ['.']);
  497. $this->assertEquals($expected, $result);
  498. $expected = [
  499. [
  500. $Folder->path,
  501. $Folder->path . DS . 'visible_folder',
  502. $Folder->path . DS . 'visible_folder' . DS . '.git',
  503. $Folder->path . DS . '.svn',
  504. $Folder->path . DS . '.svn' . DS . 'inhiddenfolder',
  505. ],
  506. [
  507. $Folder->path . DS . 'not_hidden.txt',
  508. $Folder->path . DS . '.hidden.txt',
  509. $Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php',
  510. $Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php',
  511. ],
  512. ];
  513. $result = $Folder->tree(null, false);
  514. sort($result[0]);
  515. sort($expected[0]);
  516. sort($result[1]);
  517. sort($expected[1]);
  518. $this->assertEquals($expected, $result);
  519. $Folder->delete();
  520. }
  521. /**
  522. * testWindowsPath method
  523. *
  524. * @return void
  525. */
  526. public function testWindowsPath()
  527. {
  528. $this->assertFalse(Folder::isWindowsPath('0:\\cake\\is\\awesome'));
  529. $this->assertTrue(Folder::isWindowsPath('C:\\cake\\is\\awesome'));
  530. $this->assertTrue(Folder::isWindowsPath('d:\\cake\\is\\awesome'));
  531. $this->assertTrue(Folder::isWindowsPath('\\\\vmware-host\\Shared Folders\\file'));
  532. }
  533. /**
  534. * testIsAbsolute method
  535. *
  536. * @return void
  537. */
  538. public function testIsAbsolute()
  539. {
  540. $this->assertFalse(Folder::isAbsolute('path/to/file'));
  541. $this->assertFalse(Folder::isAbsolute('cake/'));
  542. $this->assertFalse(Folder::isAbsolute('path\\to\\file'));
  543. $this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
  544. $this->assertFalse(Folder::isAbsolute('\\path/to/file'));
  545. $this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
  546. $this->assertFalse(Folder::isAbsolute('notRegisteredStreamWrapper://example'));
  547. $this->assertFalse(Folder::isAbsolute('://example'));
  548. $this->assertTrue(Folder::isAbsolute('/usr/local'));
  549. $this->assertTrue(Folder::isAbsolute('//path/to/file'));
  550. $this->assertTrue(Folder::isAbsolute('C:\\cake'));
  551. $this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
  552. $this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
  553. $this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
  554. $this->assertTrue(Folder::isAbsolute('http://www.example.com'));
  555. }
  556. /**
  557. * testIsSlashTerm method
  558. *
  559. * @return void
  560. */
  561. public function testIsSlashTerm()
  562. {
  563. $this->assertFalse(Folder::isSlashTerm('cake'));
  564. $this->assertTrue(Folder::isSlashTerm('C:\\cake\\'));
  565. $this->assertTrue(Folder::isSlashTerm('/usr/local/'));
  566. }
  567. /**
  568. * testStatic method
  569. *
  570. * @return void
  571. */
  572. public function testSlashTerm()
  573. {
  574. $result = Folder::slashTerm('/path/to/file');
  575. $this->assertEquals('/path/to/file/', $result);
  576. }
  577. /**
  578. * testNormalizeFullPath method
  579. *
  580. * @return void
  581. */
  582. public function testNormalizeFullPath()
  583. {
  584. $path = '/path/to\file';
  585. $expected = '/path/to/file';
  586. $result = Folder::normalizeFullPath($path);
  587. $this->assertEquals($expected, $result);
  588. $path = '\\path\\to\file';
  589. $expected = '/path/to/file';
  590. $result = Folder::normalizeFullPath($path);
  591. $this->assertEquals($expected, $result);
  592. $path = 'C:\\path/to/file';
  593. $expected = 'C:\\path\\to\\file';
  594. $result = Folder::normalizeFullPath($path);
  595. $this->assertEquals($expected, $result);
  596. }
  597. /**
  598. * correctSlashFor method
  599. *
  600. * @return void
  601. */
  602. public function testCorrectSlashFor()
  603. {
  604. $path = '/path/to/file';
  605. $result = Folder::correctSlashFor($path);
  606. $this->assertEquals('/', $result);
  607. $path = '\\path\\to\\file';
  608. $result = Folder::correctSlashFor($path);
  609. $this->assertEquals('/', $result);
  610. $path = 'C:\\path\to\\file';
  611. $result = Folder::correctSlashFor($path);
  612. $this->assertEquals('\\', $result);
  613. }
  614. /**
  615. * testFind method
  616. *
  617. * @return void
  618. */
  619. public function testFind()
  620. {
  621. $Folder = new Folder();
  622. $Folder->cd(CORE_PATH . 'config');
  623. $result = $Folder->find();
  624. $expected = ['config.php'];
  625. $this->assertSame(array_diff($expected, $result), []);
  626. $this->assertSame(array_diff($expected, $result), []);
  627. $result = $Folder->find('.*', true);
  628. $expected = ['bootstrap.php', 'cacert.pem', 'config.php'];
  629. $this->assertSame($expected, $result);
  630. $result = $Folder->find('.*\.php');
  631. $expected = ['bootstrap.php', 'config.php'];
  632. $this->assertSame(array_diff($expected, $result), []);
  633. $this->assertSame(array_diff($expected, $result), []);
  634. $result = $Folder->find('.*\.php', true);
  635. $expected = ['bootstrap.php', 'config.php'];
  636. $this->assertSame($expected, $result);
  637. $result = $Folder->find('.*ig\.php');
  638. $expected = ['config.php'];
  639. $this->assertSame($expected, $result);
  640. $result = $Folder->find('config\.php');
  641. $expected = ['config.php'];
  642. $this->assertSame($expected, $result);
  643. $Folder = new Folder(TMP . 'tests/', true);
  644. new File($Folder->pwd() . DS . 'paths.php', true);
  645. $Folder->create($Folder->pwd() . DS . 'testme');
  646. $Folder->cd('testme');
  647. $result = $Folder->find('paths\.php');
  648. $expected = [];
  649. $this->assertSame($expected, $result);
  650. $Folder->cd($Folder->pwd() . '/..');
  651. $result = $Folder->find('paths\.php');
  652. $expected = ['paths.php'];
  653. $this->assertSame($expected, $result);
  654. }
  655. /**
  656. * testFindRecursive method
  657. *
  658. * @return void
  659. */
  660. public function testFindRecursive()
  661. {
  662. $Folder = new Folder(CORE_PATH . 'config');
  663. $result = $Folder->findRecursive('(config|paths)\.php');
  664. $expected = [
  665. CORE_PATH . 'config' . DS . 'config.php'
  666. ];
  667. $this->assertSame([], array_diff($expected, $result));
  668. $result = $Folder->findRecursive('(config|bootstrap)\.php', true);
  669. $expected = [
  670. CORE_PATH . 'config' . DS . 'bootstrap.php',
  671. CORE_PATH . 'config' . DS . 'config.php'
  672. ];
  673. $this->assertSame($expected, $result);
  674. $path = TMP . 'tests' . DS;
  675. $Folder = new Folder($path, true);
  676. $Folder->create($path . 'sessions');
  677. $Folder->create($path . 'testme');
  678. $Folder->cd($path . 'testme');
  679. $File = new File($Folder->pwd() . DS . 'paths.php');
  680. $File->create();
  681. $Folder->cd($path . 'sessions');
  682. $result = $Folder->findRecursive('paths\.php');
  683. $expected = [];
  684. $this->assertSame($expected, $result);
  685. $Folder->cd($path . 'testme');
  686. $File = new File($Folder->pwd() . DS . 'my.php');
  687. $File->create();
  688. $Folder->cd($path);
  689. $result = $Folder->findRecursive('(paths|my)\.php');
  690. $expected = [
  691. $path . 'testme' . DS . 'my.php',
  692. $path . 'testme' . DS . 'paths.php'
  693. ];
  694. $this->assertSame(sort($expected), sort($result));
  695. $result = $Folder->findRecursive('(paths|my)\.php', true);
  696. $expected = [
  697. $path . 'testme' . DS . 'my.php',
  698. $path . 'testme' . DS . 'paths.php'
  699. ];
  700. $this->assertSame($expected, $result);
  701. }
  702. /**
  703. * testConstructWithNonExistentPath method
  704. *
  705. * @return void
  706. */
  707. public function testConstructWithNonExistentPath()
  708. {
  709. $path = TMP . 'tests' . DS;
  710. $Folder = new Folder($path . 'config_non_existent', true);
  711. $this->assertDirectoryExists($path . 'config_non_existent');
  712. $Folder->cd($path);
  713. }
  714. /**
  715. * testDirSize method
  716. *
  717. * @return void
  718. */
  719. public function testDirSize()
  720. {
  721. $path = TMP . 'tests' . DS;
  722. $Folder = new Folder($path . 'config_non_existent', true);
  723. $this->assertEquals(0, $Folder->dirSize());
  724. $File = new File($Folder->pwd() . DS . 'my.php', true, 0777);
  725. $File->create();
  726. $File->write('something here');
  727. $File->close();
  728. $this->assertEquals(14, $Folder->dirSize());
  729. }
  730. /**
  731. * test that errors and messages can be restarted
  732. *
  733. * @return void
  734. */
  735. public function testReset()
  736. {
  737. $path = TMP . 'tests' . DS . 'folder_delete_test';
  738. mkdir($path, 0777, true);
  739. $folder = $path . DS . 'sub';
  740. mkdir($folder);
  741. $file = $folder . DS . 'file';
  742. touch($file);
  743. chmod($folder, 0555);
  744. chmod($file, 0444);
  745. $Folder = new Folder($folder);
  746. $return = $Folder->delete();
  747. $this->assertFalse($return);
  748. $messages = $Folder->messages();
  749. $errors = $Folder->errors();
  750. $expected = [
  751. $file . ' NOT removed',
  752. $folder . ' NOT removed',
  753. ];
  754. sort($expected);
  755. sort($errors);
  756. $this->assertEmpty($messages);
  757. $this->assertEquals($expected, $errors);
  758. chmod($file, 0644);
  759. chmod($folder, 0755);
  760. $return = $Folder->delete();
  761. $this->assertTrue($return);
  762. $messages = $Folder->messages();
  763. $errors = $Folder->errors();
  764. $expected = [
  765. $file . ' removed',
  766. $folder . ' removed',
  767. ];
  768. sort($expected);
  769. sort($messages);
  770. $this->assertEmpty($errors);
  771. $this->assertEquals($expected, $messages);
  772. }
  773. /**
  774. * testDelete method
  775. *
  776. * @return void
  777. */
  778. public function testDelete()
  779. {
  780. $path = TMP . 'tests' . DS . 'folder_delete_test';
  781. mkdir($path, 0777, true);
  782. touch($path . DS . 'file_1');
  783. mkdir($path . DS . 'level_1_1');
  784. touch($path . DS . 'level_1_1/file_1_1');
  785. mkdir($path . DS . 'level_1_1/level_2_1');
  786. touch($path . DS . 'level_1_1/level_2_1/file_2_1');
  787. touch($path . DS . 'level_1_1/level_2_1/file_2_2');
  788. mkdir($path . DS . 'level_1_1/level_2_2');
  789. $Folder = new Folder($path, true);
  790. $return = $Folder->delete();
  791. $this->assertTrue($return);
  792. $messages = $Folder->messages();
  793. $errors = $Folder->errors();
  794. $this->assertEquals([], $errors);
  795. $expected = [
  796. $path . DS . 'file_1 removed',
  797. $path . DS . 'level_1_1' . DS . 'file_1_1 removed',
  798. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1 removed',
  799. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2 removed',
  800. $path . DS . 'level_1_1' . DS . 'level_2_1 removed',
  801. $path . DS . 'level_1_1' . DS . 'level_2_2 removed',
  802. $path . DS . 'level_1_1 removed',
  803. $path . ' removed'
  804. ];
  805. sort($expected);
  806. sort($messages);
  807. $this->assertEquals($expected, $messages);
  808. }
  809. /**
  810. * testCopy method
  811. *
  812. * Verify that subdirectories existing in both destination and source directory
  813. * are merged recursively.
  814. *
  815. * @return void
  816. */
  817. public function testCopy()
  818. {
  819. /** @var string $path
  820. * @var string $folderOne
  821. * @var string $folderOneA
  822. * @var string $folderTwo
  823. * @var string $folderTwoB
  824. * @var string $folderThree
  825. * @var string $fileOne
  826. * @var string $fileTwo
  827. * @var string $fileOneA
  828. * @var string $fileTwoB
  829. */
  830. extract($this->_setupFilesystem());
  831. $Folder = new Folder($folderOne);
  832. $result = $Folder->copy($folderThree);
  833. $this->assertTrue($result);
  834. $this->assertFileExists($folderThree . DS . 'file1.php');
  835. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  836. $Folder = new Folder($folderTwo);
  837. $result = $Folder->copy($folderThree);
  838. $this->assertTrue($result);
  839. $this->assertFileExists($folderThree . DS . 'file1.php');
  840. $this->assertFileExists($folderThree . DS . 'file2.php');
  841. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  842. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  843. $Folder = new Folder($path);
  844. $Folder->delete();
  845. }
  846. /**
  847. * testCopyWithMerge method
  848. *
  849. * Verify that subdirectories existing in both destination and source directory
  850. * are merged recursively.
  851. *
  852. * @return void
  853. */
  854. public function testCopyWithMerge()
  855. {
  856. /** @var string $path
  857. * @var string $folderOne
  858. * @var string $folderOneA
  859. * @var string $folderTwo
  860. * @var string $folderTwoB
  861. * @var string $folderThree
  862. * @var string $fileOne
  863. * @var string $fileTwo
  864. * @var string $fileOneA
  865. * @var string $fileTwoB
  866. */
  867. extract($this->_setupFilesystem());
  868. $Folder = new Folder($folderOne);
  869. $result = $Folder->copy($folderThree);
  870. $this->assertTrue($result);
  871. $this->assertFileExists($folderThree . DS . 'file1.php');
  872. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  873. $Folder = new Folder($folderTwo);
  874. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::MERGE]);
  875. $this->assertTrue($result);
  876. $this->assertFileExists($folderThree . DS . 'file1.php');
  877. $this->assertFileExists($folderThree . DS . 'file2.php');
  878. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  879. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  880. }
  881. /**
  882. * testCopyWithSkip method
  883. *
  884. * Verify that directories and files are copied recursively
  885. * even if the destination directory already exists.
  886. * Subdirectories existing in both destination and source directory
  887. * are skipped and not merged or overwritten.
  888. *
  889. * @return void
  890. */
  891. public function testCopyWithSkip()
  892. {
  893. /** @var string $path
  894. * @var string $folderOne
  895. * @var string $folderOneA
  896. * @var string $folderTwo
  897. * @var string $folderTwoB
  898. * @var string $folderThree
  899. * @var string $fileOne
  900. * @var string $fileTwo
  901. * @var string $fileOneA
  902. * @var string $fileTwoB
  903. */
  904. extract($this->_setupFilesystem());
  905. $Folder = new Folder($folderOne);
  906. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  907. $this->assertTrue($result);
  908. $this->assertFileExists($folderTwo . DS . 'file1.php');
  909. $this->assertFileExists($folderTwo . DS . 'folderA' . DS . 'fileA.php');
  910. $Folder = new Folder($folderTwo);
  911. $Folder->delete();
  912. $Folder = new Folder($folderOne);
  913. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  914. $this->assertTrue($result);
  915. $this->assertFileExists($folderTwo . DS . 'file1.php');
  916. $this->assertFileExists($folderTwo . DS . 'folderA' . DS . 'fileA.php');
  917. $Folder = new Folder($folderTwo);
  918. $Folder->delete();
  919. new Folder($folderTwo, true);
  920. new Folder($folderTwo . DS . 'folderB', true);
  921. file_put_contents($folderTwo . DS . 'file2.php', 'touched');
  922. file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  923. $Folder = new Folder($folderTwo);
  924. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::SKIP]);
  925. $this->assertTrue($result);
  926. $this->assertFileExists($folderThree . DS . 'file2.php');
  927. $this->assertStringEqualsFile($folderThree . DS . 'file2.php', 'touched');
  928. $this->assertStringEqualsFile($folderThree . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  929. }
  930. /**
  931. * Test that SKIP mode skips files too.
  932. *
  933. * @return void
  934. */
  935. public function testCopyWithSkipFileSkipped()
  936. {
  937. $path = TMP . 'folder_test';
  938. $folderOne = $path . DS . 'folder1';
  939. $folderTwo = $path . DS . 'folder2';
  940. new Folder($path, true);
  941. new Folder($folderOne, true);
  942. new Folder($folderTwo, true);
  943. file_put_contents($folderOne . DS . 'fileA.txt', 'Folder One File');
  944. file_put_contents($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  945. $Folder = new Folder($folderOne);
  946. $result = $Folder->copy(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  947. $this->assertTrue($result);
  948. $this->assertStringEqualsFile($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  949. }
  950. /**
  951. * testCopyWithOverwrite
  952. *
  953. * Verify that subdirectories existing in both destination and source directory
  954. * are overwritten/replaced recursively.
  955. *
  956. * @return void
  957. */
  958. public function testCopyWithOverwrite()
  959. {
  960. /** @var string $path
  961. * @var string $folderOne
  962. * @var string $folderOneA
  963. * @var string $folderTwo
  964. * @var string $folderTwoB
  965. * @var string $folderThree
  966. * @var string $fileOne
  967. * @var string $fileTwo
  968. * @var string $fileOneA
  969. * @var string $fileTwoB
  970. */
  971. extract($this->_setupFilesystem());
  972. $Folder = new Folder($folderOne);
  973. $Folder->copy(['to' => $folderThree, 'scheme' => Folder::OVERWRITE]);
  974. $this->assertFileExists($folderThree . DS . 'file1.php');
  975. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  976. $Folder = new Folder($folderTwo);
  977. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::OVERWRITE]);
  978. $this->assertTrue($result);
  979. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  980. $Folder = new Folder($folderOne);
  981. unlink($fileOneA);
  982. $result = $Folder->copy(['to' => $folderThree, 'scheme' => Folder::OVERWRITE]);
  983. $this->assertTrue($result);
  984. $this->assertFileExists($folderThree . DS . 'file1.php');
  985. $this->assertFileExists($folderThree . DS . 'file2.php');
  986. $this->assertFileNotExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  987. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  988. }
  989. /**
  990. * testCopyWithoutRecursive
  991. *
  992. * Verify that only the files exist in the target directory.
  993. *
  994. * @return void
  995. */
  996. public function testCopyWithoutRecursive()
  997. {
  998. /** @var string $path
  999. * @var string $folderOne
  1000. * @var string $folderOneA
  1001. * @var string $folderTwo
  1002. * @var string $folderTwoB
  1003. * @var string $folderThree
  1004. * @var string $fileOne
  1005. * @var string $fileTwo
  1006. * @var string $fileOneA
  1007. * @var string $fileTwoB
  1008. */
  1009. extract($this->_setupFilesystem());
  1010. $Folder = new Folder($folderOne);
  1011. $Folder->copy(['to' => $folderThree, 'recursive' => false]);
  1012. $this->assertFileExists($folderThree . DS . 'file1.php');
  1013. $this->assertDirectoryNotExists($folderThree . DS . 'folderA');
  1014. $this->assertFileNotExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  1015. }
  1016. /**
  1017. * Setup filesystem for copy tests
  1018. * $path: folder_test/
  1019. * - folder1/file1.php
  1020. * - folder1/folderA/fileA.php
  1021. * - folder2/file2.php
  1022. * - folder2/folderB/fileB.php
  1023. * - folder3/
  1024. *
  1025. * @return array Filenames to extract in the test methods
  1026. */
  1027. protected function _setupFilesystem()
  1028. {
  1029. $path = TMP . 'tests';
  1030. $folderOne = $path . DS . 'folder1';
  1031. $folderOneA = $folderOne . DS . 'folderA';
  1032. $folderTwo = $path . DS . 'folder2';
  1033. $folderTwoB = $folderTwo . DS . 'folderB';
  1034. $folderThree = $path . DS . 'folder3';
  1035. $fileOne = $folderOne . DS . 'file1.php';
  1036. $fileTwo = $folderTwo . DS . 'file2.php';
  1037. $fileOneA = $folderOneA . DS . 'fileA.php';
  1038. $fileTwoB = $folderTwoB . DS . 'fileB.php';
  1039. new Folder($path, true);
  1040. new Folder($folderOne, true);
  1041. new Folder($folderOneA, true);
  1042. new Folder($folderTwo, true);
  1043. new Folder($folderTwoB, true);
  1044. new Folder($folderThree, true);
  1045. touch($fileOne);
  1046. touch($fileTwo);
  1047. touch($fileOneA);
  1048. touch($fileTwoB);
  1049. return compact(
  1050. 'path',
  1051. 'folderOne',
  1052. 'folderOneA',
  1053. 'folderTwo',
  1054. 'folderTwoB',
  1055. 'folderThree',
  1056. 'fileOne',
  1057. 'fileOneA',
  1058. 'fileTwo',
  1059. 'fileTwoB'
  1060. );
  1061. }
  1062. /**
  1063. * testMove method
  1064. *
  1065. * Verify that directories and files are moved recursively
  1066. * even if the destination directory already exists.
  1067. * Subdirectories existing in both destination and source directory
  1068. * are merged recursively.
  1069. *
  1070. * @return void
  1071. */
  1072. public function testMove()
  1073. {
  1074. /** @var string $path
  1075. * @var string $folderOne
  1076. * @var string $folderOneA
  1077. * @var string $folderTwo
  1078. * @var string $folderTwoB
  1079. * @var string $folderThree
  1080. * @var string $fileOne
  1081. * @var string $fileTwo
  1082. * @var string $fileOneA
  1083. * @var string $fileTwoB
  1084. */
  1085. extract($this->_setupFilesystem());
  1086. $Folder = new Folder($folderOne);
  1087. $result = $Folder->move($folderTwo);
  1088. $this->assertTrue($result);
  1089. $this->assertFileExists($folderTwo . '/file1.php');
  1090. $this->assertDirectoryExists($folderTwo . '/folderB');
  1091. $this->assertFileExists($folderTwo . '/folderB/fileB.php');
  1092. $this->assertFileNotExists($fileOne);
  1093. $this->assertFileExists($folderTwo . '/folderA');
  1094. $this->assertFileNotExists($folderOneA);
  1095. $this->assertFileNotExists($fileOneA);
  1096. $Folder = new Folder($folderTwo);
  1097. $Folder->delete();
  1098. new Folder($folderOne, true);
  1099. new Folder($folderOneA, true);
  1100. touch($fileOne);
  1101. touch($fileOneA);
  1102. $Folder = new Folder($folderOne);
  1103. $result = $Folder->move($folderTwo);
  1104. $this->assertTrue($result);
  1105. $this->assertFileExists($folderTwo . '/file1.php');
  1106. $this->assertDirectoryExists($folderTwo . '/folderA');
  1107. $this->assertFileExists($folderTwo . '/folderA/fileA.php');
  1108. $this->assertFileNotExists($fileOne);
  1109. $this->assertFileNotExists($folderOneA);
  1110. $this->assertFileNotExists($fileOneA);
  1111. $Folder = new Folder($folderTwo);
  1112. $Folder->delete();
  1113. new Folder($folderOne, true);
  1114. new Folder($folderOneA, true);
  1115. new Folder($folderTwo, true);
  1116. new Folder($folderTwoB, true);
  1117. touch($fileOne);
  1118. touch($fileOneA);
  1119. new Folder($folderOne . '/folderB', true);
  1120. touch($folderOne . '/folderB/fileB.php');
  1121. file_put_contents($folderTwoB . '/fileB.php', 'untouched');
  1122. $Folder = new Folder($folderOne);
  1123. $result = $Folder->move($folderTwo);
  1124. $this->assertTrue($result);
  1125. $this->assertFileExists($folderTwo . '/file1.php');
  1126. $this->assertStringEqualsFile($folderTwoB . '/fileB.php', '');
  1127. $this->assertFileNotExists($fileOne);
  1128. $this->assertFileNotExists($folderOneA);
  1129. $this->assertFileNotExists($fileOneA);
  1130. $Folder = new Folder($path);
  1131. $Folder->delete();
  1132. }
  1133. /**
  1134. * testMoveWithSkip method
  1135. *
  1136. * Verify that directories and files are moved recursively
  1137. * even if the destination directory already exists.
  1138. * Subdirectories existing in both destination and source directory
  1139. * are skipped and not merged or overwritten.
  1140. *
  1141. * @return void
  1142. */
  1143. public function testMoveWithSkip()
  1144. {
  1145. /** @var string $path
  1146. * @var string $folderOne
  1147. * @var string $folderOneA
  1148. * @var string $folderTwo
  1149. * @var string $folderTwoB
  1150. * @var string $folderThree
  1151. * @var string $fileOne
  1152. * @var string $fileTwo
  1153. * @var string $fileOneA
  1154. * @var string $fileTwoB
  1155. */
  1156. extract($this->_setupFilesystem());
  1157. $Folder = new Folder($folderOne);
  1158. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1159. $this->assertTrue($result);
  1160. $this->assertFileExists($folderTwo . '/file1.php');
  1161. $this->assertDirectoryExists($folderTwo . '/folderB');
  1162. $this->assertFileExists($folderTwoB . '/fileB.php');
  1163. $this->assertFileNotExists($fileOne);
  1164. $this->assertFileNotExists($folderOneA);
  1165. $this->assertFileNotExists($fileOneA);
  1166. $Folder = new Folder($folderTwo);
  1167. $Folder->delete();
  1168. new Folder($folderOne, true);
  1169. new Folder($folderOneA, true);
  1170. new Folder($folderTwo, true);
  1171. touch($fileOne);
  1172. touch($fileOneA);
  1173. $Folder = new Folder($folderOne);
  1174. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1175. $this->assertTrue($result);
  1176. $this->assertFileExists($folderTwo . '/file1.php');
  1177. $this->assertDirectoryExists($folderTwo . '/folderA');
  1178. $this->assertFileExists($folderTwo . '/folderA/fileA.php');
  1179. $this->assertFileNotExists($fileOne);
  1180. $this->assertFileNotExists($folderOneA);
  1181. $this->assertFileNotExists($fileOneA);
  1182. $Folder = new Folder($folderTwo);
  1183. $Folder->delete();
  1184. new Folder($folderOne, true);
  1185. new Folder($folderOneA, true);
  1186. new Folder($folderTwo, true);
  1187. new Folder($folderTwoB, true);
  1188. touch($fileOne);
  1189. touch($fileOneA);
  1190. file_put_contents($folderTwoB . '/fileB.php', 'untouched');
  1191. $Folder = new Folder($folderOne);
  1192. $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]);
  1193. $this->assertTrue($result);
  1194. $this->assertFileExists($folderTwo . '/file1.php');
  1195. $this->assertStringEqualsFile($folderTwoB . '/fileB.php', 'untouched');
  1196. $this->assertFileNotExists($fileOne);
  1197. $this->assertFileNotExists($folderOneA);
  1198. $this->assertFileNotExists($fileOneA);
  1199. $Folder = new Folder($path);
  1200. $Folder->delete();
  1201. }
  1202. public function testMoveWithoutRecursive()
  1203. {
  1204. /** @var string $path
  1205. * @var string $folderOne
  1206. * @var string $folderOneA
  1207. * @var string $folderTwo
  1208. * @var string $folderTwoB
  1209. * @var string $folderThree
  1210. * @var string $fileOne
  1211. * @var string $fileTwo
  1212. * @var string $fileOneA
  1213. * @var string $fileTwoB
  1214. */
  1215. extract($this->_setupFilesystem());
  1216. $Folder = new Folder($folderOne);
  1217. $result = $Folder->move(['to' => $folderTwo, 'recursive' => false]);
  1218. $this->assertTrue($result);
  1219. $this->assertFileExists($folderTwo . '/file1.php');
  1220. $this->assertDirectoryNotExists($folderTwo . '/folderA');
  1221. $this->assertFileNotExists($folderTwo . '/folderA/fileA.php');
  1222. }
  1223. /**
  1224. * testSortByTime method
  1225. *
  1226. * Verify that the order using modified time is correct.
  1227. *
  1228. * @return void
  1229. */
  1230. public function testSortByTime()
  1231. {
  1232. $Folder = new Folder(TMP . 'tests', true);
  1233. $file2 = new File($Folder->pwd() . DS . 'file_2.tmp');
  1234. $file2->create();
  1235. sleep(1);
  1236. $file1 = new File($Folder->pwd() . DS . 'file_1.tmp');
  1237. $file1->create();
  1238. $results = $Folder->find('.*', Folder::SORT_TIME);
  1239. $this->assertSame(['file_2.tmp', 'file_1.tmp'], $results);
  1240. }
  1241. /**
  1242. * Verify that the order using name is correct.
  1243. */
  1244. public function testSortByName()
  1245. {
  1246. $Folder = new Folder(TMP . 'tests', true);
  1247. $fileA = new File($Folder->pwd() . DS . 'a.txt');
  1248. $fileA->create();
  1249. $fileC = new File($Folder->pwd() . DS . 'c.txt');
  1250. $fileC->create();
  1251. sleep(1);
  1252. $fileB = new File($Folder->pwd() . DS . 'b.txt');
  1253. $fileB->create();
  1254. $results = $Folder->find('.*', Folder::SORT_NAME);
  1255. $this->assertSame(['a.txt', 'b.txt', 'c.txt'], $results);
  1256. }
  1257. /**
  1258. * testIsRegisteredStreamWrapper
  1259. *
  1260. * @return void
  1261. */
  1262. public function testIsRegisteredStreamWrapper()
  1263. {
  1264. foreach (stream_get_wrappers() as $wrapper) {
  1265. $this->assertTrue(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1266. $this->assertFalse(Folder::isRegisteredStreamWrapper("bad." . $wrapper . "://path/to/file"));
  1267. }
  1268. $wrapper = 'unit.test1-';
  1269. $this->assertFalse(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1270. stream_wrapper_register($wrapper, self::class);
  1271. $this->assertTrue(Folder::isRegisteredStreamWrapper($wrapper . "://path/to/file"));
  1272. stream_wrapper_unregister($wrapper);
  1273. }
  1274. }