0

I'm stumped on how to get this test to work. Does anyone know how to get this test to work for full coverage? Thank you.

/// PDFViewerController_Test


@isTest
public class PDFViewerController_Test {
    
    @isTest static void getMapIdTitle(){   
       
      ContentDocumentLink a = new ContentDocumentLink(
      ContentDocumentId='0693D0000005ixzQAA', 
      LinkedEntityId='0013D00000QEgMaQAL'  
      );
              
      ContentDocument b = new ContentDocument(
       Title='Acme.pdf',
       ID='0013D00000QEgMaQAL'
       );
               
      ContentVersion c = new ContentVersion(
      PathOnClient='Acme.pdf',
      ContentDocumentId='0693D0000005ixzQAA',
      Title='Acme'
      );
      List<ContentVersion> ContentVersionLists = new List<ContentVersion>();
      ContentVersionLists.add(c);
      insert ContentVersionLists;
        
       Map<ID, String> map2 = PDFViewerController.getRelatedFilesByRecordId(b.Title);
       integer i = map2.size();
        
        for (ContentVersion docLink : ContentVersionLists) {
        string myTempId = c.Title;
              
       System.assertEquals(docLink.Title, map2.get(myTempId)) ;
    }
  } 
}

///Apex Class

public with sharing class PDFViewerController {
    @AuraEnabled(cacheable=true)
    public static Map<ID, String> getRelatedFilesByRecordId(String recordId) {
        // Get record file IDs        
        List<ContentDocumentLink> files = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :recordId];
        List<ID> fileIDs = new List<ID>();
        for (ContentDocumentLink docLink : files) {
            fileIDs.add(docLink.ContentDocumentId);
        }

        // Filter PDF files 
        List<ContentVersion> docs = [SELECT ContentDocumentId, FileExtension, Title 
            FROM ContentVersion WHERE ContentDocumentId IN : fileIDs AND FileExtension='pdf'];
        Map<ID, String> mapIdTitle = new Map<ID, String>();
        for (ContentVersion docLink : docs) {
            mapIdTitle.put(docLink.ContentDocumentId, docLink.Title);
        }

        return mapIdTitle;
    }
}

2 Answers 2

3

You cannot perform DML on ContentDocument (and possibly, ContentDocumentLink too). You only need to insert a test record into ContentVersion and salesforce will automatically create corresponding test records into ContentDocument and ContentDocumentLink objects (which you can query after ContentVersion insert DML).

Following code should be good enough in your apex test class to get a 100% code coverage (but I recommend you should write test methods for negative test cases as well).

ContentVersion cVersion = new ContentVersion(
  Title = 'Acme',
  PathOnClient = 'Acme.pdf',
  VersionData = Blob.valueOf('Apex Acme Test'),
  IsMajorVersion = true
);
insert cVersion;

ContentVersion cvAfterInsert = 
  [SELECT Id, Title, PathOnClient, VersionData, IsMajorVersion, ContentDocumentId 
   FROM ContentVersion LIMIT 1];

ContentDocumentLink cdl = [SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink 
   WHERE ContentDocumentId = :cvAfterInsert.ContentDocumentId LIMIT 1];

Map<ID, String> map2 = PDFViewerController.getRelatedFilesByRecordId(cdl.LinkedEntityId);
System.assertEquals(cVersion.Title, map2.get(cvAfterInsert.ContentDocumentId));

Also, never update the lookup or master-detail or ID field as some arbitrary hardcoded string values of ID. Anytime you perform an insert DML on an sObject or List, it will automatically get the ID value and you can use this reference to update or insert related records.

1
  • Thank you for answering my question! Props to you. Aug 15, 2020 at 18:52
1

I may be wrong here, but don't you need to insert ContentDocumentLink a and ContentDocument b too?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .