Browse Source

Merge pull request #11043 from Ni-san/11040_parameter_recursive_for_copy_directory

fixes #11040: make copyDirectory method work correctly with parameter 'recursive'
batch-query-test
Dmitry Naumenko 9 years ago
parent
commit
4353825180
  1. 1
      framework/CHANGELOG.md
  2. 5
      framework/helpers/BaseFileHelper.php
  3. 79
      tests/framework/helpers/FileHelperTest.php

1
framework/CHANGELOG.md

@ -22,6 +22,7 @@ Yii Framework 2 Change Log
- Enh #10764: `yii\helpers\Html::tag()` and `::beginTag()` return content without any HTML when the `$tag` attribute is `false` or `null` (pana1990) - Enh #10764: `yii\helpers\Html::tag()` and `::beginTag()` return content without any HTML when the `$tag` attribute is `false` or `null` (pana1990)
- Enh #10941: Added `yii\helpers\ArrayHelper::isTraversable`, added support for traversable selections for dropdownList, radioList and checkboxList in `yii\helpers\Html` (sammousa) - Enh #10941: Added `yii\helpers\ArrayHelper::isTraversable`, added support for traversable selections for dropdownList, radioList and checkboxList in `yii\helpers\Html` (sammousa)
- Eng #10976: `Inflector::transliterate()` now uses `strtr` instead of `str_replace` (DrDeath72) - Eng #10976: `Inflector::transliterate()` now uses `strtr` instead of `str_replace` (DrDeath72)
- Bug #11040: Check parameter 'recursive' and disable recursive copying with option 'recursive' => false in method BaseFileHelper::copyDirectory (Ni-san)
- Chg: HTMLPurifier dependency updated to `~4.6` (samdark) - Chg: HTMLPurifier dependency updated to `~4.6` (samdark)
2.0.7 February 14, 2016 2.0.7 February 14, 2016

5
framework/helpers/BaseFileHelper.php

@ -282,7 +282,10 @@ class BaseFileHelper
@chmod($to, $options['fileMode']); @chmod($to, $options['fileMode']);
} }
} else { } else {
static::copyDirectory($from, $to, $options); // recursive copy, defaults to true
if (!isset($options['recursive']) || $options['recursive']) {
static::copyDirectory($from, $to, $options);
}
} }
if (isset($options['afterCopy'])) { if (isset($options['afterCopy'])) {
call_user_func($options['afterCopy'], $from, $to); call_user_func($options['afterCopy'], $from, $to);

79
tests/framework/helpers/FileHelperTest.php

@ -138,6 +138,85 @@ class FileHelperTest extends TestCase
} }
} }
public function testCopyDirectoryRecursive()
{
$srcDirName = 'test_src_dir_rec';
$structure = [
'directory1' => [
'file1.txt' => 'file 1 content',
'file2.txt' => 'file 2 content',
],
'directory2' => [
'file3.txt' => 'file 3 content',
'file4.txt' => 'file 4 content',
],
'file5.txt' => 'file 5 content',
];
$this->createFileStructure([
$srcDirName => $structure
]);
$basePath = $this->testFilePath;
$srcDirName = $basePath . DIRECTORY_SEPARATOR . $srcDirName;
$dstDirName = $basePath . DIRECTORY_SEPARATOR . 'test_dst_dir';
FileHelper::copyDirectory($srcDirName, $dstDirName);
$this->assertFileExists($dstDirName, 'Destination directory does not exist!');
$checker = function ($structure, $dstDirName) use (&$checker) {
foreach ($structure as $name => $content) {
if (is_array($content)) {
$checker($content, $dstDirName . DIRECTORY_SEPARATOR . $name);
} else {
$fileName = $dstDirName . DIRECTORY_SEPARATOR . $name;
$this->assertFileExists($fileName);
$this->assertEquals($content, file_get_contents($fileName), 'Incorrect file content!');
}
}
};
$checker($structure, $dstDirName);
}
public function testCopyDirectoryNotRecursive()
{
$srcDirName = 'test_src_dir_not_rec';
$structure = [
'directory1' => [
'file1.txt' => 'file 1 content',
'file2.txt' => 'file 2 content',
],
'directory2' => [
'file3.txt' => 'file 3 content',
'file4.txt' => 'file 4 content',
],
'file5.txt' => 'file 5 content',
];
$this->createFileStructure([
$srcDirName => $structure
]);
$basePath = $this->testFilePath;
$srcDirName = $basePath . DIRECTORY_SEPARATOR . $srcDirName;
$dstDirName = $basePath . DIRECTORY_SEPARATOR . 'test_dst_dir';
FileHelper::copyDirectory($srcDirName, $dstDirName, ['recursive' => false]);
$this->assertFileExists($dstDirName, 'Destination directory does not exist!');
foreach ($structure as $name => $content) {
$fileName = $dstDirName . DIRECTORY_SEPARATOR . $name;
if (is_array($content)) {
$this->assertFileNotExists($fileName);
} else {
$this->assertFileExists($fileName);
$this->assertEquals($content, file_get_contents($fileName), 'Incorrect file content!');
}
}
}
/** /**
* @depends testCopyDirectory * @depends testCopyDirectory
*/ */

Loading…
Cancel
Save