I have a process that has been built that presents a visualforce page to the user, they first select a price book, and a section on page is refreshed. They are presented with a list of products from that price book. Once they select a product, a record is created on the Quoted_Products__c custom object. This process works fine and my problem is I have no idea how to write a unit test for simulating user selections on the visualforce page.
I have no test code to show as I have no idea where start. Doing some Google Fu I am only finding how to create test users and to run a test as a user and the like. I am not seeing anything on how to simulate a user making selections. Do I just make some assumptions on user selection and then run the very last process that creates the records? If so, how to set the required variables into the test page environment?
Below is my Controller and the Visualforce page. Any pointers would be great.
Visualforce Page
<apex:page Controller="queryProduct">
<apex:pageBlock >
<apex:pageBlockSection title="Active Quote Detail">
<apex:pageBlockTable value="{! quotedetails }" var="qd">
<apex:column value="{! qd.Id }"/>
<apex:column value="{! qd.Name }"/>
<apex:column value="{! qd.Account__r.Name }"/>
<apex:column value="{! qd.CurrencyIsocode }"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:pageBlockSection title="Pricebook selection">
<apex:form >
<apex:outputLabel value="Which Pricebook? :"/>
<apex:selectList value="{! selected_Pricebook}" size="1">
<apex:selectOptions value="{! pricebook_name}"/>
</apex:selectList>
<br /><br />
<apex:commandButton value="Show Products" action="{!search}" rerender="out"/>
<apex:commandButton value="Clear" action="{!clear}"/>
</apex:form>
</apex:pageBlockSection>
<apex:outputPanel id="out">
<apex:pageBlock title="Selected PriceBook" rendered="{!If(selected_Pricebook !=null,true,false)}">
<apex:pageBlockSection >
<apex:outputText value="{!selected_Pricebook}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Product Selection" rendered="{!If(selected_Pricebook !=null,true,false)}">
<br />
<apex:form >
Product Filter:
<apex:inputText id="filterValue" value="{!filterValue}" />
<apex:commandButton value="Filter" action="{!search}" rerender="out"/>
Enter characters to filter product selection.
<br /><br /><br />
</apex:form>
<apex:form >
<apex:pageblocktable id="allprod" value="{!Products}" var="allprod">
<apex:column headervalue="Select Product">
<apex:actionsupport action="{!useProduct}" event="onclick" rerender="out2">
<input type="radio" />
<apex:param name="selected_Product" value="{!allprod.Product2Id}">
</apex:param>
</apex:actionsupport>
</apex:column>
<apex:column headervalue="Product Code">
<apex:outputfield value="{!allprod.Name}">
</apex:outputfield></apex:column>
<apex:column headervalue="Product Code">
<apex:outputfield value="{!allprod.Product2.ProductCode}">
</apex:outputfield></apex:column>
<apex:column headervalue="Product Description">
<apex:outputfield value="{!allprod.Product2.Description}">
</apex:outputfield></apex:column>
</apex:pageblocktable>
</apex:form>
</apex:pageBlock>
<apex:outputPanel rendered="{! ProdError }">
<h1><div align="center">
There are no products that match the currency of this quote in this PriceBook?
</div></h1>
</apex:outputPanel>
</apex:outputPanel>
<apex:outputPanel id="out2" rendered="{!If(selected_Product !=null,true,false)}">
<p>You have selected:</p>
<apex:outputText value="{!selected_Product}"/>
</apex:outputPanel>
</apex:pageBlock>
Apex Controller
public class queryProduct {
//unit test ideas, need to add data into the fields via script and submit it seems
//https://developer.salesforce.com/forums/?id=906F0000000ALYKIA4
//Variables for working with the quote
public string quoteId;
public static List<Quote__c> myQuote;
//Variables for working with the pricebook
public String selected_Pricebook {get;set;}
public List<String> tmpString;// {get; set;}
public List<SelectOption> pricebook_name {get; set;}
public boolean ProdError { get; set; }
//Variables for working with the selected product
public string selected_Product {get;set;}
public string filterValue{get; set;}
public static List<Quote__c> getQuotedetails() {
string quoteId = ApexPages.currentPage().getparameters().get('Id');
System.debug( 'Quote Id is ' + quoteId);
myQuote = [SELECT Id, Name, Account__r.Name, Opportunity__r.Name, CurrencyIsoCode FROM Quote__c where Id =:quoteId];
system.debug(string.valueOf(myQuote));
return myQuote;
}
public queryProduct(){
//Getting list of price books to show user. This list from their user profile assignment.
pricebook_name = new List<SelectOption>();
tmpString = new List<String>();
User a = [SELECT PriceBooks__c
FROM User
WHERE Id =:UserInfo.getUserId()];
tmpString.addAll(a.PriceBooks__c.split(';'));
System.debug( 'Price books ' + tmpString);
for( String b : tmpString ) {
pricebook_name.add( new SelectOption( b, b ) );
}
}
public List<PricebookEntry> getProducts(){
//Getting the products in the selected price book
//Need to turn the string into and ID first.
System.debug( 'in getProducts, selected pricebook is: ' + selected_Pricebook);
List<Pricebook2> pId = [Select Id from PriceBook2 where Name=:selected_Pricebook limit 1];
List<PricebookEntry> productList;
String baseQuery = 'Select Name, Product2Id, Product2.ProductCode, Product2.Description,UnitPrice from PriceBookEntry where Pricebook2Id=\''+pId[0].Id+'\' and CurrencyIsoCode =\''+myQuote[0].CurrencyIsoCode+'\'';
if( !string.ISBLANK(filterValue) ) {
string searchValue = '\'%'+ filterValue +'%\'';
baseQuery = baseQuery + 'and Name like '+searchValue;
}
System.debug( 'in getProducts, product list query is: ' + baseQuery);
productList = database.query(baseQuery);
//Is there products to return? or a blank result set?
if( productList.isEmpty() ) {
ProdError = TRUE;
} else {
ProdError = FALSE;
}
System.debug('ProdError status: '+ProdError);
System.debug('in getProducts prodlist var is: '+productList);
return productList;
}
public PageReference search(){
return null;
}
public PageReference useProduct(){
//Need that price book ID again.
List<Pricebook2> pId = [Select Id from PriceBook2 where Name=:selected_Pricebook limit 1];
System.debug( 'in useProduct stage, Price book is seen as - ' + pId );
String selected_Product = System.currentPagereference().getParameters().get('selected_Product');
System.debug( 'in useProduct and the selected_Product ' + selected_Product);
List<Product2> p = [select Description, ProductCode from Product2 WHERE Id=:selected_Product];
System.debug( 'in useProduct stage, Product query is - ' + p );
List<PricebookEntry> pb = [select UnitPrice from PricebookEntry WHERE Product2Id=:selected_Product and CurrencyIsoCode =:myQuote[0].CurrencyIsoCode];
System.debug( 'in useProduct stage, PriceBookEntry is - ' + pb );
//Create new quoted product record, now that all the data is collected
QuotedProduct__c qp = new QuotedProduct__c(Quote__c = myQuote[0].Id,
CurrencyIsoCode = myQuote[0].CurrencyIsoCode,
Price_Book__c = pId[0].Id,
Product__c = selected_Product,
ListPrice__c = pb[0].UnitPrice,
Starting_Price__c = pb[0].UnitPrice,
Quantity__c = 1,
ProductCode2__c = p[0].ProductCode,
ProductDescription2__c = p[0].Description
);
try{
insert qp;
} catch (System.DmlException e){
System.debug('ERROR: Creating the Quoted Product:' + e);
}
//Send user back to the new quoted Product record
PageReference pageRef = new PageReference('/' + qp.Id);
pageRef.setRedirect(true);
return pageRef;
}
public void clear(){
selected_Pricebook = null;
}}
Thanks,