diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 1b1340a9ef17..f947784c068b 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -2084,28 +2084,6 @@ public static function mergeCases( continue; } - // CRM-11662 Copy Case custom data - $extends = array('case'); - $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends); - if ($groupTree) { - foreach ($groupTree as $groupID => $group) { - $table[$groupTree[$groupID]['table_name']] = array('entity_id'); - foreach ($group['fields'] as $fieldID => $field) { - $table[$groupTree[$groupID]['table_name']][] = $groupTree[$groupID]['fields'][$fieldID]['column_name']; - } - } - - foreach ($table as $tableName => $tableColumns) { - $insert = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $tableColumns) . ') '; - $tableColumns[0] = $mainCaseId; - $select = 'SELECT ' . implode(', ', $tableColumns); - $from = ' FROM ' . $tableName; - $where = " WHERE {$tableName}.entity_id = {$otherCaseId}"; - $query = $insert . $select . $from . $where; - $dao = CRM_Core_DAO::executeQuery($query); - } - } - $mainCaseIds[] = $mainCaseId; //insert record for case contact. $otherCaseContact = new CRM_Case_DAO_CaseContact(); diff --git a/tests/phpunit/CRM/Case/BAO/CaseTest.php b/tests/phpunit/CRM/Case/BAO/CaseTest.php index fae0799d6999..1921c600ec7c 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(array(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() { @@ -261,7 +323,8 @@ public function testCaseClosure() { 'activity_date_time' => $now_date, 'target_contact_id' => $client_id, 'source_contact_id' => $loggedInUser, - 'subject' => 'null', // yeah this is extra weird, but without it you get the wrong subject + // yeah this is extra weird, but without it you get the wrong subject + 'subject' => 'null', ]; $form->postProcess($actParams);