FileTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. * testUtf8Filenames
  109. *
  110. * @link https://github.com/cakephp/cakephp/issues/11749
  111. * @return void
  112. */
  113. public function testUtf8Filenames()
  114. {
  115. $File = new File(TMP . 'tests/permissions/نام فارسی.php', true);
  116. $this->assertEquals('نام فارسی', $File->name());
  117. $this->assertTrue($File->exists());
  118. $this->assertTrue($File->readable());
  119. }
  120. /**
  121. * Test _basename method
  122. * @dataProvider baseNameValueProvider
  123. * @return void
  124. */
  125. public function testBasename($path, $suffix)
  126. {
  127. $class = new \ReflectionClass('Cake\Filesystem\File');
  128. $method = $class->getMethod('_basename');
  129. $method->setAccessible(true);
  130. if ($suffix === null) {
  131. $this->assertEquals(basename($path), $method->invokeArgs(null, [$path]));
  132. } else {
  133. $this->assertEquals(basename($path, $suffix), $method->invokeArgs(null, [$path, $suffix]));
  134. }
  135. }
  136. /**
  137. * Data provider for testBasename().
  138. *
  139. * @return array
  140. */
  141. public function baseNameValueProvider()
  142. {
  143. return [
  144. ['folder/نام.txt', null],
  145. ['folder/نام فارسی.txt', null],
  146. ['نام.txt', null],
  147. ['نام فارسی.txt', null],
  148. ['/نام.txt', null],
  149. ['/نام فارسی.txt', null],
  150. //
  151. ['folder/نام.txt', '.txt'],
  152. ['folder/نام فارسی.txt', '.txt'],
  153. ['نام.txt', '.txt'],
  154. ['نام فارسی.txt', '.txt'],
  155. ['/نام.txt', '.txt'],
  156. ['/نام فارسی.txt', '.txt'],
  157. //
  158. ['abcde.ab', '.abe'],
  159. ['/etc/sudoers.d', null],
  160. ['/etc/.d', '.d'],
  161. ['/etc/sudoers.d', '.d'],
  162. ['/etc/passwd', null],
  163. ['/etc/', null],
  164. ['.', null],
  165. ['/', null],
  166. ];
  167. }
  168. /**
  169. * testPermission method
  170. *
  171. * @return void
  172. */
  173. public function testPermission()
  174. {
  175. $this->skipIf(DS === '\\', 'File permissions tests not supported on Windows.');
  176. $dir = TMP . 'tests' . DS . 'permissions' . DS;
  177. $old = umask();
  178. umask(0002);
  179. $file = $dir . 'permission_' . uniqid();
  180. $expecting = decoct(0664 & ~umask());
  181. $File = new File($file, true);
  182. $result = $File->perms();
  183. $this->assertEquals($expecting, $result);
  184. $File->delete();
  185. umask(0022);
  186. $file = $dir . 'permission_' . uniqid();
  187. $expecting = decoct(0644 & ~umask());
  188. $File = new File($file, true);
  189. $result = $File->perms();
  190. $this->assertEquals($expecting, $result);
  191. $File->delete();
  192. umask(0422);
  193. $file = $dir . 'permission_' . uniqid();
  194. $expecting = decoct(0244 & ~umask());
  195. $File = new File($file, true);
  196. $result = $File->perms();
  197. $this->assertEquals($expecting, $result);
  198. $File->delete();
  199. umask(0444);
  200. $file = $dir . 'permission_' . uniqid();
  201. $expecting = decoct(0222 & ~umask());
  202. $File = new File($file, true);
  203. $result = $File->perms();
  204. $this->assertEquals($expecting, $result);
  205. $File->delete();
  206. umask($old);
  207. }
  208. /**
  209. * testRead method
  210. *
  211. * @return void
  212. */
  213. public function testRead()
  214. {
  215. $file = __FILE__;
  216. $this->File = new File($file);
  217. $result = $this->File->read();
  218. $expecting = file_get_contents(__FILE__);
  219. $this->assertEquals($expecting, $result);
  220. $this->assertTrue(!is_resource($this->File->handle));
  221. $this->File->lock = true;
  222. $result = $this->File->read();
  223. $expecting = file_get_contents(__FILE__);
  224. $this->assertEquals(trim($expecting), $result);
  225. $this->File->lock = null;
  226. $data = $expecting;
  227. $expecting = substr($data, 0, 3);
  228. $result = $this->File->read(3);
  229. $this->assertEquals($expecting, $result);
  230. $this->assertInternalType('resource', $this->File->handle);
  231. $expecting = substr($data, 3, 3);
  232. $result = $this->File->read(3);
  233. $this->assertEquals($expecting, $result);
  234. }
  235. /**
  236. * testOffset method
  237. *
  238. * @return void
  239. */
  240. public function testOffset()
  241. {
  242. $this->File->close();
  243. $result = $this->File->offset();
  244. $this->assertFalse($result);
  245. $this->assertNotInternalType('resource', $this->File->handle);
  246. $success = $this->File->offset(0);
  247. $this->assertTrue($success);
  248. $this->assertInternalType('resource', $this->File->handle);
  249. $result = $this->File->offset();
  250. $expected = 0;
  251. $this->assertSame($expected, $result);
  252. $data = file_get_contents(__FILE__);
  253. $success = $this->File->offset(5);
  254. $expected = substr($data, 5, 3);
  255. $result = $this->File->read(3);
  256. $this->assertTrue($success);
  257. $this->assertEquals($expected, $result);
  258. $result = $this->File->offset();
  259. $expected = 5 + 3;
  260. $this->assertSame($expected, $result);
  261. }
  262. /**
  263. * testOpen method
  264. *
  265. * @return void
  266. */
  267. public function testOpen()
  268. {
  269. $this->File->handle = null;
  270. $r = $this->File->open();
  271. $this->assertInternalType('resource', $this->File->handle);
  272. $this->assertTrue($r);
  273. $handle = $this->File->handle;
  274. $r = $this->File->open();
  275. $this->assertTrue($r);
  276. $this->assertTrue($handle === $this->File->handle);
  277. $this->assertInternalType('resource', $this->File->handle);
  278. $r = $this->File->open('r', true);
  279. $this->assertTrue($r);
  280. $this->assertFalse($handle === $this->File->handle);
  281. $this->assertInternalType('resource', $this->File->handle);
  282. }
  283. /**
  284. * testClose method
  285. *
  286. * @return void
  287. */
  288. public function testClose()
  289. {
  290. $this->File->handle = null;
  291. $this->assertNotInternalType('resource', $this->File->handle);
  292. $this->assertTrue($this->File->close());
  293. $this->assertNotInternalType('resource', $this->File->handle);
  294. $this->File->handle = fopen(__FILE__, 'r');
  295. $this->assertInternalType('resource', $this->File->handle);
  296. $this->assertTrue($this->File->close());
  297. $this->assertFalse(is_resource($this->File->handle));
  298. }
  299. /**
  300. * testCreate method
  301. *
  302. * @return void
  303. */
  304. public function testCreate()
  305. {
  306. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  307. $File = new File($tmpFile, true, 0777);
  308. $this->assertTrue($File->exists());
  309. }
  310. /**
  311. * Tests the exists() method.
  312. *
  313. * @return void
  314. */
  315. public function testExists()
  316. {
  317. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  318. $file = new File($tmpFile, true, 0777);
  319. $this->assertTrue($file->exists(), 'absolute path should exist');
  320. $file = new File('file://' . $tmpFile, false);
  321. $this->assertTrue($file->exists(), 'file:// should exist.');
  322. $file = new File('/something/bad', false);
  323. $this->assertFalse($file->exists(), 'missing file should not exist.');
  324. }
  325. /**
  326. * testOpeningNonExistentFileCreatesIt method
  327. *
  328. * @return void
  329. */
  330. public function testOpeningNonExistentFileCreatesIt()
  331. {
  332. $someFile = new File(TMP . 'some_file.txt', false);
  333. $this->assertTrue($someFile->open());
  334. $this->assertEquals('', $someFile->read());
  335. $someFile->close();
  336. $someFile->delete();
  337. }
  338. /**
  339. * testPrepare method
  340. *
  341. * @return void
  342. */
  343. public function testPrepare()
  344. {
  345. $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
  346. if (DS === '\\') {
  347. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  348. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  349. } else {
  350. $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
  351. }
  352. $this->assertSame($expected, File::prepare($string));
  353. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  354. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  355. $this->assertSame($expected, File::prepare($string, true));
  356. }
  357. /**
  358. * testReadable method
  359. *
  360. * @return void
  361. */
  362. public function testReadable()
  363. {
  364. $someFile = new File(TMP . 'some_file.txt', false);
  365. $this->assertTrue($someFile->open());
  366. $this->assertTrue($someFile->readable());
  367. $someFile->close();
  368. $someFile->delete();
  369. }
  370. /**
  371. * testWritable method
  372. *
  373. * @return void
  374. */
  375. public function testWritable()
  376. {
  377. $someFile = new File(TMP . 'some_file.txt', false);
  378. $this->assertTrue($someFile->open());
  379. $this->assertTrue($someFile->writable());
  380. $someFile->close();
  381. $someFile->delete();
  382. }
  383. /**
  384. * testExecutable method
  385. *
  386. * @return void
  387. */
  388. public function testExecutable()
  389. {
  390. $someFile = new File(TMP . 'some_file.txt', false);
  391. $this->assertTrue($someFile->open());
  392. $this->assertFalse($someFile->executable());
  393. $someFile->close();
  394. $someFile->delete();
  395. }
  396. /**
  397. * testLastAccess method
  398. *
  399. * @return void
  400. */
  401. public function testLastAccess()
  402. {
  403. $someFile = new File(TMP . 'some_file.txt', false);
  404. $this->assertFalse($someFile->lastAccess());
  405. $this->assertTrue($someFile->open());
  406. $this->assertWithinRange(time(), $someFile->lastAccess(), 2);
  407. $someFile->close();
  408. $someFile->delete();
  409. }
  410. /**
  411. * testLastChange method
  412. *
  413. * @return void
  414. */
  415. public function testLastChange()
  416. {
  417. $someFile = new File(TMP . 'some_file.txt', false);
  418. $this->assertFalse($someFile->lastChange());
  419. $this->assertTrue($someFile->open('r+'));
  420. $this->assertWithinRange(time(), $someFile->lastChange(), 2);
  421. $someFile->write('something');
  422. $this->assertWithinRange(time(), $someFile->lastChange(), 2);
  423. $someFile->close();
  424. $someFile->delete();
  425. }
  426. /**
  427. * testWrite method
  428. *
  429. * @return void
  430. */
  431. public function testWrite()
  432. {
  433. if (!$tmpFile = $this->_getTmpFile()) {
  434. return false;
  435. }
  436. if (file_exists($tmpFile)) {
  437. unlink($tmpFile);
  438. }
  439. $TmpFile = new File($tmpFile);
  440. $this->assertFileNotExists($tmpFile);
  441. $this->assertNotInternalType('resource', $TmpFile->handle);
  442. $testData = ['CakePHP\'s', ' test suite', ' was here ...', ''];
  443. foreach ($testData as $data) {
  444. $r = $TmpFile->write($data);
  445. $this->assertTrue($r);
  446. $this->assertFileExists($tmpFile);
  447. $this->assertEquals($data, file_get_contents($tmpFile));
  448. $this->assertInternalType('resource', $TmpFile->handle);
  449. $TmpFile->close();
  450. }
  451. unlink($tmpFile);
  452. }
  453. /**
  454. * testAppend method
  455. *
  456. * @return void
  457. */
  458. public function testAppend()
  459. {
  460. if (!$tmpFile = $this->_getTmpFile()) {
  461. return false;
  462. }
  463. if (file_exists($tmpFile)) {
  464. unlink($tmpFile);
  465. }
  466. $TmpFile = new File($tmpFile);
  467. $this->assertFileNotExists($tmpFile);
  468. $fragments = ['CakePHP\'s', ' test suite', ' was here ...'];
  469. $data = null;
  470. $size = 0;
  471. foreach ($fragments as $fragment) {
  472. $r = $TmpFile->append($fragment);
  473. $this->assertTrue($r);
  474. $this->assertFileExists($tmpFile);
  475. $data = $data . $fragment;
  476. $this->assertEquals($data, file_get_contents($tmpFile));
  477. $newSize = $TmpFile->size();
  478. $this->assertTrue($newSize > $size);
  479. $size = $newSize;
  480. $TmpFile->close();
  481. }
  482. $TmpFile->append('');
  483. $this->assertEquals($data, file_get_contents($tmpFile));
  484. $TmpFile->close();
  485. }
  486. /**
  487. * testDelete method
  488. *
  489. * @return void
  490. */
  491. public function testDelete()
  492. {
  493. if (!$tmpFile = $this->_getTmpFile()) {
  494. return false;
  495. }
  496. if (!file_exists($tmpFile)) {
  497. touch($tmpFile);
  498. }
  499. $TmpFile = new File($tmpFile);
  500. $this->assertFileExists($tmpFile);
  501. $result = $TmpFile->delete();
  502. $this->assertTrue($result);
  503. $this->assertFileNotExists($tmpFile);
  504. $TmpFile = new File('/this/does/not/exist');
  505. $result = $TmpFile->delete();
  506. $this->assertFalse($result);
  507. }
  508. /**
  509. * Windows has issues unlinking files if there are
  510. * active filehandles open.
  511. *
  512. * @return void
  513. */
  514. public function testDeleteAfterRead()
  515. {
  516. if (!$tmpFile = $this->_getTmpFile()) {
  517. return false;
  518. }
  519. if (!file_exists($tmpFile)) {
  520. touch($tmpFile);
  521. }
  522. $File = new File($tmpFile);
  523. $File->read();
  524. $this->assertTrue($File->delete());
  525. }
  526. /**
  527. * testCopy method
  528. *
  529. * @return void
  530. */
  531. public function testCopy()
  532. {
  533. $dest = TMP . 'tests/cakephp.file.test.tmp';
  534. $file = __FILE__;
  535. $this->File = new File($file);
  536. $result = $this->File->copy($dest);
  537. $this->assertTrue($result);
  538. $result = $this->File->copy($dest, true);
  539. $this->assertTrue($result);
  540. $result = $this->File->copy($dest, false);
  541. $this->assertFalse($result);
  542. $this->File->close();
  543. unlink($dest);
  544. $TmpFile = new File('/this/does/not/exist');
  545. $result = $TmpFile->copy($dest);
  546. $this->assertFalse($result);
  547. $TmpFile->close();
  548. }
  549. /**
  550. * Test mime()
  551. *
  552. * @return void
  553. */
  554. public function testMime()
  555. {
  556. $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
  557. $path = TEST_APP . 'webroot/img/cake.power.gif';
  558. $file = new File($path);
  559. $expected = 'image/gif';
  560. if (function_exists('mime_content_type') && mime_content_type($file->pwd()) === false) {
  561. $expected = false;
  562. }
  563. $this->assertEquals($expected, $file->mime());
  564. }
  565. /**
  566. * getTmpFile method
  567. *
  568. * @param bool $paintSkip
  569. * @return void
  570. */
  571. protected function _getTmpFile($paintSkip = true)
  572. {
  573. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  574. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  575. return $tmpFile;
  576. }
  577. if ($paintSkip) {
  578. $trace = debug_backtrace();
  579. $caller = $trace[0]['function'];
  580. $shortPath = dirname($tmpFile);
  581. $message = sprintf('[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
  582. $this->markTestSkipped($message);
  583. }
  584. return false;
  585. }
  586. /**
  587. * testReplaceText method
  588. *
  589. * @return void
  590. */
  591. public function testReplaceText()
  592. {
  593. $TestFile = new File(TEST_APP . 'vendor/welcome.php');
  594. $TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp');
  595. // Copy the test file to the temporary location
  596. $TestFile->copy($TmpFile->path, true);
  597. // Replace the contents of the temporary file
  598. $result = $TmpFile->replaceText('welcome.php', 'welcome.tmp');
  599. $this->assertTrue($result);
  600. // Double check
  601. $expected = 'This is the welcome.tmp file in vendors directory';
  602. $contents = $TmpFile->read();
  603. $this->assertContains($expected, $contents);
  604. $search = ['This is the', 'welcome.php file', 'in tmp directory'];
  605. $replace = ['This should be a', 'welcome.tmp file', 'in the Lib directory'];
  606. // Replace the contents of the temporary file
  607. $result = $TmpFile->replaceText($search, $replace);
  608. $this->assertTrue($result);
  609. // Double check
  610. $expected = 'This should be a welcome.tmp file in vendors directory';
  611. $contents = $TmpFile->read();
  612. $this->assertContains($expected, $contents);
  613. $TmpFile->delete();
  614. }
  615. /**
  616. * Tests that no path is being set for passed file paths that
  617. * do not exist.
  618. *
  619. * @return void
  620. */
  621. public function testNoPartialPathBeingSetForNonExistentPath()
  622. {
  623. $TmpFile = new File('/non/existent/file');
  624. $this->assertNull($TmpFile->pwd());
  625. $this->assertNull($TmpFile->path);
  626. }
  627. }