FileTest.php 17 KB

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