FileTest.php 14 KB

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