function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Justin MitchellJustin Mitchell 

Unit testing trigger on Chatter posts with attachments

I have this trigger that works when I test it manually, but I am having trouble writing a unit test that actually initiates the trigger. Therefore I can't get more than about 30% coverage. 

The gist of the trigger is this: when a chatter post is made in a chatter group called "Deal Committee", and there is an attachment to the chatter post, then that document is added to a Library called "Deal Committee Attachments". If the document is already in the LIbrary, then it updates a field called Date_Added_to_Library__c to NOW()

Here's the trigger. The comments are mainly for my benefit. :)
(I am aware I have two queries inside a FOR loop and that's not good, and I would like to rewrite it when I have a chance, but for now I'm on a tight schedule and it's what I have and it works. Suggestions around fixing that are welcome but mainly I want to get this unit test written.)
trigger dealCommitteeChatterAttachment on FeedItem (after insert) {
    
    //find the ID of the chatter group called "Deal Committee" and then find the id of the Library called "Deal Committee Attachments
    id cgid = [SELECT id FROM CollaborationGroup WHERE Name = 'Deal Committee'].id;
    id lid  = [SELECT id FROM ContentWorkspace WHERE Name = 'Deal Committee Attachments'].id;
    Set<ID> ids = Trigger.newMap.keySet();
     
    for (FeedItem f : Trigger.new) {
        
        //whenever a new chatter post is made, first check if it's being posted in the Deal Committee group AND check if it has any attachments
        if (f.ParentId == Id.valueOf(cgid) && f.Type == 'ContentPost' ) {
            
            //If either answer (from above) is false, then do nothing
            //If both answers (from above) are true, then do a check to find out if the attached document has already been shared in the Deal Committee Attachments library
            ContentVersion[] contentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :f.RelatedRecordId];            
            integer duplicatecheck     = [SELECT count() from ContentWorkspaceDoc WHERE ContentDocumentID = :contentId[0].ContentDocumentId];
                
                //If it has been shared to the library already, don't add it again, but simply update the "Date Added to Library" field to NOW()
                if( duplicatecheck > 0 ) {
                   ContentVersion cv = contentId[0];
                   cv.Date_Added_to_Library__c = System.now();
                   update cv;
                }
                
                //If it has never been added to the library, then add it           
                else if( duplicatecheck == 0 ) { 
            
                    ContentWorkspaceDoc c = new ContentWorkspaceDoc();            
                    c.ContentDocumentID   = contentId[0].ContentDocumentId;
                    c.ContentWorkspaceID  = Id.valueOf(lid);                       
                    insert c;
                }
        } 
                
    //Otherwise do nothing
    else  {}
    }
}

Here's my unit test as it is now. It says it covers the trigger at 31%. As far as my knowledge goes, it should be covering everything. Clearly I'm wrong but I don't understand what I'm missing. I think it's not even initiating the trigger to create the ContentWorkspaceDoc at all, which means none of the other code is going to run, and i don't know why.
@isTest
private class dealCommitteeChatterAttachment_test {
           
    @isTest(SeeAllData=True) static void addToLibraryTest() {
        List<CollaborationGroup> cgId = [SELECT id FROM CollaborationGroup WHERE Name = 'Deal Committee' LIMIT 1];
        List<ContentWorkspace> lid = [SELECT id FROM ContentWorkspace WHERE Name = 'Deal Committee Attachments' LIMIT 1];

        ContentVersion cv = new ContentVersion();
        cv.ContentURL = '<a target="_blank" href="http://www.google.com/">http://www.google.com</a>';
        cv.Title ='Google.com'; 
        insert cv;

        ContentVersion datecheck1 = [SELECT Date_Added_to_Library__c,ContentDocumentId FROM ContentVersion WHERE id = :cv.id];
        System.Debug(datecheck1.ContentDocumentId);
        
        FeedItem fi1 = new FeedItem();
        fi1.ParentId = cgid[0].id;
        fi1.body     = 'test';
        fi1.Type     = 'ContentPost';
        insert fi1; 

        FeedAttachment fa1 = new FeedAttachment();
        fa1.FeedEntityId = fi1.id;
        fa1.type         = 'Content';
        fa1.RecordId     = cv.id;
        insert fa1;
            
        FeedItem fi2 = new FeedItem();
        fi2.ParentId = cgid[0].id;
        fi2.body     = 'test 2';
        fi2.Type     = 'ContentPost';
        insert fi2;

        FeedAttachment fa2 = new FeedAttachment();
        fa2.FeedEntityId = fi2.id;
        fa2.type         = 'Content';
        fa2.RecordId     = cv.id;
        insert fa2;        

        ContentVersion datecheck2 = [SELECT Date_Added_to_Library__c FROM ContentVersion WHERE id = :cv.id];
		System.Debug(datecheck1.Date_Added_to_Library__c);
        System.Debug(datecheck2.Date_Added_to_Library__c);
        
    }
}