Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of PR #15051 to 5.16 #15084

Merged
merged 2 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
65 changes: 64 additions & 1 deletion tests/phpunit/CRM/Case/BAO/CaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down