CRM-19948: Store the logged in contact ID as the file uploader
authorDavi Alexandre <davi@davialexandre.com.br>
Tue, 31 Jan 2017 20:30:30 +0000 (18:30 -0200)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Tue, 22 May 2018 19:42:21 +0000 (01:12 +0530)
api/v3/Attachment.php
tests/phpunit/api/v3/AttachmentTest.php

index 9c097038db75451c5bdd6b2a9e68dbad26cdaa13..b58fda4aa10038e167ba9e119039da1a5f37d11a 100644 (file)
@@ -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'],
index 3aef35979489edd18db0d01124d5e2b906b6129f..041984a50e3313c510e20e754d98350d8df60f57 100644 (file)
@@ -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