From 7aab005804775d32b5e30f7f68664755cd5eed99 Mon Sep 17 00:00:00 2001 From: Davi Alexandre Date: Tue, 31 Jan 2017 18:30:30 -0200 Subject: [PATCH] CRM-19948: Store the logged in contact ID as the file uploader --- api/v3/Attachment.php | 3 + tests/phpunit/api/v3/AttachmentTest.php | 76 +++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/api/v3/Attachment.php b/api/v3/Attachment.php index 9c097038db..b58fda4aa1 100644 --- a/api/v3/Attachment.php +++ b/api/v3/Attachment.php @@ -147,6 +147,7 @@ function civicrm_api3_attachment_create($params) { $fileDao->copyValues($file); if (!$id) { $fileDao->uri = CRM_Utils_File::makeFileName($name); + $fileDao->created_id = CRM_Core_Session::getLoggedInContactID(); } $fileDao->save(); @@ -299,6 +300,7 @@ function __civicrm_api3_attachment_find($params, $id, $file, $entityFile, $isTru 'cf.mime_type', 'cf.description', 'cf.upload_date', + 'cf.created_id', 'cef.entity_table', 'cef.entity_id', )); @@ -431,6 +433,7 @@ function _civicrm_api3_attachment_format_result($fileDao, $entityFileDao, $retur 'entity_table' => $entityFileDao->entity_table, 'entity_id' => $entityFileDao->entity_id, 'icon' => CRM_Utils_File::getIconFromMimeType($fileDao->mime_type), + 'created_id' => $fileDao->created_id, ); $result['url'] = CRM_Utils_System::url( 'civicrm/file', 'reset=1&id=' . $result['id'] . '&eid=' . $result['entity_id'], diff --git a/tests/phpunit/api/v3/AttachmentTest.php b/tests/phpunit/api/v3/AttachmentTest.php index 3aef359794..041984a50e 100644 --- a/tests/phpunit/api/v3/AttachmentTest.php +++ b/tests/phpunit/api/v3/AttachmentTest.php @@ -408,6 +408,82 @@ class api_v3_AttachmentTest extends CiviUnitTestCase { $this->assertEquals('fa-file-text-o', $createResult['values'][$fileId]['icon']); } + public function testCreateShouldSetCreatedIdAsTheLoggedInUser() { + $loggedInUser = $this->createLoggedInUser(); + + $testEntityClass = 'CRM_Activity_DAO_Activity'; + $entity = CRM_Core_DAO::createTestObject($testEntityClass); + $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); + $this->assertTrue(is_numeric($entity->id)); + + $createResult = $this->callAPISuccess('Attachment', 'create', array( + 'name' => self::getFilePrefix() . 'exampleFromContent.txt', + 'mime_type' => 'text/plain', + 'content' => 'My test content', + 'entity_table' => $entity_table, + 'entity_id' => $entity->id, + )); + + $fileId = $createResult['id']; + $this->assertEquals($loggedInUser, $createResult['values'][$fileId]['created_id']); + } + + public function testCreateShouldKeepCreatedIdEmptyIfTheresNoLoggedInUser() { + $testEntityClass = 'CRM_Activity_DAO_Activity'; + $entity = CRM_Core_DAO::createTestObject($testEntityClass); + $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); + $this->assertTrue(is_numeric($entity->id)); + + $createResult = $this->callAPISuccess('Attachment', 'create', array( + 'name' => self::getFilePrefix() . 'exampleFromContent.txt', + 'mime_type' => 'text/plain', + 'content' => 'My test content', + 'entity_table' => $entity_table, + 'entity_id' => $entity->id, + )); + + $fileId = $createResult['id']; + $this->assertEmpty($createResult['values'][$fileId]['created_id']); + } + + public function testCreateShouldNotUpdateTheCreatedId() { + $testEntityClass = 'CRM_Activity_DAO_Activity'; + $entity = CRM_Core_DAO::createTestObject($testEntityClass); + $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); + $this->assertTrue(is_numeric($entity->id)); + + $attachmentParams = array( + 'name' => self::getFilePrefix() . 'exampleFromContent.txt', + 'mime_type' => 'text/plain', + 'description' => 'My test description', + 'content' => 'My test content', + 'entity_table' => $entity_table, + 'entity_id' => $entity->id, + ); + + $createResult = $this->callAPISuccess('Attachment', 'create', $attachmentParams); + + $fileId = $createResult['id']; + $this->assertEmpty($createResult['values'][$fileId]['created_id']); + + $attachmentParams['id'] = $fileId; + $attachmentParams['description'] = 'My updated description'; + + $loggedInUser = $this->createLoggedInUser(); + + $this->callAPISuccess('Attachment', 'create', $attachmentParams); + + $updatedAttachment = $this->callAPISuccess('Attachment', 'get', array( + 'id' => $fileId, + 'entity_id' => $attachmentParams['entity_id'], + 'entity_table' => $attachmentParams['entity_table'] + )); + + $this->assertNotEmpty($loggedInUser); + $this->assertEmpty($updatedAttachment['values'][$fileId]['created_id']); + $this->assertEquals($attachmentParams['description'], $updatedAttachment['values'][$fileId]['description']); + } + /** * @param $getParams * @param $expectedNames -- 2.25.1