mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
MINOR: Refactor the SSViewerTest to make it easier to add more tests.
From: Sam Minnee <sam@silverstripe.com> git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@93718 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
66e4b42942
commit
ae04c15e35
@ -13,6 +13,16 @@ class SSViewerTest extends SapphireTest {
|
|||||||
$this->assertEquals('Test partial template: var value', trim(preg_replace("/<!--.*-->/U",'',$result)));
|
$this->assertEquals('Test partial template: var value', trim(preg_replace("/<!--.*-->/U",'',$result)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small helper to render templates from strings
|
||||||
|
*/
|
||||||
|
function render($templateString, $data = null) {
|
||||||
|
$t = SSViewer::fromString($templateString);
|
||||||
|
if(!$data) $data = new SSViewerTestFixture();
|
||||||
|
return $t->process($data);
|
||||||
|
}
|
||||||
|
|
||||||
function testRequirements() {
|
function testRequirements() {
|
||||||
$requirements = $this->getMock("Requirements_Backend", array("javascript", "css"));
|
$requirements = $this->getMock("Requirements_Backend", array("javascript", "css"));
|
||||||
$jsFile = 'sapphire/tests/forms/a.js';
|
$jsFile = 'sapphire/tests/forms/a.js';
|
||||||
@ -23,92 +33,124 @@ class SSViewerTest extends SapphireTest {
|
|||||||
|
|
||||||
Requirements::set_backend($requirements);
|
Requirements::set_backend($requirements);
|
||||||
|
|
||||||
$data = new ArrayData(array());
|
$template = $this->render("<% require javascript($jsFile) %>
|
||||||
|
<% require css($cssFile) %>");
|
||||||
$viewer = SSViewer::fromString(<<<SS
|
|
||||||
<% require javascript($jsFile) %>
|
|
||||||
<% require css($cssFile) %>
|
|
||||||
SS
|
|
||||||
);
|
|
||||||
$template = $viewer->process($data);
|
|
||||||
$this->assertFalse((bool)trim($template), "Should be no content in this return.");
|
$this->assertFalse((bool)trim($template), "Should be no content in this return.");
|
||||||
}
|
}
|
||||||
|
|
||||||
function testComments() {
|
function testComments() {
|
||||||
$viewer = SSViewer::fromString(<<<SS
|
$output = $this->render(<<<SS
|
||||||
This is my template<%-- this is a comment --%>This is some content<%-- this is another comment --%>This is the final content
|
This is my template<%-- this is a comment --%>This is some content<%-- this is another comment --%>This is the final content
|
||||||
SS
|
SS
|
||||||
);
|
);
|
||||||
$output = $viewer->process(new ArrayData(array()));
|
|
||||||
|
|
||||||
$this->assertEquals("This is my templateThis is some contentThis is the final content", preg_replace("/\n?<!--.*-->\n?/U",'',$output));
|
$this->assertEquals("This is my templateThis is some contentThis is the final content", preg_replace("/\n?<!--.*-->\n?/U",'',$output));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testObjectDotArguments() {
|
function testObjectDotArguments() {
|
||||||
// one argument
|
|
||||||
$viewer = SSViewer::fromString(<<<SS
|
|
||||||
\$TestObject.methodWithOneArgument(one)
|
|
||||||
SS
|
|
||||||
);
|
|
||||||
$obj = new SSViewerTest_ViewableData();
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$viewer->process(new ArrayData(array('TestObject'=>$obj))),
|
$this->render("\$TestObject.methodWithOneArgument(one)"),
|
||||||
"arg1:one",
|
"TestObject.methodWithOneArgument(one)",
|
||||||
"Object method calls in dot notation work with one argument"
|
"Object method calls in dot notation work with one argument"
|
||||||
);
|
);
|
||||||
|
|
||||||
// two arguments
|
// two arguments
|
||||||
$viewer = SSViewer::fromString(<<<SS
|
|
||||||
\$TestObject.methodWithTwoArguments(one,two)
|
|
||||||
SS
|
|
||||||
);
|
|
||||||
$obj = new SSViewerTest_ViewableData();
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$viewer->process(new ArrayData(array('TestObject'=>$obj))),
|
$this->render("\$TestObject.methodWithTwoArguments(one,two)"),
|
||||||
"arg1:one,arg2:two",
|
"TestObject.methodWithTwoArguments(one,two)",
|
||||||
"Object method calls in dot notation work with two arguments"
|
"Object method calls in dot notation work with two arguments"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testBaseTagGeneration() {
|
function testBaseTagGeneration() {
|
||||||
// XHTML wil have a closed base tag
|
// XHTML wil have a closed base tag
|
||||||
$tmpl1 = SSViewer::fromString('<?xml version="1.0" encoding="UTF-8"?>
|
$tmpl1 = '<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
<html>
|
<html>
|
||||||
<head><% base_tag %></head>
|
<head><% base_tag %></head>
|
||||||
<body><p>test</p><body>
|
<body><p>test</p><body>
|
||||||
</html>');
|
</html>';
|
||||||
$this->assertRegExp('/<head><base href=".*"><\/base><\/head>/', $tmpl1->process(new ViewableData()));
|
$this->assertRegExp('/<head><base href=".*"><\/base><\/head>/', $this->render($tmpl1));
|
||||||
|
|
||||||
// HTML4 and 5 will only have it for IE
|
// HTML4 and 5 will only have it for IE
|
||||||
$tmpl2 = SSViewer::fromString('<!DOCTYPE html>
|
$tmpl2 = '<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head><% base_tag %></head>
|
<head><% base_tag %></head>
|
||||||
<body><p>test</p><body>
|
<body><p>test</p><body>
|
||||||
</html>');
|
</html>';
|
||||||
$this->assertRegExp('/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/', $tmpl2->process(new ViewableData()));
|
$this->assertRegExp('/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/', $this->render($tmpl2));
|
||||||
|
|
||||||
|
|
||||||
$tmpl3 = SSViewer::fromString('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
$tmpl3 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||||
<html>
|
<html>
|
||||||
<head><% base_tag %></head>
|
<head><% base_tag %></head>
|
||||||
<body><p>test</p><body>
|
<body><p>test</p><body>
|
||||||
</html>');
|
</html>';
|
||||||
$this->assertRegExp('/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/', $tmpl3->process(new ViewableData()));
|
$this->assertRegExp('/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/', $this->render($tmpl3));
|
||||||
|
|
||||||
// Check that the content negotiator converts to the equally legal formats
|
// Check that the content negotiator converts to the equally legal formats
|
||||||
$negotiator = new ContentNegotiator();
|
$negotiator = new ContentNegotiator();
|
||||||
|
|
||||||
$response = new SS_HTTPResponse($tmpl1->process(new ViewableData()));
|
$response = new SS_HTTPResponse($this->render($tmpl1));
|
||||||
$negotiator->html($response);
|
$negotiator->html($response);
|
||||||
$this->assertRegExp('/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/', $response->getBody());
|
$this->assertRegExp('/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/', $response->getBody());
|
||||||
|
|
||||||
$response = new SS_HTTPResponse($tmpl1->process(new ViewableData()));
|
$response = new SS_HTTPResponse($this->render($tmpl1));
|
||||||
$negotiator->xhtml($response);
|
$negotiator->xhtml($response);
|
||||||
$this->assertRegExp('/<head><base href=".*"><\/base><\/head>/', $response->getBody());
|
$this->assertRegExp('/<head><base href=".*"><\/base><\/head>/', $response->getBody());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A test fixture that will echo back the template item
|
||||||
|
*/
|
||||||
|
class SSViewerTestFixture extends ViewableData {
|
||||||
|
protected $name;
|
||||||
|
|
||||||
|
function __construct($name = null) {
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function argedName($fieldName, $arguments) {
|
||||||
|
$childName = $this->name ? "$this->name.$fieldName" : $fieldName;
|
||||||
|
if($arguments) return $childName . '(' . implode(',', $arguments) . ')';
|
||||||
|
else return $childName;
|
||||||
|
}
|
||||||
|
function obj($fieldName, $arguments=null, $forceReturnedObject=true, $cache=false, $cacheName=null) {
|
||||||
|
$childName = $this->argedName($fieldName, $arguments);
|
||||||
|
|
||||||
|
// Special field name Loop### to create a list
|
||||||
|
if(preg_match('/^Loop([0-9]+)$/', $fieldName, $matches)) {
|
||||||
|
$output = new DataObjectSet();
|
||||||
|
for($i=0;$i<$matches[1];$i++) $output->push(new SSViewerTestFixture($childName));
|
||||||
|
return $output;
|
||||||
|
|
||||||
|
} else if(preg_match('/NotSet/i', $fieldName)) {
|
||||||
|
return new ViewableData();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return new SSViewerTestFixture($childName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function XML_val($fieldName, $arguments = null) {
|
||||||
|
if(preg_match('/NotSet/i', $fieldName)) {
|
||||||
|
return '';
|
||||||
|
} else if(preg_match('/Raw/i', $fieldName)) {
|
||||||
|
return $fieldName;
|
||||||
|
} else {
|
||||||
|
return '[out:' . $this->argedName($fieldName, $arguments) . ']';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasValue($fieldName, $arguments = null) {
|
||||||
|
return (bool)$this->XML_val($fieldName, $arguments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
class SSViewerTest_ViewableData extends ViewableData implements TestOnly {
|
class SSViewerTest_ViewableData extends ViewableData implements TestOnly {
|
||||||
function methodWithOneArgument($arg1) {
|
function methodWithOneArgument($arg1) {
|
||||||
return "arg1:{$arg1}";
|
return "arg1:{$arg1}";
|
||||||
@ -117,4 +159,5 @@ class SSViewerTest_ViewableData extends ViewableData implements TestOnly {
|
|||||||
function methodWithTwoArguments($arg1, $arg2) {
|
function methodWithTwoArguments($arg1, $arg2) {
|
||||||
return "arg1:{$arg1},arg2:{$arg2}";
|
return "arg1:{$arg1},arg2:{$arg2}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
Loading…
Reference in New Issue
Block a user