diff --git a/cake/libs/file.php b/cake/libs/file.php index 9a7edcc79..1378e2fbe 100644 --- a/cake/libs/file.php +++ b/cake/libs/file.php @@ -91,6 +91,7 @@ class File extends Object { if (!is_dir($path)) { $this->name = basename($path); } + if (!$this->exists()) { if ($create === true) { if ($this->safe($path) && $this->create() === false) { @@ -141,6 +142,7 @@ class File extends Object { return false; } } + $this->handle = fopen($this->pwd(), $mode); if (is_resource($this->handle)) { return true; @@ -207,9 +209,9 @@ class File extends Object { * @return string * @access public */ - function prepare($data) { + function prepare($data, $forceWindows = false) { $lineBreak = "\n"; - if (substr(PHP_OS,0,3) == "WIN") { + if (substr(PHP_OS,0,3) == "WIN" || $forceWindows === true) { $lineBreak = "\r\n"; } return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak)); @@ -236,7 +238,7 @@ class File extends Object { if (fwrite($this->handle, $data) !== false) { $success = true; } - if($this->lock !== null) { + if ($this->lock !== null) { flock($this->handle, LOCK_UN); } } diff --git a/cake/tests/cases/libs/file.test.php b/cake/tests/cases/libs/file.test.php index 35b71cabb..bcbd57d90 100644 --- a/cake/tests/cases/libs/file.test.php +++ b/cake/tests/cases/libs/file.test.php @@ -66,6 +66,10 @@ class FileTest extends UnitTestCase { $expecting = md5_file($file); $this->assertEqual($result, $expecting); + $result = $this->File->md5(true); + $expecting = md5_file($file); + $this->assertEqual($result, $expecting); + $result = $this->File->size(); $expecting = filesize($file); $this->assertEqual($result, $expecting); @@ -148,13 +152,6 @@ class FileTest extends UnitTestCase { $this->assertTrue($r); $this->assertFalse($handle === $this->File->handle); $this->assertTrue(is_resource($this->File->handle)); - - $InvalidFile =& new File('invalid-file.invalid-ext'); - $expecting =& new PatternExpectation('/could not open/i'); - $this->expectError($expecting); - $InvalidFile->open(); - - $this->File->close(); } function testClose() { @@ -175,6 +172,67 @@ class FileTest extends UnitTestCase { $this->assertTrue($File->exists()); } + function testOpeningNonExistantFileCreatesIt() { + $someFile =& new File('some_file.txt', false); + $this->assertTrue($someFile->open()); + $this->assertEqual($someFile->read(), ''); + $someFile->close(); + $someFile->delete(); + } + + function testPrepare() { + $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere"; + $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere"; + $this->assertIdentical(File::prepare($string), $expected); + + $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\nfor\r\n\r\n\r\n\r\n\r\nhere"; + $this->assertIdentical(File::prepare($string, true), $expected); + } + + function testReadable() { + $someFile =& new File('some_file.txt', false); + $this->assertTrue($someFile->open()); + $this->assertTrue($someFile->readable()); + $someFile->close(); + $someFile->delete(); + } + + function testWritable() { + $someFile =& new File('some_file.txt', false); + $this->assertTrue($someFile->open()); + $this->assertTrue($someFile->writable()); + $someFile->close(); + $someFile->delete(); + } + + function testExecutable() { + $someFile =& new File('some_file.txt', false); + $this->assertTrue($someFile->open()); + $this->assertFalse($someFile->executable()); + $someFile->close(); + $someFile->delete(); + } + + function testLastAccess() { + $someFile =& new File('some_file.txt', false); + $this->assertFalse($someFile->lastAccess()); + $this->assertTrue($someFile->open()); + $this->assertEqual($someFile->lastAccess(), time()); + $someFile->close(); + $someFile->delete(); + } + + function testLastChange() { + $someFile =& new File('some_file.txt', false); + $this->assertFalse($someFile->lastChange()); + $this->assertTrue($someFile->open('r+')); + $this->assertEqual($someFile->lastChange(), time()); + $someFile->write('something'); + $this->assertEqual($someFile->lastChange(), time()); + $someFile->close(); + $someFile->delete(); + } + function testWrite() { if (!$tmpFile = $this->_getTmpFile()) { return false; @@ -263,5 +321,65 @@ class FileTest extends UnitTestCase { } return false; } + + function testGetFullPathIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getFullPath(); + $this->assertError(); + } + + function testGetNameIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getName(); + $this->assertError(); + } + + function testFilenameIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->filename(); + $this->assertError(); + } + + function testGetExtIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getExt(); + $this->assertError(); + } + + function testGetMd5IsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getMd5(); + $this->assertError(); + } + + function testGetSizeIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getSize(); + $this->assertError(); + } + + function testGetOwnerIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getOwner(); + $this->assertError(); + } + + function testGetGroupIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getGroup(); + $this->assertError(); + } + + function testGetChmodIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getChmod(); + $this->assertError(); + } + + function testGetFolderIsDeprecated() { + $someFile =& new File('some_file.txt', false); + $someFile->getFolder(); + $this->assertError(); + } } ?> \ No newline at end of file