0

I don't know how can I test my contentdocumentlink, in the log I got the information

EXCEPTION_THROWN [18]|System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.

but I think I only selected one record.

For the "Wrapper implements Comparable", how can I test it? I read a bit about the testclass for that, but its not clear for me.

Main class

public class DokuBox_6 {
    
    public List<Wrapper> wrapperList {get; set;}
    public String currentRecordId {get; set;}
    
    public DokuBox_6() {
        currentRecordId = ApexPages.currentPage().getParameters().get('id');
        
        if(wrapperList == null) {
            wrapperList = new List<Wrapper>();
            
            for(Task a: [select Id, Subject, CreatedDate, Createdby.Name, RecordType.Name from Task where WhoId =: currentRecordId limit 10]) {
                wrapperList.add(new Wrapper(a, 'Task'));
            }
            for(Event b: [select Id, Subject, CreatedDate, Createdby.Name from Event where WhoId =: currentRecordId limit 10]) {
                wrapperList.add(new Wrapper(b, 'Event'));
            }
            for(ContentDocumentLink d: [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.createdDate, ContentDocument.Createdby.Name, ContentDocument.FileExtension FROM ContentDocumentLink WHERE LinkedEntityId =: currentRecordId limit 10]) {
                wrapperList.add(new Wrapper(d, 'Note/File'));
            }
            wrapperList.sort();
        }
    }
    
    public class Wrapper implements Comparable {
        
        private final String objectLabel;
        private final Id id;
        private final String subject;
        private final DateTime createDate;
        private final String Ersteller;
        private final String Typ;
        
        public wrapper(Task t, String oName) { 
            objectLabel     = oName;
            id              = t.Id;
            subject         = t.Subject;
            createDate      = t.CreatedDate;
            Ersteller       = t.CreatedBy.Name;
            Typ             = t.RecordType.Name;
        }
        
        public wrapper(Event e, String oName) {
            objectLabel     = oName;
            id              = e.Id;
            subject         = e.Subject;
            createDate      = e.CreatedDate;
            Ersteller       = e.CreatedBy.Name;
        }
        
        public wrapper(ContentDocumentLink f, String oName) {
            objectLabel     = oName;
            id              = f.ContentDocumentId;
            subject         = f.ContentDocument.Title;
            createDate      = f.ContentDocument.CreatedDate;
            Ersteller       = f.ContentDocument.CreatedBy.Name;
            Typ             = f.ContentDocument.FileExtension;
        }
        
        public Id getId() {
            return id;
        }
        
        public String getSubject() {
            return subject;
        }
        
        public String getTyp() {
            return Typ;
        }    
        
        public DateTime getCreatedDate() {
            return createDate;
        }
        
        public String getObjectLabel() {
            return objectLabel;
        }

        public String getErsteller() {
            return Ersteller;
        }
        
        public Integer compareTo(Object compareTo) {
            Wrapper compareToActivity = (Wrapper) compareTo;
            DateTime compareToCreateDate = compareToActivity.getCreatedDate();
            if (createDate > compareToCreateDate) {
                return -1;
            }
            if (createDate < compareToCreateDate) {
                return 1;
            }
            return 0;
        }
    }
    
}

Test class

@isTest
private class DokuBox_Test {
    
    static testMethod void testMethod1() {

        DateTime cur=System.now();
        
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        Contact con = new Contact(Firstname = 'John', Lastname = 'Doe');
        insert con;
        
        Task tsk = new Task(
            Subject = 'Call', 
            ActivityDate = Date.today().addDays(-1), 
            Description = 'Test', 
            WhoId = acc.Id
        );
        insert tsk;
        
        Event evt = new Event(
            WhoId = acc.id,
            Subject = 'Strategy',
            StartDateTime = Date.newInstance(cur.year(), cur.month(), 1) + 1,
            EndDateTime = Date.newInstance(cur.year(), cur.month(), 1 + 1)
        );
        insert evt;
        
        ContentVersion conVer = new ContentVersion(
            Title = 'Penguins',
            PathOnClient = 'Penguins.docx',
            VersionData = Blob.valueOf('Test Content'),
            IsMajorVersion = true,
            Vertraulichkeitsstufe__c = 'Intern',
            Dokumentenklasse__c = 'Geschäftsbrief'
        );
        insert conVer;  
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = acc.id;
        cdl.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =: conVer.id].ContentDocumentId;
        cdl.shareType = 'I';
        insert cdl;
        
        PageReference DokuBox_6v = Page.DokuBox_6v; // Assuming that the VF Page name is DokuBoxPage
        Test.setCurrentPage(DokuBox_6v);
        ApexPages.currentPage().getParameters().put('id', con.Id);
        DokuBox_6 controller = new DokuBox_6();
        System.assert(!controller.wrapperList.isEmpty());

        Test.setCurrentPage(DokuBox_6v);
        ApexPages.currentPage().getParameters().put('id', acc.Id);
        DokuBox_6 controller2 = new DokuBox_6();
              
        System.assert(!controller2.wrapperList.isEmpty());
    }
}

1 Answer 1

1

In your test class you should set the current page via Test.setCurrentPage(page) method. Then you can add to the page the id of the record you're using in your test.

Replace DokuBox_6 D6 = new DokuBox_6(); with:

PageReference dokuboxPage = Page.DokuBoxPage; // Assuming that the VF Page name is DokuBoxPage
Test.setCurrentPage(dokuboxPage);
ApexPages.currentPage().getParameters().put('id', acc.Id);
DokuBox_6 controller = new DokuBox_6();
System.assert(!controller.wrapperList.isEmpty());
// loop on wrapperList calling the get methods
for (DokuBox_6.Wrapper wrapper : controller.wrapperList) {
    System.assert(String.isNotBlank(wrapper.getId()));
    System.assert(String.isNotBlank(wrapper.getSubject()));
    System.assert(String.isNotBlank(wrapper.getObjectLabel()));
    System.assert(wrapper.getCreatedDate() != null);
    wrapper.getErsteller();
    wrapper.getTyp();
}
controller.wrapperList.sort();

One more thing, in the test class you're setting WhatId for Event and Task, but in the controller you're querying by WhoId, so actually the test now will not cover the for-loop on these two objects.
Please change the WHERE clause of those queries to WHERE WhatId =: currentRecordId if you're going to use it on Account record, as you did in the test.

2
  • WhoId is correct, I changed the testclass. Can you give me an example for the loop on wrapperList to calling get methods.
    – peXeq
    Commented Jul 22, 2021 at 11:30
  • @peXeq I edited the answer adding an example
    – RubenDG
    Commented Jul 22, 2021 at 11:41

You must log in to answer this question.

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