/tests/suites/unit/joomla/filesystem/JFolderTest.php

https://github.com/Paladin/joomla-platform · PHP · 409 lines · 242 code · 36 blank · 131 comment · 2 complexity · 78d52f6af162a015caad41352b6ba6d1 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. *
  5. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. JLoader::register('JFolder', JPATH_PLATFORM . '/joomla/filesystem/folder.php');
  9. /**
  10. * Test class for JFolder.
  11. * Generated by PHPUnit on 2011-10-26 at 19:32:37.
  12. *
  13. * @package Joomla.UnitTest
  14. * @subpackage Event
  15. * @since 11.1
  16. */
  17. class JFolderTest extends TestCase
  18. {
  19. /**
  20. * @var JFolder
  21. */
  22. protected $object;
  23. /**
  24. * Test...
  25. *
  26. * @todo Implement testCopy().
  27. *
  28. * @return void
  29. */
  30. public function testCopy()
  31. {
  32. // Remove the following lines when you implement this test.
  33. $this->markTestIncomplete(
  34. 'This test has not been implemented yet.'
  35. );
  36. }
  37. /**
  38. * Test...
  39. *
  40. * @todo Implement testCreate().
  41. *
  42. * @return void
  43. */
  44. public function testCreate()
  45. {
  46. // Remove the following lines when you implement this test.
  47. $this->markTestIncomplete(
  48. 'This test has not been implemented yet.'
  49. );
  50. }
  51. /**
  52. * Test...
  53. *
  54. * @todo Implement testDelete().
  55. *
  56. * @return void
  57. */
  58. public function testDelete()
  59. {
  60. // Remove the following lines when you implement this test.
  61. $this->markTestIncomplete(
  62. 'This test has not been implemented yet.'
  63. );
  64. }
  65. /**
  66. * Tests the JFolder::delete method with an array as an input
  67. *
  68. * @return void
  69. *
  70. * @expectedException UnexpectedValueException
  71. * @since 11.3
  72. */
  73. public function testDeleteArrayPath()
  74. {
  75. JFolder::delete(array('/path/to/folder') );
  76. }
  77. /**
  78. * Test...
  79. *
  80. * @todo Implement testMove().
  81. *
  82. * @return void
  83. */
  84. public function testMove()
  85. {
  86. // Remove the following lines when you implement this test.
  87. $this->markTestIncomplete(
  88. 'This test has not been implemented yet.'
  89. );
  90. }
  91. /**
  92. * Test...
  93. *
  94. * @covers JFile::exists
  95. *
  96. * @return void
  97. */
  98. public function testExists()
  99. {
  100. $this->assertTrue(
  101. JFolder::exists(__DIR__)
  102. );
  103. }
  104. /**
  105. * Tests the JFolder::files method.
  106. *
  107. * @return void
  108. *
  109. * @since 12.1
  110. *
  111. * @covers JFolder::files
  112. * @covers JFolder::_items
  113. */
  114. public function testFiles()
  115. {
  116. // Make sure previous test files are cleaned up
  117. $this->_cleanupTestFiles();
  118. // Make some test files and folders
  119. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test'), 0777, true);
  120. file_put_contents(JPath::clean(JPATH_TESTS . '/tmp/test/index.html'), 'test');
  121. file_put_contents(JPath::clean(JPATH_TESTS . '/tmp/test/index.txt'), 'test');
  122. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test/test'), 0777, true);
  123. file_put_contents(JPath::clean(JPATH_TESTS . '/tmp/test/test/index.html'), 'test');
  124. file_put_contents(JPath::clean(JPATH_TESTS . '/tmp/test/test/index.txt'), 'test');
  125. // Use of realpath to ensure test works for on all platforms
  126. $result = JFolder::files(JPath::clean(JPATH_TESTS . '/tmp/test'), 'index.*', true, true, array('index.html'));
  127. $result[0] = realpath($result[0]);
  128. $result[1] = realpath($result[1]);
  129. $this->assertEquals(
  130. array(
  131. JPath::clean(JPATH_TESTS . '/tmp/test/index.txt'),
  132. JPath::clean(JPATH_TESTS . '/tmp/test/test/index.txt')
  133. ),
  134. $result,
  135. 'Line: ' . __LINE__ . ' Should exclude index.html files'
  136. );
  137. // Use of realpath to ensure test works for on all platforms
  138. $result = JFolder::files(JPath::clean(JPATH_TESTS . '/tmp/test'), 'index.html', true, true);
  139. $result[0] = realpath($result[0]);
  140. $result[1] = realpath($result[1]);
  141. $this->assertEquals(
  142. array(
  143. JPath::clean(JPATH_TESTS . '/tmp/test/index.html'),
  144. JPath::clean(JPATH_TESTS . '/tmp/test/test/index.html')
  145. ),
  146. $result,
  147. 'Line: ' . __LINE__ . ' Should include full path of both index.html files'
  148. );
  149. $this->assertEquals(
  150. array(
  151. JPath::clean('index.html'),
  152. JPath::clean('index.html')
  153. ),
  154. JFolder::files(JPath::clean(JPATH_TESTS . '/tmp/test'), 'index.html', true, false),
  155. 'Line: ' . __LINE__ . ' Should include only file names of both index.html files'
  156. );
  157. // Use of realpath to ensure test works for on all platforms
  158. $result = JFolder::files(JPath::clean(JPATH_TESTS . '/tmp/test'), 'index.html', false, true);
  159. $result[0] = realpath($result[0]);
  160. $this->assertEquals(
  161. array(
  162. JPath::clean(JPATH_TESTS . '/tmp/test/index.html')
  163. ),
  164. $result,
  165. 'Line: ' . __LINE__ . ' Non-recursive should only return top folder file full path'
  166. );
  167. $this->assertEquals(
  168. array(
  169. JPath::clean('index.html')
  170. ),
  171. JFolder::files(JPath::clean(JPATH_TESTS . '/tmp/test'), 'index.html', false, false),
  172. 'Line: ' . __LINE__ . ' non-recursive should return only file name of top folder file'
  173. );
  174. $this->assertFalse(
  175. JFolder::files('/this/is/not/a/path'),
  176. 'Line: ' . __LINE__ . ' Non-existent path should return false'
  177. );
  178. $this->assertEquals(
  179. array(),
  180. JFolder::files(JPath::clean(JPATH_TESTS . '/tmp/test'), 'nothing.here', true, true, array(), array()),
  181. 'Line: ' . __LINE__ . ' When nothing matches the filter, should return empty array'
  182. );
  183. // Clean up our files
  184. $this->_cleanupTestFiles();
  185. }
  186. /**
  187. * Tests the JFolder::folders method.
  188. *
  189. * @return void
  190. *
  191. * @since 12.1
  192. *
  193. * @covers JFolder::files
  194. * @covers JFolder::_items
  195. */
  196. public function testFolders()
  197. {
  198. // Make sure previous test files are cleaned up
  199. $this->_cleanupTestFiles();
  200. // Create the test folders
  201. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test'), 0777, true);
  202. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo1'), 0777, true);
  203. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar1'), 0777, true);
  204. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar2'), 0777, true);
  205. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo2'), 0777, true);
  206. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar1'), 0777, true);
  207. mkdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar2'), 0777, true);
  208. $this->assertEquals(
  209. array(),
  210. JFolder::folders(JPath::clean(JPATH_TESTS . '/tmp/test'), 'bar1', true, true, array('foo1', 'foo2'))
  211. );
  212. // Use of realpath to ensure test works for on all platforms
  213. $result = JFolder::folders(JPath::clean(JPATH_TESTS . '/tmp/test'), 'bar1', true, true, array('foo1'));
  214. $result[0] = realpath($result[0]);
  215. $this->assertEquals(
  216. array(JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar1')),
  217. $result
  218. );
  219. // Use of realpath to ensure test works for on all platforms
  220. $result = JFolder::folders(JPath::clean(JPATH_TESTS . '/tmp/test'), 'bar1', true, true);
  221. $result[0] = realpath($result[0]);
  222. $result[1] = realpath($result[1]);
  223. $this->assertEquals(
  224. array(
  225. JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar1'),
  226. JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar1'),
  227. ),
  228. $result
  229. );
  230. // Use of realpath to ensure test works for on all platforms
  231. $result = JFolder::folders(JPath::clean(JPATH_TESTS . '/tmp/test'), 'bar', true, true);
  232. $result[0] = realpath($result[0]);
  233. $result[1] = realpath($result[1]);
  234. $result[2] = realpath($result[2]);
  235. $result[3] = realpath($result[3]);
  236. $this->assertEquals(
  237. array(
  238. JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar1'),
  239. JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar2'),
  240. JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar1'),
  241. JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar2'),
  242. ),
  243. $result
  244. );
  245. // Use of realpath to ensure test works for on all platforms
  246. $result = JFolder::folders(JPath::clean(JPATH_TESTS . '/tmp/test'), '.', true, true);
  247. $result[0] = realpath($result[0]);
  248. $result[1] = realpath($result[1]);
  249. $result[2] = realpath($result[2]);
  250. $result[3] = realpath($result[3]);
  251. $result[4] = realpath($result[4]);
  252. $result[5] = realpath($result[5]);
  253. $this->assertEquals(
  254. array(
  255. JPath::clean(JPATH_TESTS . '/tmp/test/foo1'),
  256. JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar1'),
  257. JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar2'),
  258. JPath::clean(JPATH_TESTS . '/tmp/test/foo2'),
  259. JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar1'),
  260. JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar2'),
  261. ),
  262. $result
  263. );
  264. $this->assertEquals(
  265. array(
  266. JPath::clean('bar1'),
  267. JPath::clean('bar1'),
  268. JPath::clean('bar2'),
  269. JPath::clean('bar2'),
  270. JPath::clean('foo1'),
  271. JPath::clean('foo2'),
  272. ),
  273. JFolder::folders(JPath::clean(JPATH_TESTS . '/tmp/test'), '.', true, false)
  274. );
  275. // Use of realpath to ensure test works for on all platforms
  276. $result = JFolder::folders(JPath::clean(JPATH_TESTS . '/tmp/test'), '.', false, true);
  277. $result[0] = realpath($result[0]);
  278. $result[1] = realpath($result[1]);
  279. $this->assertEquals(
  280. array(
  281. JPath::clean(JPATH_TESTS . '/tmp/test/foo1'),
  282. JPath::clean(JPATH_TESTS . '/tmp/test/foo2'),
  283. ),
  284. $result
  285. );
  286. $this->assertEquals(
  287. array(
  288. JPath::clean('foo1'),
  289. JPath::clean('foo2'),
  290. ),
  291. JFolder::folders(JPath::clean(JPATH_TESTS . '/tmp/test'), '.', false, false, array(), array())
  292. );
  293. $this->assertFalse(
  294. JFolder::folders('this/is/not/a/path'),
  295. 'Line: ' . __LINE__ . ' Non-existent path should return false'
  296. );
  297. // Clean up the folders
  298. rmdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar2'));
  299. rmdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo2/bar1'));
  300. rmdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo2'));
  301. rmdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar2'));
  302. rmdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo1/bar1'));
  303. rmdir(JPath::clean(JPATH_TESTS . '/tmp/test/foo1'));
  304. rmdir(JPath::clean(JPATH_TESTS . '/tmp/test'));
  305. }
  306. /**
  307. * Test...
  308. *
  309. * @todo Implement testListFolderTree().
  310. *
  311. * @return void
  312. */
  313. public function testListFolderTree()
  314. {
  315. // Remove the following lines when you implement this test.
  316. $this->markTestIncomplete(
  317. 'This test has not been implemented yet.'
  318. );
  319. }
  320. /**
  321. * Tests the JFolder::makeSafe method.
  322. *
  323. * @return void
  324. *
  325. * @since 12.1
  326. *
  327. * @covers JFolder::makeSafe
  328. */
  329. public function testMakeSafe()
  330. {
  331. $actual = JFolder::makeSafe('test1/testdirectory');
  332. $this->assertEquals('test1/testdirectory', $actual);
  333. }
  334. /**
  335. * Convenience method to cleanup before and after testFiles
  336. *
  337. * @return void
  338. *
  339. * @since 12.1
  340. */
  341. private function _cleanupTestFiles()
  342. {
  343. $this->_cleanupFile(JPath::clean(JPATH_TESTS . '/tmp/test/test/index.html'));
  344. $this->_cleanupFile(JPath::clean(JPATH_TESTS . '/tmp/test/test/index.txt'));
  345. $this->_cleanupFile(JPath::clean(JPATH_TESTS . '/tmp/test/test'));
  346. $this->_cleanupFile(JPath::clean(JPATH_TESTS . '/tmp/test/index.html'));
  347. $this->_cleanupFile(JPath::clean(JPATH_TESTS . '/tmp/test/index.txt'));
  348. $this->_cleanupFile(JPath::clean(JPATH_TESTS . '/tmp/test'));
  349. }
  350. /**
  351. * Convenience method to clean up for files test
  352. *
  353. * @param string $path The path to clean
  354. *
  355. * @return void
  356. *
  357. * @since 12.1
  358. */
  359. private function _cleanupFile($path)
  360. {
  361. if (file_exists($path))
  362. {
  363. if (is_file($path))
  364. {
  365. unlink($path);
  366. }
  367. elseif (is_dir($path))
  368. {
  369. rmdir($path);
  370. }
  371. }
  372. }
  373. }