FileTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. /**
  3. * FileTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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/2.0/en/development/testing.html 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. $Folder = new Folder();
  53. $Folder->delete(TMP . 'tests' . DS . 'permissions');
  54. }
  55. /**
  56. * testBasic method
  57. *
  58. * @return void
  59. */
  60. public function testBasic() {
  61. $file = CAKE . DS . 'LICENSE.txt';
  62. $this->File = new File($file, false);
  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' => 'txt',
  71. 'filename' => 'LICENSE',
  72. 'filesize' => filesize($file),
  73. 'mime' => 'text/plain'
  74. );
  75. if (
  76. !function_exists('finfo_open') &&
  77. (!function_exists('mime_content_type') ||
  78. function_exists('mime_content_type') &&
  79. mime_content_type($this->File->pwd()) === false)
  80. ) {
  81. $expecting['mime'] = false;
  82. }
  83. $this->assertEquals($expecting, $result);
  84. $result = $this->File->ext();
  85. $expecting = 'txt';
  86. $this->assertEquals($expecting, $result);
  87. $result = $this->File->name();
  88. $expecting = 'LICENSE';
  89. $this->assertEquals($expecting, $result);
  90. $result = $this->File->md5();
  91. $expecting = md5_file($file);
  92. $this->assertEquals($expecting, $result);
  93. $result = $this->File->md5(true);
  94. $expecting = md5_file($file);
  95. $this->assertEquals($expecting, $result);
  96. $result = $this->File->size();
  97. $expecting = filesize($file);
  98. $this->assertEquals($expecting, $result);
  99. $result = $this->File->owner();
  100. $expecting = fileowner($file);
  101. $this->assertEquals($expecting, $result);
  102. $result = $this->File->group();
  103. $expecting = filegroup($file);
  104. $this->assertEquals($expecting, $result);
  105. $result = $this->File->Folder();
  106. $this->assertInstanceOf('Folder', $result);
  107. }
  108. /**
  109. * testPermission method
  110. */
  111. public function testPermission() {
  112. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
  113. $dir = TMP . 'tests' . DS . 'permissions' . DS;
  114. $Folder = new Folder($dir);
  115. $old = umask();
  116. umask(0002);
  117. $file = $dir . 'permission_' . uniqid();
  118. $expecting = decoct(0664 & ~umask());
  119. $File = new File($file, true);
  120. $result = $File->perms();
  121. $this->assertEquals($expecting, $result);
  122. $File->delete();
  123. umask(0022);
  124. $file = $dir . 'permission_' . uniqid();
  125. $expecting = decoct(0644 & ~umask());
  126. $File = new File($file, true);
  127. $result = $File->perms();
  128. $this->assertEquals($expecting, $result);
  129. $File->delete();
  130. umask(0422);
  131. $file = $dir . 'permission_' . uniqid();
  132. $expecting = decoct(0244 & ~umask());
  133. $File = new File($file, true);
  134. $result = $File->perms();
  135. $this->assertEquals($expecting, $result);
  136. $File->delete();
  137. umask(0444);
  138. $file = $dir . 'permission_' . uniqid();
  139. $expecting = decoct(0222 & ~umask());
  140. $File = new File($file, true);
  141. $result = $File->perms();
  142. $this->assertEquals($expecting, $result);
  143. $File->delete();
  144. umask($old);
  145. }
  146. /**
  147. * testRead method
  148. *
  149. * @return void
  150. */
  151. public function testRead() {
  152. $file = __FILE__;
  153. $this->File = new File($file);
  154. $result = $this->File->read();
  155. $expecting = file_get_contents(__FILE__);
  156. $this->assertEquals($expecting, $result);
  157. $this->assertTrue(!is_resource($this->File->handle));
  158. $this->File->lock = true;
  159. $result = $this->File->read();
  160. $expecting = file_get_contents(__FILE__);
  161. $this->assertEquals(trim($expecting), $result);
  162. $this->File->lock = null;
  163. $data = $expecting;
  164. $expecting = substr($data, 0, 3);
  165. $result = $this->File->read(3);
  166. $this->assertEquals($expecting, $result);
  167. $this->assertTrue(is_resource($this->File->handle));
  168. $expecting = substr($data, 3, 3);
  169. $result = $this->File->read(3);
  170. $this->assertEquals($expecting, $result);
  171. }
  172. /**
  173. * testOffset method
  174. *
  175. * @return void
  176. */
  177. public function testOffset() {
  178. $this->File->close();
  179. $result = $this->File->offset();
  180. $this->assertFalse($result);
  181. $this->assertFalse(is_resource($this->File->handle));
  182. $success = $this->File->offset(0);
  183. $this->assertTrue($success);
  184. $this->assertTrue(is_resource($this->File->handle));
  185. $result = $this->File->offset();
  186. $expecting = 0;
  187. $this->assertSame($result, $expecting);
  188. $data = file_get_contents(__FILE__);
  189. $success = $this->File->offset(5);
  190. $expecting = substr($data, 5, 3);
  191. $result = $this->File->read(3);
  192. $this->assertTrue($success);
  193. $this->assertEquals($expecting, $result);
  194. $result = $this->File->offset();
  195. $expecting = 5 + 3;
  196. $this->assertSame($result, $expecting);
  197. }
  198. /**
  199. * testOpen method
  200. *
  201. * @return void
  202. */
  203. public function testOpen() {
  204. $this->File->handle = null;
  205. $r = $this->File->open();
  206. $this->assertTrue(is_resource($this->File->handle));
  207. $this->assertTrue($r);
  208. $handle = $this->File->handle;
  209. $r = $this->File->open();
  210. $this->assertTrue($r);
  211. $this->assertTrue($handle === $this->File->handle);
  212. $this->assertTrue(is_resource($this->File->handle));
  213. $r = $this->File->open('r', true);
  214. $this->assertTrue($r);
  215. $this->assertFalse($handle === $this->File->handle);
  216. $this->assertTrue(is_resource($this->File->handle));
  217. }
  218. /**
  219. * testClose method
  220. *
  221. * @return void
  222. */
  223. public function testClose() {
  224. $this->File->handle = null;
  225. $this->assertFalse(is_resource($this->File->handle));
  226. $this->assertTrue($this->File->close());
  227. $this->assertFalse(is_resource($this->File->handle));
  228. $this->File->handle = fopen(__FILE__, 'r');
  229. $this->assertTrue(is_resource($this->File->handle));
  230. $this->assertTrue($this->File->close());
  231. $this->assertFalse(is_resource($this->File->handle));
  232. }
  233. /**
  234. * testCreate method
  235. *
  236. * @return void
  237. */
  238. public function testCreate() {
  239. $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  240. $File = new File($tmpFile, true, 0777);
  241. $this->assertTrue($File->exists());
  242. }
  243. /**
  244. * testOpeningNonExistentFileCreatesIt method
  245. *
  246. * @return void
  247. */
  248. public function testOpeningNonExistentFileCreatesIt() {
  249. $someFile = new File(TMP . 'some_file.txt', false);
  250. $this->assertTrue($someFile->open());
  251. $this->assertEquals('', $someFile->read());
  252. $someFile->close();
  253. $someFile->delete();
  254. }
  255. /**
  256. * testPrepare method
  257. *
  258. * @return void
  259. */
  260. public function testPrepare() {
  261. $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
  262. if (DS == '\\') {
  263. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  264. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  265. } else {
  266. $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
  267. }
  268. $this->assertSame(File::prepare($string), $expected);
  269. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  270. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  271. $this->assertSame(File::prepare($string, true), $expected);
  272. }
  273. /**
  274. * testReadable method
  275. *
  276. * @return void
  277. */
  278. public function testReadable() {
  279. $someFile = new File(TMP . 'some_file.txt', false);
  280. $this->assertTrue($someFile->open());
  281. $this->assertTrue($someFile->readable());
  282. $someFile->close();
  283. $someFile->delete();
  284. }
  285. /**
  286. * testWritable method
  287. *
  288. * @return void
  289. */
  290. public function testWritable() {
  291. $someFile = new File(TMP . 'some_file.txt', false);
  292. $this->assertTrue($someFile->open());
  293. $this->assertTrue($someFile->writable());
  294. $someFile->close();
  295. $someFile->delete();
  296. }
  297. /**
  298. * testExecutable method
  299. *
  300. * @return void
  301. */
  302. public function testExecutable() {
  303. $someFile = new File(TMP . 'some_file.txt', false);
  304. $this->assertTrue($someFile->open());
  305. $this->assertFalse($someFile->executable());
  306. $someFile->close();
  307. $someFile->delete();
  308. }
  309. /**
  310. * testLastAccess method
  311. *
  312. * @return void
  313. */
  314. public function testLastAccess() {
  315. $ts = time();
  316. $someFile = new File(TMP . 'some_file.txt', false);
  317. $this->assertFalse($someFile->lastAccess());
  318. $this->assertTrue($someFile->open());
  319. $this->assertTrue($someFile->lastAccess() >= $ts);
  320. $someFile->close();
  321. $someFile->delete();
  322. }
  323. /**
  324. * testLastChange method
  325. *
  326. * @return void
  327. */
  328. public function testLastChange() {
  329. $ts = time();
  330. $someFile = new File(TMP . 'some_file.txt', false);
  331. $this->assertFalse($someFile->lastChange());
  332. $this->assertTrue($someFile->open('r+'));
  333. $this->assertTrue($someFile->lastChange() >= $ts);
  334. $someFile->write('something');
  335. $this->assertTrue($someFile->lastChange() >= $ts);
  336. $someFile->close();
  337. $someFile->delete();
  338. }
  339. /**
  340. * testWrite method
  341. *
  342. * @return void
  343. */
  344. public function testWrite() {
  345. if (!$tmpFile = $this->_getTmpFile()) {
  346. return false;
  347. };
  348. if (file_exists($tmpFile)) {
  349. unlink($tmpFile);
  350. }
  351. $TmpFile = new File($tmpFile);
  352. $this->assertFalse(file_exists($tmpFile));
  353. $this->assertFalse(is_resource($TmpFile->handle));
  354. $testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
  355. foreach ($testData as $data) {
  356. $r = $TmpFile->write($data);
  357. $this->assertTrue($r);
  358. $this->assertTrue(file_exists($tmpFile));
  359. $this->assertEquals($data, file_get_contents($tmpFile));
  360. $this->assertTrue(is_resource($TmpFile->handle));
  361. $TmpFile->close();
  362. }
  363. unlink($tmpFile);
  364. }
  365. /**
  366. * testAppend method
  367. *
  368. * @return void
  369. */
  370. public function testAppend() {
  371. if (!$tmpFile = $this->_getTmpFile()) {
  372. return false;
  373. };
  374. if (file_exists($tmpFile)) {
  375. unlink($tmpFile);
  376. }
  377. $TmpFile = new File($tmpFile);
  378. $this->assertFalse(file_exists($tmpFile));
  379. $fragments = array('CakePHP\'s', ' test suite', ' was here ...', '');
  380. $data = null;
  381. foreach ($fragments as $fragment) {
  382. $r = $TmpFile->append($fragment);
  383. $this->assertTrue($r);
  384. $this->assertTrue(file_exists($tmpFile));
  385. $data = $data . $fragment;
  386. $this->assertEquals($data, file_get_contents($tmpFile));
  387. $TmpFile->close();
  388. }
  389. }
  390. /**
  391. * testDelete method
  392. *
  393. * @return void
  394. */
  395. public function testDelete() {
  396. if (!$tmpFile = $this->_getTmpFile()) {
  397. return false;
  398. }
  399. if (!file_exists($tmpFile)) {
  400. touch($tmpFile);
  401. }
  402. $TmpFile = new File($tmpFile);
  403. $this->assertTrue(file_exists($tmpFile));
  404. $result = $TmpFile->delete();
  405. $this->assertTrue($result);
  406. $this->assertFalse(file_exists($tmpFile));
  407. $TmpFile = new File('/this/does/not/exist');
  408. $result = $TmpFile->delete();
  409. $this->assertFalse($result);
  410. }
  411. /**
  412. * Windows has issues unlinking files if there are
  413. * active filehandles open.
  414. *
  415. * @return void
  416. */
  417. public function testDeleteAfterRead() {
  418. if (!$tmpFile = $this->_getTmpFile()) {
  419. return false;
  420. }
  421. if (!file_exists($tmpFile)) {
  422. touch($tmpFile);
  423. }
  424. $File = new File($tmpFile);
  425. $File->read();
  426. $this->assertTrue($File->delete());
  427. }
  428. /**
  429. * testCopy method
  430. *
  431. * @return void
  432. */
  433. public function testCopy() {
  434. $dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  435. $file = __FILE__;
  436. $this->File = new File($file);
  437. $result = $this->File->copy($dest);
  438. $this->assertTrue($result);
  439. $result = $this->File->copy($dest, true);
  440. $this->assertTrue($result);
  441. $result = $this->File->copy($dest, false);
  442. $this->assertFalse($result);
  443. $this->File->close();
  444. unlink($dest);
  445. $TmpFile = new File('/this/does/not/exist');
  446. $result = $TmpFile->copy($dest);
  447. $this->assertFalse($result);
  448. $TmpFile->close();
  449. }
  450. /**
  451. * Test mime()
  452. *
  453. * @return void
  454. */
  455. public function testMime() {
  456. $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
  457. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
  458. $file = new File($path);
  459. $expected = 'image/gif';
  460. if (function_exists('mime_content_type') && false === mime_content_type($file->pwd())) {
  461. $expected = false;
  462. }
  463. $this->assertEquals($expected, $file->mime());
  464. }
  465. /**
  466. * getTmpFile method
  467. *
  468. * @param bool $paintSkip
  469. * @return void
  470. */
  471. protected function _getTmpFile($paintSkip = true) {
  472. $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  473. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  474. return $tmpFile;
  475. };
  476. if ($paintSkip) {
  477. $trace = debug_backtrace();
  478. $caller = $trace[0]['function'];
  479. $shortPath = dirname($tmpFile);
  480. $message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
  481. $this->markTestSkipped($message);
  482. }
  483. return false;
  484. }
  485. }