From 33d245c89dbceb4f5a6b3d11b745edd44ed44e45 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 9 Mar 2017 21:28:16 -0500 Subject: [PATCH] CRM-20029 - Copy custom files when duplicating an event --- CRM/Core/BAO/CustomField.php | 7 ++++--- CRM/Core/BAO/File.php | 2 +- CRM/Event/BAO/Event.php | 2 +- CRM/Utils/File.php | 26 ++++++++++++++++++++++---- tests/phpunit/CRM/Utils/FileTest.php | 22 ++++++++++++++++++++++ 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index b5dff555e5..3e74216e11 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1651,9 +1651,6 @@ SELECT id $config = CRM_Core_Config::singleton(); - $fName = $value['name']; - $mimeType = $value['type']; - // If we are already passing the file id as a value then retrieve and set the file data if (CRM_Utils_Rule::integer($value)) { $fileDAO = new CRM_Core_DAO_File(); @@ -1665,6 +1662,10 @@ SELECT id $mimeType = $fileDAO->mime_type; } } + else { + $fName = $value['name']; + $mimeType = $value['type']; + } $filename = pathinfo($fName, PATHINFO_BASENAME); diff --git a/CRM/Core/BAO/File.php b/CRM/Core/BAO/File.php index b0e5a65a5a..2eb059de2c 100644 --- a/CRM/Core/BAO/File.php +++ b/CRM/Core/BAO/File.php @@ -666,7 +666,7 @@ AND CEF.entity_id = %2"; CRM_Core_Error::fatal('Request signature is invalid'); } - CRM_Core_BAO_File::deleteEntityFile($params['entityTable'], $params['entityID'], NULL, $params['fileID']); + self::deleteEntityFile($params['entityTable'], $params['entityID'], NULL, $params['fileID']); } diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index c1e9c35a28..cd9d8294db 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -1038,7 +1038,7 @@ WHERE civicrm_event.is_active = 1 if (in_array($field, $htmlType)) { $fileValues = CRM_Core_BAO_File::path($value, $oldEventID); $customParams["custom_{$field}_-1"] = array( - 'name' => $fileValues[0], + 'name' => CRM_Utils_File::duplicate($fileValues[0]), 'type' => $fileValues[1], ); } diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index c5906f4b90..1ca478649f 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -293,16 +293,19 @@ class CRM_Utils_File { * The directory where the file should be saved. * @param string $contents * Optional: the contents of the file. + * @param string $fileName * * @return string * The filename saved, or FALSE on failure. */ - public static function createFakeFile($dir, $contents = 'delete me') { + public static function createFakeFile($dir, $contents = 'delete me', $fileName = NULL) { $dir = self::addTrailingSlash($dir); - $file = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC); - $success = file_put_contents($dir . $file, $contents); + if (!$fileName) { + $fileName = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC); + } + $success = file_put_contents($dir . $fileName, $contents); - return ($success === FALSE) ? FALSE : $file; + return ($success === FALSE) ? FALSE : $fileName; } /** @@ -461,6 +464,21 @@ class CRM_Utils_File { } } + /** + * Copies a file + * + * @param $filePath + * @return mixed + */ + public static function duplicate($filePath) { + $oldName = pathinfo($filePath, PATHINFO_FILENAME); + $uniqID = md5(uniqid(rand(), TRUE)); + $newName = preg_replace('/(_[\w]{32})$/', '', $oldName) . '_' . $uniqID; + $newPath = str_replace($oldName, $newName, $filePath); + copy($filePath, $newPath); + return $newPath; + } + /** * Get files for the extension. * diff --git a/tests/phpunit/CRM/Utils/FileTest.php b/tests/phpunit/CRM/Utils/FileTest.php index 5327246c33..5e1659d93b 100644 --- a/tests/phpunit/CRM/Utils/FileTest.php +++ b/tests/phpunit/CRM/Utils/FileTest.php @@ -51,4 +51,26 @@ class CRM_Utils_FileTest extends CiviUnitTestCase { } } + public function fileExtensions() { + return array( + array('txt'), + array('danger'), + ); + } + + /** + * @dataProvider fileExtensions + * @param string $ext + */ + public function testDuplicate($ext) { + $fileName = CRM_Utils_File::makeFileName('test' . rand(100, 999) . ".$ext"); + CRM_Utils_File::createFakeFile('/tmp', 'test file content', $fileName); + $newFile = CRM_Utils_File::duplicate("/tmp/$fileName"); + $this->assertNotEquals("/tmp/$fileName", $newFile); + $contents = file_get_contents($newFile); + $this->assertEquals('test file content', $contents); + unlink("/tmp/$fileName"); + unlink($newFile); + } + } -- 2.25.1