FileTest.php 20 KB

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