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
Rishant KumarRishant Kumar 

Hi everyone Help me to write a test class for the idea home page controller apx

public class IdeaHomeController {
    
    
    public class IdeaDetail{
        @AuraEnabled
        public string ideaid;
        @AuraEnabled
        public string ideaownername;
        @AuraEnabled
        public string ideaownerphotourl;
        @AuraEnabled
        public string title;
        @AuraEnabled
        public string ideabody;
        @AuraEnabled
        public List<Comment> comments;
        @AuraEnabled
        public string VoteScore;
        @AuraEnabled
        public string NumComments;  
        @AuraEnabled
        public boolean showeditbtn;
        @AuraEnabled
        public string categories;
    }
    
    public class Comment{
        
        @AuraEnabled
        public string commentownerphotourl;
        @AuraEnabled
        public string commentownername;
        @AuraEnabled
        public string commentbody;
        @AuraEnabled
        public datetime createddate;
        @AuraEnabled
        public boolean showeditbtn;
        @AuraEnabled
        public string commentid;
    }
    
    @AuraEnabled
    public static boolean CurrentIdeaId(String ideaId){
        return true;
    }
    
    @AuraEnabled
    public static IdeaDetail getmycommentsonly(String ideaid){
        Idea IdeaRec = new Idea();
        List<IdeaComment> ideaCommentList = new List<IdeaComment>();
        list<comment> commentwrapperList = new list<comment>();
        IdeaDetail ideawrapper = new IdeaDetail();
        ideawrapper = getIdeaDetails(ideaid);
        integer count = 0;
        for(Comment ic : ideawrapper.comments){
            if(ic.showeditbtn){
                Comment comm = new Comment();
                comm.commentownerphotourl = ic.commentownerphotourl;
                comm.commentownername = ic.commentownername;
                comm.commentbody = ic.commentbody;
                comm.createddate = ic.createddate;
                comm.commentid = ic.commentid;
                comm.showeditbtn = ic.showeditbtn;
                commentwrapperList.add(comm);
            }
        }
        ideawrapper.comments  = commentwrapperList;
        return ideawrapper;
    }
    
