
Hi there,
I have created a VF page to display an image attachment from a Contact on a custom object. The VF page and controller are working, but I am having trouble with the test class for it. The problem I am having is that I am not sure how to reference the current record in the test.
Controller:
Public Class displayImageExtension {
public Fund_Relationships__c currentRecord {get; set;}
public displayImageExtension(ApexPages.StandardController controller) {
currentRecord = [SELECT Id, Contact__c FROM
Fund_Relationships__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}
public String getFileId() {
String fileId = '';
List<Attachment> attachedFiles = [select Id FROM Attachment WHERE parentId =:currentRecord.Contact__c AND Name LIKE '%Sig%' order By LastModifiedDate DESC limit 1];
if (attachedFiles.size() == 0){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'No signature image found'));
return null;
}
if( attachedFiles != null && attachedFiles.size() > 0 ) {
fileId = attachedFiles[0].Id;
}
return fileId;
}
}
VF Page
<apex:page standardController="Fund_Relationships__c" extensions="displayImageExtension">
<apex:form >
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}" width="300" rendered="{!IF(FileId !="",True,False)}"/>
<apex:pageMessages id="showmsg"></apex:pageMessages>
</apex:form>
</apex:page>
Test Class (23% coverage)
@isTest
private class TestdisplayImageExtension {
static testMethod void MyTest(){
Account myaccount = new Account (name='Test');
insert myaccount;
Contact mycontact = new Contact (firstname='Billy', lastname='Baggins');
insert mycontact;
Attachment attach = new Attachment();
attach.Name = 'Signature_File';
Blob bodyBlob = Blob.valueOf('Testing Body of Attachment');
attach.body = bodyBlob;
attach.parentId = mycontact.id;
insert attach;
Fund__c myfund = new Fund__c();
myfund.Name = 'Test Fund';
myfund.Account__c = myaccount.id;
myfund.Entity__c = 'SIF';
myfund.Fund_Type__c = 'Social Enterprise Loan';
myfund.Type__c = 'Term Loan';
myfund.Status__c = 'New';
myfund.Open_Date__c = date.today();
insert myfund;
Fund_Relationships__c fundauth = new Fund_Relationships__c();
fundauth.Contact__c = mycontact.id;
fundauth.Fund__c = myfund.id;
fundauth.Role__c = 'General';
fundauth.Role_Status__c = 'Current';
fundauth.Start_Date__c = date.today();
insert fundauth;
displayImageExtension controller = new displayImageExtension(new ApexPages.StandardController(fundauth));
List < Attachment > attachedFiles = [select id, name from Attachment where parent.id = : mycontact.id];
System.assertEquals(1, attachedFiles.size());
String newAttachId = controller.getFileId();
System.assertEquals(newAttachId, attachedFiles[0].Id);
}
}
Any help with this would be greatly appreciated!
-- Ken
Try this code..change VF page na,me @isTest
private class TestdisplayImageExtension {
static testMethod void MyTest(){
Account myaccount = new Account (name='Test');
insert myaccount;
Contact mycontact = new Contact (firstname='Billy', lastname='Baggins');
insert mycontact;
Attachment attach = new Attachment();
attach.Name = 'Signature_File';
Blob bodyBlob = Blob.valueOf('Testing Body of Attachment');
attach.body = bodyBlob;
attach.parentId = mycontact.id;
insert attach;
Fund__c myfund = new Fund__c();
myfund.Name = 'Test Fund';
myfund.Account__c = myaccount.id;
myfund.Entity__c = 'SIF';
myfund.Fund_Type__c = 'Social Enterprise Loan';
myfund.Type__c = 'Term Loan';
myfund.Status__c = 'New';
myfund.Open_Date__c = date.today();
insert myfund;
Fund_Relationships__c fundauth = new Fund_Relationships__c();
fundauth.Contact__c = mycontact.id;
fundauth.Fund__c = myfund.id;
fundauth.Role__c = 'General';
fundauth.Role_Status__c = 'Current';
fundauth.Start_Date__c = date.today();
insert fundauth;
PageReference pageRef = Page.YOUTVFPAGEAPINAME;
Test.setCurrentPage(pageRef);
// put the lead id as a parameter
ApexPages.currentPage().getParameters().put('id',fundauth.id);
displayImageExtension controller = new displayImageExtension(new ApexPages.StandardController(fundauth));
List < Attachment > attachedFiles = [select id, name from Attachment where parent.id = : mycontact.id];
System.assertEquals(1, attachedFiles.size());
String newAttachId = controller.getFileId();
System.assertEquals(newAttachId, attachedFiles[0].Id);
}
}