diff --git a/src/Shell/Task/AssetsTask.php b/src/Shell/Task/AssetsTask.php index 36979941928..9ce631b57ec 100644 --- a/src/Shell/Task/AssetsTask.php +++ b/src/Shell/Task/AssetsTask.php @@ -49,7 +49,7 @@ public function symlink($name = null) */ public function copy($name = null) { - $this->_process($this->_list($name), true); + $this->_process($this->_list($name), true, $this->param('overwrite')); } /** @@ -112,9 +112,10 @@ protected function _list($name = null) * * @param array $plugins List of plugins to process * @param bool $copy Force copy mode. Default false. + * @param bool $overwrite Overwrite existing files. * @return void */ - protected function _process($plugins, $copy = false) + protected function _process($plugins, $copy = false, $overwrite = false) { foreach ($plugins as $plugin => $config) { $this->out(); @@ -128,7 +129,7 @@ protected function _process($plugins, $copy = false) continue; } - if (file_exists($config['destDir'] . $config['link'])) { + if (file_exists($config['destDir'] . $config['link']) && !$overwrite) { $this->out( $config['destDir'] . $config['link'] . ' already exists', 1, @@ -241,6 +242,9 @@ public function getOptionParser() ])->addArgument('name', [ 'help' => 'A specific plugin you want to symlink assets for.', 'optional' => true, + ])->addOption('overwrite', [ + 'help' => 'Overwrite existing files.', + 'boolean' => true ]); return $parser; diff --git a/tests/TestCase/Shell/Task/AssetsTaskTest.php b/tests/TestCase/Shell/Task/AssetsTaskTest.php index 9317da10909..b006ef22546 100644 --- a/tests/TestCase/Shell/Task/AssetsTaskTest.php +++ b/tests/TestCase/Shell/Task/AssetsTaskTest.php @@ -204,4 +204,35 @@ public function testCopy() $folder = new Folder(WWW_ROOT . 'company'); $folder->delete(); } + + /** + * testCopyOverwrite + * + * @return void + */ + public function testCopyOverwrite() + { + Plugin::load('TestPlugin'); + + $this->Task->copy(); + + $path = WWW_ROOT . 'test_plugin'; + $dir = new \SplFileInfo($path); + $this->assertTrue($dir->isDir()); + $this->assertFileExists($path . DS . 'root.js'); + + unlink($path . DS . 'root.js'); + + $this->Task->copy(); + + $this->assertFileNotExists($path . DS . 'root.js'); + + $this->Task->params['overwrite'] = true; + $this->Task->copy(); + + $this->assertFileExists($path . DS . 'root.js'); + + $folder = new Folder($path); + $folder->delete(); + } }