From cbc663739b5a3e2f062e1e87d96a073deff84767 Mon Sep 17 00:00:00 2001 From: Bunyamin Inan Date: Wed, 15 Apr 2020 18:21:57 +0200 Subject: [PATCH 1/6] Test case created for the github issue #27866 --- .../Filesystem/Test/Unit/Io/FileTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php diff --git a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php new file mode 100644 index 0000000000000..ac8028db383b9 --- /dev/null +++ b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php @@ -0,0 +1,45 @@ +getTmpDir(); + \file_put_contents("{$tmpDir}/{$sourceFileName}", $content); + + $file = new File(); + $targetFileName = "target-file.txt"; + $targetFileHandle = \fopen("{$tmpDir}/{$targetFileName}" , 'w'); + $file->cd($tmpDir); + $result = $file->read($sourceFileName, $targetFileHandle); + + $targetContent = file_get_contents("{$tmpDir}/{$targetFileName}"); + $this->assertEquals($content, $targetContent); + } +} From d177789b991704da08996f42ef00ff886baa982a Mon Sep 17 00:00:00 2001 From: Bunyamin Inan Date: Wed, 15 Apr 2020 18:32:03 +0200 Subject: [PATCH 2/6] When the second argument is a resource, use fwrite() to transfer the content --- lib/internal/Magento/Framework/Filesystem/Io/File.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Filesystem/Io/File.php b/lib/internal/Magento/Framework/Filesystem/Io/File.php index 2e81325c8ec57..cb9ca843d30f5 100644 --- a/lib/internal/Magento/Framework/Filesystem/Io/File.php +++ b/lib/internal/Magento/Framework/Filesystem/Io/File.php @@ -441,10 +441,13 @@ public function cd($dir) public function read($filename, $dest = null) { $this->_cwd(); - if ($dest !== null) { - $result = @copy($filename, $dest); - } else { + if ($dest === null) { $result = @file_get_contents($filename); + } elseif (is_resource($dest)) { + $result = @file_get_contents($filename); + fwrite($dest, $result); + } elseif (is_string($dest)) { + $result = @copy($filename, $dest); } $this->_iwd(); From 40243209277f540cb376e7d952a98a967c00546c Mon Sep 17 00:00:00 2001 From: Bunyamin Date: Wed, 26 Aug 2020 08:50:41 +0200 Subject: [PATCH 3/6] Redundant method removed. Test name converted into camelCase --- .../Framework/Filesystem/Test/Unit/Io/FileTest.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php index ac8028db383b9..87ced3674f70a 100644 --- a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php +++ b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php @@ -7,11 +7,6 @@ class FileTest extends TestCase { - protected function setUp() - { - - } - private function getTmpDir() { $tmpDir = '/tmp/magento-' . \microtime(true); @@ -23,10 +18,9 @@ private function getTmpDir() /** * To cover the issue on GitHub: #27866 - * @test * @throws LocalizedException */ - public function read_should_copy_the_source_file_to_the_given_file_resource() + public function testReadShouldCopyTheSourceFileToTheGivenGileResource() { $content = \mt_rand(); $sourceFileName = "source-file.txt"; From ed111ae759aa7b924846f9ded51030a25be0082f Mon Sep 17 00:00:00 2001 From: Bunyamin Date: Wed, 26 Aug 2020 10:03:29 +0200 Subject: [PATCH 4/6] Copyright added to the new file --- .../Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php index 87ced3674f70a..28a7e76305ec5 100644 --- a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php +++ b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php @@ -1,4 +1,8 @@ Date: Wed, 26 Aug 2020 20:04:24 +0200 Subject: [PATCH 5/6] Fix static errors --- .../Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php index 28a7e76305ec5..093a17fa29c5d 100644 --- a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php +++ b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php @@ -24,7 +24,7 @@ private function getTmpDir() * To cover the issue on GitHub: #27866 * @throws LocalizedException */ - public function testReadShouldCopyTheSourceFileToTheGivenGileResource() + public function testReadShouldCopyTheSourceFileToTheGivenFileResource() { $content = \mt_rand(); $sourceFileName = "source-file.txt"; @@ -33,9 +33,9 @@ public function testReadShouldCopyTheSourceFileToTheGivenGileResource() $file = new File(); $targetFileName = "target-file.txt"; - $targetFileHandle = \fopen("{$tmpDir}/{$targetFileName}" , 'w'); + $targetFileHandle = \fopen("{$tmpDir}/{$targetFileName}", 'w'); $file->cd($tmpDir); - $result = $file->read($sourceFileName, $targetFileHandle); + $file->read($sourceFileName, $targetFileHandle); $targetContent = file_get_contents("{$tmpDir}/{$targetFileName}"); $this->assertEquals($content, $targetContent); From 2b2aceed74fceefbcafdd90dda3a8438a00328eb Mon Sep 17 00:00:00 2001 From: Bunyamin Date: Wed, 2 Sep 2020 19:23:39 +0200 Subject: [PATCH 6/6] Fixing static tests; --- lib/internal/Magento/Framework/Filesystem/Io/File.php | 2 ++ .../Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Filesystem/Io/File.php b/lib/internal/Magento/Framework/Filesystem/Io/File.php index cb9ca843d30f5..70608194b726b 100644 --- a/lib/internal/Magento/Framework/Filesystem/Io/File.php +++ b/lib/internal/Magento/Framework/Filesystem/Io/File.php @@ -440,6 +440,8 @@ public function cd($dir) */ public function read($filename, $dest = null) { + $result = false; + $this->_cwd(); if ($dest === null) { $result = @file_get_contents($filename); diff --git a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php index 093a17fa29c5d..0ec3f34a0972d 100644 --- a/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php +++ b/lib/internal/Magento/Framework/Filesystem/Test/Unit/Io/FileTest.php @@ -26,7 +26,7 @@ private function getTmpDir() */ public function testReadShouldCopyTheSourceFileToTheGivenFileResource() { - $content = \mt_rand(); + $content = \random_int(0, 1000); $sourceFileName = "source-file.txt"; $tmpDir = $this->getTmpDir(); \file_put_contents("{$tmpDir}/{$sourceFileName}", $content);