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
Priyadarshini Manoharan 3Priyadarshini Manoharan 3 

I want the trigger code to be executed only for a Community User profile. An if condition to check if the feed item created by user has community profile. This if condition is causing the test class to lose coverage

I want the trigger code to be executed only for a Community User profile. An if condition to check if the feed item created by user has community profile. This if condition is causing the test class to lose coverage from 100% to 47%.
I added System.runAs for community user profile for entrie code. However there is no change in coverage.
Trigger
trigger EmailonChatterPostbyCustomer on FeedItem (after insert, after update) 
{
 
       List<EntitySubscription> stEntitySubList = new List<EntitySubscription>();
       Set<Id> CaseIds = new Set<Id>();
       Set<Id> OwnerIds = new Set<Id>();
 
        for (FeedItem f: trigger.new) 
        {
              Profile p = [SELECT Id, Name FROM Profile WHERE Name='DPS Customer Community'];
              if(f.CreatedBy.Profile.Name == p.Name)
              {
                CaseIds.add(f.ParentId);
              }
                  
        }
             
             List<Case> ca = new List<Case>([SELECT Id,CaseNumber,OwnerId,Owner.Name,Owner.Type FROM Case where id in :CaseIds and DPSProduct__c != 'ConnectMe']);
            
            
             
            /*for(Case c : ca)
            {
                if(c.Owner.Type=='User')
                {
                
                    c.Customer_Posted_on_Chatter__c = true;
                }
                       
            }
            update ca;*/
            
                DPSCustomSetting__c mbox = DPSCustomSetting__c.getInstance();
                string mailbox = mbox.DefaultMailBox__c;
  
                OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address =: mailbox];
                
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                if ( owea.size() > 0 ) 
                {    mail.setOrgWideEmailAddressId(owea.get(0).Id);}
                
                
                
                List<EntitySubscription> followers = [select id, subscriberid, subscriber.name, subscriber.email, parentid from EntitySubscription where parentid in :CaseIds];
    
                System.debug('***id subscriberid subscriber.name subscriber.email' + followers);
    
    
                String[] toAddresses = new List<String>();
                String[] toUserId = new List<String>();
                
                 if(followers.size() > 0)
                 {
                    /* for(Case c : ca)
                        {
                            if(c.Owner.Type=='User')
                            {
                            
                                c.Customer_Posted_on_Chatter__c = true;
                            }
                                   
                        }
                        update ca; 
                        */ 
                    
                                        
                    for (EntitySubscription follower : followers)
                    {
                        toAddresses.add(follower.subscriber.email);
                        toUserId.add(follower.subscriberid);
                    }
               
                
                    EmailTemplate et = [Select id from EmailTemplate where name = 'Customer Posts on Case Chatter Feed'];
                 
                    for(EntitySubscription es : followers)
                    {
                       
                       mail.setToAddresses(toAddresses);
                     
                         mail.setTargetObjectId(es.subscriberid);
                         mail.setWhatId(es.parentid);                   
                         mail.setTemplateID(et.Id);
                         System.debug('*****toAddresses' + toAddresses);        
                         mail.setSaveAsActivity(false);              
                    } 
                
                    if(mail != null ) 
                    {
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
                       for(Case c : ca)
                        {
                            if(c.Owner.Type=='User')
                            {
                            
                                c.Customer_Posted_on_Chatter__c = true;
                            }
                                   
                        }
                        update ca;            
                    }    
                         
                 }
                
}

Test Class

