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
XIOXIO 

Apex Test Class for FeedItem Issues

Hello Developers,

I'm having a lot of issues with creating an apex test class for the FeedItem apex trigger below. The trigger basically blocks all users from deleting attachments from Chatter Feed on the custom object (Documents Manager) expect the certain few users and profile. Any assistance creating the test class so we can deploy to production will be greatly appreciated!!
trigger ChatterDeleteBlocker on FeedItem (After Delete) {
 User u = [SELECT id, Name, ProfileId,Profile.Name FROM User WHERE id = :UserInfo.getUserId()];
 for(FeedItem fi : trigger.old) {
      
      Document_Manager__c dm = new Document_Manager__c();
      
      try {
         dm = [Select Id from Document_Manager__c where Id =:fi.ParentId];
      }
      catch(Exception e) {
         dm = null;
      }
     
      if (dm != null) {
        if(fi.type == 'ContentPost' && (u.Profile.Name != 'System Administrator') && (u.Name != 'Bryan Girkins'
           && u.Name != 'Mary Loy' && u.Name != 'Judit Szilagyi' && u.Name != 'Sarah Swanson' && u.Name != 'Jeremy DalleTezze')) {
            fi.addError('You do not have the permissions to delete files from chatter feed');
        }
      }
 }
}



 
Best Answer chosen by XIO
Andrew EchevarriaAndrew Echevarria
In this answer, someone provided an answer on how to write a test class for a FeedItem trigger :)
https://developer.salesforce.com/forums/?id=906F000000093BgIAI

All Answers

Andrew EchevarriaAndrew Echevarria
In this answer, someone provided an answer on how to write a test class for a FeedItem trigger :)
https://developer.salesforce.com/forums/?id=906F000000093BgIAI
This was selected as the best answer
XIOXIO
Thanks Andrew! I found the same article but it didn't help me much. Below is my current test class:
@isTest
private class TestFeedItemTriggers {

    static testMethod void ChatterDeleteBlocker(){
    
        Id DMRecordTypeId = Schema.SObjectType.Document_Manager__c.getRecordTypeInfosByName().get('Research Report').getRecordTypeId();
        
        Service_Discipline__c sd = TestDataUtils.createServiceDiscipline('Field Services', 'ab');
        List<Document_Manager__c> dmList = new List<Document_Manager__c>();
        Document_Manager__c dm = new Document_Manager__c();
        dm.Name='abc';
        dm.Document_Full_Name__c = 'abc';
        dm.Document_Type__c = 'Research Report';
        dm.Service_Discipline_s__c = 'Field Services';
        dm.Publication_Date__c = Date.today();
        dm.Document_Access__c = 'All Members';
        dm.Research_Report_Status__c = 'Published to Website';  
        dmList.add(dm);
        Database.SaveResult[] srList = Database.insert(dmList, false);
                       
        List<Document_Manager__c> newDM = [select id, name from Document_Manager__c where id =: dm.Id];
        System.assertEquals(0, newDM.size());
       
        FeedItem f = new FeedItem();
        f.Body = 'legal test';
        f.parentID = UserInfo.getUserId();
        
        insert f;
        
        Profile p = [select id from profile where Name = :'System Administrator' limit 1];
        User pu = new User(profileId = p.id, username = 'jaxen@yahoo.com',
        email = 'jaxen@yahoo.com',
        emailencodingkey = 'UTF-8',
        localesidkey = 'en_US',
        languagelocalekey = 'en_US',
        timezonesidkey = 'America/Los_Angeles',
        alias='jaxn', 
        lastname='lastname');
        
        
        insert pu;
        
            }
}

 
Andrew EchevarriaAndrew Echevarria
Shouldn't it be Before Delete since you're quering the record after it has been deleted?