FileTest.php 20 KB

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