FolderTest.php 44 KB

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