FileTest.php 13 KB

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