FolderTest.php 46 KB

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