From 9260a78ea3cdb8d0f786f262edd1bcfc27d6ed43 Mon Sep 17 00:00:00 2001 From: Phally Date: Sat, 26 Oct 2013 14:38:38 +0200 Subject: [PATCH] Fixes last items for #1665. - Changes double quotes to single quotes. - Documents and tests array-based string replace. - Rewrites test case. Rewrites the test case because the tests didn't work as well as they should be. The test file copied itself to the tmp directory, did some string replace stuff on it and then loaded the data to test its contents. However in those contents were also the expectations, so assertContains() would always see the string in the file... even if the replacing didn't work. Closes #1665. --- lib/Cake/Test/Case/Utility/FileTest.php | 20 +++++++++++++++++--- lib/Cake/Utility/File.php | 11 ++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/FileTest.php b/lib/Cake/Test/Case/Utility/FileTest.php index 4a96eef41..5d963e012 100644 --- a/lib/Cake/Test/Case/Utility/FileTest.php +++ b/lib/Cake/Test/Case/Utility/FileTest.php @@ -569,18 +569,32 @@ class FileTest extends CakeTestCase { * @return void */ public function testReplaceText() { - $TestFile = new File(__FILE__); + $TestFile = new File(dirname(__FILE__) . '/../../test_app/Vendor/welcome.php'); $TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp'); + // Copy the test file to the temporary location $TestFile->copy($TmpFile->path, true); // Replace the contents of the tempory file - $result = $TmpFile->replaceText("* testReplaceText method", "* testReplaceText method passed"); + $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("* testReplaceText method passed", $contents); + $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); + $this->assertTrue($result); + + // Double check + $expected = 'This should be a welcome.tmp file in vendors directory'; + $contents = $TmpFile->read(); + $this->assertContains($expected, $contents); $TmpFile->delete(); } diff --git a/lib/Cake/Utility/File.php b/lib/Cake/Utility/File.php index 1a7c0c815..4cdd226cb 100644 --- a/lib/Cake/Utility/File.php +++ b/lib/Cake/Utility/File.php @@ -588,13 +588,14 @@ class File { } /** - * Searches for a given text and replaces the text if found - * @param string $search - * @param string $replace + * Searches for a given text and replaces the text if found. + * + * @param string|array $search Text(s) to search for. + * @param string|array $replace Text(s) to replace with. * @return boolean Success */ public function replaceText($search, $replace) { - if (!$this->open("r+")) { + if (!$this->open('r+')) { return false; } @@ -604,7 +605,7 @@ class File { } } - $replaced = $this->write(str_replace($search, $replace, $this->read()), "w", true); + $replaced = $this->write(str_replace($search, $replace, $this->read()), 'w', true); if ($this->lock !== null) { flock($this->handle, LOCK_UN);