FileTest.php 18 KB

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