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

dev/core#1753 - Attachments aren't deleted when deleting activity #17298

Merged
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
2 changes: 2 additions & 0 deletions CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public static function deleteActivity(&$params, $moveToTrash = FALSE) {
// CRM-13994 delete activity entity_tag
$query = "DELETE FROM civicrm_entity_tag WHERE entity_table = 'civicrm_activity' AND entity_id = %1";
$dao = CRM_Core_DAO::executeQuery($query, [1 => [$activity->id, 'Positive']]);

CRM_Core_BAO_File::deleteEntityFile('civicrm_activity', $activity->id);
}
}
else {
Expand Down
47 changes: 47 additions & 0 deletions tests/phpunit/CRM/Activity/Form/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,53 @@ public function testActivityDelete() {
$this->assertTargetActivityIds($expectedActivityIds);
}

/**
* Test deleting an activity that has an attachment.
*/
public function testActivityDeleteWithAttachment() {
$loggedInUser = $this->createLoggedInUser();
// Create an activity
$activity = $this->callAPISuccess('Activity', 'create', [
'source_contact_id' => $loggedInUser,
'activity_type_id' => 'Meeting',
'subject' => 'test with attachment',
'status_id' => 'Completed',
'target_id' => $this->target,
]);
$this->assertNotEmpty($activity['id']);

// Add an attachment - this will also create it in the filesystem.
$attachment = $this->callAPISuccess('Attachment', 'create', [
'name' => 'abc.txt',
'mime_type' => 'text/plain',
'entity_id' => $activity['id'],
'entity_table' => 'civicrm_activity',
'content' => 'delete me',
]);
$this->assertNotEmpty($attachment['id']);

// Check the file is actually there
$file_path = $attachment['values'][$attachment['id']]['path'];
$this->assertTrue(file_exists($file_path));

// Call our local helper function to use the form to delete
$this->deleteActivity($activity['id']);

// File should be gone from the filesystem
$this->assertFalse(file_exists($file_path), "File is still in filesystem $file_path");

// Shouldn't be an entry in civicrm_entity_file
$query_params = [1 => [$activity['id'], 'Integer']];
$entity_file_id = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_entity_file WHERE entity_table='civicrm_activity' AND entity_id = %1", $query_params);
$this->assertEmpty($entity_file_id, 'Entry is still in civicrm_entity_file table.');

// In this situation there also shouldn't be an entry in civicrm_file since
// there's no other references to it.
$query_params = [1 => [$attachment['id'], 'Integer']];
$file_id = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_file WHERE id = %1", $query_params);
$this->assertEmpty($file_id, 'Entry is still in civicrm_file table.');
}

/**
* Asserts that the target contact has the expected activity IDs
*
Expand Down