FileTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 (
  76. !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. $this->assertEquals('', $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. *
  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 paths are directories like '/etc/sudoers.d'
  133. if (!is_dir($path)) {
  134. // Check the name after running __construct()
  135. $result = $File->name;
  136. $expecting = basename($path);
  137. $this->assertEquals($expecting, $result);
  138. }
  139. // Check 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. unset($File->info['extension']);
  145. $this->assertEquals(basename($path), $File->name());
  146. } else {
  147. $File->info['extension'] = $suffix;
  148. $this->assertEquals(basename($path, '.' . $suffix), $File->name());
  149. }
  150. }
  151. /**
  152. * Data provider for testBasename().
  153. *
  154. * @return array
  155. */
  156. public function baseNameValueProvider()
  157. {
  158. return [
  159. ['folder/نام.txt', null, false],
  160. ['folder/نام فارسی.txt', null, false],
  161. ['نام.txt', null, true],
  162. ['نام فارسی.txt', null, true],
  163. ['/نام.txt', null, true],
  164. ['/نام فارسی.txt', null, true],
  165. //
  166. ['folder/نام.txt', 'txt', false],
  167. ['folder/نام فارسی.txt', 'txt', false],
  168. ['نام.txt', 'txt', true],
  169. ['نام فارسی.txt', 'txt', true],
  170. ['/نام.txt', 'txt', true],
  171. ['/نام فارسی.txt', 'txt', true],
  172. //
  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->assertNotInternalType('resource', $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->assertInternalType('resource', $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->assertInternalType('resource', $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->assertInternalType('resource', $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->assertInternalType('resource', $this->File->handle);
  293. $r = $this->File->open('r', true);
  294. $this->assertTrue($r);
  295. $this->assertNotSame($handle, $this->File->handle);
  296. $this->assertInternalType('resource', $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->assertInternalType('resource', $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->assertEquals('', $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->assertInternalType('resource', $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. $TmpFile = new File($tmpFile);
  509. $this->assertFileExists($tmpFile);
  510. $result = $TmpFile->delete();
  511. $this->assertTrue($result);
  512. $this->assertFileNotExists($tmpFile);
  513. $TmpFile = new File('/this/does/not/exist');
  514. $result = $TmpFile->delete();
  515. $this->assertFalse($result);
  516. }
  517. /**
  518. * Windows has issues unlinking files if there are
  519. * active filehandles open.
  520. *
  521. * @return void
  522. */
  523. public function testDeleteAfterRead()
  524. {
  525. $tmpFile = $this->_getTmpFile();
  526. if (!file_exists($tmpFile)) {
  527. touch($tmpFile);
  528. }
  529. $File = new File($tmpFile);
  530. $File->read();
  531. $this->assertTrue($File->delete());
  532. }
  533. /**
  534. * testCopy method
  535. *
  536. * @return void
  537. */
  538. public function testCopy()
  539. {
  540. $dest = TMP . 'tests/cakephp.file.test.tmp';
  541. $file = __FILE__;
  542. $this->File = new File($file);
  543. $result = $this->File->copy($dest);
  544. $this->assertTrue($result);
  545. $result = $this->File->copy($dest, true);
  546. $this->assertTrue($result);
  547. $result = $this->File->copy($dest, false);
  548. $this->assertFalse($result);
  549. $this->File->close();
  550. unlink($dest);
  551. $TmpFile = new File('/this/does/not/exist');
  552. $result = $TmpFile->copy($dest);
  553. $this->assertFalse($result);
  554. $TmpFile->close();
  555. }
  556. /**
  557. * Test mime()
  558. *
  559. * @return void
  560. */
  561. public function testMime()
  562. {
  563. $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
  564. $path = TEST_APP . 'webroot/img/cake.power.gif';
  565. $file = new File($path);
  566. $expected = 'image/gif';
  567. if (function_exists('mime_content_type') && mime_content_type($file->pwd()) === false) {
  568. $expected = false;
  569. }
  570. $this->assertEquals($expected, $file->mime());
  571. }
  572. /**
  573. * getTmpFile method
  574. *
  575. * @param bool $paintSkip
  576. * @return void
  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->assertContains($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->assertContains($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. }