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 (!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. $tmpFile = $this->_getTmpFile();
  448. if (file_exists($tmpFile)) {
  449. unlink($tmpFile);
  450. }
  451. $TmpFile = new File($tmpFile);
  452. $this->assertFileNotExists($tmpFile);
  453. $this->assertNull($TmpFile->handle);
  454. $testData = ['CakePHP\'s', ' test suite', ' was here ...', ''];
  455. foreach ($testData as $data) {
  456. $r = $TmpFile->write($data);
  457. $this->assertTrue($r);
  458. $this->assertFileExists($tmpFile);
  459. $this->assertStringEqualsFile($tmpFile, $data);
  460. $this->assertInternalType('resource', $TmpFile->handle);
  461. $TmpFile->close();
  462. }
  463. unlink($tmpFile);
  464. }
  465. /**
  466. * testAppend method
  467. *
  468. * @return void
  469. */
  470. public function testAppend()
  471. {
  472. $tmpFile = $this->_getTmpFile();
  473. if (file_exists($tmpFile)) {
  474. unlink($tmpFile);
  475. }
  476. $TmpFile = new File($tmpFile);
  477. $this->assertFileNotExists($tmpFile);
  478. $fragments = ['CakePHP\'s', ' test suite', ' was here ...'];
  479. $data = null;
  480. $size = 0;
  481. foreach ($fragments as $fragment) {
  482. $r = $TmpFile->append($fragment);
  483. $this->assertTrue($r);
  484. $this->assertFileExists($tmpFile);
  485. $data = $data . $fragment;
  486. $this->assertStringEqualsFile($tmpFile, $data);
  487. $newSize = $TmpFile->size();
  488. $this->assertTrue($newSize > $size);
  489. $size = $newSize;
  490. $TmpFile->close();
  491. }
  492. $TmpFile->append('');
  493. $this->assertStringEqualsFile($tmpFile, $data);
  494. $TmpFile->close();
  495. }
  496. /**
  497. * testDelete method
  498. *
  499. * @return void
  500. */
  501. public function testDelete()
  502. {
  503. $tmpFile = $this->_getTmpFile();
  504. if (!file_exists($tmpFile)) {
  505. touch($tmpFile);
  506. }
  507. $TmpFile = new File($tmpFile);
  508. $this->assertFileExists($tmpFile);
  509. $result = $TmpFile->delete();
  510. $this->assertTrue($result);
  511. $this->assertFileNotExists($tmpFile);
  512. $TmpFile = new File('/this/does/not/exist');
  513. $result = $TmpFile->delete();
  514. $this->assertFalse($result);
  515. }
  516. /**
  517. * Windows has issues unlinking files if there are
  518. * active filehandles open.
  519. *
  520. * @return void
  521. */
  522. public function testDeleteAfterRead()
  523. {
  524. $tmpFile = $this->_getTmpFile();
  525. if (!file_exists($tmpFile)) {
  526. touch($tmpFile);
  527. }
  528. $File = new File($tmpFile);
  529. $File->read();
  530. $this->assertTrue($File->delete());
  531. }
  532. /**
  533. * testCopy method
  534. *
  535. * @return void
  536. */
  537. public function testCopy()
  538. {
  539. $dest = TMP . 'tests/cakephp.file.test.tmp';
  540. $file = __FILE__;
  541. $this->File = new File($file);
  542. $result = $this->File->copy($dest);
  543. $this->assertTrue($result);
  544. $result = $this->File->copy($dest, true);
  545. $this->assertTrue($result);
  546. $result = $this->File->copy($dest, false);
  547. $this->assertFalse($result);
  548. $this->File->close();
  549. unlink($dest);
  550. $TmpFile = new File('/this/does/not/exist');
  551. $result = $TmpFile->copy($dest);
  552. $this->assertFalse($result);
  553. $TmpFile->close();
  554. }
  555. /**
  556. * Test mime()
  557. *
  558. * @return void
  559. */
  560. public function testMime()
  561. {
  562. $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
  563. $path = TEST_APP . 'webroot/img/cake.power.gif';
  564. $file = new File($path);
  565. $expected = 'image/gif';
  566. if (function_exists('mime_content_type') && mime_content_type($file->pwd()) === false) {
  567. $expected = false;
  568. }
  569. $this->assertEquals($expected, $file->mime());
  570. }
  571. /**
  572. * getTmpFile method
  573. *
  574. * @param bool $paintSkip
  575. * @return void
  576. */
  577. protected function _getTmpFile()
  578. {
  579. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  580. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  581. return $tmpFile;
  582. }
  583. $trace = debug_backtrace();
  584. $caller = $trace[0]['function'];
  585. $shortPath = dirname($tmpFile);
  586. $message = sprintf('[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
  587. $this->markTestSkipped($message);
  588. }
  589. /**
  590. * testReplaceText method
  591. *
  592. * @return void
  593. */
  594. public function testReplaceText()
  595. {
  596. $TestFile = new File(TEST_APP . 'vendor/welcome.php');
  597. $TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp');
  598. // Copy the test file to the temporary location
  599. $TestFile->copy($TmpFile->path, true);
  600. // Replace the contents of the temporary file
  601. $result = $TmpFile->replaceText('welcome.php', 'welcome.tmp');
  602. $this->assertTrue($result);
  603. // Double check
  604. $expected = 'This is the welcome.tmp file in vendors directory';
  605. $contents = $TmpFile->read();
  606. $this->assertContains($expected, $contents);
  607. $search = ['This is the', 'welcome.php file', 'in tmp directory'];
  608. $replace = ['This should be a', 'welcome.tmp file', 'in the Lib directory'];
  609. // Replace the contents of the temporary file
  610. $result = $TmpFile->replaceText($search, $replace);
  611. $this->assertTrue($result);
  612. // Double check
  613. $expected = 'This should be a welcome.tmp file in vendors directory';
  614. $contents = $TmpFile->read();
  615. $this->assertContains($expected, $contents);
  616. $TmpFile->delete();
  617. }
  618. /**
  619. * Tests that no path is being set for passed file paths that
  620. * do not exist.
  621. *
  622. * @return void
  623. */
  624. public function testNoPartialPathBeingSetForNonExistentPath()
  625. {
  626. $TmpFile = new File('/non/existent/file');
  627. $this->assertNull($TmpFile->pwd());
  628. $this->assertNull($TmpFile->path);
  629. }
  630. }