Quantcast
Viewing latest article 6
Browse Latest Browse All 68

Access Errors on FeedAttachment for Chatter Test Class

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.


Viewing latest article 6
Browse Latest Browse All 68

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>