FileTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Filesystem;
  16. use Cake\Filesystem\File;
  17. use Cake\Filesystem\Folder;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * FileTest class
  21. *
  22. */
  23. class FileTest extends TestCase
  24. {
  25. /**
  26. * File property
  27. *
  28. * @var mixed
  29. */
  30. public $File = null;
  31. /**
  32. * setup the test case
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. parent::setUp();
  39. $file = __FILE__;
  40. $this->File = new File($file);
  41. }
  42. /**
  43. * tearDown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown()
  48. {
  49. parent::tearDown();
  50. $this->File->close();
  51. unset($this->File);
  52. $Folder = new Folder();
  53. $Folder->delete(TMP . 'tests/permissions');
  54. }
  55. /**
  56. * testBasic method
  57. *
  58. * @return void
  59. */
  60. public function testBasic()
  61. {
  62. $file = CORE_PATH . DS . 'LICENSE.txt';
  63. $this->File = new File($file, false);
  64. $result = $this->File->name;
  65. $expecting = basename($file);
  66. $this->assertEquals($expecting, $result);
  67. $result = $this->File->info();
  68. $expecting = [
  69. 'dirname' => dirname($file),
  70. 'basename' => basename($file),
  71. 'extension' => 'txt',
  72. 'filename' => 'LICENSE',
  73. 'filesize' => filesize($file),
  74. 'mime' => 'text/plain'
  75. ];
  76. if (!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('Cake\Filesystem\Folder', $result);
  107. }
  108. /**
  109. * testPermission method
  110. *
  111. * @return void
  112. */
  113. public function testPermission()
  114. {
  115. $this->skipIf(DS === '\\', 'File permissions tests not supported on Windows.');
  116. $dir = TMP . 'tests' . DS . 'permissions' . DS;
  117. $old = umask();
  118. umask(0002);
  119. $file = $dir . 'permission_' . uniqid();
  120. $expecting = decoct(0664 & ~umask());
  121. $File = new File($file, true);
  122. $result = $File->perms();
  123. $this->assertEquals($expecting, $result);
  124. $File->delete();
  125. umask(0022);
  126. $file = $dir . 'permission_' . uniqid();
  127. $expecting = decoct(0644 & ~umask());
  128. $File = new File($file, true);
  129. $result = $File->perms();
  130. $this->assertEquals($expecting, $result);
  131. $File->delete();
  132. umask(0422);
  133. $file = $dir . 'permission_' . uniqid();
  134. $expecting = decoct(0244 & ~umask());
  135. $File = new File($file, true);
  136. $result = $File->perms();
  137. $this->assertEquals($expecting, $result);
  138. $File->delete();
  139. umask(0444);
  140. $file = $dir . 'permission_' . uniqid();
  141. $expecting = decoct(0222 & ~umask());
  142. $File = new File($file, true);
  143. $result = $File->perms();
  144. $this->assertEquals($expecting, $result);
  145. $File->delete();
  146. umask($old);
  147. }
  148. /**
  149. * testRead method
  150. *
  151. * @return void
  152. */
  153. public function testRead()
  154. {
  155. $file = __FILE__;
  156. $this->File = new File($file);
  157. $result = $this->File->read();
  158. $expecting = file_get_contents(__FILE__);
  159. $this->assertEquals($expecting, $result);
  160. $this->assertTrue(!is_resource($this->File->handle));
  161. $this->File->lock = true;
  162. $result = $this->File->read();
  163. $expecting = file_get_contents(__FILE__);
  164. $this->assertEquals(trim($expecting), $result);
  165. $this->File->lock = null;
  166. $data = $expecting;
  167. $expecting = substr($data, 0, 3);
  168. $result = $this->File->read(3);
  169. $this->assertEquals($expecting, $result);
  170. $this->assertTrue(is_resource($this->File->handle));
  171. $expecting = substr($data, 3, 3);
  172. $result = $this->File->read(3);
  173. $this->assertEquals($expecting, $result);
  174. }
  175. /**
  176. * testOffset method
  177. *
  178. * @return void
  179. */
  180. public function testOffset()
  181. {
  182. $this->File->close();
  183. $result = $this->File->offset();
  184. $this->assertFalse($result);
  185. $this->assertFalse(is_resource($this->File->handle));
  186. $success = $this->File->offset(0);
  187. $this->assertTrue($success);
  188. $this->assertTrue(is_resource($this->File->handle));
  189. $result = $this->File->offset();
  190. $expected = 0;
  191. $this->assertSame($expected, $result);
  192. $data = file_get_contents(__FILE__);
  193. $success = $this->File->offset(5);
  194. $expected = substr($data, 5, 3);
  195. $result = $this->File->read(3);
  196. $this->assertTrue($success);
  197. $this->assertEquals($expected, $result);
  198. $result = $this->File->offset();
  199. $expected = 5 + 3;
  200. $this->assertSame($expected, $result);
  201. }
  202. /**
  203. * testOpen method
  204. *
  205. * @return void
  206. */
  207. public function testOpen()
  208. {
  209. $this->File->handle = null;
  210. $r = $this->File->open();
  211. $this->assertTrue(is_resource($this->File->handle));
  212. $this->assertTrue($r);
  213. $handle = $this->File->handle;
  214. $r = $this->File->open();
  215. $this->assertTrue($r);
  216. $this->assertTrue($handle === $this->File->handle);
  217. $this->assertTrue(is_resource($this->File->handle));
  218. $r = $this->File->open('r', true);
  219. $this->assertTrue($r);
  220. $this->assertFalse($handle === $this->File->handle);
  221. $this->assertTrue(is_resource($this->File->handle));
  222. }
  223. /**
  224. * testClose method
  225. *
  226. * @return void
  227. */
  228. public function testClose()
  229. {
  230. $this->File->handle = null;
  231. $this->assertFalse(is_resource($this->File->handle));
  232. $this->assertTrue($this->File->close());
  233. $this->assertFalse(is_resource($this->File->handle));
  234. $this->File->handle = fopen(__FILE__, 'r');
  235. $this->assertTrue(is_resource($this->File->handle));
  236. $this->assertTrue($this->File->close());
  237. $this->assertFalse(is_resource($this->File->handle));
  238. }
  239. /**
  240. * testCreate method
  241. *
  242. * @return void
  243. */
  244. public function testCreate()
  245. {
  246. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  247. $File = new File($tmpFile, true, 0777);
  248. $this->assertTrue($File->exists());
  249. }
  250. /**
  251. * Tests the exists() method.
  252. *
  253. * @return void
  254. */
  255. public function testExists()
  256. {
  257. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  258. $file = new File($tmpFile, true, 0777);
  259. $this->assertTrue($file->exists(), 'absolute path should exist');
  260. $file = new File('file://' . $tmpFile, false);
  261. $this->assertTrue($file->exists(), 'file:// should exist.');
  262. $file = new File('/something/bad', false);
  263. $this->assertFalse($file->exists(), 'missing file should not exist.');
  264. }
  265. /**
  266. * testOpeningNonExistentFileCreatesIt method
  267. *
  268. * @return void
  269. */
  270. public function testOpeningNonExistentFileCreatesIt()
  271. {
  272. $someFile = new File(TMP . 'some_file.txt', false);
  273. $this->assertTrue($someFile->open());
  274. $this->assertEquals('', $someFile->read());
  275. $someFile->close();
  276. $someFile->delete();
  277. }
  278. /**
  279. * testPrepare method
  280. *
  281. * @return void
  282. */
  283. public function testPrepare()
  284. {
  285. $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
  286. if (DS === '\\') {
  287. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  288. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  289. } else {
  290. $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
  291. }
  292. $this->assertSame($expected, File::prepare($string));
  293. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  294. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  295. $this->assertSame($expected, File::prepare($string, true));
  296. }
  297. /**
  298. * testReadable method
  299. *
  300. * @return void
  301. */
  302. public function testReadable()
  303. {
  304. $someFile = new File(TMP . 'some_file.txt', false);
  305. $this->assertTrue($someFile->open());
  306. $this->assertTrue($someFile->readable());
  307. $someFile->close();
  308. $someFile->delete();
  309. }
  310. /**
  311. * testWritable method
  312. *
  313. * @return void
  314. */
  315. public function testWritable()
  316. {
  317. $someFile = new File(TMP . 'some_file.txt', false);
  318. $this->assertTrue($someFile->open());
  319. $this->assertTrue($someFile->writable());
  320. $someFile->close();
  321. $someFile->delete();
  322. }
  323. /**
  324. * testExecutable method
  325. *
  326. * @return void
  327. */
  328. public function testExecutable()
  329. {
  330. $someFile = new File(TMP . 'some_file.txt', false);
  331. $this->assertTrue($someFile->open());
  332. $this->assertFalse($someFile->executable());
  333. $someFile->close();
  334. $someFile->delete();
  335. }
  336. /**
  337. * testLastAccess method
  338. *
  339. * @return void
  340. */
  341. public function testLastAccess()
  342. {
  343. $someFile = new File(TMP . 'some_file.txt', false);
  344. $this->assertFalse($someFile->lastAccess());
  345. $this->assertTrue($someFile->open());
  346. $this->assertWithinRange(time(), $someFile->lastAccess(), 2);
  347. $someFile->close();
  348. $someFile->delete();
  349. }
  350. /**
  351. * testLastChange method
  352. *
  353. * @return void
  354. */
  355. public function testLastChange()
  356. {
  357. $someFile = new File(TMP . 'some_file.txt', false);
  358. $this->assertFalse($someFile->lastChange());
  359. $this->assertTrue($someFile->open('r+'));
  360. $this->assertWithinRange(time(), $someFile->lastChange(), 2);
  361. $someFile->write('something');
  362. $this->assertWithinRange(time(), $someFile->lastChange(), 2);
  363. $someFile->close();
  364. $someFile->delete();
  365. }
  366. /**
  367. * testWrite method
  368. *
  369. * @return void
  370. */
  371. public function testWrite()
  372. {
  373. if (!$tmpFile = $this->_getTmpFile()) {
  374. return false;
  375. }
  376. if (file_exists($tmpFile)) {
  377. unlink($tmpFile);
  378. }
  379. $TmpFile = new File($tmpFile);
  380. $this->assertFalse(file_exists($tmpFile));
  381. $this->assertFalse(is_resource($TmpFile->handle));
  382. $testData = ['CakePHP\'s', ' test suite', ' was here ...', ''];
  383. foreach ($testData as $data) {
  384. $r = $TmpFile->write($data);
  385. $this->assertTrue($r);
  386. $this->assertTrue(file_exists($tmpFile));
  387. $this->assertEquals($data, file_get_contents($tmpFile));
  388. $this->assertTrue(is_resource($TmpFile->handle));
  389. $TmpFile->close();
  390. }
  391. unlink($tmpFile);
  392. }
  393. /**
  394. * testAppend method
  395. *
  396. * @return void
  397. */
  398. public function testAppend()
  399. {
  400. if (!$tmpFile = $this->_getTmpFile()) {
  401. return false;
  402. }
  403. if (file_exists($tmpFile)) {
  404. unlink($tmpFile);
  405. }
  406. $TmpFile = new File($tmpFile);
  407. $this->assertFalse(file_exists($tmpFile));
  408. $fragments = ['CakePHP\'s', ' test suite', ' was here ...'];
  409. $data = null;
  410. $size = 0;
  411. foreach ($fragments as $fragment) {
  412. $r = $TmpFile->append($fragment);
  413. $this->assertTrue($r);
  414. $this->assertTrue(file_exists($tmpFile));
  415. $data = $data . $fragment;
  416. $this->assertEquals($data, file_get_contents($tmpFile));
  417. $newSize = $TmpFile->size();
  418. $this->assertTrue($newSize > $size);
  419. $size = $newSize;
  420. $TmpFile->close();
  421. }
  422. $TmpFile->append('');
  423. $this->assertEquals($data, file_get_contents($tmpFile));
  424. $TmpFile->close();
  425. }
  426. /**
  427. * testDelete method
  428. *
  429. * @return void
  430. */
  431. public function testDelete()
  432. {
  433. if (!$tmpFile = $this->_getTmpFile()) {
  434. return false;
  435. }
  436. if (!file_exists($tmpFile)) {
  437. touch($tmpFile);
  438. }
  439. $TmpFile = new File($tmpFile);
  440. $this->assertTrue(file_exists($tmpFile));
  441. $result = $TmpFile->delete();
  442. $this->assertTrue($result);
  443. $this->assertFalse(file_exists($tmpFile));
  444. $TmpFile = new File('/this/does/not/exist');
  445. $result = $TmpFile->delete();
  446. $this->assertFalse($result);
  447. }
  448. /**
  449. * Windows has issues unlinking files if there are
  450. * active filehandles open.
  451. *
  452. * @return void
  453. */
  454. public function testDeleteAfterRead()
  455. {
  456. if (!$tmpFile = $this->_getTmpFile()) {
  457. return false;
  458. }
  459. if (!file_exists($tmpFile)) {
  460. touch($tmpFile);
  461. }
  462. $File = new File($tmpFile);
  463. $File->read();
  464. $this->assertTrue($File->delete());
  465. }
  466. /**
  467. * testCopy method
  468. *
  469. * @return void
  470. */
  471. public function testCopy()
  472. {
  473. $dest = TMP . 'tests/cakephp.file.test.tmp';
  474. $file = __FILE__;
  475. $this->File = new File($file);
  476. $result = $this->File->copy($dest);
  477. $this->assertTrue($result);
  478. $result = $this->File->copy($dest, true);
  479. $this->assertTrue($result);
  480. $result = $this->File->copy($dest, false);
  481. $this->assertFalse($result);
  482. $this->File->close();
  483. unlink($dest);
  484. $TmpFile = new File('/this/does/not/exist');
  485. $result = $TmpFile->copy($dest);
  486. $this->assertFalse($result);
  487. $TmpFile->close();
  488. }
  489. /**
  490. * Test mime()
  491. *
  492. * @return void
  493. */
  494. public function testMime()
  495. {
  496. $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
  497. $path = TEST_APP . 'webroot/img/cake.power.gif';
  498. $file = new File($path);
  499. $expected = 'image/gif';
  500. if (function_exists('mime_content_type') && mime_content_type($file->pwd()) === false) {
  501. $expected = false;
  502. }
  503. $this->assertEquals($expected, $file->mime());
  504. }
  505. /**
  506. * getTmpFile method
  507. *
  508. * @param bool $paintSkip
  509. * @return void
  510. */
  511. protected function _getTmpFile($paintSkip = true)
  512. {
  513. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  514. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  515. return $tmpFile;
  516. }
  517. if ($paintSkip) {
  518. $trace = debug_backtrace();
  519. $caller = $trace[0]['function'];
  520. $shortPath = dirname($tmpFile);
  521. $message = sprintf('[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
  522. $this->markTestSkipped($message);
  523. }
  524. return false;
  525. }
  526. /**
  527. * testReplaceText method
  528. *
  529. * @return void
  530. */
  531. public function testReplaceText()
  532. {
  533. $TestFile = new File(TEST_APP . 'vendor/welcome.php');
  534. $TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp');
  535. // Copy the test file to the temporary location
  536. $TestFile->copy($TmpFile->path, true);
  537. // Replace the contents of the temporary file
  538. $result = $TmpFile->replaceText('welcome.php', 'welcome.tmp');
  539. $this->assertTrue($result);
  540. // Double check
  541. $expected = 'This is the welcome.tmp file in vendors directory';
  542. $contents = $TmpFile->read();
  543. $this->assertContains($expected, $contents);
  544. $search = ['This is the', 'welcome.php file', 'in tmp directory'];
  545. $replace = ['This should be a', 'welcome.tmp file', 'in the Lib directory'];
  546. // Replace the contents of the temporary file
  547. $result = $TmpFile->replaceText($search, $replace);
  548. $this->assertTrue($result);
  549. // Double check
  550. $expected = 'This should be a welcome.tmp file in vendors directory';
  551. $contents = $TmpFile->read();
  552. $this->assertContains($expected, $contents);
  553. $TmpFile->delete();
  554. }
  555. /**
  556. * Tests that no path is being set for passed file paths that
  557. * do not exist.
  558. *
  559. * @return void
  560. */
  561. public function testNoPartialPathBeingSetForNonExistentPath()
  562. {
  563. $TmpFile = new File('/non/existent/file');
  564. $this->assertNull($TmpFile->pwd());
  565. $this->assertNull($TmpFile->path);
  566. }
  567. }