    @AuraEnabled
    public static IdeaDetail getIdeaDetails(String ideaid){
        
        Idea IdeaRec = new Idea();
        List<IdeaComment> ideaCommentList = new List<IdeaComment>();
        list<comment> commentwrapperList = new list<comment>();
        IdeaDetail ideawrapper = new IdeaDetail();
        
        if(Schema.SObjectType.Idea.isQueryable() && 
           Schema.sObjectType.Idea.fields.ID.isAccessible() &&
           Schema.sObjectType.Idea.fields.Title.isAccessible() &&
           Schema.sObjectType.Idea.fields.Body.isAccessible()&&
           Schema.sObjectType.Idea.fields.Categories.isAccessible() &&
           Schema.sObjectType.Idea.fields.CreatorName.isAccessible() &&
           Schema.sObjectType.Idea.fields.NumComments.isAccessible() &&
           Schema.sObjectType.Idea.fields.CommunityId.isAccessible() &&
           Schema.sObjectType.Idea.fields.CreatorSmallPhotoUrl.isAccessible() &&
           Schema.sObjectType.Idea.fields.VoteScore.isAccessible() &&
          Schema.sObjectType.Idea.fields.createdById.isAccessible()){
              system.debug('ideaid: '+ideaid);
               IdeaRec = [Select Id,CreatorSmallPhotoUrl,Categories,VoteScore,Title,IsHtml,Body,CommunityId,CreatorName, NumComments,createdById,CreatedDate From Idea Where id =:ideaid];
               system.debug('IdeaRec: '+IdeaRec);
           }
        
        if(Schema.SObjectType.IdeaComment.isQueryable()&& 
           Schema.sObjectType.IdeaComment.fields.Id.isAccessible() &&
           Schema.sObjectType.IdeaComment.fields.CommentBody.isAccessible() &&
           Schema.sObjectType.IdeaComment.fields.UpVotes.isAccessible() &&
           Schema.sObjectType.IdeaComment.fields.CreatorName.isAccessible() &&
           Schema.sObjectType.IdeaComment.fields.IdeaId.isAccessible() &&
           Schema.sObjectType.IdeaComment.fields.createdById.isAccessible()){
               ideaCommentList = [Select Id, CommentBody, CreatorSmallPhotoUrl, CreatorName,CreatedDate,createdById, IdeaId, Idea.Title From IdeaComment Where IdeaId = :IdeaId order By createddate desc  LIMIT 50000  ];
               system.debug('ideaCommentList: '+ideaCommentList);
           }
        
        if(ideaCommentList != null && ideaCommentList.size()>0){
            for(IdeaComment idcomm : ideaCommentList){
                Comment comm = new Comment();
                comm.commentownerphotourl = idcomm.CreatorSmallPhotoUrl;
                comm.commentownername = idcomm.CreatorName;
                comm.commentbody = idcomm.CommentBody;
                comm.createddate = idcomm.CreatedDate;
                comm.commentid = String.valueOf(idcomm.Id).substring(0,15);
                if(userinfo.getUserId().equalsIgnoreCase(idcomm.CreatedById)){
                    comm.showeditbtn = true;
                }else{
                    comm.showeditbtn = false;
                }
                commentwrapperList.add(comm);
            }
        }
        
        system.debug('commentwrapperList: '+commentwrapperList);
        ideawrapper.ideaid = IdeaRec.Id;
        ideawrapper.ideaownerphotourl = IdeaRec.CreatorSmallPhotoUrl;
        ideawrapper.ideaownername = IdeaRec.CreatorName;
        ideawrapper.title = IdeaRec.title;
        ideawrapper.ideabody = IdeaRec.body;
        ideawrapper.comments = commentwrapperList;
        ideawrapper.VoteScore = String.valueOf(IdeaRec.VoteScore);
        ideawrapper.NumComments = String.valueOf(IdeaRec.NumComments);
        ideawrapper.Categories = IdeaRec.Categories;
        if(userinfo.getUserId().equalsIgnoreCase(IdeaRec.CreatedById)){
            ideawrapper.showeditbtn = true;
        }else{
            ideawrapper.showeditbtn = false;
        }
        
        return ideawrapper;
    }
    
    @AuraEnabled
    public static List<Idea> InsertIdea(String CommunityId, String title, string description, string categories){
        
        String nid = Network.getNetworkId();
        IF(CommunityId=='None' || CommunityId=='undefined' || CommunityId==null){
            List<Community> idealst = new List<Community>();
            if(Schema.sObjectType.Community.fields.ID.isAccessible()&&
               Schema.sObjectType.Community.fields.IsActive.isAccessible()&&
               Schema.sObjectType.Community.fields.Name.isAccessible())
                idealst =[Select Id, Name From Community WHERE IsActive=true and networkId = :nid LIMIT 1];
            if(idealst!=null && idealst.size()>0){
                CommunityId = idealst[0].id;
            } 
        }
        
        Idea i = new Idea();
        i.Title = title;
        i.Body = description;
        i.Categories = categories;
        i.CommunityId = CommunityId;
       
        Insert i;
        
        List<Idea> idealist = AllIdeas(CommunityId);
        return idealist;
    }
    
    @AuraEnabled
    public static IdeaDetail UpdateIdeaInSystem(String ideaId, String title, string description, string categories){
        
        Idea i = new Idea();
        i.Id = ideaId;
        i.Title = title;
        i.Body = description;
        i.Categories = categories;
       
        Update i;
        
        IdeaDetail idetail = getIdeaDetails(ideaId);
        return idetail;
    }
    
