PageRenderTime 148ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/folder.test.php

https://github.com/hardsshah/bookmarks
PHP | 570 lines | 473 code | 30 blank | 67 comment | 0 complexity | 92f00fc00c96452fd1bca64f12e42156 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * FolderTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', 'File');
  28. /**
  29. * FolderTest class
  30. *
  31. * @package cake
  32. * @subpackage cake.tests.cases.libs
  33. */
  34. class FolderTest extends CakeTestCase {
  35. /**
  36. * testBasic method
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. function testBasic() {
  42. $path = dirname(__FILE__);
  43. $Folder =& new Folder($path);
  44. $result = $Folder->pwd();
  45. $this->assertEqual($result, $path);
  46. $result = Folder::addPathElement($path, 'test');
  47. $expected = $path . DS . 'test';
  48. $this->assertEqual($result, $expected);
  49. $result = $Folder->cd(ROOT);
  50. $expected = ROOT;
  51. $this->assertEqual($result, $expected);
  52. $result = $Folder->cd(ROOT . DS . 'non-existent');
  53. $this->assertFalse($result);
  54. }
  55. /**
  56. * testInPath method
  57. *
  58. * @access public
  59. * @return void
  60. */
  61. function testInPath() {
  62. $path = dirname(dirname(__FILE__));
  63. $inside = dirname($path) . DS;
  64. $Folder =& new Folder($path);
  65. $result = $Folder->pwd();
  66. $this->assertEqual($result, $path);
  67. $result = Folder::isSlashTerm($inside);
  68. $this->assertTrue($result);
  69. $result = $Folder->realpath('tests/');
  70. $this->assertEqual($result, $path . DS .'tests' . DS);
  71. $result = $Folder->inPath('tests' . DS);
  72. $this->assertTrue($result);
  73. $result = $Folder->inPath(DS . 'non-existing' . $inside);
  74. $this->assertFalse($result);
  75. }
  76. /**
  77. * testOperations method
  78. *
  79. * @access public
  80. * @return void
  81. */
  82. function testOperations() {
  83. $path = TEST_CAKE_CORE_INCLUDE_PATH . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel';
  84. $Folder =& new Folder($path);
  85. $result = is_dir($Folder->pwd());
  86. $this->assertTrue($result);
  87. $new = TMP . 'test_folder_new';
  88. $result = $Folder->create($new);
  89. $this->assertTrue($result);
  90. $copy = TMP . 'test_folder_copy';
  91. $result = $Folder->copy($copy);
  92. $this->assertTrue($result);
  93. $copy = TMP . 'test_folder_copy';
  94. $result = $Folder->cp($copy);
  95. $this->assertTrue($result);
  96. $copy = TMP . 'test_folder_copy';
  97. $result = $Folder->chmod($copy, 0755, false);
  98. $this->assertTrue($result);
  99. $result = $Folder->cd($copy);
  100. $this->assertTrue($result);
  101. $mv = TMP . 'test_folder_mv';
  102. $result = $Folder->move($mv);
  103. $this->assertTrue($result);
  104. $mv = TMP . 'test_folder_mv_2';
  105. $result = $Folder->mv($mv);
  106. $this->assertTrue($result);
  107. $result = $Folder->delete($new);
  108. $this->assertTrue($result);
  109. $result = $Folder->delete($mv);
  110. $this->assertTrue($result);
  111. $result = $Folder->rm($mv);
  112. $this->assertTrue($result);
  113. $new = APP . 'index.php';
  114. $result = $Folder->create($new);
  115. $this->assertFalse($result);
  116. $expected = $new . ' is a file';
  117. $result = array_pop($Folder->errors());
  118. $this->assertEqual($result, $expected);
  119. $new = TMP . 'test_folder_new';
  120. $result = $Folder->create($new);
  121. $this->assertTrue($result);
  122. $result = $Folder->cd($new);
  123. $this->assertTrue($result);
  124. $result = $Folder->delete();
  125. $this->assertTrue($result);
  126. $Folder =& new Folder('non-existent');
  127. $result = $Folder->pwd();
  128. $this->assertNull($result);
  129. }
  130. /**
  131. * testChmod method
  132. *
  133. * @return void
  134. * @access public
  135. */
  136. function testChmod() {
  137. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Folder permissions tests not supported on Windows');
  138. $path = TEST_CAKE_CORE_INCLUDE_PATH . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel';
  139. $Folder =& new Folder($path);
  140. $subdir = 'test_folder_new';
  141. $new = TMP . $subdir;
  142. $this->assertTrue($Folder->create($new));
  143. $this->assertTrue($Folder->create($new . DS . 'test1'));
  144. $this->assertTrue($Folder->create($new . DS . 'test2'));
  145. $filePath = $new . DS . 'test1.php';
  146. $File =& new File($filePath);
  147. $this->assertTrue($File->create());
  148. $copy = TMP . 'test_folder_copy';
  149. $this->assertTrue($Folder->chmod($new, 0777, true));
  150. $this->assertEqual($File->perms(), '0777');
  151. $Folder->delete($new);
  152. }
  153. /**
  154. * testRealPathForWebroot method
  155. *
  156. * @access public
  157. * @return void
  158. */
  159. function testRealPathForWebroot() {
  160. $Folder = new Folder('files/');
  161. $this->assertEqual(realpath('files/'), $Folder->path);
  162. }
  163. /**
  164. * testZeroAsDirectory method
  165. *
  166. * @access public
  167. * @return void
  168. */
  169. function testZeroAsDirectory() {
  170. $Folder =& new Folder(TMP);
  171. $new = TMP . '0';
  172. $this->assertTrue($Folder->create($new));
  173. $result = $Folder->read(true, true);
  174. $expected = array('0', 'cache', 'logs', 'sessions', 'tests');
  175. $this->assertEqual($expected, $result[0]);
  176. $result = $Folder->read(true, array('.', '..', 'logs', '.svn'));
  177. $expected = array('0', 'cache', 'sessions', 'tests');
  178. $this->assertEqual($expected, $result[0]);
  179. $result = $Folder->delete($new);
  180. $this->assertTrue($result);
  181. }
  182. /**
  183. * testFolderRead method
  184. *
  185. * @access public
  186. * @return void
  187. */
  188. function testFolderRead() {
  189. $Folder =& new Folder(TMP);
  190. $expected = array('cache', 'logs', 'sessions', 'tests');
  191. $result = $Folder->read(true, true);
  192. $this->assertEqual($result[0], $expected);
  193. $Folder->path = TMP . DS . 'non-existent';
  194. $expected = array(array(), array());
  195. $result = $Folder->read(true, true);
  196. $this->assertEqual($result, $expected);
  197. }
  198. /**
  199. * testFolderTree method
  200. *
  201. * @access public
  202. * @return void
  203. */
  204. function testFolderTree() {
  205. $Folder =& new Folder();
  206. $expected = array(
  207. array(
  208. TEST_CAKE_CORE_INCLUDE_PATH . 'config',
  209. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode',
  210. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding'
  211. ),
  212. array(
  213. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php',
  214. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'paths.php',
  215. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0080_00ff.php',
  216. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0100_017f.php',
  217. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0180_024F.php',
  218. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0250_02af.php',
  219. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0370_03ff.php',
  220. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0400_04ff.php',
  221. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0500_052f.php',
  222. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '0530_058f.php',
  223. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '1e00_1eff.php',
  224. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '1f00_1fff.php',
  225. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2100_214f.php',
  226. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2150_218f.php',
  227. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2460_24ff.php',
  228. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c00_2c5f.php',
  229. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c60_2c7f.php',
  230. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c80_2cff.php',
  231. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'unicode' . DS . 'casefolding' . DS . 'ff00_ffef.php'
  232. )
  233. );
  234. $result = $Folder->tree(TEST_CAKE_CORE_INCLUDE_PATH . 'config', false);
  235. $this->assertIdentical(array_diff($expected[0], $result[0]), array());
  236. $this->assertIdentical(array_diff($result[0], $expected[0]), array());
  237. $result = $Folder->tree(TEST_CAKE_CORE_INCLUDE_PATH . 'config', false, 'dir');
  238. $this->assertIdentical(array_diff($expected[0], $result), array());
  239. $this->assertIdentical(array_diff($result, $expected[0]), array());
  240. $result = $Folder->tree(TEST_CAKE_CORE_INCLUDE_PATH . 'config', false, 'files');
  241. $this->assertIdentical(array_diff($expected[1], $result), array());
  242. $this->assertIdentical(array_diff($result, $expected[1]), array());
  243. }
  244. /**
  245. * testWindowsPath method
  246. *
  247. * @access public
  248. * @return void
  249. */
  250. function testWindowsPath() {
  251. $this->assertFalse(Folder::isWindowsPath('0:\\cake\\is\\awesome'));
  252. $this->assertTrue(Folder::isWindowsPath('C:\\cake\\is\\awesome'));
  253. $this->assertTrue(Folder::isWindowsPath('d:\\cake\\is\\awesome'));
  254. }
  255. /**
  256. * testIsAbsolute method
  257. *
  258. * @access public
  259. * @return void
  260. */
  261. function testIsAbsolute() {
  262. $this->assertFalse(Folder::isAbsolute('path/to/file'));
  263. $this->assertFalse(Folder::isAbsolute('cake/'));
  264. $this->assertFalse(Folder::isAbsolute('path\\to\\file'));
  265. $this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
  266. $this->assertFalse(Folder::isAbsolute('\\path/to/file'));
  267. $this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
  268. $this->assertTrue(Folder::isAbsolute('/usr/local'));
  269. $this->assertTrue(Folder::isAbsolute('//path/to/file'));
  270. $this->assertTrue(Folder::isAbsolute('C:\\cake'));
  271. $this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
  272. $this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
  273. }
  274. /**
  275. * testIsSlashTerm method
  276. *
  277. * @access public
  278. * @return void
  279. */
  280. function testIsSlashTerm() {
  281. $this->assertFalse(Folder::isSlashTerm('cake'));
  282. $this->assertTrue(Folder::isSlashTerm('C:\\cake\\'));
  283. $this->assertTrue(Folder::isSlashTerm('/usr/local/'));
  284. }
  285. /**
  286. * testStatic method
  287. *
  288. * @access public
  289. * @return void
  290. */
  291. function testSlashTerm() {
  292. $result = Folder::slashTerm('/path/to/file');
  293. $this->assertEqual($result, '/path/to/file/');
  294. }
  295. /**
  296. * testNormalizePath method
  297. *
  298. * @access public
  299. * @return void
  300. */
  301. function testNormalizePath() {
  302. $path = '/path/to/file';
  303. $result = Folder::normalizePath($path);
  304. $this->assertEqual($result, '/');
  305. $path = '\\path\\\to\\\file';
  306. $result = Folder::normalizePath($path);
  307. $this->assertEqual($result, '/');
  308. $path = 'C:\\path\\to\\file';
  309. $result = Folder::normalizePath($path);
  310. $this->assertEqual($result, '\\');
  311. }
  312. /**
  313. * correctSlashFor method
  314. *
  315. * @access public
  316. * @return void
  317. */
  318. function testCorrectSlashFor() {
  319. $path = '/path/to/file';
  320. $result = Folder::correctSlashFor($path);
  321. $this->assertEqual($result, '/');
  322. $path = '\\path\\to\\file';
  323. $result = Folder::correctSlashFor($path);
  324. $this->assertEqual($result, '/');
  325. $path = 'C:\\path\to\\file';
  326. $result = Folder::correctSlashFor($path);
  327. $this->assertEqual($result, '\\');
  328. }
  329. /**
  330. * testInCakePath method
  331. *
  332. * @access public
  333. * @return void
  334. */
  335. function testInCakePath() {
  336. $Folder =& new Folder();
  337. $Folder->cd(ROOT);
  338. $path = 'C:\\path\\to\\file';
  339. $result = $Folder->inCakePath($path);
  340. $this->assertFalse($result);
  341. $path = ROOT;
  342. $Folder->cd(ROOT);
  343. $result = $Folder->inCakePath($path);
  344. $this->assertFalse($result);
  345. // WHY DOES THIS FAIL ??
  346. $path = DS . 'cake' . DS . 'config';
  347. $Folder->cd(ROOT . DS . 'cake' . DS . 'config');
  348. $result = $Folder->inCakePath($path);
  349. $this->assertTrue($result);
  350. }
  351. /**
  352. * testFind method
  353. *
  354. * @access public
  355. * @return void
  356. */
  357. function testFind() {
  358. $Folder =& new Folder();
  359. $Folder->cd(TEST_CAKE_CORE_INCLUDE_PATH . 'config');
  360. $result = $Folder->find();
  361. $expected = array('config.php', 'paths.php');
  362. $this->assertIdentical(array_diff($expected, $result), array());
  363. $this->assertIdentical(array_diff($result, $expected), array());
  364. $result = $Folder->find('.*', true);
  365. $expected = array('config.php', 'paths.php');
  366. $this->assertIdentical($result, $expected);
  367. $result = $Folder->find('.*\.php');
  368. $expected = array('config.php', 'paths.php');
  369. $this->assertIdentical(array_diff($expected, $result), array());
  370. $this->assertIdentical(array_diff($result, $expected), array());
  371. $result = $Folder->find('.*\.php', true);
  372. $expected = array('config.php', 'paths.php');
  373. $this->assertIdentical($result, $expected);
  374. $result = $Folder->find('.*ig\.php');
  375. $expected = array('config.php');
  376. $this->assertIdentical($result, $expected);
  377. $result = $Folder->find('paths\.php');
  378. $expected = array('paths.php');
  379. $this->assertIdentical($result, $expected);
  380. $Folder->cd(TMP);
  381. $file = new File($Folder->pwd() . DS . 'paths.php', true);
  382. $Folder->mkdir($Folder->pwd() . DS . 'testme');
  383. $Folder->cd('testme');
  384. $result = $Folder->find('paths\.php');
  385. $expected = array();
  386. $this->assertIdentical($result, $expected);
  387. $Folder->cd($Folder->pwd() . '/..');
  388. $result = $Folder->find('paths\.php');
  389. $expected = array('paths.php');
  390. $this->assertIdentical($result, $expected);
  391. $Folder->cd(TMP);
  392. $Folder->delete($Folder->pwd() . DS . 'testme');
  393. $file->delete();
  394. }
  395. /**
  396. * testFindRecursive method
  397. *
  398. * @access public
  399. * @return void
  400. */
  401. function testFindRecursive() {
  402. $Folder =& new Folder();
  403. $Folder->cd(TEST_CAKE_CORE_INCLUDE_PATH);
  404. $result = $Folder->findRecursive('(config|paths)\.php');
  405. $expected = array(
  406. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php',
  407. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'paths.php'
  408. );
  409. $this->assertIdentical(array_diff($expected, $result), array());
  410. $this->assertIdentical(array_diff($result, $expected), array());
  411. $result = $Folder->findRecursive('(config|paths)\.php', true);
  412. $expected = array(
  413. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php',
  414. TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'paths.php'
  415. );
  416. $this->assertIdentical($result, $expected);
  417. $Folder->cd(TMP);
  418. $Folder->mkdir($Folder->pwd() . DS . 'testme');
  419. $Folder->cd('testme');
  420. $File =& new File($Folder->pwd() . DS . 'paths.php');
  421. $File->create();
  422. $Folder->cd(TMP . 'sessions');
  423. $result = $Folder->findRecursive('paths\.php');
  424. $expected = array();
  425. $this->assertIdentical($result, $expected);
  426. $Folder->cd(TMP . 'testme');
  427. $File =& new File($Folder->pwd() . DS . 'my.php');
  428. $File->create();
  429. $Folder->cd($Folder->pwd() . '/../..');
  430. $result = $Folder->findRecursive('(paths|my)\.php');
  431. $expected = array(
  432. TMP . 'testme' . DS . 'my.php',
  433. TMP . 'testme' . DS . 'paths.php'
  434. );
  435. $this->assertIdentical(array_diff($expected, $result), array());
  436. $this->assertIdentical(array_diff($result, $expected), array());
  437. $result = $Folder->findRecursive('(paths|my)\.php', true);
  438. $expected = array(
  439. TMP . 'testme' . DS . 'my.php',
  440. TMP . 'testme' . DS . 'paths.php'
  441. );
  442. $this->assertIdentical($result, $expected);
  443. $Folder->cd(TEST_CAKE_CORE_INCLUDE_PATH . 'config');
  444. $Folder->cd(TMP);
  445. $Folder->delete($Folder->pwd() . DS . 'testme');
  446. $File->delete();
  447. }
  448. /**
  449. * testConstructWithNonExistantPath method
  450. *
  451. * @access public
  452. * @return void
  453. */
  454. function testConstructWithNonExistantPath() {
  455. $Folder =& new Folder(TMP . 'config_non_existant', true);
  456. $this->assertTrue(is_dir(TMP . 'config_non_existant'));
  457. $Folder->cd(TMP);
  458. $Folder->delete($Folder->pwd() . 'config_non_existant');
  459. }
  460. /**
  461. * testDirSize method
  462. *
  463. * @access public
  464. * @return void
  465. */
  466. function testDirSize() {
  467. $Folder =& new Folder(TMP . 'config_non_existant', true);
  468. $this->assertEqual($Folder->dirSize(), 0);
  469. $File =& new File($Folder->pwd() . DS . 'my.php', true, 0777);
  470. $File->create();
  471. $File->write('something here');
  472. $File->close();
  473. $this->assertEqual($Folder->dirSize(), 14);
  474. $Folder->cd(TMP);
  475. $Folder->delete($Folder->pwd() . 'config_non_existant');
  476. }
  477. /**
  478. * testDelete method
  479. *
  480. * @access public
  481. * @return void
  482. */
  483. function testDelete() {
  484. $path = TMP . 'folder_delete_test';
  485. $Folder =& new Folder($path, true);
  486. touch(TMP . 'folder_delete_test' . DS . 'file1');
  487. touch(TMP . 'folder_delete_test' . DS . 'file2');
  488. $return = $Folder->delete();
  489. $this->assertTrue($return);
  490. $messages = $Folder->messages();
  491. $errors = $Folder->errors();
  492. $this->assertEqual($errors, array());
  493. $expected = array(
  494. $path . ' created',
  495. $path . DS . 'file1 removed',
  496. $path . DS . 'file2 removed',
  497. $path . ' removed'
  498. );
  499. $this->assertEqual($expected, $messages);
  500. }
  501. }
  502. ?>