FileTest.php 20 KB

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