FileTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. /**
  3. * FileTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Utility;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\Utility\File;
  20. use Cake\Utility\Folder;
  21. /**
  22. * FileTest class
  23. *
  24. */
  25. class FileTest extends TestCase {
  26. /**
  27. * File property
  28. *
  29. * @var mixed null
  30. */
  31. public $File = null;
  32. /**
  33. * setup the test case
  34. *
  35. * @return void
  36. */
  37. public function setUp() {
  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. parent::tearDown();
  49. $this->File->close();
  50. unset($this->File);
  51. $Folder = new Folder();
  52. $Folder->delete(TMP . 'tests/permissions');
  53. }
  54. /**
  55. * testBasic method
  56. *
  57. * @return void
  58. */
  59. public function testBasic() {
  60. $file = CORE_PATH . DS . 'LICENSE.txt';
  61. $this->File = new File($file, false);
  62. $result = $this->File->name;
  63. $expecting = basename($file);
  64. $this->assertEquals($expecting, $result);
  65. $result = $this->File->info();
  66. $expecting = array(
  67. 'dirname' => dirname($file),
  68. 'basename' => basename($file),
  69. 'extension' => 'txt',
  70. 'filename' => 'LICENSE',
  71. 'filesize' => filesize($file),
  72. 'mime' => 'text/plain'
  73. );
  74. if (
  75. !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. $expecting = 'txt';
  85. $this->assertEquals($expecting, $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\Utility\Folder', $result);
  106. }
  107. /**
  108. * testPermission method
  109. */
  110. public function testPermission() {
  111. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
  112. $dir = TMP . 'tests' . DS . 'permissions' . DS;
  113. $old = umask();
  114. umask(0002);
  115. $file = $dir . 'permission_' . uniqid();
  116. $expecting = decoct(0664 & ~umask());
  117. $File = new File($file, true);
  118. $result = $File->perms();
  119. $this->assertEquals($expecting, $result);
  120. $File->delete();
  121. umask(0022);
  122. $file = $dir . 'permission_' . uniqid();
  123. $expecting = decoct(0644 & ~umask());
  124. $File = new File($file, true);
  125. $result = $File->perms();
  126. $this->assertEquals($expecting, $result);
  127. $File->delete();
  128. umask(0422);
  129. $file = $dir . 'permission_' . uniqid();
  130. $expecting = decoct(0244 & ~umask());
  131. $File = new File($file, true);
  132. $result = $File->perms();
  133. $this->assertEquals($expecting, $result);
  134. $File->delete();
  135. umask(0444);
  136. $file = $dir . 'permission_' . uniqid();
  137. $expecting = decoct(0222 & ~umask());
  138. $File = new File($file, true);
  139. $result = $File->perms();
  140. $this->assertEquals($expecting, $result);
  141. $File->delete();
  142. umask($old);
  143. }
  144. /**
  145. * testRead method
  146. *
  147. * @return void
  148. */
  149. public function testRead() {
  150. $file = __FILE__;
  151. $this->File = new File($file);
  152. $result = $this->File->read();
  153. $expecting = file_get_contents(__FILE__);
  154. $this->assertEquals($expecting, $result);
  155. $this->assertTrue(!is_resource($this->File->handle));
  156. $this->File->lock = true;
  157. $result = $this->File->read();
  158. $expecting = file_get_contents(__FILE__);
  159. $this->assertEquals(trim($expecting), $result);
  160. $this->File->lock = null;
  161. $data = $expecting;
  162. $expecting = substr($data, 0, 3);
  163. $result = $this->File->read(3);
  164. $this->assertEquals($expecting, $result);
  165. $this->assertTrue(is_resource($this->File->handle));
  166. $expecting = substr($data, 3, 3);
  167. $result = $this->File->read(3);
  168. $this->assertEquals($expecting, $result);
  169. }
  170. /**
  171. * testOffset method
  172. *
  173. * @return void
  174. */
  175. public function testOffset() {
  176. $this->File->close();
  177. $result = $this->File->offset();
  178. $this->assertFalse($result);
  179. $this->assertFalse(is_resource($this->File->handle));
  180. $success = $this->File->offset(0);
  181. $this->assertTrue($success);
  182. $this->assertTrue(is_resource($this->File->handle));
  183. $result = $this->File->offset();
  184. $expected = 0;
  185. $this->assertSame($expected, $result);
  186. $data = file_get_contents(__FILE__);
  187. $success = $this->File->offset(5);
  188. $expected = substr($data, 5, 3);
  189. $result = $this->File->read(3);
  190. $this->assertTrue($success);
  191. $this->assertEquals($expected, $result);
  192. $result = $this->File->offset();
  193. $expected = 5 + 3;
  194. $this->assertSame($expected, $result);
  195. }
  196. /**
  197. * testOpen method
  198. *
  199. * @return void
  200. */
  201. public function testOpen() {
  202. $this->File->handle = null;
  203. $r = $this->File->open();
  204. $this->assertTrue(is_resource($this->File->handle));
  205. $this->assertTrue($r);
  206. $handle = $this->File->handle;
  207. $r = $this->File->open();
  208. $this->assertTrue($r);
  209. $this->assertTrue($handle === $this->File->handle);
  210. $this->assertTrue(is_resource($this->File->handle));
  211. $r = $this->File->open('r', true);
  212. $this->assertTrue($r);
  213. $this->assertFalse($handle === $this->File->handle);
  214. $this->assertTrue(is_resource($this->File->handle));
  215. }
  216. /**
  217. * testClose method
  218. *
  219. * @return void
  220. */
  221. public function testClose() {
  222. $this->File->handle = null;
  223. $this->assertFalse(is_resource($this->File->handle));
  224. $this->assertTrue($this->File->close());
  225. $this->assertFalse(is_resource($this->File->handle));
  226. $this->File->handle = fopen(__FILE__, 'r');
  227. $this->assertTrue(is_resource($this->File->handle));
  228. $this->assertTrue($this->File->close());
  229. $this->assertFalse(is_resource($this->File->handle));
  230. }
  231. /**
  232. * testCreate method
  233. *
  234. * @return void
  235. */
  236. public function testCreate() {
  237. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  238. $File = new File($tmpFile, true, 0777);
  239. $this->assertTrue($File->exists());
  240. }
  241. /**
  242. * testOpeningNonExistentFileCreatesIt method
  243. *
  244. * @return void
  245. */
  246. public function testOpeningNonExistentFileCreatesIt() {
  247. $someFile = new File(TMP . 'some_file.txt', false);
  248. $this->assertTrue($someFile->open());
  249. $this->assertEquals('', $someFile->read());
  250. $someFile->close();
  251. $someFile->delete();
  252. }
  253. /**
  254. * testPrepare method
  255. *
  256. * @return void
  257. */
  258. public function testPrepare() {
  259. $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
  260. if (DS === '\\') {
  261. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  262. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  263. } else {
  264. $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
  265. }
  266. $this->assertSame($expected, File::prepare($string));
  267. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  268. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  269. $this->assertSame($expected, File::prepare($string, true));
  270. }
  271. /**
  272. * testReadable method
  273. *
  274. * @return void
  275. */
  276. public function testReadable() {
  277. $someFile = new File(TMP . 'some_file.txt', false);
  278. $this->assertTrue($someFile->open());
  279. $this->assertTrue($someFile->readable());
  280. $someFile->close();
  281. $someFile->delete();
  282. }
  283. /**
  284. * testWritable method
  285. *
  286. * @return void
  287. */
  288. public function testWritable() {
  289. $someFile = new File(TMP . 'some_file.txt', false);
  290. $this->assertTrue($someFile->open());
  291. $this->assertTrue($someFile->writable());
  292. $someFile->close();
  293. $someFile->delete();
  294. }
  295. /**
  296. * testExecutable method
  297. *
  298. * @return void
  299. */
  300. public function testExecutable() {
  301. $someFile = new File(TMP . 'some_file.txt', false);
  302. $this->assertTrue($someFile->open());
  303. $this->assertFalse($someFile->executable());
  304. $someFile->close();
  305. $someFile->delete();
  306. }
  307. /**
  308. * testLastAccess method
  309. *
  310. * @return void
  311. */
  312. public function testLastAccess() {
  313. $someFile = new File(TMP . 'some_file.txt', false);
  314. $this->assertFalse($someFile->lastAccess());
  315. $this->assertTrue($someFile->open());
  316. $this->assertWithinMargin($someFile->lastAccess(), time(), 2);
  317. $someFile->close();
  318. $someFile->delete();
  319. }
  320. /**
  321. * testLastChange method
  322. *
  323. * @return void
  324. */
  325. public function testLastChange() {
  326. $someFile = new File(TMP . 'some_file.txt', false);
  327. $this->assertFalse($someFile->lastChange());
  328. $this->assertTrue($someFile->open('r+'));
  329. $this->assertWithinMargin($someFile->lastChange(), time(), 2);
  330. $someFile->write('something');
  331. $this->assertWithinMargin($someFile->lastChange(), time(), 2);
  332. $someFile->close();
  333. $someFile->delete();
  334. }
  335. /**
  336. * testWrite method
  337. *
  338. * @return void
  339. */
  340. public function testWrite() {
  341. if (!$tmpFile = $this->_getTmpFile()) {
  342. return false;
  343. }
  344. if (file_exists($tmpFile)) {
  345. unlink($tmpFile);
  346. }
  347. $TmpFile = new File($tmpFile);
  348. $this->assertFalse(file_exists($tmpFile));
  349. $this->assertFalse(is_resource($TmpFile->handle));
  350. $testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
  351. foreach ($testData as $data) {
  352. $r = $TmpFile->write($data);
  353. $this->assertTrue($r);
  354. $this->assertTrue(file_exists($tmpFile));
  355. $this->assertEquals($data, file_get_contents($tmpFile));
  356. $this->assertTrue(is_resource($TmpFile->handle));
  357. $TmpFile->close();
  358. }
  359. unlink($tmpFile);
  360. }
  361. /**
  362. * testAppend method
  363. *
  364. * @return void
  365. */
  366. public function testAppend() {
  367. if (!$tmpFile = $this->_getTmpFile()) {
  368. return false;
  369. }
  370. if (file_exists($tmpFile)) {
  371. unlink($tmpFile);
  372. }
  373. $TmpFile = new File($tmpFile);
  374. $this->assertFalse(file_exists($tmpFile));
  375. $fragments = array('CakePHP\'s', ' test suite', ' was here ...');
  376. $data = null;
  377. $size = 0;
  378. foreach ($fragments as $fragment) {
  379. $r = $TmpFile->append($fragment);
  380. $this->assertTrue($r);
  381. $this->assertTrue(file_exists($tmpFile));
  382. $data = $data . $fragment;
  383. $this->assertEquals($data, file_get_contents($tmpFile));
  384. $newSize = $TmpFile->size();
  385. $this->assertTrue($newSize > $size);
  386. $size = $newSize;
  387. $TmpFile->close();
  388. }
  389. $TmpFile->append('');
  390. $this->assertEquals($data, file_get_contents($tmpFile));
  391. $TmpFile->close();
  392. }
  393. /**
  394. * testDelete method
  395. *
  396. * @return void
  397. */
  398. public function testDelete() {
  399. if (!$tmpFile = $this->_getTmpFile()) {
  400. return false;
  401. }
  402. if (!file_exists($tmpFile)) {
  403. touch($tmpFile);
  404. }
  405. $TmpFile = new File($tmpFile);
  406. $this->assertTrue(file_exists($tmpFile));
  407. $result = $TmpFile->delete();
  408. $this->assertTrue($result);
  409. $this->assertFalse(file_exists($tmpFile));
  410. $TmpFile = new File('/this/does/not/exist');
  411. $result = $TmpFile->delete();
  412. $this->assertFalse($result);
  413. }
  414. /**
  415. * Windows has issues unlinking files if there are
  416. * active filehandles open.
  417. *
  418. * @return void
  419. */
  420. public function testDeleteAfterRead() {
  421. if (!$tmpFile = $this->_getTmpFile()) {
  422. return false;
  423. }
  424. if (!file_exists($tmpFile)) {
  425. touch($tmpFile);
  426. }
  427. $File = new File($tmpFile);
  428. $File->read();
  429. $this->assertTrue($File->delete());
  430. }
  431. /**
  432. * testCopy method
  433. *
  434. * @return void
  435. */
  436. public function testCopy() {
  437. $dest = TMP . 'tests/cakephp.file.test.tmp';
  438. $file = __FILE__;
  439. $this->File = new File($file);
  440. $result = $this->File->copy($dest);
  441. $this->assertTrue($result);
  442. $result = $this->File->copy($dest, true);
  443. $this->assertTrue($result);
  444. $result = $this->File->copy($dest, false);
  445. $this->assertFalse($result);
  446. $this->File->close();
  447. unlink($dest);
  448. $TmpFile = new File('/this/does/not/exist');
  449. $result = $TmpFile->copy($dest);
  450. $this->assertFalse($result);
  451. $TmpFile->close();
  452. }
  453. /**
  454. * Test mime()
  455. *
  456. * @return void
  457. */
  458. public function testMime() {
  459. $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
  460. $path = TEST_APP . 'webroot/img/cake.power.gif';
  461. $file = new File($path);
  462. $expected = 'image/gif';
  463. if (function_exists('mime_content_type') && false === mime_content_type($file->pwd())) {
  464. $expected = false;
  465. }
  466. $this->assertEquals($expected, $file->mime());
  467. }
  468. /**
  469. * getTmpFile method
  470. *
  471. * @param boolean $paintSkip
  472. * @return void
  473. */
  474. protected function _getTmpFile($paintSkip = true) {
  475. $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
  476. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  477. return $tmpFile;
  478. };
  479. if ($paintSkip) {
  480. $trace = debug_backtrace();
  481. $caller = $trace[0]['function'];
  482. $shortPath = dirname($tmpFile);
  483. $message = sprintf('[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
  484. $this->markTestSkipped($message);
  485. }
  486. return false;
  487. }
  488. /**
  489. * testReplaceText method
  490. *
  491. * @return void
  492. */
  493. public function testReplaceText() {
  494. $TestFile = new File(TEST_APP . 'vendor/welcome.php');
  495. $TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp');
  496. // Copy the test file to the temporary location
  497. $TestFile->copy($TmpFile->path, true);
  498. // Replace the contents of the tempory file
  499. $result = $TmpFile->replaceText('welcome.php', 'welcome.tmp');
  500. $this->assertTrue($result);
  501. // Double check
  502. $expected = 'This is the welcome.tmp file in vendors directory';
  503. $contents = $TmpFile->read();
  504. $this->assertContains($expected, $contents);
  505. $search = array('This is the', 'welcome.php file', 'in tmp directory');
  506. $replace = array('This should be a', 'welcome.tmp file', 'in the Lib directory');
  507. // Replace the contents of the tempory file
  508. $result = $TmpFile->replaceText($search, $replace);
  509. $this->assertTrue($result);
  510. // Double check
  511. $expected = 'This should be a welcome.tmp file in vendors directory';
  512. $contents = $TmpFile->read();
  513. $this->assertContains($expected, $contents);
  514. $TmpFile->delete();
  515. }
  516. }