FolderTest.php 33 KB

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