diff --git a/src/View/JSMinifier.php b/src/View/JSMinifier.php deleted file mode 100644 index d63be3dd2..000000000 --- a/src/View/JSMinifier.php +++ /dev/null @@ -1,30 +0,0 @@ -getMessage(); - user_error("Failed to minify {$filename}, exception: {$message}", E_USER_WARNING); - } finally { - return $content . ";\n"; - } - } -} diff --git a/tests/php/View/SSViewerTest.php b/tests/php/View/SSViewerTest.php index a3403719e..bb58fd184 100644 --- a/tests/php/View/SSViewerTest.php +++ b/tests/php/View/SSViewerTest.php @@ -8,7 +8,6 @@ use SilverStripe\Control\ContentNegotiator; use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injector; -use SilverStripe\Dev\Debug; use SilverStripe\Dev\SapphireTest; use SilverStripe\i18n\i18n; use SilverStripe\ORM\DataObject; @@ -31,7 +30,7 @@ use SilverStripe\View\SSTemplateParser; use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore; use Exception; -classSSViewerTest extends SapphireTest +class SSViewerTest extends SapphireTest { /** @@ -39,90 +38,90 @@ classSSViewerTest extends SapphireTest * * @var array */ -protected $oldServer = array(); + protected $oldServer = array(); -protected static $extra_dataobjects = array( + protected static $extra_dataobjects = array( SSViewerTest\TestObject::class, ); -protected function setUp() -{ - parent::setUp(); - SSViewer::config()->update('source_file_comments', false); - SSViewer_FromString::config()->update('cache_template', false); - TestAssetStore::activate('SSViewerTest'); - $this->oldServer = $_SERVER; -} + protected function setUp() + { + parent::setUp(); + SSViewer::config()->update('source_file_comments', false); + SSViewer_FromString::config()->update('cache_template', false); + TestAssetStore::activate('SSViewerTest'); + $this->oldServer = $_SERVER; + } -protected function tearDown() -{ - $_SERVER = $this->oldServer; - TestAssetStore::reset(); - parent::tearDown(); -} + protected function tearDown() + { + $_SERVER = $this->oldServer; + TestAssetStore::reset(); + parent::tearDown(); + } /** * Tests for {@link Config::inst()->get('SSViewer', 'theme')} for different behaviour * of user defined themes via {@link SiteConfig} and default theme * when no user themes are defined. */ -public function testCurrentTheme() -{ - SSViewer::config()->update('theme', 'mytheme'); - $this->assertEquals( + public function testCurrentTheme() + { + SSViewer::config()->update('theme', 'mytheme'); + $this->assertEquals( 'mytheme', SSViewer::config()->uninherited('theme'), 'Current theme is the default - user has not defined one' - ); -} + ); + } /** * Test that a template without a
tag still renders. */ -public function testTemplateWithoutHeadRenders() -{ - $data = new ArrayData( + public function testTemplateWithoutHeadRenders() + { + $data = new ArrayData( array( 'Var' => 'var value' ) - ); + ); - $result = $data->renderWith("SSViewerTestPartialTemplate"); - $this->assertEquals('Test partial template: var value', trim(preg_replace("//U", '', $result))); -} + $result = $data->renderWith("SSViewerTestPartialTemplate"); + $this->assertEquals('Test partial template: var value', trim(preg_replace("//U", '', $result))); + } -public function testIncludeScopeInheritance() -{ - $data = $this->getScopeInheritanceTestData(); - $expected = array( + public function testIncludeScopeInheritance() + { + $data = $this->getScopeInheritanceTestData(); + $expected = array( 'Item 1 - First-ODD top:Item 1', 'Item 2 - EVEN top:Item 2', 'Item 3 - ODD top:Item 3', 'Item 4 - EVEN top:Item 4', 'Item 5 - ODD top:Item 5', 'Item 6 - Last-EVEN top:Item 6', - ); + ); - $result = $data->renderWith('SSViewerTestIncludeScopeInheritance'); - $this->assertExpectedStrings($result, $expected); + $result = $data->renderWith('SSViewerTestIncludeScopeInheritance'); + $this->assertExpectedStrings($result, $expected); - // reset results for the tests that include arguments (the title is passed as an arg) - $expected = array( + // reset results for the tests that include arguments (the title is passed as an arg) + $expected = array( 'Item 1 _ Item 1 - First-ODD top:Item 1', 'Item 2 _ Item 2 - EVEN top:Item 2', 'Item 3 _ Item 3 - ODD top:Item 3', 'Item 4 _ Item 4 - EVEN top:Item 4', 'Item 5 _ Item 5 - ODD top:Item 5', 'Item 6 _ Item 6 - Last-EVEN top:Item 6', - ); + ); - $result = $data->renderWith('SSViewerTestIncludeScopeInheritanceWithArgs'); - $this->assertExpectedStrings($result, $expected); -} + $result = $data->renderWith('SSViewerTestIncludeScopeInheritanceWithArgs'); + $this->assertExpectedStrings($result, $expected); + } -public function testIncludeTruthyness() -{ - $data = new ArrayData( + public function testIncludeTruthyness() + { + $data = new ArrayData( array( 'Title' => 'TruthyTest', 'Items' => new ArrayList( @@ -137,11 +136,11 @@ public function testIncludeTruthyness() ) ) ) - ); - $result = $data->renderWith('SSViewerTestIncludeScopeInheritanceWithArgs'); + ); + $result = $data->renderWith('SSViewerTestIncludeScopeInheritanceWithArgs'); - // We should not end up with empty values appearing as empty - $expected = array( + // We should not end up with empty values appearing as empty + $expected = array( 'Item 1 _ Item 1 - First-ODD top:Item 1', 'Untitled - EVEN top:', '1 _ 1 - ODD top:1', @@ -149,13 +148,13 @@ public function testIncludeTruthyness() 'Untitled - ODD top:', 'Untitled - EVEN top:0', '7 _ 7 - Last-ODD top:7' - ); - $this->assertExpectedStrings($result, $expected); -} + ); + $this->assertExpectedStrings($result, $expected); + } -private function getScopeInheritanceTestData() -{ - return new ArrayData( + private function getScopeInheritanceTestData() + { + return new ArrayData( array( 'Title' => 'TopTitleValue', 'Items' => new ArrayList( @@ -169,18 +168,18 @@ private function getScopeInheritanceTestData() ) ) ) - ); -} - -private function assertExpectedStrings($result, $expected) -{ - foreach ($expected as $expectedStr) { - $this->assertTrue( - (boolean) preg_match("/{$expectedStr}/", $result), - "Didn't find '{$expectedStr}' in:\n{$result}" ); } -} + + private function assertExpectedStrings($result, $expected) + { + foreach ($expected as $expectedStr) { + $this->assertTrue( + (boolean) preg_match("/{$expectedStr}/", $result), + "Didn't find '{$expectedStr}' in:\n{$result}" + ); + } + } /** * Small helper to render templates from strings @@ -190,103 +189,103 @@ private function assertExpectedStrings($result, $expected) * @param bool $cacheTemplate * @return string */ -public function render($templateString, $data = null, $cacheTemplate = false) -{ - $t = SSViewer::fromString($templateString, $cacheTemplate); - if (!$data) { - $data = new SSViewerTest\TestFixture(); + public function render($templateString, $data = null, $cacheTemplate = false) + { + $t = SSViewer::fromString($templateString, $cacheTemplate); + if (!$data) { + $data = new SSViewerTest\TestFixture(); + } + return trim(''.$t->process($data)); } - return trim(''.$t->process($data)); -} -public function testRequirements() -{ - $requirements = $this->getMockBuilder(Requirements_Backend::class)->setMethods(array("javascript", "css")) + public function testRequirements() + { + $requirements = $this->getMockBuilder(Requirements_Backend::class)->setMethods(array("javascript", "css")) ->getMock(); - $jsFile = FRAMEWORK_DIR . '/tests/forms/a.js'; - $cssFile = FRAMEWORK_DIR . '/tests/forms/a.js'; + $jsFile = FRAMEWORK_DIR . '/tests/forms/a.js'; + $cssFile = FRAMEWORK_DIR . '/tests/forms/a.js'; - $requirements->expects($this->once())->method('javascript')->with($jsFile); - $requirements->expects($this->once())->method('css')->with($cssFile); + $requirements->expects($this->once())->method('javascript')->with($jsFile); + $requirements->expects($this->once())->method('css')->with($cssFile); - $origReq = Requirements::backend(); - Requirements::set_backend($requirements); - $template = $this->render( + $origReq = Requirements::backend(); + Requirements::set_backend($requirements); + $template = $this->render( "<% require javascript($jsFile) %> <% require css($cssFile) %>" - ); - Requirements::set_backend($origReq); + ); + Requirements::set_backend($origReq); - $this->assertFalse((bool)trim($template), "Should be no content in this return."); -} - -public function testRequirementsCombine() -{ - $testBackend = Injector::inst()->create(Requirements_Backend::class); - $testBackend->setSuffixRequirements(false); - //$combinedTestFilePath = BASE_PATH . '/' . $testBackend->getCombinedFilesFolder() . '/testRequirementsCombine.js'; - - $jsFile = $this->getCurrentRelativePath() . '/SSViewerTest/javascript/bad.js'; - $jsFileContents = file_get_contents(BASE_PATH . '/' . $jsFile); - $testBackend->combineFiles('testRequirementsCombine.js', array($jsFile)); - - // secondly, make sure that requirements is generated, even though minification failed - $testBackend->processCombinedFiles(); - $js = array_keys($testBackend->getJavascript()); - $combinedTestFilePath = BASE_PATH . reset($js); - $this->assertContains('_combinedfiles/testRequirementsCombine-4c0e97a.js', $combinedTestFilePath); - - // and make sure the combined content matches the input content, i.e. no loss of functionality - if (!file_exists($combinedTestFilePath)) { - $this->fail('No combined file was created at expected path: '.$combinedTestFilePath); + $this->assertFalse((bool)trim($template), "Should be no content in this return."); } - $combinedTestFileContents = file_get_contents($combinedTestFilePath); - $this->assertContains($jsFileContents, $combinedTestFileContents); -} -public function testRequirementsMinification() -{ - $testBackend = Injector::inst()->create(Requirements_Backend::class); - $testBackend->setSuffixRequirements(false); - $testBackend->setMinifyCombinedFiles(true); - $testFile = $this->getCurrentRelativePath() . '/SSViewerTest/javascript/RequirementsTest_a.js'; - $testFileContent = file_get_contents($testFile); + public function testRequirementsCombine() + { + $testBackend = Injector::inst()->create(Requirements_Backend::class); + $testBackend->setSuffixRequirements(false); + //$combinedTestFilePath = BASE_PATH . '/' . $testBackend->getCombinedFilesFolder() . '/testRequirementsCombine.js'; - $mockMinifier = $this->getMockBuilder(Requirements_Minifier::class) + $jsFile = $this->getCurrentRelativePath() . '/SSViewerTest/javascript/bad.js'; + $jsFileContents = file_get_contents(BASE_PATH . '/' . $jsFile); + $testBackend->combineFiles('testRequirementsCombine.js', array($jsFile)); + + // secondly, make sure that requirements is generated, even though minification failed + $testBackend->processCombinedFiles(); + $js = array_keys($testBackend->getJavascript()); + $combinedTestFilePath = BASE_PATH . reset($js); + $this->assertContains('_combinedfiles/testRequirementsCombine-4c0e97a.js', $combinedTestFilePath); + + // and make sure the combined content matches the input content, i.e. no loss of functionality + if (!file_exists($combinedTestFilePath)) { + $this->fail('No combined file was created at expected path: '.$combinedTestFilePath); + } + $combinedTestFileContents = file_get_contents($combinedTestFilePath); + $this->assertContains($jsFileContents, $combinedTestFileContents); + } + + public function testRequirementsMinification() + { + $testBackend = Injector::inst()->create(Requirements_Backend::class); + $testBackend->setSuffixRequirements(false); + $testBackend->setMinifyCombinedFiles(true); + $testFile = $this->getCurrentRelativePath() . '/SSViewerTest/javascript/RequirementsTest_a.js'; + $testFileContent = file_get_contents($testFile); + + $mockMinifier = $this->getMockBuilder(Requirements_Minifier::class) ->setMethods(['minify']) ->getMock(); - $mockMinifier->expects($this->once()) + $mockMinifier->expects($this->once()) ->method('minify') ->with( $testFileContent, 'js', $testFile ); - $testBackend->setMinifier($mockMinifier); - $testBackend->combineFiles('testRequirementsMinified.js', array($testFile)); - $testBackend->processCombinedFiles(); + $testBackend->setMinifier($mockMinifier); + $testBackend->combineFiles('testRequirementsMinified.js', array($testFile)); + $testBackend->processCombinedFiles(); - $testBackend->setMinifyCombinedFiles(false); - $mockMinifier->expects($this->never()) + $testBackend->setMinifyCombinedFiles(false); + $mockMinifier->expects($this->never()) ->method('minify'); - $testBackend->processCombinedFiles(); + $testBackend->processCombinedFiles(); - $this->setExpectedExceptionRegExp( + $this->setExpectedExceptionRegExp( Exception::class, '/minification service/' - ); + ); - $testBackend->setMinifyCombinedFiles(true); - $testBackend->setMinifier(null); - $testBackend->processCombinedFiles(); -} + $testBackend->setMinifyCombinedFiles(true); + $testBackend->setMinifier(null); + $testBackend->processCombinedFiles(); + } -public function testComments() -{ - $output = $this->render( + public function testComments() + { + $output = $this->render( <<test
'; - $this->assertRegExp('/test
'; - $this->assertRegExp( + $this->assertRegExp( '/test
'; - $this->assertRegExp( + $this->assertRegExp( '/[out:Arg1]
[out:Arg2]
' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A %>'), 'A
[out:Arg2]
' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A, Arg2=B %>'), 'A
B
' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A Bare String, Arg2=B Bare String %>'), 'A Bare String
B Bare String
' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render( '<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=$B %>', new ArrayData(array('B' => 'Bar')) ), 'A
Bar
' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render( '<% include SSViewerTestIncludeWithArguments Arg1="A" %>', new ArrayData(array('Arg1' => 'Foo', 'Arg2' => 'Bar')) ), 'A
Bar
' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render( '<% include SSViewerTestIncludeScopeInheritanceWithArgsInLoop Title="SomeArg" %>', new ArrayData( @@ -1009,17 +1008,17 @@ public function testIncludeWithArguments() ) ), 'SomeArg - Foo - Bar - SomeArg' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render( '<% include SSViewerTestIncludeScopeInheritanceWithArgsInWith Title="A" %>', new ArrayData(array('Item' => new ArrayData(array('Title' =>'B')))) ), 'A - B - A' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render( '<% include SSViewerTestIncludeScopeInheritanceWithArgsInNestedWith Title="A" %>', new ArrayData( @@ -1032,9 +1031,9 @@ public function testIncludeWithArguments() ) ), 'A - B - C - B - A' - ); + ); - $this->assertEquals( + $this->assertEquals( $this->render( '<% include SSViewerTestIncludeScopeInheritanceWithUpAndTop Title="A" %>', new ArrayData( @@ -1047,9 +1046,9 @@ public function testIncludeWithArguments() ) ), 'A - A - A' - ); + ); - $data = new ArrayData( + $data = new ArrayData( array( 'Nested' => new ArrayData( array( @@ -1058,36 +1057,36 @@ public function testIncludeWithArguments() ), 'Object' => new ArrayData(array('Key' => 'B')) ) - ); + ); - $tmpl = SSViewer::fromString('<% include SSViewerTestIncludeObjectArguments A=$Nested.Object, B=$Object %>'); - $res = $tmpl->process($data); - $this->assertEqualIgnoringWhitespace('A B', $res, 'Objects can be passed as named arguments'); -} + $tmpl = SSViewer::fromString('<% include SSViewerTestIncludeObjectArguments A=$Nested.Object, B=$Object %>'); + $res = $tmpl->process($data); + $this->assertEqualIgnoringWhitespace('A B', $res, 'Objects can be passed as named arguments'); + } -public function testNamespaceInclude() -{ - $data = new ArrayData([]); + public function testNamespaceInclude() + { + $data = new ArrayData([]); - $this->assertEquals( + $this->assertEquals( "tests:( NamespaceInclude\n )", $this->render('tests:( <% include Namespace\NamespaceInclude %> )', $data), 'Backslashes work for namespace references in includes' - ); + ); - $this->assertEquals( + $this->assertEquals( "tests:( NamespaceInclude\n )", $this->render('tests:( <% include Namespace/NamespaceInclude %> )', $data), 'Forward slashes work for namespace references in includes' - ); -} + ); + } -public function testRecursiveInclude() -{ - $view = new SSViewer(array('Includes/SSViewerTestRecursiveInclude')); + public function testRecursiveInclude() + { + $view = new SSViewer(array('Includes/SSViewerTestRecursiveInclude')); - $data = new ArrayData( + $data = new ArrayData( array( 'Title' => 'A', 'Children' => new ArrayList( @@ -1108,79 +1107,79 @@ public function testRecursiveInclude() ) ), ) - ); + ); - $result = $view->process($data); - // We don't care about whitespace - $rationalisedResult = trim(preg_replace('/\s+/', ' ', $result)); + $result = $view->process($data); + // We don't care about whitespace + $rationalisedResult = trim(preg_replace('/\s+/', ' ', $result)); - $this->assertEquals('A A1 A1 i A1 ii A2 A3', $rationalisedResult); -} + $this->assertEquals('A A1 A1 i A1 ii A2 A3', $rationalisedResult); + } -public function assertEqualIgnoringWhitespace($a, $b, $message = '') -{ - $this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message); -} + public function assertEqualIgnoringWhitespace($a, $b, $message = '') + { + $this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message); + } /** * See {@link ViewableDataTest} for more extensive casting tests, * this test just ensures that basic casting is correctly applied during template parsing. */ -public function testCastingHelpers() -{ - $vd = new SSViewerTest\TestViewableData(); - $vd->TextValue = 'html'; - $vd->HTMLValue = 'html'; - $vd->UncastedValue = 'html'; + public function testCastingHelpers() + { + $vd = new SSViewerTest\TestViewableData(); + $vd->TextValue = 'html'; + $vd->HTMLValue = 'html'; + $vd->UncastedValue = 'html'; - // Value casted as "Text" - $this->assertEquals( + // Value casted as "Text" + $this->assertEquals( '<b>html</b>', $t = SSViewer::fromString('$TextValue')->process($vd) - ); - $this->assertEquals( + ); + $this->assertEquals( 'html', $t = SSViewer::fromString('$TextValue.RAW')->process($vd) - ); - $this->assertEquals( + ); + $this->assertEquals( '<b>html</b>', $t = SSViewer::fromString('$TextValue.XML')->process($vd) - ); + ); - // Value casted as "HTMLText" - $this->assertEquals( + // Value casted as "HTMLText" + $this->assertEquals( 'html', $t = SSViewer::fromString('$HTMLValue')->process($vd) - ); - $this->assertEquals( + ); + $this->assertEquals( 'html', $t = SSViewer::fromString('$HTMLValue.RAW')->process($vd) - ); - $this->assertEquals( + ); + $this->assertEquals( '<b>html</b>', $t = SSViewer::fromString('$HTMLValue.XML')->process($vd) - ); + ); - // Uncasted value (falls back to ViewableData::$default_cast="Text") - $vd = new SSViewerTest\TestViewableData(); - $vd->UncastedValue = 'html'; - $this->assertEquals( + // Uncasted value (falls back to ViewableData::$default_cast="Text") + $vd = new SSViewerTest\TestViewableData(); + $vd->UncastedValue = 'html'; + $this->assertEquals( '<b>html</b>', $t = SSViewer::fromString('$UncastedValue')->process($vd) - ); - $this->assertEquals( + ); + $this->assertEquals( 'html', $t = SSViewer::fromString('$UncastedValue.RAW')->process($vd) - ); - $this->assertEquals( + ); + $this->assertEquals( '<b>html</b>', $t = SSViewer::fromString('$UncastedValue.XML')->process($vd) - ); -} + ); + } -public function testSSViewerBasicIteratorSupport() -{ - $data = new ArrayData( + public function testSSViewerBasicIteratorSupport() + { + $data = new ArrayData( array( 'Set' => new ArrayList( array( @@ -1197,129 +1196,129 @@ public function testSSViewerBasicIteratorSupport() ) ) ) - ); + ); - //base test - $result = $this->render('<% loop Set %>$Number<% end_loop %>', $data); - $this->assertEquals("12345678910", $result, "Numbers rendered in order"); + //base test + $result = $this->render('<% loop Set %>$Number<% end_loop %>', $data); + $this->assertEquals("12345678910", $result, "Numbers rendered in order"); - //test First - $result = $this->render('<% loop Set %><% if First %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("1", $result, "Only the first number is rendered"); + //test First + $result = $this->render('<% loop Set %><% if First %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("1", $result, "Only the first number is rendered"); - //test Last - $result = $this->render('<% loop Set %><% if Last %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("10", $result, "Only the last number is rendered"); + //test Last + $result = $this->render('<% loop Set %><% if Last %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("10", $result, "Only the last number is rendered"); - //test Even - $result = $this->render('<% loop Set %><% if Even() %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("246810", $result, "Even numbers rendered in order"); + //test Even + $result = $this->render('<% loop Set %><% if Even() %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("246810", $result, "Even numbers rendered in order"); - //test Even with quotes - $result = $this->render('<% loop Set %><% if Even("1") %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("246810", $result, "Even numbers rendered in order"); + //test Even with quotes + $result = $this->render('<% loop Set %><% if Even("1") %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("246810", $result, "Even numbers rendered in order"); - //test Even without quotes - $result = $this->render('<% loop Set %><% if Even(1) %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("246810", $result, "Even numbers rendered in order"); + //test Even without quotes + $result = $this->render('<% loop Set %><% if Even(1) %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("246810", $result, "Even numbers rendered in order"); - //test Even with zero-based start index - $result = $this->render('<% loop Set %><% if Even("0") %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("13579", $result, "Even (with zero-based index) numbers rendered in order"); + //test Even with zero-based start index + $result = $this->render('<% loop Set %><% if Even("0") %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("13579", $result, "Even (with zero-based index) numbers rendered in order"); - //test Odd - $result = $this->render('<% loop Set %><% if Odd %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("13579", $result, "Odd numbers rendered in order"); + //test Odd + $result = $this->render('<% loop Set %><% if Odd %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("13579", $result, "Odd numbers rendered in order"); - //test FirstLast - $result = $this->render('<% loop Set %><% if FirstLast %>$Number$FirstLast<% end_if %><% end_loop %>', $data); - $this->assertEquals("1first10last", $result, "First and last numbers rendered in order"); + //test FirstLast + $result = $this->render('<% loop Set %><% if FirstLast %>$Number$FirstLast<% end_if %><% end_loop %>', $data); + $this->assertEquals("1first10last", $result, "First and last numbers rendered in order"); - //test Middle - $result = $this->render('<% loop Set %><% if Middle %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("23456789", $result, "Middle numbers rendered in order"); + //test Middle + $result = $this->render('<% loop Set %><% if Middle %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("23456789", $result, "Middle numbers rendered in order"); - //test MiddleString - $result = $this->render( + //test MiddleString + $result = $this->render( '<% loop Set %><% if MiddleString == "middle" %>$Number$MiddleString<% end_if %>' . '<% end_loop %>', $data - ); - $this->assertEquals( + ); + $this->assertEquals( "2middle3middle4middle5middle6middle7middle8middle9middle", $result, "Middle numbers rendered in order" - ); + ); - //test EvenOdd - $result = $this->render('<% loop Set %>$EvenOdd<% end_loop %>', $data); - $this->assertEquals( + //test EvenOdd + $result = $this->render('<% loop Set %>$EvenOdd<% end_loop %>', $data); + $this->assertEquals( "oddevenoddevenoddevenoddevenoddeven", $result, "Even and Odd is returned in sequence numbers rendered in order" - ); + ); - //test Pos - $result = $this->render('<% loop Set %>$Pos<% end_loop %>', $data); - $this->assertEquals("12345678910", $result, '$Pos is rendered in order'); + //test Pos + $result = $this->render('<% loop Set %>$Pos<% end_loop %>', $data); + $this->assertEquals("12345678910", $result, '$Pos is rendered in order'); - //test Pos - $result = $this->render('<% loop Set %>$Pos(0)<% end_loop %>', $data); - $this->assertEquals("0123456789", $result, '$Pos(0) is rendered in order'); + //test Pos + $result = $this->render('<% loop Set %>$Pos(0)<% end_loop %>', $data); + $this->assertEquals("0123456789", $result, '$Pos(0) is rendered in order'); - //test FromEnd - $result = $this->render('<% loop Set %>$FromEnd<% end_loop %>', $data); - $this->assertEquals("10987654321", $result, '$FromEnd is rendered in order'); + //test FromEnd + $result = $this->render('<% loop Set %>$FromEnd<% end_loop %>', $data); + $this->assertEquals("10987654321", $result, '$FromEnd is rendered in order'); - //test FromEnd - $result = $this->render('<% loop Set %>$FromEnd(0)<% end_loop %>', $data); - $this->assertEquals("9876543210", $result, '$FromEnd(0) rendered in order'); + //test FromEnd + $result = $this->render('<% loop Set %>$FromEnd(0)<% end_loop %>', $data); + $this->assertEquals("9876543210", $result, '$FromEnd(0) rendered in order'); - //test Total - $result = $this->render('<% loop Set %>$TotalItems<% end_loop %>', $data); - $this->assertEquals("10101010101010101010", $result, "10 total items X 10 are returned"); + //test Total + $result = $this->render('<% loop Set %>$TotalItems<% end_loop %>', $data); + $this->assertEquals("10101010101010101010", $result, "10 total items X 10 are returned"); - //test Modulus - $result = $this->render('<% loop Set %>$Modulus(2,1)<% end_loop %>', $data); - $this->assertEquals("1010101010", $result, "1-indexed pos modular divided by 2 rendered in order"); + //test Modulus + $result = $this->render('<% loop Set %>$Modulus(2,1)<% end_loop %>', $data); + $this->assertEquals("1010101010", $result, "1-indexed pos modular divided by 2 rendered in order"); - //test MultipleOf 3 - $result = $this->render('<% loop Set %><% if MultipleOf(3) %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("369", $result, "Only numbers that are multiples of 3 are returned"); + //test MultipleOf 3 + $result = $this->render('<% loop Set %><% if MultipleOf(3) %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("369", $result, "Only numbers that are multiples of 3 are returned"); - //test MultipleOf 4 - $result = $this->render('<% loop Set %><% if MultipleOf(4) %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("48", $result, "Only numbers that are multiples of 4 are returned"); + //test MultipleOf 4 + $result = $this->render('<% loop Set %><% if MultipleOf(4) %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("48", $result, "Only numbers that are multiples of 4 are returned"); - //test MultipleOf 5 - $result = $this->render('<% loop Set %><% if MultipleOf(5) %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("510", $result, "Only numbers that are multiples of 5 are returned"); + //test MultipleOf 5 + $result = $this->render('<% loop Set %><% if MultipleOf(5) %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("510", $result, "Only numbers that are multiples of 5 are returned"); - //test MultipleOf 10 - $result = $this->render('<% loop Set %><% if MultipleOf(10,1) %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("10", $result, "Only numbers that are multiples of 10 (with 1-based indexing) are returned"); + //test MultipleOf 10 + $result = $this->render('<% loop Set %><% if MultipleOf(10,1) %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("10", $result, "Only numbers that are multiples of 10 (with 1-based indexing) are returned"); - //test MultipleOf 9 zero-based - $result = $this->render('<% loop Set %><% if MultipleOf(9,0) %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals( + //test MultipleOf 9 zero-based + $result = $this->render('<% loop Set %><% if MultipleOf(9,0) %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals( "110", $result, "Only numbers that are multiples of 9 with zero-based indexing are returned. (The first and last item)" - ); + ); - //test MultipleOf 11 - $result = $this->render('<% loop Set %><% if MultipleOf(11) %>$Number<% end_if %><% end_loop %>', $data); - $this->assertEquals("", $result, "Only numbers that are multiples of 11 are returned. I.e. nothing returned"); -} + //test MultipleOf 11 + $result = $this->render('<% loop Set %><% if MultipleOf(11) %>$Number<% end_if %><% end_loop %>', $data); + $this->assertEquals("", $result, "Only numbers that are multiples of 11 are returned. I.e. nothing returned"); + } /** * Test $Up works when the scope $Up refers to was entered with a "with" block */ -public function testUpInWith() -{ + public function testUpInWith() + { - // Data to run the loop tests on - three levels deep - $data = new ArrayData( + // Data to run the loop tests on - three levels deep + $data = new ArrayData( array( 'Name' => 'Top', 'Foo' => new ArrayData( @@ -1343,71 +1342,71 @@ public function testUpInWith() ) ) ) - ); + ); - // Basic functionality - $this->assertEquals( + // Basic functionality + $this->assertEquals( 'BarFoo', $this->render('<% with Foo %><% with Bar %>{$Name}{$Up.Name}<% end_with %><% end_with %>', $data) - ); + ); - // Two level with block, up refers to internally referenced Bar - $this->assertEquals( + // Two level with block, up refers to internally referenced Bar + $this->assertEquals( 'BarFoo', $this->render('<% with Foo.Bar %>{$Name}{$Up.Name}<% end_with %>', $data) - ); + ); - // Stepping up & back down the scope tree - $this->assertEquals( + // Stepping up & back down the scope tree + $this->assertEquals( 'BazBarQux', $this->render('<% with Foo.Bar.Baz %>{$Name}{$Up.Name}{$Up.Qux.Name}<% end_with %>', $data) - ); + ); - // Using $Up in a with block - $this->assertEquals( + // Using $Up in a with block + $this->assertEquals( 'BazBarQux', $this->render( '<% with Foo.Bar.Baz %>{$Name}<% with $Up %>{$Name}{$Qux.Name}<% end_with %>' .'<% end_with %>', $data ) - ); + ); - // Stepping up & back down the scope tree with with blocks - $this->assertEquals( + // Stepping up & back down the scope tree with with blocks + $this->assertEquals( 'BazBarQuxBarBaz', $this->render( '<% with Foo.Bar.Baz %>{$Name}<% with $Up %>{$Name}<% with Qux %>{$Name}<% end_with %>' . '{$Name}<% end_with %>{$Name}<% end_with %>', $data ) - ); + ); - // Using $Up.Up, where first $Up points to a previous scope entered using $Up, thereby skipping up to Foo - $this->assertEquals( + // Using $Up.Up, where first $Up points to a previous scope entered using $Up, thereby skipping up to Foo + $this->assertEquals( 'Foo', $this->render( '<% with Foo.Bar.Baz %><% with Up %><% with Qux %>{$Up.Up.Name}<% end_with %><% end_with %>' . '<% end_with %>', $data ) - ); + ); - // Using $Up.Up, where first $Up points to an Up used in a local scope lookup, should still skip to Foo - $this->assertEquals( + // Using $Up.Up, where first $Up points to an Up used in a local scope lookup, should still skip to Foo + $this->assertEquals( 'Foo', $this->render('<% with Foo.Bar.Baz.Up.Qux %>{$Up.Up.Name}<% end_with %>', $data) - ); -} + ); + } /** * Test $Up works when the scope $Up refers to was entered with a "loop" block */ -public function testUpInLoop() -{ + public function testUpInLoop() + { - // Data to run the loop tests on - one sequence of three items, each with a subitem - $data = new ArrayData( + // Data to run the loop tests on - one sequence of three items, each with a subitem + $data = new ArrayData( array( 'Name' => 'Top', 'Foo' => new ArrayList( @@ -1445,20 +1444,20 @@ public function testUpInLoop() ) ) ) - ); + ); - // Make sure inside a loop, $Up refers to the current item of the loop - $this->assertEqualIgnoringWhitespace( + // Make sure inside a loop, $Up refers to the current item of the loop + $this->assertEqualIgnoringWhitespace( '111 222 333', $this->render( '<% loop $Foo %>$Name<% with $Sub %>$Up.Name<% end_with %>$Name<% end_loop %>', $data ) - ); + ); - // Make sure inside a loop, looping over $Up uses a separate iterator, - // and doesn't interfere with the original iterator - $this->assertEqualIgnoringWhitespace( + // Make sure inside a loop, looping over $Up uses a separate iterator, + // and doesn't interfere with the original iterator + $this->assertEqualIgnoringWhitespace( '1Bar123Bar1 2Baz123Baz2 3Qux123Qux3', $this->render( '<% loop $Foo %> @@ -1472,11 +1471,11 @@ public function testUpInLoop() <% end_loop %>', $data ) - ); + ); - // Make sure inside a loop, looping over $Up uses a separate iterator, - // and doesn't interfere with the original iterator or local lookups - $this->assertEqualIgnoringWhitespace( + // Make sure inside a loop, looping over $Up uses a separate iterator, + // and doesn't interfere with the original iterator or local lookups + $this->assertEqualIgnoringWhitespace( '1 Bar1 123 1Bar 1 2 Baz2 123 2Baz 2 3 Qux3 123 3Qux 3', $this->render( '<% loop $Foo %> @@ -1490,18 +1489,18 @@ public function testUpInLoop() <% end_loop %>', $data ) - ); -} + ); + } /** * Test that nested loops restore the loop variables correctly when pushing and popping states */ -public function testNestedLoops() -{ + public function testNestedLoops() + { - // Data to run the loop tests on - one sequence of three items, one with child elements - // (of a different size to the main sequence) - $data = new ArrayData( + // Data to run the loop tests on - one sequence of three items, one with child elements + // (of a different size to the main sequence) + $data = new ArrayData( array( 'Foo' => new ArrayList( array( @@ -1539,23 +1538,23 @@ public function testNestedLoops() ) ), ) - ); + ); - // Make sure that including a loop inside a loop will not destroy the internal count of - // items, checked by using "Last" - $this->assertEqualIgnoringWhitespace( + // Make sure that including a loop inside a loop will not destroy the internal count of + // items, checked by using "Last" + $this->assertEqualIgnoringWhitespace( '1ab23last', $this->render( '<% loop $Foo %>$Name<% loop Children %>$Name<% end_loop %><% if Last %>last<% end_if %>' . '<% end_loop %>', $data ) - ); -} + ); + } -public function testLayout() -{ - $this->useTestTheme( + public function testLayout() + { + $this->useTestTheme( __DIR__.'/SSViewerTest', 'layouttest', function () { @@ -1565,15 +1564,15 @@ public function testLayout() $template = new SSViewer(array('Shortcodes', 'Page')); $this->assertEquals("[file_link]\n\n", $template->process(new ArrayData(array()))); } - ); -} + ); + } /** * @covers \SilverStripe\View\SSViewer::get_templates_by_class() */ -public function testGetTemplatesByClass() -{ - $this->useTestTheme( + public function testGetTemplatesByClass() + { + $this->useTestTheme( __DIR__ . '/SSViewerTest', 'layouttest', function () { @@ -1644,25 +1643,25 @@ public function testGetTemplatesByClass() $this->setExpectedException('InvalidArgumentException'); SSViewer::get_templates_by_class(array()); } - ); -} + ); + } -public function testRewriteHashlinks() -{ - SSViewer::config()->update('rewrite_hash_links', true); + public function testRewriteHashlinks() + { + SSViewer::config()->update('rewrite_hash_links', true); - $_SERVER['HTTP_HOST'] = 'www.mysite.com'; - $_SERVER['REQUEST_URI'] = '//file.com?foo"onclick="alert(\'xss\')""'; + $_SERVER['HTTP_HOST'] = 'www.mysite.com'; + $_SERVER['REQUEST_URI'] = '//file.com?foo"onclick="alert(\'xss\')""'; - // Emulate SSViewer::process() - // Note that leading double slashes have been rewritten to prevent these being mis-interepreted - // as protocol-less absolute urls - $base = Convert::raw2att('/file.com?foo"onclick="alert(\'xss\')""'); + // Emulate SSViewer::process() + // Note that leading double slashes have been rewritten to prevent these being mis-interepreted + // as protocol-less absolute urls + $base = Convert::raw2att('/file.com?foo"onclick="alert(\'xss\')""'); - $tmplFile = TEMP_FOLDER . '/SSViewerTest_testRewriteHashlinks_' . sha1(rand()) . '.ss'; + $tmplFile = TEMP_FOLDER . '/SSViewerTest_testRewriteHashlinks_' . sha1(rand()) . '.ss'; - // Note: SSViewer_FromString doesn't rewrite hash links. - file_put_contents( + // Note: SSViewer_FromString doesn't rewrite hash links. + file_put_contents( $tmplFile, ' @@ -1675,51 +1674,51 @@ public function testRewriteHashlinks() ' - ); - $tmpl = new SSViewer($tmplFile); - $obj = new ViewableData(); - $obj->InsertedLink = DBField::create_field( + ); + $tmpl = new SSViewer($tmplFile); + $obj = new ViewableData(); + $obj->InsertedLink = DBField::create_field( 'HTMLFragment', 'InsertedLink' - ); - $obj->ExternalInsertedLink = DBField::create_field( + ); + $obj->ExternalInsertedLink = DBField::create_field( 'HTMLFragment', 'ExternalInsertedLink' - ); - $result = $tmpl->process($obj); - $this->assertContains( + ); + $result = $tmpl->process($obj); + $this->assertContains( 'InsertedLink', $result - ); - $this->assertContains( + ); + $this->assertContains( 'ExternalInsertedLink', $result - ); - $this->assertContains( + ); + $this->assertContains( 'InlineLink', $result - ); - $this->assertContains( + ); + $this->assertContains( 'ExternalInlineLink', $result - ); - $this->assertContains( + ); + $this->assertContains( '', $result, 'SSTemplateParser should only rewrite anchor hrefs' - ); + ); - unlink($tmplFile); -} + unlink($tmplFile); + } -public function testRewriteHashlinksInPhpMode() -{ - SSViewer::config()->update('rewrite_hash_links', 'php'); + public function testRewriteHashlinksInPhpMode() + { + SSViewer::config()->update('rewrite_hash_links', 'php'); - $tmplFile = TEMP_FOLDER . '/SSViewerTest_testRewriteHashlinksInPhpMode_' . sha1(rand()) . '.ss'; + $tmplFile = TEMP_FOLDER . '/SSViewerTest_testRewriteHashlinksInPhpMode_' . sha1(rand()) . '.ss'; - // Note: SSViewer_FromString doesn't rewrite hash links. - file_put_contents( + // Note: SSViewer_FromString doesn't rewrite hash links. + file_put_contents( $tmplFile, ' @@ -1730,40 +1729,40 @@ public function testRewriteHashlinksInPhpMode() ' - ); - $tmpl = new SSViewer($tmplFile); - $obj = new ViewableData(); - $obj->InsertedLink = DBField::create_field( + ); + $tmpl = new SSViewer($tmplFile); + $obj = new ViewableData(); + $obj->InsertedLink = DBField::create_field( 'HTMLFragment', 'InsertedLink' - ); - $result = $tmpl->process($obj); + ); + $result = $tmpl->process($obj); - $code = <<<'EOC' + $code = <<<'EOC' #anchor">InsertedLink EOC; - $this->assertContains($code, $result); - // TODO Fix inline links in PHP mode - // $this->assertContains( - // '', $result, 'SSTemplateParser should only rewrite anchor hrefs' - ); + ); - unlink($tmplFile); -} + unlink($tmplFile); + } -public function testRenderWithSourceFileComments() -{ - Director::set_environment_type('dev'); - SSViewer::config()->update('source_file_comments', true); - $i = __DIR__ . '/SSViewerTest/templates/Includes'; - $f = __DIR__ . '/SSViewerTest/templates/SSViewerTestComments'; - $templates = array( + public function testRenderWithSourceFileComments() + { + Director::set_environment_type('dev'); + SSViewer::config()->update('source_file_comments', true); + $i = __DIR__ . '/SSViewerTest/templates/Includes'; + $f = __DIR__ . '/SSViewerTest/templates/SSViewerTestComments'; + $templates = array( array( 'name' => 'SSViewerTestCommentsFullSource', 'expected' => "" @@ -1840,81 +1839,81 @@ public function testRenderWithSourceFileComments() . "" . "", ), - ); - foreach ($templates as $template) { - $this->_renderWithSourceFileComments('SSViewerTestComments/'.$template['name'], $template['expected']); + ); + foreach ($templates as $template) { + $this->_renderWithSourceFileComments('SSViewerTestComments/'.$template['name'], $template['expected']); + } + } + private function _renderWithSourceFileComments($name, $expected) + { + $viewer = new SSViewer(array($name)); + $data = new ArrayData(array()); + $result = $viewer->process($data); + $expected = str_replace(array("\r", "\n"), '', $expected); + $result = str_replace(array("\r", "\n"), '', $result); + $this->assertEquals($result, $expected); } -} -private function _renderWithSourceFileComments($name, $expected) -{ - $viewer = new SSViewer(array($name)); - $data = new ArrayData(array()); - $result = $viewer->process($data); - $expected = str_replace(array("\r", "\n"), '', $expected); - $result = str_replace(array("\r", "\n"), '', $result); - $this->assertEquals($result, $expected); -} -public function testLoopIteratorIterator() -{ - $list = new PaginatedList(new ArrayList()); - $viewer = new SSViewer_FromString('<% loop List %>$ID - $FirstName