CRM-20029 - Copy custom files when duplicating an event
authorColeman Watts <coleman@civicrm.org>
Fri, 10 Mar 2017 02:28:16 +0000 (21:28 -0500)
committerColeman Watts <coleman@civicrm.org>
Tue, 14 Mar 2017 15:38:07 +0000 (11:38 -0400)
CRM/Core/BAO/CustomField.php
CRM/Core/BAO/File.php
CRM/Event/BAO/Event.php
CRM/Utils/File.php
tests/phpunit/CRM/Utils/FileTest.php

index b5dff555e58838ba52eb27834c87f0d3060c1c01..3e74216e11b9c5d5f046d9d3999951c42e45ee33 100644 (file)
@@ -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);
 
index b0e5a65a5abf190f922ac26c75517d50f47e40f4..2eb059de2cbe982c21be025e670a3e6313ffb9d9 100644 (file)
@@ -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']);
   }
 
 
index c1e9c35a28ef8277b9a7ca099aa4acbbf65a895e..cd9d8294dba10f9a7aafd4d21256c1c8df8c907e 100644 (file)
@@ -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],
             );
           }
index c5906f4b90d3f8795b13e45e82597c68799758a3..1ca478649f69aea5ff94356ee3a0179209dba676 100644 (file)
@@ -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.
    *
index 5327246c331c6897332aef60b5c5190fa78c525d..5e1659d93b64eb6fac6f3ee61d46ed22b95c2fd1 100644 (file)
@@ -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);
+  }
+
 }