You need to sign in to do that
Don't have an account?
Jason Adler 9
Test class for SaveAsPdfExtension!
Can anyone help to write a test class for the SaveAsPdfExtension seen here (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_output_pdf_print_as_pdf_button.htm):
public class SaveAsPdfExtension {
// Required extension constructor (empty, no-op)
public SaveAsPDFExtension(ApexPages.StandardController controller) {}
// Determines what kind of rendering to use for the page request
public String renderingService { get; private set; }
// Allow the page to set the PDF file name
public String renderedFileName {
get;
set { renderedFileName = this.sanitizeFileName(value); }
}
// Rendered content MIME type, used to affect HTTP response
public String renderedContentType {
get {
String renderedContentType = 'text/html'; // the default
if( ! this.renderingAsHtml() ) {
// Provides a MIME type for a PDF document
renderedContentType = 'application/pdf';
// Add a file name for the PDF file
if( this.renderedFileName != null) {
// This is supposed to set the file name, but it doesn't work
renderedContentType += '#' + this.renderedFileName;
// This is a work-around to set the file name
ApexPages.currentPage().getHeaders().put(
'content-disposition', 'attachment; filename=' +
this.renderedFileName);
}
}
return renderedContentType;
}
}
// Are we rendering to HTML or PDF?
public Boolean renderingAsHtml() {
return ( (renderingService == null) ||
( ! renderingService.startsWith('PDF')) );
}
// Action method to save (or "print") to PDF
public PageReference saveToPdf() {
renderingService = 'PDF';
return null;
}
// Private helper -- basic, conservative santization
private String sanitizeFileName(String unsafeName) {
String allowedCharacters = '0-9a-zA-Z-_.';
String sanitizedName =
unsafeName.replaceAll('[^' + allowedCharacters + ']', '');
// You might also want to check filename length,
// that the filename ends in '.pdf', etc.
return(sanitizedName);
}
}
public class SaveAsPdfExtension {
// Required extension constructor (empty, no-op)
public SaveAsPDFExtension(ApexPages.StandardController controller) {}
// Determines what kind of rendering to use for the page request
public String renderingService { get; private set; }
// Allow the page to set the PDF file name
public String renderedFileName {
get;
set { renderedFileName = this.sanitizeFileName(value); }
}
// Rendered content MIME type, used to affect HTTP response
public String renderedContentType {
get {
String renderedContentType = 'text/html'; // the default
if( ! this.renderingAsHtml() ) {
// Provides a MIME type for a PDF document
renderedContentType = 'application/pdf';
// Add a file name for the PDF file
if( this.renderedFileName != null) {
// This is supposed to set the file name, but it doesn't work
renderedContentType += '#' + this.renderedFileName;
// This is a work-around to set the file name
ApexPages.currentPage().getHeaders().put(
'content-disposition', 'attachment; filename=' +
this.renderedFileName);
}
}
return renderedContentType;
}
}
// Are we rendering to HTML or PDF?
public Boolean renderingAsHtml() {
return ( (renderingService == null) ||
( ! renderingService.startsWith('PDF')) );
}
// Action method to save (or "print") to PDF
public PageReference saveToPdf() {
renderingService = 'PDF';
return null;
}
// Private helper -- basic, conservative santization
private String sanitizeFileName(String unsafeName) {
String allowedCharacters = '0-9a-zA-Z-_.';
String sanitizedName =
unsafeName.replaceAll('[^' + allowedCharacters + ']', '');
// You might also want to check filename length,
// that the filename ends in '.pdf', etc.
return(sanitizedName);
}
}
Look at the test class example here