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
umakanth mettuumakanth mettu 

chatter trigger Help on Test class

Hi All,
we want to activate the chatter for email to case in lightning but we do not want any person to post or comment using chatter.
I have written two triggers for not posting anything and also commenting via case feed both are working fine but failed to write a test classes for both.
FeedItem:
trigger ChatterCommentItem on FeedItem (before insert) {
for(FeedItem fi : trigger.new)
        if(fi.type == 'ContentPost' || fi.type == 'TextPost')
            fi.addError('you do not have permissions to post any comment or File.Please contact your system administrator.');
}
FeedComment
trigger ChatterComment on FeedComment (Before Insert) { 
    for(FeedComment fc : trigger.new)
if(fc.CommentType == 'ContentComment' || fc.CommentType == 'TextComment' )
            fc.addError('you do not have permissions to post any comment or File.Please contact your system administrator.');
}

I have written feed comment trigger for case object when the case status or any changes happens case feed is generated so no one should comment on it any help on the test classes for both triggers would be appriciated thank you.
Raj VakatiRaj Vakati
try this code
 
@isTest
private class FeedItems_Test {
    
    static testMethod void linkChatterFiles_Oppty() {
        System.Test.startTest();
        
        FeedItem f = new FeedItem();
        f.ParentId = UserInfo.getUserId();
f.type = 'ContentPost';
        f.body = 'test';
        insert f;
        
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
fc.CommentType = 'ContentComment' ;
        fc.FeedItemId = f.Id;   // please add this
        insert fc;
        System.Test.stopTest();
        
    }
    
    
    
}

 
devedeve
Hi umakanth,

Please check test classes;-

For FeedItem:-

@isTest
private class ChatterCommentItemtest {

    @isTest
    static void testBeforeInsert() {
        FeedItem fi = new FeedItem();
        fi.name = 'fi';
        fi.ParentId = UserInfo.getUserId();
        fi.type =  'ContentPost';
        
        Test.startTest();
            Database.SaveResult result = Database.insert(fi, false);
        Test.stopTest();
        
        System.assertNotEquals(null, result.getErrors()[0].getMessage(), 'error message');
    }
    
}

For FeedComment;-​

@isTest
private class ChatterCommentFeedCommenttest {

    @isTest
    static void testBeforeInsert() {
        FeedComment fc = new FeedComment();
        fc.name = 'fc';
        fc.CommentType =  'ContentComment';
        
        Test.startTest();
            Database.SaveResult result = Database.insert(fc, false);
        Test.stopTest();
        
        System.assertNotEquals(null, result.getErrors()[0].getMessage(), 'error message');
    }
    
}