Testing Content
Testing extensions of cms.ContentTemplateController is accomplished by using the class cms.TestControllerFactory. The TestControllerFactory wraps a ContentTemplateController instance and allows a developer to easily set test values for use by the controller.
Example
The following example shows a very simple article content template that include properties for a title and a body, along with a class that provides 100% test coverage with strong assertions for all parts of the code.
global class SimpleArticle extends cms.ContentTemplateController {
public String title {
get {
String storedTitle = getProperty('title');
if (storedTitle == null) return '';
return storedTitle;
}
}
public String body {
get {
return getProperty('body');
}
}
public override String getHtml() {
return '<article>'
+ '<h1>' + title.escapeHtml4() + '</h1>'
+ '<div>' + body + '</div>'
+ '</article>';
}
public SimpleArticle() {
super();
}
}
The following class tests the above SimpleArticle template.
@isTest
private class SimpleArticle_Test {
static SimpleArticle controller;
static cms.TestControllerFactory testFactory;
static void setup() {
controller = new SimpleArticle();
testFactory = new cms.TestControllerFactory(controller);
}
@isTest static void testTitle_null() {
setup();
testFactory.addContentProperty('title', null);
System.assertEquals('', controller.title, 'Title accessor returns empty string for a stored null.');
}
@isTest static void testTitle_value() {
setup();
testFactory.addContentProperty('title', 'testTitle');
System.assertEquals('testTitle', controller.title, 'Title accessor returns the stored value.');
}
@isTest static void testBody() {
setup();
testFactory.addContentProperty('body', 'testBody');
System.assertEquals('testBody', controller.body, 'Body accessor returns the stored value.');
}
@isTest static void testGetHtml() {
setup();
String storedTitle = 'Title < with > html & characters';
String storedBody = '<p>Body</p>';
testFactory.addContentProperty('title', storedTitle);
testFactory.addContentProperty('body', storedBody);
String html = controller.getHtml();
System.assert(html.contains(storedTitle.escapeHtml4()), 'getHtml should contain the html-escaped title');
System.assert(html.contains(storedBody), 'getHtml should contain the unescaped body');
}
}
cms.TestControllerFactory
Signatures
- cms.TestControllerFactory(cms.ContentTemplateController controller)
- cms.TestControllerFactory(cms.ContentTemplateController controller, String siteName)
Parameters
- controller : The cms.ContentTemplateController instance to wrap
- siteName : The site name to use for testing (defaults to 'TestSite')
addContentProperty
Sets a property name value pair for use by the wrapped ContentTemplateController in getProperty calls.
Signatures
- addContentProperty(String name, String value)
- addContentProperty(String name, String value, String valueType)
- addContentProperty(String name, String value, Boolean overwrite)
- addContentProperty(String name, String value, String valueType, Boolean overwrite)
Parameters
- name : The name of the property to set
- value : The site name to use for testing
- valueType : The property type (default: 'Text')
- overwrite : Whether or not to overwrite an existing property with the same name (default: true)
addCLIProperty
Sets a property name value pair for use by the wrapped ContentTemplateController in getLayoutInstanceProperty calls.
Signatures
- addCLIProperty(String name, String value)
- addCLIProperty(String name, String value, String valueType)
- addCLIProperty(String name, String value, Boolean overwrite)
- addCLIProperty(String name, String value, String valueType, Boolean overwrite)
Parameters
- name : The name of the property to set
- value : The site name to use for testing
- valueType : The property type (default: 'Text')
- overwrite : Whether or not to overwrite an existing property with the same name (default: true)
addPCLIProperty
Sets a property name value pair for use by the wrapped ContentTemplateController in getPageProperty calls.
Signatures
- addPCLIProperty(String name, String value)
- addPCLIProperty(String name, String value, String valueType)
- addPCLIProperty(String name, String value, Boolean overwrite)
- addPCLIProperty(String name, String value, String valueType, Boolean overwrite)
Parameters
- name : The name of the property to set
- value : The site name to use for testing
- valueType : The property type (default: 'Text')
- overwrite : Whether or not to overwrite an existing property with the same name (default: true)
setOriginalPublishStartDate
Sets the origin publish start date for use by the wrapped ContentTemplateController in getOriginalPublishedStartDate calls.
Signatures
- setOriginalPublishStartDate(DateTime startDate)
Parameters
- startDate : The origin publish start date to test with
setRenderLanguage
Sets the language code for use by the wrapped ContentTemplateController in getRenderLanguage calls.
Signatures
- setRenderLanguage(String languageCode)
Parameters
- languageCode : The language code to set
setContent
Instead of setting individual properties, this method takes a fully-created ContentBundle.
Signatures
- setContent(ContentBundle bundle)
Parameters
- bundle : A fully initialized ContentBundle