    @AuraEnabled
    public static IdeaDetail InsertCommenttosystem(String commentBody, String ideaId){
        
        system.debug('commentBody:'+commentBody);
        system.debug('ideaId:'+ideaId);
        IdeaComment ic = new IdeaComment();
        ic.IdeaId = ideaId;
        ic.CommentBody = commentBody;
        Insert ic;
        
        IdeaDetail idetail = getIdeaDetails(ideaId);
        return idetail;
    }
    
    @AuraEnabled
    public static IdeaDetail EditComment(String commentId, String commentBody, String ideaId){
        
        IdeaComment ic = new IdeaComment();
        ic.id = commentId;
        ic.CommentBody = commentBody;
        Update ic;
        
        IdeaDetail idetail = getIdeaDetails(ideaId);
        return idetail;
    }
    
    @AuraEnabled
    public static IdeaDetail deleteComment(String commentId){
        system.debug('commentId: '+commentId);
        IdeaComment idc = [select id, IdeaId from IdeaComment where id = :commentId];
        String ideaid = idc.IdeaId;
        Delete idc;
        IdeaDetail idetail = getIdeaDetails(ideaid);
        return idetail;
    }
    
    @AuraEnabled
    public static IdeaDetail deleteIdea(String ideaid){
        system.debug('commentId: '+ideaid);
        Delete [select id from Idea where id = :ideaid];
        return null;
    }
    
    
    @AuraEnabled(cacheable=true)
    public static List<string> getDetailsOnLoad(){
        List<String> options = new List<String>();
        
        Schema.DescribeFieldResult fieldResult =  Idea.Categories.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
        for( Schema.PicklistEntry f : ple){
            options.add(f.getLabel());
        }          
        return options;
    }
    
    
    @AuraEnabled
    public static List<String> getStatus(){
        List<String> options = new List<String>();
        
        Schema.DescribeFieldResult fieldResult = Idea.Status.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry pickListVal : ple){
            options.add(pickListVal.getLabel());
        }   
        return options;
    }
    
    @AuraEnabled
    public static List<community> getZones(){
        list<community> zonelist = new list<community>();
        String nid = Network.getNetworkId();
        for(community comm : [select id, name from Community where IsActive = true and networkId = :nid]){
            zonelist.add(comm);
        }
        return zonelist;
    }
    
    @AuraEnabled
    public static List<String> getCategories(){
        List<String> pickListValuesList= new List<String>();
        Schema.DescribeFieldResult fieldResult = Idea.Categories.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry pickListVal : ple){
            pickListValuesList.add(pickListVal.getLabel());
        }    
        system.debug(pickListValuesList);
        return pickListValuesList;
    }
    
    
    @AuraEnabled
    public static List<Idea> getStatusIdeas(String Category, String selectedZone, String selectedStatus){
        list<idea> idealist = new list<idea>();
        list<idea> iList = new list<idea>(AllIdeas(selectedZone));
        if(iList != null && iList.size()>0){
            if(String.isNotBlank(Category) && String.isNotBlank(selectedStatus)){
                for(idea idearec : iList){
                    Boolean result = false;
                    Boolean statresult = false;
                    if(String.isNotBlank(idearec.categories)){
                        result = idearec.categories.containsIgnoreCase(Category);
                    }
                    if(String.isNotBlank(idearec.status)){
                        statresult = idearec.status.containsIgnoreCase(selectedStatus);
                    }
                    if(result && statresult){
                        idealist.add(idearec);
                    }
                }
            }else if(String.isNotBlank(selectedStatus)){
                for(idea idearec : iList){
                    Boolean statresult = false;
                    if(String.isNotBlank(idearec.status)){
                        statresult = idearec.status.containsIgnoreCase(selectedStatus);
                    }
                    if(statresult){
                        idealist.add(idearec);
                    }
                }
            }
        }
        return idealist;
    }
    
    @AuraEnabled
    public static List<Idea> getZoneIdeas(String zone){
        list<idea> iList = new list<idea>(AllIdeas(zone));
        return iList;
    }
    
    @AuraEnabled
    public static List<Idea> searchIdeas(string zone, string category, string searchString){
        
        system.debug('VALUES: '+zone+' '+category+' '+searchString);
        String nid = Network.getNetworkId();
        
        IF(zone=='None' || zone=='undefined' || zone==null){
            List<Community> idealst = new List<Community>();
            if(Schema.sObjectType.Community.fields.ID.isAccessible()&&
               Schema.sObjectType.Community.fields.IsActive.isAccessible()&&
               Schema.sObjectType.Community.fields.Name.isAccessible())
                idealst =[Select Id, Name From Community WHERE IsActive=true and networkId = :nid LIMIT 1];
            if(idealst!=null && idealst.size()>0) zone = idealst[0].id;
        }
        String query = 'select Id,CreatorSmallPhotoUrl,Categories,VoteScore,Title,IsHtml,Body,CommunityId,CreatorName, NumComments, CreatedDate from Idea';
        String whereclause = 'where CommunityId =:zone';
        if(String.isNotBlank(searchString)){
            String searchInput ='\'%' + searchString  + '%\'';
            whereclause = whereclause + ' AND Title like ' + searchInput;
        }
        
        List<Idea> idealist = new List<Idea>();
        List<Idea> resultIdeas = Database.query(query + ' ' + whereclause);
        system.debug('resultIdeas: '+resultIdeas);
        
        if(String.isNotBlank(category)){
            for(idea idearec : resultIdeas){
                if(String.isNotBlank(idearec.categories)){
                    Boolean result = idearec.categories.contains(category);
                    if(result){
                        idealist.add(idearec);
                    }
                }
            }
        }else{
            return resultIdeas;
        }
        
        system.debug('idealist'+idealist);
        return idealist;
    }
    
    @AuraEnabled
    public static List<Idea> AllIdeas(string CommunityId){
        System.debug('CommunityId1 '+CommunityId);
        List<Idea> ideasList = new List<Idea>();
        String nid = Network.getNetworkId();
        IF(CommunityId=='None' || CommunityId=='undefined' || CommunityId==null){
            List<Community> idealst = new List<Community>();
            if(Schema.sObjectType.Community.fields.ID.isAccessible()&&
               Schema.sObjectType.Community.fields.IsActive.isAccessible()&&
               Schema.sObjectType.Community.fields.Name.isAccessible())
                idealst =[Select Id, Name From Community WHERE IsActive=true and networkId = :nid LIMIT 1];
            if(idealst!=null && idealst.size()>0) CommunityId = idealst[0].id;
            system.debug('first');
            
        }
        System.debug('CommunityId '+CommunityId);
        if(Schema.SObjectType.Idea.isQueryable() && 
           Schema.sObjectType.Idea.fields.ID.isAccessible() &&
           Schema.sObjectType.Idea.fields.Title.isAccessible() &&
           Schema.sObjectType.Idea.fields.Body.isAccessible()&&
           Schema.sObjectType.Idea.fields.Categories.isAccessible() &&
           Schema.sObjectType.Idea.fields.CreatorName.isAccessible() &&
           Schema.sObjectType.Idea.fields.NumComments.isAccessible() &&
           Schema.sObjectType.Idea.fields.CommunityId.isAccessible() &&
           Schema.sObjectType.Idea.fields.CreatorSmallPhotoUrl.isAccessible() &&
           Schema.sObjectType.Idea.fields.VoteScore.isAccessible() &&
           Schema.sObjectType.Idea.fields.Status.isAccessible()){
               
               system.debug('second');
               ideasList = [Select Id,CreatorSmallPhotoUrl,Categories,Status,VoteScore,Title,IsHtml,Body,CommunityId,CreatorName, NumComments, CreatedDate From Idea Where CommunityId =:CommunityId order by createddate desc LIMIT 50000];
           }
        
        System.debug('ideasList '+ideasList);
        return ideasList;
    }
    
}