diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 2047cca0c3..051e5eda19 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -25,6 +25,7 @@ Yii Framework 2 Change Log - Bug #3311: Fixed the bug that `yii\di\Container::has()` did not return correct value (mgrechanik, qiangxue) - Bug #3327: Fixed "Unable to find debug data" when logging objects with circular references (jarekkozak, samdark) - Bug #3368: Fix for comparing numeric attributes in JavaScript (technixp) +- Bug #3393: Fix `yii\helpers\FileHelper::copyDirectory()` pattern not working (klimov-paul) - Bug #3431: Allow using extended ErrorHandler class from the app namespace (cebe) - Bug #3436: Fixed the issue that `ServiceLocator` still returns the old component after calling `set()` with a new definition (qiangxue) - Bug #3458: Fixed the bug that the image rendered by `CaptchaAction` was using a wrong content type (MDMunir, qiangxue) diff --git a/framework/helpers/BaseFileHelper.php b/framework/helpers/BaseFileHelper.php index edf81a1df0..731341ff08 100644 --- a/framework/helpers/BaseFileHelper.php +++ b/framework/helpers/BaseFileHelper.php @@ -209,6 +209,9 @@ class BaseFileHelper if ($handle === false) { throw new InvalidParamException('Unable to open directory: ' . $src); } + if (!isset($options['basePath'])) { + $options['basePath'] = realpath($src); + } while (($file = readdir($handle)) !== false) { if ($file === '.' || $file === '..') { continue; diff --git a/tests/unit/framework/helpers/FileHelperTest.php b/tests/unit/framework/helpers/FileHelperTest.php index 3eb8f35e1c..d013133eb3 100644 --- a/tests/unit/framework/helpers/FileHelperTest.php +++ b/tests/unit/framework/helpers/FileHelperTest.php @@ -407,4 +407,42 @@ class FileHelperTest extends TestCase FileHelper::localize($viewFile, $currentLanguage, $sourceLanguage) ); } + + /** + * @see https://github.com/yiisoft/yii2/issues/3393 + * + * @depends testCopyDirectory + * @depends testFindFiles + */ + public function testCopyDirectoryExclude() + { + $srcDirName = 'test_src_dir'; + $textFiles = [ + 'file1.txt' => 'text file 1 content', + 'file2.txt' => 'text file 2 content', + ]; + $dataFiles = [ + 'file1.dat' => 'data file 1 content', + 'file2.dat' => 'data file 2 content', + ]; + $this->createFileStructure([ + $srcDirName => array_merge($textFiles, $dataFiles) + ]); + + $basePath = $this->testFilePath; + $srcDirName = $basePath . DIRECTORY_SEPARATOR . $srcDirName; + $dstDirName = $basePath . DIRECTORY_SEPARATOR . 'test_dst_dir'; + + FileHelper::copyDirectory($srcDirName, $dstDirName, ['only' => ['*.dat']]); + + $this->assertFileExists($dstDirName, 'Destination directory does not exist!'); + $copiedFiles = FileHelper::findFiles($dstDirName); + $this->assertCount(2, $copiedFiles, 'wrong files count copied'); + + foreach ($dataFiles as $name => $content) { + $fileName = $dstDirName . DIRECTORY_SEPARATOR . $name; + $this->assertFileExists($fileName); + $this->assertEquals($content, file_get_contents($fileName), 'Incorrect file content!'); + } + } }