1

I am designing a test class for a Chatter component controller I did not design, and I am having issues creating test data. I have posted code below my issue description. I have removed most of the irrelevant code and denote the key lines with a number followed by >>.

Issue: To cover method prepareContentVersionList(), I need to have at least one CollaborationGroupFeed object with type="ContentPost". In the constructor at point (1), a collaborationGroupFeed object is fetched from getGroupFiles() (2) using the query at (4), which is then sent to prepareContentVersionList() at (3). (5) checks that at least one object was sent into prepareContentVersionList(), and that is where I am missing coverage.

I have tried making the correct test data in my test class but am getting an error when inserting a FeedAttachment at (6).

"System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You can't share a file from a different community.: []"

See code below.

Controller:

public class CEP_ProtectedChatterController {
    public Boolean isMimic{get;set;}
    public Boolean emailPref{get;set;}
    public Id groupId = null;
    list<CollaborationGroupFeed> collGrpFeed{get;set;}
    list<CollaborationGroupMember> cgmList{get;set;}
    list<NetworkMember> nmList{get;set;}
    public Map<Id,List<documentVersionWrapper>> dvMap{get;set;}
    public Boolean acceptedChatterTC{get;set;}
    Public User currentUser;
    public Id getGroupId() {
        return groupId;
    }


    public CEP_ProtectedChatterController(){
         this(apexpages.currentpage().getparameters().get('groupid'));
         dvMap=new Map<Id,List<documentVersionWrapper>>();
         emailPref=checkEmailOnFeedEnabled();
1.>>     collGrpFeed=getGroupFiles();
2.>>     if(null != collGrpFeed)
3.>>        prepareContentVersionList(collGrpFeed);
    }

    Public list<CollaborationGroupFeed> getGroupFiles(){
4.>>    list<CollaborationGroupFeed> output = [SELECT Title,RelatedRecordId FROM CollaborationGroupFeed WHERE Type='ContentPost' AND ParentId=:groupId];
        if(output != null && output.size() > 0){
            return output;
        }
        return null;
    }

    public void prepareContentVersionList(List<CollaborationGroupFeed> collGrpFeed){

        //collGrpFeed:initialized from groupfile function with list of feed files
5.>>        if(collGrpFeed != null && collGrpFeed.size() > 0){
            list<Id> idlist=new list<Id>();
            for(CollaborationGroupFeed cgfi:collGrpFeed)
                idlist.add(cgfi.RelatedRecordId);
            //pull all content version entry
            list<String> cvlstId=new list<String>();
            list<ContentVersion> cvlst= [SELECT ContentDocumentId,Description,Document_Type__c,Id,Title,VersionNumber FROM ContentVersion where Id in  :idlist];
            for(ContentVersion cv :cvlst){
                cvlstId.add(cv.ContentDocumentId);
            }
            cvlst= [SELECT ContentDocumentId,Description,Document_Type__c,Id,Title,IsLatest,VersionNumber FROM ContentVersion where ContentDocumentId in  :cvlstId  order by VersionNumber desc ];
            if(dvMap == null) dvMap=new Map<Id,List<documentVersionWrapper>>();
            for(ContentVersion cv :cvlst){
                //check if entry in Map exist

                List<documentVersionWrapper> docVersionWrapList=dvMap.get(cv.ContentDocumentId);
                //its first entry
                if(docVersionWrapList== null)
                    docVersionWrapList=new List<documentVersionWrapper>();

                documentVersionWrapper docVersionWrapperData=  new documentVersionWrapper(cv);
                docVersionWrapList.add(docVersionWrapperData);
                dvMap.put(cv.ContentDocumentId,docVersionWrapList);
            }
         }

    }

Test Class

    static TestMethod void testProtectedChatterController() {
        Profile profile = [SELECT Id FROM Profile WHERE Profile.Name LIKE 'CEP%Community%External%User' LIMIT 1];

        Account TSTAccount = CEP_TestUtilAuraEnabled.createAccount('TSTACCount');
        insert TSTAccount;

        Contact TSTContact=CEP_TestUtilAuraEnabled.createContact(TSTAccount.id);
        insert TSTContact;


        Profile apiAdminProfile = [SELECT Id FROM Profile WHERE Profile.Name = 'API Administrator - Non-SSO' LIMIT 1];
        //Profile sysAdminProfile = [SELECT Id FROM Profile WHERE Profile.Name LIKE 'System%Admin%' LIMIT 1];

        User apiAdminUser = [SELECT Id,Name FROM User WHERE Profile.Id=:apiAdminProfile.Id AND IsActive=true LIMIT 1];
        //User sysAdminUser = [SELECT Id,Name FROM User WHERE Profile.Id=:sysAdminProfile.Id AND IsActive=true LIMIT 1];
        User commUser  = CEP_TestUtilAuraEnabled.createUser(profile.Id);
        Network community = [SELECT Id,Name FROM Network WHERE Name LIKE '%CEP Community%' LIMIT 1];
        CollaborationGroup testGroup = new CollaborationGroup(Name='CEPprotectedChatterTestGroup',CollaborationType='Private',NetworkId=community.id);
        System.runAs(apiAdminUser){
            commUser.ContactId=TSTContact.Id;
            commUser.IsActive=true;
            insert commUser;

            insert testGroup;

            CollaborationGroupMember cgm = new CollaborationGroupMember();
            cgm.collaborationGroupId = testGroup.id;
            cgm.MemberId = commUser.id;
            insert cgm;
            //Create content for fi2
            ContentVersion cv = new ContentVersion();
            cv.Title = 'Test';
            cv.PathOnClient = 'test';
            cv.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
            insert cv;

            // Generates chatter posts. fi2 will contain content and is necessary to test prepareContentVersionList() in component
            FeedItem fi1 = new FeedItem(Body='Test1', networkScope=community.Id, Title='TextTest', ParentId=testGroup.id);
            FeedItem fi2 = new FeedItem(Body='Test2', networkScope=community.Id, Title='FileTest', ParentId=testGroup.id);
            insert fi1;
            insert fi2;
            CollaborationGroupFeed cg=new CollaborationGroupFeed();
            // Attach content to fi2        
            FeedAttachment feedAttachment = new FeedAttachment();
            feedAttachment.FeedEntityId = fi2.Id;          
            feedAttachment.RecordId = cv.Id;  
            feedAttachment.Title = 'FileName';            
            feedAttachment.Type = 'Content';
6.>>        insert feedAttachment; 

        }
     ... 
     ...

As I understand it, I cannot manually set the FeedItem type to "ContentPost" manually; it must have a related feedAttachment to have that type. If someone can give me insight into this to bypass the error, it would be helpful. Thank you.

1 Answer 1

1


There is a catch here. If a FeedItem record is created with Type = 'ContentPost', following are also required fields:
- ContentFileName
- ContentData (Blob Data)

On top of that these fields are deprecated for API version 35.0 above. So, try changing the API version to 35.0 or below and then try creating a FeedItem record with above data in the test class, then it would work.

Resources:
Link to Resource-1
Lint to Resource-2

Thanks..!!

1
  • 1
    This is the correct solution to my issue, thank you
    – AusNC
    Commented Dec 6, 2019 at 14:00

You must log in to answer this question.

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