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