I wrote a Class and a Test class I got the coverage to 60% however I am stuck on getting the rest. The lines that are not passing is that if my query is empty update fields. I am not sure how to test this.
trigger SetAsHomePage on Page__c (after insert, after update) {
private List<Page__c> tounflag {get;set;}
if(trigger.new.size() > 1) return;
for(Page__c p:Trigger.new) {
if(!p.Home_Page__c) continue;
tounflag = [select Id, Name from Page__c where Home_Page__c = true and Site_Id__c =:p.Site_Id__c and Id !=:p.Id];
if(tounflag.isEmpty()) continue;
for(Page__c pu:tounflag) {
pu.Home_Page__c = false;
pu.Site_Id__c = null;
}
update tounflag;
}
Public Static testMethod void SetAsNoPagesFound()
{
Site site = [select Id from Site limit 1];
String siteid = site.Id;
//set up page and template folders
CMSFolder__c pagefolder = new CMSFolder__c(Site_Id__c = siteid.substring(0,15),Type__c = 'Page', Name='test');
insert pagefolder;
CMSFolder__c templatefolder = new CMSFolder__c(Type__c = 'PageTemplate', Name='test');
insert templatefolder;
//set up a test template
PageTemplate__c template = new PageTemplate__c(Name='test', VisualForce_Page_Name__c='TestTemplate', ContentBlockNames__c = 'a,b,c', Folder__c = templatefolder.Id);
insert template;
//set up a test page
Page__c pg = new Page__c(Name='testpage', PageTemplate__c = template.Id, Home_Page__c = true, Folder__c = pagefolder.Id);
insert pg;
pg.Home_Page__c = False;
update pg;
}
}