FileTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <?php
  2. /**
  3. * FileTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Utility
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('File', 'Utility');
  20. App::uses('Folder', 'Utility');
  21. /**
  22. * FileTest class
  23. *
  24. * @package Cake.Test.Case.Utility
  25. */
  26. class FileTest extends CakeTestCase {
  27. /**
  28. * File property
  29. *
  30. * @var mixed null
  31. */
  32. public $File = null;
  33. /**
  34. * setup the test case
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. $file = __FILE__;
  41. $this->File = new File($file);
  42. }
  43. /**
  44. * tearDown method
  45. *
  46. * @return void
  47. */
  48. public function tearDown() {
  49. parent::tearDown();
  50. $this->File->close();
  51. unset($this->File);
  52. }
  53. /**
  54. * testBasic method
  55. *
  56. * @return void
  57. */
  58. public function testBasic() {
  59. $file = __FILE__;
  60. $result = $this->File->pwd();
  61. $expecting = $file;
  62. $this->assertEquals($expecting, $result);
  63. $result = $this->File->name;
  64. $expecting = basename(__FILE__);
  65. $this->assertEquals($expecting, $result);
  66. $result = $this->File->info();
  67. $expecting = array(
  68. 'dirname' => dirname(__FILE__),
  69. 'basename' => basename(__FILE__),
  70. 'extension' => 'php',
  71. 'filename' => 'FileTest',
  72. 'filesize' => filesize($file),
  73. 'mime' => 'text/x-php'
  74. );
  75. if (!function_exists('finfo_open') && (!function_exists('mime_content_type') ||
  76. function_exists('mime_content_type') && false === mime_content_type($this->File->pwd()))) {
  77. $expecting['mime'] = false;
  78. }
  79. $this->assertEquals($expecting, $result);
  80. $result = $this->File->ext();
  81. $expecting = 'php';
  82. $this->assertEquals($expecting, $result);
  83. $result = $this->File->name();
  84. $expecting = 'FileTest';
  85. $this->assertEquals($expecting, $result);
  86. $result = $this->File->md5();
  87. $expecting = md5_file($file);
  88. $this->assertEquals($expecting, $result);
  89. $result = $this->File->md5(true);
  90. $expecting = md5_file($file);
  91. $this->assertEquals($expecting, $result);
  92. $result = $this->File->size();
  93. $expecting = filesize($file);
  94. $this->assertEquals($expecting, $result);
  95. $result = $this->File->owner();
  96. $expecting = fileowner($file);
  97. $this->assertEquals($expecting, $result);
  98. $result = $this->File->group();
  99. $expecting = filegroup($file);
  100. $this->assertEquals($expecting, $result);
  101. $result = $this->File->Folder();
  102. $this->assertInstanceOf('Folder', $result);
  103. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
  104. $result = $this->File->perms();
  105. $expecting = decoct(0644 & ~umask());
  106. $this->assertEquals($expecting, $result);
  107. }
  108. /**
  109. * testRead method
  110. *
  111. * @return void
  112. */
  113. public function testRead() {
  114. $file = __FILE__;
  115. $this->File = new File($file);
  116. $result = $this->File->read();
  117. $expecting = file_get_contents(__FILE__);
  118. $this->assertEquals($expecting, $result);
  119. $this->assertTrue(!is_resource($this->File->handle));
  120. $this->File->lock = true;
  121. $result = $this->File->read();
  122. $expecting = file_get_contents(__FILE__);
  123. $this->assertEquals(trim($expecting), $result);
  124. $this->File->lock = null;
  125. $data = $expecting;
  126. $expecting = substr($data, 0, 3);
  127. $result = $this->File->read(3);
  128. $this->assertEquals($expecting, $result);
  129. $this->assertTrue(is_resource($this->File->handle));
  130. $expecting = substr($data, 3, 3);
  131. $result = $this->File->read(3);
  132. $this->assertEquals($expecting, $result);
  133. }
  134. /**
  135. * testOffset method
  136. *
  137. * @return void
  138. */
  139. public function testOffset() {
  140. $this->File->close();
  141. $result = $this->File->offset();
  142. $this->assertFalse($result);
  143. $this->assertFalse(is_resource($this->File->handle));
  144. $success = $this->File->offset(0);
  145. $this->assertTrue($success);
  146. $this->assertTrue(is_resource($this->File->handle));
  147. $result = $this->File->offset();
  148. $expecting = 0;
  149. $this->assertSame($result, $expecting);
  150. $data = file_get_contents(__FILE__);
  151. $success = $this->File->offset(5);
  152. $expecting = substr($data, 5, 3);
  153. $result = $this->File->read(3);
  154. $this->assertTrue($success);
  155. $this->assertEquals($expecting, $result);
  156. $result = $this->File->offset();
  157. $expecting = 5 + 3;
  158. $this->assertSame($result, $expecting);
  159. }
  160. /**
  161. * testOpen method
  162. *
  163. * @return void
  164. */
  165. public function testOpen() {
  166. $this->File->handle = null;
  167. $r = $this->File->open();
  168. $this->assertTrue(is_resource($this->File->handle));
  169. $this->assertTrue($r);
  170. $handle = $this->File->handle;
  171. $r = $this->File->open();
  172. $this->assertTrue($r);
  173. $this->assertTrue($handle === $this->File->handle);
  174. $this->assertTrue(is_resource($this->File->handle));
  175. $r = $this->File->open('r', true);
  176. $this->assertTrue($r);
  177. $this->assertFalse($handle === $this->File->handle);
  178. $this->assertTrue(is_resource($this->File->handle));
  179. }
  180. /**
  181. * testClose method
  182. *
  183. * @return void
  184. */
  185. public function testClose() {
  186. $this->File->handle = null;
  187. $this->assertFalse(is_resource($this->File->handle));
  188. $this->assertTrue($this->File->close());
  189. $this->assertFalse(is_resource($this->File->handle));
  190. $this->File->handle = fopen(__FILE__, 'r');
  191. $this->assertTrue(is_resource($this->File->handle));
  192. $this->assertTrue($this->File->close());
  193. $this->assertFalse(is_resource($this->File->handle));
  194. }
  195. /**
  196. * testCreate method
  197. *
  198. * @return void
  199. */
  200. public function testCreate() {
  201. $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  202. $File = new File($tmpFile, true, 0777);
  203. $this->assertTrue($File->exists());
  204. }
  205. /**
  206. * testOpeningNonExistentFileCreatesIt method
  207. *
  208. * @return void
  209. */
  210. public function testOpeningNonExistentFileCreatesIt() {
  211. $someFile = new File(TMP . 'some_file.txt', false);
  212. $this->assertTrue($someFile->open());
  213. $this->assertEquals('', $someFile->read());
  214. $someFile->close();
  215. $someFile->delete();
  216. }
  217. /**
  218. * testPrepare method
  219. *
  220. * @return void
  221. */
  222. public function testPrepare() {
  223. $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
  224. if (DS == '\\') {
  225. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  226. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  227. } else {
  228. $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
  229. }
  230. $this->assertSame(File::prepare($string), $expected);
  231. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  232. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  233. $this->assertSame(File::prepare($string, true), $expected);
  234. }
  235. /**
  236. * testReadable method
  237. *
  238. * @return void
  239. */
  240. public function testReadable() {
  241. $someFile = new File(TMP . 'some_file.txt', false);
  242. $this->assertTrue($someFile->open());
  243. $this->assertTrue($someFile->readable());
  244. $someFile->close();
  245. $someFile->delete();
  246. }
  247. /**
  248. * testWritable method
  249. *
  250. * @return void
  251. */
  252. public function testWritable() {
  253. $someFile = new File(TMP . 'some_file.txt', false);
  254. $this->assertTrue($someFile->open());
  255. $this->assertTrue($someFile->writable());
  256. $someFile->close();
  257. $someFile->delete();
  258. }
  259. /**
  260. * testExecutable method
  261. *
  262. * @return void
  263. */
  264. public function testExecutable() {
  265. $someFile = new File(TMP . 'some_file.txt', false);
  266. $this->assertTrue($someFile->open());
  267. $this->assertFalse($someFile->executable());
  268. $someFile->close();
  269. $someFile->delete();
  270. }
  271. /**
  272. * testLastAccess method
  273. *
  274. * @return void
  275. */
  276. public function testLastAccess() {
  277. $ts = time();
  278. $someFile = new File(TMP . 'some_file.txt', false);
  279. $this->assertFalse($someFile->lastAccess());
  280. $this->assertTrue($someFile->open());
  281. $this->assertTrue($someFile->lastAccess() >= $ts);
  282. $someFile->close();
  283. $someFile->delete();
  284. }
  285. /**
  286. * testLastChange method
  287. *
  288. * @return void
  289. */
  290. public function testLastChange() {
  291. $ts = time();
  292. $someFile = new File(TMP . 'some_file.txt', false);
  293. $this->assertFalse($someFile->lastChange());
  294. $this->assertTrue($someFile->open('r+'));
  295. $this->assertTrue($someFile->lastChange() >= $ts);
  296. $someFile->write('something');
  297. $this->assertTrue($someFile->lastChange() >= $ts);
  298. $someFile->close();
  299. $someFile->delete();
  300. }
  301. /**
  302. * testWrite method
  303. *
  304. * @return void
  305. */
  306. public function testWrite() {
  307. if (!$tmpFile = $this->_getTmpFile()) {
  308. return false;
  309. };
  310. if (file_exists($tmpFile)) {
  311. unlink($tmpFile);
  312. }
  313. $TmpFile = new File($tmpFile);
  314. $this->assertFalse(file_exists($tmpFile));
  315. $this->assertFalse(is_resource($TmpFile->handle));
  316. $testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
  317. foreach ($testData as $data) {
  318. $r = $TmpFile->write($data);
  319. $this->assertTrue($r);
  320. $this->assertTrue(file_exists($tmpFile));
  321. $this->assertEquals($data, file_get_contents($tmpFile));
  322. $this->assertTrue(is_resource($TmpFile->handle));
  323. $TmpFile->close();
  324. }
  325. unlink($tmpFile);
  326. }
  327. /**
  328. * testAppend method
  329. *
  330. * @return void
  331. */
  332. public function testAppend() {
  333. if (!$tmpFile = $this->_getTmpFile()) {
  334. return false;
  335. };
  336. if (file_exists($tmpFile)) {
  337. unlink($tmpFile);
  338. }
  339. $TmpFile = new File($tmpFile);
  340. $this->assertFalse(file_exists($tmpFile));
  341. $fragments = array('CakePHP\'s', ' test suite', ' was here ...', '');
  342. $data = null;
  343. foreach ($fragments as $fragment) {
  344. $r = $TmpFile->append($fragment);
  345. $this->assertTrue($r);
  346. $this->assertTrue(file_exists($tmpFile));
  347. $data = $data . $fragment;
  348. $this->assertEquals($data, file_get_contents($tmpFile));
  349. $TmpFile->close();
  350. }
  351. }
  352. /**
  353. * testDelete method
  354. *
  355. * @return void
  356. */
  357. public function testDelete() {
  358. if (!$tmpFile = $this->_getTmpFile()) {
  359. return false;
  360. }
  361. if (!file_exists($tmpFile)) {
  362. touch($tmpFile);
  363. }
  364. $TmpFile = new File($tmpFile);
  365. $this->assertTrue(file_exists($tmpFile));
  366. $result = $TmpFile->delete();
  367. $this->assertTrue($result);
  368. $this->assertFalse(file_exists($tmpFile));
  369. $TmpFile = new File('/this/does/not/exist');
  370. $result = $TmpFile->delete();
  371. $this->assertFalse($result);
  372. }
  373. /**
  374. * Windows has issues unlinking files if there are
  375. * active filehandles open.
  376. *
  377. * @return void
  378. */
  379. public function testDeleteAfterRead() {
  380. if (!$tmpFile = $this->_getTmpFile()) {
  381. return false;
  382. }
  383. if (!file_exists($tmpFile)) {
  384. touch($tmpFile);
  385. }
  386. $File = new File($tmpFile);
  387. $File->read();
  388. $this->assertTrue($File->delete());
  389. }
  390. /**
  391. * testCopy method
  392. *
  393. * @return void
  394. */
  395. public function testCopy() {
  396. $dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  397. $file = __FILE__;
  398. $this->File = new File($file);
  399. $result = $this->File->copy($dest);
  400. $this->assertTrue($result);
  401. $result = $this->File->copy($dest, true);
  402. $this->assertTrue($result);
  403. $result = $this->File->copy($dest, false);
  404. $this->assertFalse($result);
  405. $this->File->close();
  406. unlink($dest);
  407. $TmpFile = new File('/this/does/not/exist');
  408. $result = $TmpFile->copy($dest);
  409. $this->assertFalse($result);
  410. $TmpFile->close();
  411. }
  412. /**
  413. * Test mime()
  414. *
  415. * @return void
  416. */
  417. public function testMime() {
  418. $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
  419. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
  420. $file = new File($path);
  421. $expected = 'image/gif';
  422. if (function_exists('mime_content_type') && false === mime_content_type($file->pwd())) {
  423. $expected = false;
  424. }
  425. $this->assertEquals($expected, $file->mime());
  426. }
  427. /**
  428. * getTmpFile method
  429. *
  430. * @param bool $paintSkip
  431. * @return void
  432. */
  433. protected function _getTmpFile($paintSkip = true) {
  434. $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  435. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  436. return $tmpFile;
  437. };
  438. if ($paintSkip) {
  439. $trace = debug_backtrace();
  440. $caller = $trace[0]['function'];
  441. $shortPath = dirname($tmpFile);
  442. $message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
  443. $this->markTestSkipped($message);
  444. }
  445. return false;
  446. }
  447. }