+ Start a Discussion
Bertrand DBBertrand DB 

Help for a test class: custom controller and pass string

Hello,
I'm trying to create a test class for the following:

A custom controller:
public class TextVFPDFController {
    public Opportunity opp{get;set;}
    public OpportunityContactRole oppRole{get;set;}
    public Account oppAccount{get;set;}
    public Contact oppContactRole{get;set;}
    public TextVFPDFController(){
        Id oppId = apexpages.currentpage().getparameters().get('id');
		opp = [select id,Name,AccountId,tech_minimum_invoiced_replication__c from opportunity where id=: oppId];
        oppRole = [select ContactId,IsPrimary from OpportunityContactRole where OpportunityId=:oppId];
        oppContactRole = [select Id,LastName,FirstName,Phone,MobilePhone,Email from Contact where Id=:oppRole.ContactId];
        oppAccount = [select Id,name,BillingStreet,BillingPostalCode,BillingCity,BillingCountry from Account where Id=:opp.AccountId];
    }
}

This (creates a PDF):
public class PDFController {
	@auraEnabled
    public static void savePDFOpportunity(String recordId, String whichButton, String name, String file){
        PageReference pdfPage = new PageReference('/apex/'+whichButton);
        pdfPage.getParameters().put('Id', recordId);
        Blob pdfContent = pdfPage.getContent();

        // We define nice names for the document generated, according to the button variable
if (whichButton=='Bon_de_commande_Lunch_Pass'){
    file='Bon de commande.pdf';
    name='Bon de commande';
} else if (whichButton=='Contract'){
    file='Contrat.pdf';
    name='Contrat';
} else {
    file='Document.pdf';
    name='Document';
}
        
ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
conVer.PathOnClient=file;// The files name, extension is very important here which will help the file in preview.
conVer.Title = name; // Display name of the files
conVer.VersionData = pdfContent; // converting your binary string to Blog
insert conVer;
     
// First get the content document Id from ContentVersion
Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
 
//Create ContentDocumentLink
ContentDocumentLink cDe = new ContentDocumentLink();
cDe.ContentDocumentId = conDoc;
cDe.LinkedEntityId = recordId; // you can use objectId,GroupId etc
cDe.ShareType = 'V'; // Inferred permission, checkout description of ContentDocumentLink object for more details
cDe.Visibility = 'AllUsers';
insert cDe;
        
    }
}

My Visualforce page is like:
<apex:page Controller="TextVFPDFController" showHeader="false" applyHtmlTag="false" renderAs="pdf">
{!oppAccount.Name}
{!oppContactRole.LastName}
</apex:page>

I don't know to handle all of this. Shall I create one or two test classes, as they both work together?

So far, I only did this:
@isTest
public class testTextVFPDFController {
	 static testMethod void globaldata() 
	 {
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
        testAccount.BillingStreet='3 Paris street' ;
		testAccount.BillingPostalCode='75015' ;
        testAccount.BillingCity='Paris' ;
        testAccount.BillingCountry='France' ;
		insert testAccount;
         
		Contact cont = new Contact ();
		cont.FirstName = 'Steve';
		cont.LastName = 'Jobs';
		cont.Email='steve@email.com';
		cont.Phone='12345678';
        cont.MobilePhone='234567890';
        cont.AccountId=testAccount.Id;
		insert cont;
                 
		Opportunity opp = new Opportunity ();
		opp.Name='Test Opportunity' ;
        opp.StageName='New';
        opp.CloseDate=System.today().addMonths(1);
        opp.AccountId=testAccount.Id ;
        opp.tech_minimum_invoiced_replication__c=20 ;
		insert opp;
        
        OpportunityContactRole contactrole = new OpportunityContactRole();
        contactrole.IsPrimary = true;
        contactrole.ContactId=cont.Id;
        contactrole.OpportunityId = opp.Id;
        insert contactrole;
        
		Test.StartTest(); 

		Test.StopTest();
	 }
}

Any suggestion?

Thank you!
Best Answer chosen by Bertrand DB
Bertrand DBBertrand DB
Finally did it:

TextVFPDFController, PDFController 
@isTest
public class testTextVFPDFController {
	 static testMethod void globaldata() 
	 {
		Account testAccount = new Account();
// my data
		insert testAccount;
         
		Contact cont = new Contact ();
// my data
		insert cont;
                 
		Opportunity opp = new Opportunity ();
// my data
		insert opp;
        
        OpportunityContactRole contactrole = new OpportunityContactRole();
// my data
        insert contactrole;

        Test.StartTest();
        PageReference pgRef = Page.MyVisualForcePage;
        Test.setCurrentPage(pgRef);
		ApexPages.currentPage().getParameters().put('id', opp.Id);
		TextVFPDFController testPage = new TextVFPDFController();
        Test.StopTest();
 
         
	 }
}
@isTest
public class testPDFController {

	 static testMethod void isFirstCase() 
	 {
		Account testAccount = new Account();
// My data
		insert testAccount;
         
		Contact cont = new Contact ();
// My data
		insert cont;
                 
		Opportunity opp = new Opportunity ();
// My data
		insert opp;
        
        OpportunityContactRole contactrole = new OpportunityContactRole();
// My data
        insert contactrole;

        Test.StartTest();
        PageReference pgRef = Page.FirstCase;
        Test.setCurrentPage(pgRef);
        PDFController.savePDFOpportunity(opp.Id,'VF Page','','');
        Test.StopTest();
 
         
	 }
    
	 static testMethod void is2ndCase() 
	 {
		Account testAccount = new Account();
// My data
		insert testAccount;
         
		Contact cont = new Contact ();
// My data
		insert cont;
                 
		Opportunity opp = new Opportunity ();
// My data
		insert opp;
        
        OpportunityContactRole contactrole = new OpportunityContactRole();
// My data
        insert contactrole;

        Test.StartTest();
        PageReference pgRef = Page.MyVFPage;
        Test.setCurrentPage(pgRef);
        PDFController.savePDFOpportunity(opp.Id,'2ndCase','','');
        Test.StopTest();
 
         
	 }
	 static testMethod void isOther() 
	 {
		Account testAccount = new Account();
// My data
		insert testAccount;
         
		Contact cont = new Contact ();
// My data
		insert cont;
                 
		Opportunity opp = new Opportunity ();
// My data
		insert opp;
        
        OpportunityContactRole contactrole = new OpportunityContactRole();
// My data
        insert contactrole;

        Test.StartTest();
        PageReference pgRef = Page.MyVFPage;
        Test.setCurrentPage(pgRef);
        PDFController.savePDFOpportunity(opp.Id,'Random value','','');
        Test.StopTest();
 
         
	 }
}