From a577477f55ef25290ed47679516791c6ccad3363 Mon Sep 17 00:00:00 2001
From: Justin Freeman <justin@agileware.com.au>
Date: Sat, 17 Aug 2019 07:24:26 +1000
Subject: [PATCH] CIVICRM-1290 CIVICRM-967 Add unit test: Migrate custom file
 attachments to new case on reassignment Add unit test from PR #14213 Migrate
 custom file attachments to new case on reassignment.
 https://github.com/civicrm/civicrm-core/pull/14213

---
 tests/phpunit/CRM/Case/BAO/CaseTest.php | 62 +++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/tests/phpunit/CRM/Case/BAO/CaseTest.php b/tests/phpunit/CRM/Case/BAO/CaseTest.php
index 01308094303c..2c346a4dc893 100644
--- a/tests/phpunit/CRM/Case/BAO/CaseTest.php
+++ b/tests/phpunit/CRM/Case/BAO/CaseTest.php
@@ -170,6 +170,68 @@ public function testRetrieveCaseIdsByContactId() {
     $this->assertEquals([1], $caseIds);
   }
 
+  /**
+   * Test that all custom files are migrated to new case when case is assigned to new client.
+   */
+  public function testCaseReassignForCustomFiles() {
+    $individual = $this->individualCreate();
+    $customGroup = $this->customGroupCreate(array(
+      'extends' => 'Case',
+    ));
+    $customGroup = $customGroup['values'][$customGroup['id']];
+
+    $customFileFieldA = $this->customFieldCreate(array(
+      'custom_group_id' => $customGroup['id'],
+      'html_type'       => 'File',
+      'is_active'       => 1,
+      'default_value'   => 'null',
+      'label'           => 'Custom File A',
+      'data_type'       => 'File',
+    ));
+
+    $customFileFieldB = $this->customFieldCreate(array(
+      'custom_group_id' => $customGroup['id'],
+      'html_type'       => 'File',
+      'is_active'       => 1,
+      'default_value'   => 'null',
+      'label'           => 'Custom File B',
+      'data_type'       => 'File',
+    ));
+
+    // Create two files to attach to the new case
+    $filepath = Civi::paths()->getPath('[civicrm.files]/custom');
+
+    CRM_Utils_File::createFakeFile($filepath, 'Bananas do not bend themselves without a little help.', 'i_bend_bananas.txt');
+    $fileA = $this->callAPISuccess('File', 'create', ['uri' => "$filepath/i_bend_bananas.txt"]);
+
+    CRM_Utils_File::createFakeFile($filepath, 'Wombats will bite your ankles if you run from them.', 'wombats_bite_your_ankles.txt');
+    $fileB = $this->callAPISuccess('File', 'create', ['uri' => "$filepath/wombats_bite_your_ankles.txt"]);
+
+    $caseObj = $this->createCase($individual);
+
+    $this->callAPISuccess('Case', 'create', array(
+      'id'                                => $caseObj->id,
+      'custom_' . $customFileFieldA['id'] => $fileA['id'],
+      'custom_' . $customFileFieldB['id'] => $fileB['id'],
+    ));
+
+    $reassignIndividual = $this->individualCreate();
+    $this->createLoggedInUser();
+    $newCase = CRM_Case_BAO_Case::mergeCases($reassignIndividual, $caseObj->id, $individual, NULL, TRUE);
+
+    $entityFiles = new CRM_Core_DAO_EntityFile();
+    $entityFiles->entity_id = $newCase[0];
+    $entityFiles->entity_table = $customGroup['table_name'];
+    $entityFiles->find();
+
+    $totalEntityFiles = 0;
+    while ($entityFiles->fetch()) {
+      $totalEntityFiles++;
+    }
+
+    $this->assertEquals(2, $totalEntityFiles, 'Two files should be attached with new case.');
+  }
+
   /**
    * FIXME: need to create an activity to run this test
    * function testGetCases() {