From 8ee4df1dab1cb3708546e059814f95bb1bbf7834 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Mon, 11 May 2020 20:29:48 -0400 Subject: [PATCH] delete attachments when activity is deleted --- CRM/Activity/BAO/Activity.php | 2 + .../CRM/Activity/Form/ActivityTest.php | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 332e8bac87..6d74569a65 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -182,6 +182,8 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { // 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 { diff --git a/tests/phpunit/CRM/Activity/Form/ActivityTest.php b/tests/phpunit/CRM/Activity/Form/ActivityTest.php index e65c333d7e..dad354ae17 100644 --- a/tests/phpunit/CRM/Activity/Form/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/Form/ActivityTest.php @@ -149,6 +149,53 @@ class CRM_Activity_Form_ActivityTest extends CiviUnitTestCase { $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 * -- 2.25.1