FolderTest.php 42 KB

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