2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* FileTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('File', 'Utility');
|
2011-04-17 16:03:43 +00:00
|
|
|
App::uses('Folder', 'Utility');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* FileTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class FileTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* File property
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var mixed null
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $File = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
/**
|
|
|
|
* setup the test case
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-05-31 02:46:42 +00:00
|
|
|
parent::setUp();
|
|
|
|
$file = __FILE__;
|
|
|
|
$this->File = new File($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* tearDown method
|
2010-05-31 02:46:42 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-04 21:27:51 +00:00
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2010-05-31 02:46:42 +00:00
|
|
|
$this->File->close();
|
|
|
|
unset($this->File);
|
2012-04-24 13:01:20 +00:00
|
|
|
|
|
|
|
$Folder = new Folder();
|
|
|
|
$Folder->delete(TMP . 'tests' . DS . 'permissions');
|
2010-05-31 02:46:42 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testBasic method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBasic() {
|
2012-09-01 11:40:05 +00:00
|
|
|
$file = CAKE . DS . 'LICENSE.txt';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-09-01 11:40:05 +00:00
|
|
|
$this->File = new File($file, false);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->name;
|
2012-09-01 11:40:05 +00:00
|
|
|
$expecting = basename($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->info();
|
2008-10-15 12:42:03 +00:00
|
|
|
$expecting = array(
|
2012-09-01 11:40:05 +00:00
|
|
|
'dirname' => dirname($file),
|
|
|
|
'basename' => basename($file),
|
|
|
|
'extension' => 'txt',
|
|
|
|
'filename' => 'LICENSE',
|
2011-12-19 04:26:57 +00:00
|
|
|
'filesize' => filesize($file),
|
2012-09-01 11:40:05 +00:00
|
|
|
'mime' => 'text/plain'
|
2008-10-15 12:42:03 +00:00
|
|
|
);
|
2012-09-01 11:40:05 +00:00
|
|
|
if (
|
|
|
|
!function_exists('finfo_open') &&
|
|
|
|
(!function_exists('mime_content_type') ||
|
|
|
|
function_exists('mime_content_type') &&
|
|
|
|
mime_content_type($this->File->pwd()) === false)
|
|
|
|
) {
|
2012-03-19 08:51:40 +00:00
|
|
|
$expecting['mime'] = false;
|
|
|
|
}
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->ext();
|
2012-09-01 11:40:05 +00:00
|
|
|
$expecting = 'txt';
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->name();
|
2012-09-01 11:40:05 +00:00
|
|
|
$expecting = 'LICENSE';
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->md5();
|
|
|
|
$expecting = md5_file($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->md5(true);
|
|
|
|
$expecting = md5_file($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->size();
|
|
|
|
$expecting = filesize($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->owner();
|
|
|
|
$expecting = fileowner($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->group();
|
|
|
|
$expecting = filegroup($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->Folder();
|
2011-11-16 17:56:34 +00:00
|
|
|
$this->assertInstanceOf('Folder', $result);
|
2012-04-24 13:01:20 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-04-24 13:01:20 +00:00
|
|
|
/**
|
|
|
|
* testPermission method
|
|
|
|
*/
|
|
|
|
public function testPermission() {
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
|
|
|
|
|
2012-04-24 13:01:20 +00:00
|
|
|
$dir = TMP . 'tests' . DS . 'permissions' . DS;
|
|
|
|
$old = umask();
|
|
|
|
|
|
|
|
umask(0002);
|
|
|
|
$file = $dir . 'permission_' . uniqid();
|
|
|
|
$expecting = decoct(0664 & ~umask());
|
|
|
|
$File = new File($file, true);
|
|
|
|
$result = $File->perms();
|
|
|
|
$this->assertEquals($expecting, $result);
|
|
|
|
$File->delete();
|
|
|
|
|
|
|
|
umask(0022);
|
|
|
|
$file = $dir . 'permission_' . uniqid();
|
2011-10-01 02:10:16 +00:00
|
|
|
$expecting = decoct(0644 & ~umask());
|
2012-04-24 13:01:20 +00:00
|
|
|
$File = new File($file, true);
|
|
|
|
$result = $File->perms();
|
|
|
|
$this->assertEquals($expecting, $result);
|
|
|
|
$File->delete();
|
|
|
|
|
|
|
|
umask(0422);
|
|
|
|
$file = $dir . 'permission_' . uniqid();
|
|
|
|
$expecting = decoct(0244 & ~umask());
|
|
|
|
$File = new File($file, true);
|
|
|
|
$result = $File->perms();
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2012-04-24 13:01:20 +00:00
|
|
|
$File->delete();
|
|
|
|
|
|
|
|
umask(0444);
|
|
|
|
$file = $dir . 'permission_' . uniqid();
|
|
|
|
$expecting = decoct(0222 & ~umask());
|
|
|
|
$File = new File($file, true);
|
|
|
|
$result = $File->perms();
|
|
|
|
$this->assertEquals($expecting, $result);
|
|
|
|
$File->delete();
|
|
|
|
|
|
|
|
umask($old);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRead method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRead() {
|
2010-05-29 16:47:31 +00:00
|
|
|
$file = __FILE__;
|
2010-11-13 04:05:44 +00:00
|
|
|
$this->File = new File($file);
|
2010-05-29 16:47:31 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->File->read();
|
|
|
|
$expecting = file_get_contents(__FILE__);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(!is_resource($this->File->handle));
|
|
|
|
|
2008-10-15 12:42:03 +00:00
|
|
|
$this->File->lock = true;
|
|
|
|
$result = $this->File->read();
|
|
|
|
$expecting = file_get_contents(__FILE__);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(trim($expecting), $result);
|
2008-10-15 12:42:03 +00:00
|
|
|
$this->File->lock = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = $expecting;
|
|
|
|
$expecting = substr($data, 0, 3);
|
|
|
|
$result = $this->File->read(3);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
|
|
|
|
$expecting = substr($data, 3, 3);
|
|
|
|
$result = $this->File->read(3);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testOffset method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOffset() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->File->close();
|
|
|
|
|
|
|
|
$result = $this->File->offset();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
|
|
|
$success = $this->File->offset(0);
|
|
|
|
$this->assertTrue($success);
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
|
|
|
|
$result = $this->File->offset();
|
2013-08-16 18:55:17 +00:00
|
|
|
$expected = 0;
|
|
|
|
$this->assertSame($expected, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$data = file_get_contents(__FILE__);
|
|
|
|
$success = $this->File->offset(5);
|
2013-08-16 18:55:17 +00:00
|
|
|
$expected = substr($data, 5, 3);
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->File->read(3);
|
|
|
|
$this->assertTrue($success);
|
2013-08-16 18:55:17 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->offset();
|
2013-08-16 18:55:17 +00:00
|
|
|
$expected = 5 + 3;
|
|
|
|
$this->assertSame($expected, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testOpen method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOpen() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->File->handle = null;
|
|
|
|
|
|
|
|
$r = $this->File->open();
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
$this->assertTrue($r);
|
|
|
|
|
|
|
|
$handle = $this->File->handle;
|
|
|
|
$r = $this->File->open();
|
|
|
|
$this->assertTrue($r);
|
|
|
|
$this->assertTrue($handle === $this->File->handle);
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
|
|
|
|
$r = $this->File->open('r', true);
|
|
|
|
$this->assertTrue($r);
|
|
|
|
$this->assertFalse($handle === $this->File->handle);
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testClose method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testClose() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->File->handle = null;
|
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
|
|
|
$this->assertTrue($this->File->close());
|
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
|
|
|
|
|
|
|
$this->File->handle = fopen(__FILE__, 'r');
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
$this->assertTrue($this->File->close());
|
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testCreate method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreate() {
|
2012-03-14 02:59:20 +00:00
|
|
|
$tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
|
2010-05-31 02:46:42 +00:00
|
|
|
$File = new File($tmpFile, true, 0777);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($File->exists());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* testOpeningNonExistentFileCreatesIt method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-23 23:29:53 +00:00
|
|
|
public function testOpeningNonExistentFileCreatesIt() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($someFile->open());
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals('', $someFile->read());
|
2008-05-30 11:40:08 +00:00
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testPrepare method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPrepare() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
|
2013-02-12 02:38:08 +00:00
|
|
|
if (DS === '\\') {
|
2008-11-08 03:47:09 +00:00
|
|
|
$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
|
|
|
|
$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
|
|
|
|
} else {
|
|
|
|
$expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
|
|
|
|
}
|
2013-09-18 22:17:21 +00:00
|
|
|
$this->assertSame($expected, File::prepare($string));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-15 12:42:03 +00:00
|
|
|
$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
|
|
|
|
$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
|
2013-09-18 22:17:21 +00:00
|
|
|
$this->assertSame($expected, File::prepare($string, true));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testReadable method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadable() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($someFile->open());
|
|
|
|
$this->assertTrue($someFile->readable());
|
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testWritable method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWritable() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($someFile->open());
|
|
|
|
$this->assertTrue($someFile->writable());
|
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testExecutable method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testExecutable() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($someFile->open());
|
|
|
|
$this->assertFalse($someFile->executable());
|
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testLastAccess method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLastAccess() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($someFile->lastAccess());
|
|
|
|
$this->assertTrue($someFile->open());
|
2012-09-21 01:07:14 +00:00
|
|
|
$this->assertWithinMargin($someFile->lastAccess(), time(), 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testLastChange method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLastChange() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($someFile->lastChange());
|
|
|
|
$this->assertTrue($someFile->open('r+'));
|
2012-09-21 01:07:14 +00:00
|
|
|
$this->assertWithinMargin($someFile->lastChange(), time(), 2);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$someFile->write('something');
|
2012-09-21 01:07:14 +00:00
|
|
|
$this->assertWithinMargin($someFile->lastChange(), time(), 2);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testWrite method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWrite() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
2013-10-25 17:29:33 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (file_exists($tmpFile)) {
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File($tmpFile);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse(file_exists($tmpFile));
|
|
|
|
$this->assertFalse(is_resource($TmpFile->handle));
|
|
|
|
|
|
|
|
$testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
|
|
|
|
foreach ($testData as $data) {
|
|
|
|
$r = $TmpFile->write($data);
|
|
|
|
$this->assertTrue($r);
|
|
|
|
$this->assertTrue(file_exists($tmpFile));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, file_get_contents($tmpFile));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(is_resource($TmpFile->handle));
|
|
|
|
$TmpFile->close();
|
|
|
|
|
|
|
|
}
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testAppend method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAppend() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
2013-10-25 17:29:33 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (file_exists($tmpFile)) {
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File($tmpFile);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse(file_exists($tmpFile));
|
|
|
|
|
2013-04-13 14:15:20 +00:00
|
|
|
$fragments = array('CakePHP\'s', ' test suite', ' was here ...');
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = null;
|
2013-04-13 14:15:20 +00:00
|
|
|
$size = 0;
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($fragments as $fragment) {
|
|
|
|
$r = $TmpFile->append($fragment);
|
|
|
|
$this->assertTrue($r);
|
|
|
|
$this->assertTrue(file_exists($tmpFile));
|
2011-12-16 06:52:07 +00:00
|
|
|
$data = $data . $fragment;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, file_get_contents($tmpFile));
|
2013-04-13 14:15:20 +00:00
|
|
|
$newSize = $TmpFile->size();
|
|
|
|
$this->assertTrue($newSize > $size);
|
|
|
|
$size = $newSize;
|
2008-05-30 11:40:08 +00:00
|
|
|
$TmpFile->close();
|
|
|
|
}
|
2013-04-13 14:15:20 +00:00
|
|
|
|
|
|
|
$TmpFile->append('');
|
|
|
|
$this->assertEquals($data, file_get_contents($tmpFile));
|
|
|
|
$TmpFile->close();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDelete method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDelete() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
2011-08-29 02:19:53 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!file_exists($tmpFile)) {
|
|
|
|
touch($tmpFile);
|
|
|
|
}
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File($tmpFile);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(file_exists($tmpFile));
|
|
|
|
$result = $TmpFile->delete();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertFalse(file_exists($tmpFile));
|
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File('/this/does/not/exist');
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $TmpFile->delete();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-08-29 02:19:53 +00:00
|
|
|
/**
|
|
|
|
* Windows has issues unlinking files if there are
|
|
|
|
* active filehandles open.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
public function testDeleteAfterRead() {
|
2011-08-29 02:19:53 +00:00
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!file_exists($tmpFile)) {
|
|
|
|
touch($tmpFile);
|
|
|
|
}
|
2012-02-23 16:27:40 +00:00
|
|
|
$File = new File($tmpFile);
|
2011-10-20 23:10:09 +00:00
|
|
|
$File->read();
|
|
|
|
$this->assertTrue($File->delete());
|
2011-08-29 02:19:53 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 03:27:31 +00:00
|
|
|
/**
|
|
|
|
* testCopy method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCopy() {
|
2009-10-09 03:27:31 +00:00
|
|
|
$dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
|
|
|
|
$file = __FILE__;
|
2010-05-31 02:46:42 +00:00
|
|
|
$this->File = new File($file);
|
2009-10-09 03:27:31 +00:00
|
|
|
$result = $this->File->copy($dest);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $this->File->copy($dest, true);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $this->File->copy($dest, false);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$this->File->close();
|
|
|
|
unlink($dest);
|
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File('/this/does/not/exist');
|
2009-10-09 03:27:31 +00:00
|
|
|
$result = $TmpFile->copy($dest);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$TmpFile->close();
|
|
|
|
}
|
|
|
|
|
2011-12-19 03:55:37 +00:00
|
|
|
/**
|
|
|
|
* Test mime()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testMime() {
|
2012-03-19 08:51:40 +00:00
|
|
|
$this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
|
2011-12-19 03:55:37 +00:00
|
|
|
$path = CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
|
|
|
|
$file = new File($path);
|
2012-03-24 20:32:31 +00:00
|
|
|
$expected = 'image/gif';
|
|
|
|
if (function_exists('mime_content_type') && false === mime_content_type($file->pwd())) {
|
|
|
|
$expected = false;
|
|
|
|
}
|
|
|
|
$this->assertEquals($expected, $file->mime());
|
2011-12-19 03:55:37 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* getTmpFile method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2013-07-05 14:17:23 +00:00
|
|
|
* @param boolean $paintSkip
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
protected function _getTmpFile($paintSkip = true) {
|
2010-05-29 16:47:31 +00:00
|
|
|
$tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
|
|
|
|
return $tmpFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($paintSkip) {
|
2011-06-21 21:20:04 +00:00
|
|
|
$trace = debug_backtrace();
|
|
|
|
$caller = $trace[0]['function'];
|
|
|
|
$shortPath = dirname($tmpFile);
|
|
|
|
|
|
|
|
$message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
|
|
|
|
$this->markTestSkipped($message);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-21 06:24:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* testReplaceText method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReplaceText() {
|
2013-10-26 12:38:38 +00:00
|
|
|
$TestFile = new File(dirname(__FILE__) . '/../../test_app/Vendor/welcome.php');
|
2013-09-21 06:24:14 +00:00
|
|
|
$TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp');
|
2013-10-26 12:38:38 +00:00
|
|
|
|
2013-09-21 06:24:14 +00:00
|
|
|
// Copy the test file to the temporary location
|
|
|
|
$TestFile->copy($TmpFile->path, true);
|
|
|
|
|
|
|
|
// Replace the contents of the tempory file
|
2013-10-26 12:38:38 +00:00
|
|
|
$result = $TmpFile->replaceText('welcome.php', 'welcome.tmp');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
// Double check
|
|
|
|
$expected = 'This is the welcome.tmp file in vendors directory';
|
|
|
|
$contents = $TmpFile->read();
|
|
|
|
$this->assertContains($expected, $contents);
|
|
|
|
|
|
|
|
$search = array('This is the', 'welcome.php file', 'in tmp directory');
|
|
|
|
$replace = array('This should be a', 'welcome.tmp file', 'in the Lib directory');
|
|
|
|
|
|
|
|
// Replace the contents of the tempory file
|
|
|
|
$result = $TmpFile->replaceText($search, $replace);
|
2013-09-21 06:24:14 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
// Double check
|
2013-10-26 12:38:38 +00:00
|
|
|
$expected = 'This should be a welcome.tmp file in vendors directory';
|
2013-09-21 06:24:14 +00:00
|
|
|
$contents = $TmpFile->read();
|
2013-10-26 12:38:38 +00:00
|
|
|
$this->assertContains($expected, $contents);
|
2013-09-21 06:24:14 +00:00
|
|
|
|
|
|
|
$TmpFile->delete();
|
|
|
|
}
|
2011-04-17 16:03:43 +00:00
|
|
|
}
|