FolderTest.php 47 KB

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