@isTest(SeeAllData=true)
public class TestEmailonChatterPostbyCustomer
{
    static testMethod void EmailonChatterPost()
    {
    
   // User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
    
    // System.runAs (thisUser) 
    // {
    Account portalAccount1 = new Account(
    Name = 'TestAccount' );
    insert portalAccount1;
    
    Contact contact1 = new Contact(
    FirstName = 'Test',
    Lastname = 'McTesty',
    AccountId = portalAccount1.Id,
    Email = 'test@test.com'
    );
    insert contact1;
       
       
       
       String uniqueUserName = 'portaluser' + DateTime.now().getTime() + '@testorg.com';
        // This code runs as the system user
        Profile pro = [SELECT Id FROM Profile WHERE Name='DPS Customer Community'];
        User uid = new User(Alias = 'standt', Email='standarduser@testorg.com',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = pro.Id,
        TimeZoneSidKey='America/Los_Angeles',ContactId = contact1.Id,
         UserName=uniqueUserName);
         insert uid;
         
          User us = new User();
        String ProfileId=[Select Id,Name from Profile where Name='System Administrator'].Id;
        us.FirstName = 'Support Agent';
        us.LastName  = 'DPS';
        us.Email     = 'dps@deloitte.com';
        us.Username  = 'dps@support.com';
        us.Alias     = 'ddps';
        us.ProfileId = ProfileId;
        us.Product__c = 'Revenue Intellect';
        us.Product_Access__c = 'Revenue Intellect User';
        us.TimeZoneSidKey    = 'America/Denver';
        us.LocaleSidKey      = 'en_US';
        us.EmailEncodingKey  = 'UTF-8';
        us.LanguageLocaleKey = 'en_US';
        
        insert us;
        
        
      System.runAs(uid) 
        
       {
               
            Account Acc = new Account();
            Acc.Name ='Bank of America';
            Acc.Sold_To_Flag__c = true;
            insert Acc;
            Case c = new Case(AccountId=Acc.Id, DPSIssue_Type__c= 'Data' ,Priority= 'High', DPSReason__c= 'Data question', DPSProduct__c = 'Revenue Intellect');
            insert c;
          
       // c.OwnerId = us.Id;
       // c.Customer_Posted_on_Chatter__c = true;
       // update c;
        
      /*  Profile p = [select id from profile where Name = :'System Administrator' limit 1];
        User pu = new User(profileId = p.id, username = 'tt@ff.com', email = 'pp@cc.com',
        emailencodingkey = 'UTF-8', localesidkey = 'en_US',
        languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles',
        alias='cspu', lastname='lastname');
        insert pu;


        EntitySubscription es = new EntitySubscription ( ParentId = c.id, SubscriberId = pu.Id);
        insert es;*/
        
        System.debug('UserInfo.getProfileId()' + UserInfo.getProfileId());
       
            FeedItem feed = new FeedItem (ParentId = c.id,Body = 'Hello', type = 'TextPost');
            insert feed;
      }
    // }  
        
    }
    
    static testMethod void EmailonChatterPost1()
    {
   
          User us = new User();
        String ProfileId=[Select Id,Name from Profile where Name='System Administrator'].Id;
        us.FirstName = 'Support Agent1';
        us.LastName  = 'DPS1';
        us.Email     = 'dps1@deloitte.com';
        us.Username  = 'dps1@support.com';
        us.Alias     = 'ddps';
        us.ProfileId = ProfileId;
        us.Product__c = 'Revenue Intellect';
        us.Product_Access__c = 'Revenue Intellect User';
        us.TimeZoneSidKey    = 'America/Denver';
        us.LocaleSidKey      = 'en_US';
        us.EmailEncodingKey  = 'UTF-8';
        us.LanguageLocaleKey = 'en_US';
        
        insert us;
        
        
     
               
            Account Acc = new Account();
            Acc.Name ='Bank of America';
            Acc.Sold_To_Flag__c = true;
            insert Acc;
            Case c = new Case(AccountId=Acc.Id, DPSIssue_Type__c= 'Data' ,Priority= 'High', DPSReason__c= 'Data question', DPSProduct__c = 'Revenue Intellect');
            insert c;
          
        c.OwnerId = us.Id;
        c.Customer_Posted_on_Chatter__c = true;
        update c;
        
        Profile p = [select id from profile where Name = :'System Administrator' limit 1];
        User pu = new User(profileId = p.id, username = 'tt@ff.com', email = 'pp@cc.com',
        emailencodingkey = 'UTF-8', localesidkey = 'en_US',
        languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles',
        alias='cspu', lastname='lastname');
        insert pu;


        EntitySubscription es = new EntitySubscription ( ParentId = c.id, SubscriberId = pu.Id);
        insert es;
        
        System.debug('UserInfo.getProfileId()2' + UserInfo.getProfileId());
       
            FeedItem feed = new FeedItem (ParentId = c.id,Body = 'Hello', type = 'TextPost');
            insert feed;
      
       
        
    }
}