2008-07-21 13:59:24 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\View\Tests;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use Exception;
|
|
|
|
use InvalidArgumentException;
|
2022-09-09 03:38:55 +02:00
|
|
|
use LogicException;
|
2021-10-27 04:39:47 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-02-14 04:24:12 +01:00
|
|
|
use Silverstripe\Assets\Dev\TestAssetStore;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\ContentNegotiator;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\i18n\i18n;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
|
|
|
use SilverStripe\ORM\PaginatedList;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
2017-05-20 06:32:25 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\SecurityToken;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\View\Requirements_Backend;
|
2021-10-27 04:39:47 +02:00
|
|
|
use SilverStripe\View\SSTemplateParseException;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\View\SSTemplateParser;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\SSViewer;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\View\SSViewer_FromString;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModel;
|
2016-12-30 00:27:20 +01:00
|
|
|
use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModelController;
|
2018-06-08 15:21:27 +02:00
|
|
|
use SilverStripe\View\Tests\SSViewerTest\TestViewableData;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ViewableData;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
class SSViewerTest extends SapphireTest
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backup of $_SERVER global
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $oldServer = [];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
SSViewerTest\TestObject::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2017-05-11 00:17:54 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2022-11-15 06:20:54 +01:00
|
|
|
SSViewer::config()->set('source_file_comments', false);
|
|
|
|
SSViewer_FromString::config()->set('cache_template', false);
|
2017-05-11 00:17:54 +02:00
|
|
|
TestAssetStore::activate('SSViewerTest');
|
|
|
|
$this->oldServer = $_SERVER;
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2017-05-11 00:17:54 +02:00
|
|
|
{
|
|
|
|
$_SERVER = $this->oldServer;
|
|
|
|
TestAssetStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-09-12 13:32:28 +02:00
|
|
|
/**
|
|
|
|
* Tests for themes helper functions, ensuring they behave as defined in the RFC at
|
|
|
|
* https://github.com/silverstripe/silverstripe-framework/issues/5604
|
|
|
|
*/
|
|
|
|
public function testThemesHelpers()
|
|
|
|
{
|
|
|
|
// Test set_themes()
|
|
|
|
SSViewer::set_themes(['mytheme', '$default']);
|
|
|
|
$this->assertEquals(['mytheme', '$default'], SSViewer::get_themes());
|
|
|
|
|
|
|
|
// Ensure add_themes() prepends
|
|
|
|
SSViewer::add_themes(['my_more_important_theme']);
|
|
|
|
$this->assertEquals(['my_more_important_theme', 'mytheme', '$default'], SSViewer::get_themes());
|
|
|
|
|
|
|
|
// Ensure add_themes() on theme already in cascade promotes it to the top
|
|
|
|
SSViewer::add_themes(['mytheme']);
|
|
|
|
$this->assertEquals(['mytheme', 'my_more_important_theme', '$default'], SSViewer::get_themes());
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that a template without a <head> tag still renders.
|
|
|
|
*/
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testTemplateWithoutHeadRenders()
|
|
|
|
{
|
2017-05-12 05:17:23 +02:00
|
|
|
$data = new ArrayData([ 'Var' => 'var value' ]);
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $data->renderWith("SSViewerTestPartialTemplate");
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals('Test partial template: var value', trim(preg_replace("/<!--.*-->/U", '', $result ?? '') ?? ''));
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-04-10 03:46:08 +02:00
|
|
|
/**
|
|
|
|
* Ensure global methods aren't executed
|
|
|
|
*/
|
|
|
|
public function testTemplateExecution()
|
|
|
|
{
|
|
|
|
$data = new ArrayData([ 'Var' => 'phpinfo' ]);
|
|
|
|
$result = $data->renderWith("SSViewerTestPartialTemplate");
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals('Test partial template: phpinfo', trim(preg_replace("/<!--.*-->/U", '', $result ?? '') ?? ''));
|
2018-04-10 03:46:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testIncludeScopeInheritance()
|
|
|
|
{
|
|
|
|
$data = $this->getScopeInheritanceTestData();
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
2017-05-10 06:24:30 +02:00
|
|
|
'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',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $data->renderWith('SSViewerTestIncludeScopeInheritance');
|
|
|
|
$this->assertExpectedStrings($result, $expected);
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// reset results for the tests that include arguments (the title is passed as an arg)
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
2017-05-10 06:24:30 +02:00
|
|
|
'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',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $data->renderWith('SSViewerTestIncludeScopeInheritanceWithArgs');
|
|
|
|
$this->assertExpectedStrings($result, $expected);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testIncludeTruthyness()
|
|
|
|
{
|
2017-05-12 05:17:23 +02:00
|
|
|
$data = new ArrayData([
|
|
|
|
'Title' => 'TruthyTest',
|
|
|
|
'Items' => new ArrayList([
|
|
|
|
new ArrayData(['Title' => 'Item 1']),
|
|
|
|
new ArrayData(['Title' => '']),
|
|
|
|
new ArrayData(['Title' => true]),
|
|
|
|
new ArrayData(['Title' => false]),
|
|
|
|
new ArrayData(['Title' => null]),
|
|
|
|
new ArrayData(['Title' => 0]),
|
|
|
|
new ArrayData(['Title' => 7])
|
|
|
|
])
|
|
|
|
]);
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $data->renderWith('SSViewerTestIncludeScopeInheritanceWithArgs');
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// We should not end up with empty values appearing as empty
|
2017-05-12 05:17:23 +02:00
|
|
|
$expected = [
|
|
|
|
'Item 1 _ Item 1 - First-ODD top:Item 1',
|
|
|
|
'Untitled - EVEN top:',
|
|
|
|
'1 _ 1 - ODD top:1',
|
|
|
|
'Untitled - EVEN top:',
|
|
|
|
'Untitled - ODD top:',
|
|
|
|
'Untitled - EVEN top:0',
|
|
|
|
'7 _ 7 - Last-ODD top:7',
|
|
|
|
];
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertExpectedStrings($result, $expected);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
private function getScopeInheritanceTestData()
|
|
|
|
{
|
2017-05-12 05:17:23 +02:00
|
|
|
return new ArrayData([
|
|
|
|
'Title' => 'TopTitleValue',
|
|
|
|
'Items' => new ArrayList([
|
|
|
|
new ArrayData(['Title' => 'Item 1']),
|
|
|
|
new ArrayData(['Title' => 'Item 2']),
|
|
|
|
new ArrayData(['Title' => 'Item 3']),
|
|
|
|
new ArrayData(['Title' => 'Item 4']),
|
|
|
|
new ArrayData(['Title' => 'Item 5']),
|
|
|
|
new ArrayData(['Title' => 'Item 6'])
|
|
|
|
])
|
|
|
|
]);
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
private function assertExpectedStrings($result, $expected)
|
|
|
|
{
|
|
|
|
foreach ($expected as $expectedStr) {
|
|
|
|
$this->assertTrue(
|
2022-04-14 03:12:59 +02:00
|
|
|
(boolean) preg_match("/{$expectedStr}/", $result ?? ''),
|
2017-05-12 05:17:23 +02:00
|
|
|
"Didn't find '{$expectedStr}' in:\n{$result}"
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Small helper to render templates from strings
|
|
|
|
*
|
|
|
|
* @param string $templateString
|
|
|
|
* @param null $data
|
|
|
|
* @param bool $cacheTemplate
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-05-11 00:17:54 +02:00
|
|
|
public function render($templateString, $data = null, $cacheTemplate = false)
|
|
|
|
{
|
|
|
|
$t = SSViewer::fromString($templateString, $cacheTemplate);
|
|
|
|
if (!$data) {
|
|
|
|
$data = new SSViewerTest\TestFixture();
|
|
|
|
}
|
2018-01-16 19:39:30 +01:00
|
|
|
return trim('' . $t->process($data));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testRequirements()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
/** @var Requirements_Backend|MockObject $requirements */
|
2017-05-12 05:17:23 +02:00
|
|
|
$requirements = $this
|
|
|
|
->getMockBuilder(Requirements_Backend::class)
|
2020-04-20 19:58:09 +02:00
|
|
|
->setMethods(["javascript", "css"])
|
2017-05-12 05:17:23 +02:00
|
|
|
->getMock();
|
2017-05-11 00:17:54 +02:00
|
|
|
$jsFile = FRAMEWORK_DIR . '/tests/forms/a.js';
|
|
|
|
$cssFile = FRAMEWORK_DIR . '/tests/forms/a.js';
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$requirements->expects($this->once())->method('javascript')->with($jsFile);
|
|
|
|
$requirements->expects($this->once())->method('css')->with($cssFile);
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$origReq = Requirements::backend();
|
|
|
|
Requirements::set_backend($requirements);
|
|
|
|
$template = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
"<% require javascript($jsFile) %>
|
2016-12-16 05:34:21 +01:00
|
|
|
<% require css($cssFile) %>"
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
Requirements::set_backend($origReq);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertFalse((bool)trim($template ?? ''), "Should be no content in this return.");
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testRequirementsCombine()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var Requirements_Backend $testBackend */
|
2017-05-11 00:17:54 +02:00
|
|
|
$testBackend = Injector::inst()->create(Requirements_Backend::class);
|
|
|
|
$testBackend->setSuffixRequirements(false);
|
2017-06-22 12:50:45 +02:00
|
|
|
$testBackend->setCombinedFilesEnabled(true);
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//$combinedTestFilePath = BASE_PATH . '/' . $testBackend->getCombinedFilesFolder() . '/testRequirementsCombine.js';
|
|
|
|
|
|
|
|
$jsFile = $this->getCurrentRelativePath() . '/SSViewerTest/javascript/bad.js';
|
|
|
|
$jsFileContents = file_get_contents(BASE_PATH . '/' . $jsFile);
|
2020-04-20 19:58:09 +02:00
|
|
|
$testBackend->combineFiles('testRequirementsCombine.js', [$jsFile]);
|
2017-05-11 00:17:54 +02:00
|
|
|
|
|
|
|
// secondly, make sure that requirements is generated, even though minification failed
|
|
|
|
$testBackend->processCombinedFiles();
|
2022-04-14 03:12:59 +02:00
|
|
|
$js = array_keys($testBackend->getJavascript() ?? []);
|
2018-01-12 04:25:02 +01:00
|
|
|
$combinedTestFilePath = Director::publicFolder() . reset($js);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('_combinedfiles/testRequirementsCombine-4c0e97a.js', $combinedTestFilePath);
|
2017-05-11 00:17:54 +02:00
|
|
|
|
|
|
|
// and make sure the combined content matches the input content, i.e. no loss of functionality
|
2022-04-14 03:12:59 +02:00
|
|
|
if (!file_exists($combinedTestFilePath ?? '')) {
|
2018-01-16 19:39:30 +01:00
|
|
|
$this->fail('No combined file was created at expected path: ' . $combinedTestFilePath);
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2022-04-14 03:12:59 +02:00
|
|
|
$combinedTestFileContents = file_get_contents($combinedTestFilePath ?? '');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString($jsFileContents, $combinedTestFileContents);
|
2017-05-10 06:24:30 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testComments()
|
|
|
|
{
|
2017-05-12 05:17:23 +02:00
|
|
|
$input = <<<SS
|
2012-09-26 23:34:00 +02:00
|
|
|
This is my template<%-- this is a comment --%>This is some content<%-- this is another comment --%>Final content
|
2013-03-05 16:22:09 +01:00
|
|
|
<%-- Alone multi
|
|
|
|
line comment --%>
|
|
|
|
Some more content
|
|
|
|
Mixing content and <%-- multi
|
2014-08-15 08:53:05 +02:00
|
|
|
line comment --%> Final final
|
2013-03-05 16:22:09 +01:00
|
|
|
content
|
2019-03-08 12:38:22 +01:00
|
|
|
<%--commentwithoutwhitespace--%>last content
|
2017-05-12 05:17:23 +02:00
|
|
|
SS;
|
2019-03-08 12:38:22 +01:00
|
|
|
$actual = $this->render($input);
|
|
|
|
$expected = <<<SS
|
2013-03-05 16:22:09 +01:00
|
|
|
This is my templateThis is some contentFinal content
|
|
|
|
|
|
|
|
Some more content
|
2014-08-15 08:53:05 +02:00
|
|
|
Mixing content and Final final
|
2013-03-05 16:22:09 +01:00
|
|
|
content
|
2019-03-08 12:38:22 +01:00
|
|
|
last content
|
2013-03-05 16:22:09 +01:00
|
|
|
SS;
|
2019-03-08 12:38:22 +01:00
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
|
|
|
|
$input = <<<SS
|
|
|
|
<%--
|
|
|
|
|
|
|
|
--%>empty comment1
|
|
|
|
<%-- --%>empty comment2
|
|
|
|
<%----%>empty comment3
|
|
|
|
SS;
|
|
|
|
$actual = $this->render($input);
|
|
|
|
$expected = <<<SS
|
|
|
|
empty comment1
|
|
|
|
empty comment2
|
|
|
|
empty comment3
|
|
|
|
SS;
|
|
|
|
$this->assertEquals($expected, $actual);
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testBasicText()
|
|
|
|
{
|
|
|
|
$this->assertEquals('"', $this->render('"'), 'Double-quotes are left alone');
|
|
|
|
$this->assertEquals("'", $this->render("'"), 'Single-quotes are left alone');
|
|
|
|
$this->assertEquals('A', $this->render('\\A'), 'Escaped characters are unescaped');
|
|
|
|
$this->assertEquals('\\A', $this->render('\\\\A'), 'Escaped back-slashed are correctly unescaped');
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testBasicInjection()
|
|
|
|
{
|
|
|
|
$this->assertEquals('[out:Test]', $this->render('$Test'), 'Basic stand-alone injection');
|
|
|
|
$this->assertEquals('[out:Test]', $this->render('{$Test}'), 'Basic stand-alone wrapped injection');
|
|
|
|
$this->assertEquals('A[out:Test]!', $this->render('A$Test!'), 'Basic surrounded injection');
|
|
|
|
$this->assertEquals('A[out:Test]B', $this->render('A{$Test}B'), 'Basic surrounded wrapped injection');
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals('A$B', $this->render('A\\$B'), 'No injection as $ escaped');
|
|
|
|
$this->assertEquals('A$ B', $this->render('A$ B'), 'No injection as $ not followed by word character');
|
|
|
|
$this->assertEquals('A{$ B', $this->render('A{$ B'), 'No injection as {$ not followed by word character');
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals('{$Test}', $this->render('{\\$Test}'), 'Escapes can be used to avoid injection');
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'{\\[out:Test]}',
|
|
|
|
$this->render('{\\\\$Test}'),
|
|
|
|
'Escapes before injections are correctly unescaped'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2019-03-08 13:42:14 +01:00
|
|
|
public function testBasicInjectionMismatchedBrackets()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(SSTemplateParseException::class);
|
|
|
|
$this->expectExceptionMessageMatches('/Malformed bracket injection {\$Value(.*)/');
|
2019-03-08 13:42:14 +01:00
|
|
|
$this->render('A {$Value here');
|
|
|
|
$this->fail("Parser didn't error when encountering mismatched brackets in an injection");
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testGlobalVariableCalls()
|
|
|
|
{
|
|
|
|
$this->assertEquals('automatic', $this->render('$SSViewerTest_GlobalAutomatic'));
|
|
|
|
$this->assertEquals('reference', $this->render('$SSViewerTest_GlobalReferencedByString'));
|
|
|
|
$this->assertEquals('reference', $this->render('$SSViewerTest_GlobalReferencedInArray'));
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testGlobalVariableCallsWithArguments()
|
|
|
|
{
|
|
|
|
$this->assertEquals('zz', $this->render('$SSViewerTest_GlobalThatTakesArguments'));
|
|
|
|
$this->assertEquals('zFooz', $this->render('$SSViewerTest_GlobalThatTakesArguments("Foo")'));
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'zFoo:Bar:Bazz',
|
|
|
|
$this->render('$SSViewerTest_GlobalThatTakesArguments("Foo", "Bar", "Baz")')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'zreferencez',
|
|
|
|
$this->render('$SSViewerTest_GlobalThatTakesArguments($SSViewerTest_GlobalReferencedByString)')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testGlobalVariablesAreEscaped()
|
|
|
|
{
|
|
|
|
$this->assertEquals('<div></div>', $this->render('$SSViewerTest_GlobalHTMLFragment'));
|
|
|
|
$this->assertEquals('<div></div>', $this->render('$SSViewerTest_GlobalHTMLEscaped'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'z<div></div>z',
|
|
|
|
$this->render('$SSViewerTest_GlobalThatTakesArguments($SSViewerTest_GlobalHTMLFragment)')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'z<div></div>z',
|
|
|
|
$this->render('$SSViewerTest_GlobalThatTakesArguments($SSViewerTest_GlobalHTMLEscaped)')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testCoreGlobalVariableCalls()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
$this->render('{$absoluteBaseURL}'),
|
|
|
|
'Director::absoluteBaseURL can be called from within template'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
$this->render('{$AbsoluteBaseURL}'),
|
|
|
|
'Upper-case %AbsoluteBaseURL can be called from within template'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
Director::is_ajax(),
|
|
|
|
$this->render('{$isAjax}'),
|
|
|
|
'All variations of is_ajax result in the correct call'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
Director::is_ajax(),
|
|
|
|
$this->render('{$IsAjax}'),
|
|
|
|
'All variations of is_ajax result in the correct call'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
Director::is_ajax(),
|
|
|
|
$this->render('{$is_ajax}'),
|
|
|
|
'All variations of is_ajax result in the correct call'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
Director::is_ajax(),
|
|
|
|
$this->render('{$Is_ajax}'),
|
|
|
|
'All variations of is_ajax result in the correct call'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
i18n::get_locale(),
|
|
|
|
$this->render('{$i18nLocale}'),
|
|
|
|
'i18n template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
i18n::get_locale(),
|
|
|
|
$this->render('{$get_locale}'),
|
|
|
|
'i18n template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-20 06:32:25 +02:00
|
|
|
(string)Security::getCurrentUser(),
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('{$CurrentMember}'),
|
|
|
|
'Member template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-20 06:32:25 +02:00
|
|
|
(string)Security::getCurrentUser(),
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('{$CurrentUser}'),
|
|
|
|
'Member template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-20 06:32:25 +02:00
|
|
|
(string)Security::getCurrentUser(),
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('{$currentMember}'),
|
|
|
|
'Member template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-20 06:32:25 +02:00
|
|
|
(string)Security::getCurrentUser(),
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('{$currentUser}'),
|
|
|
|
'Member template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
SecurityToken::getSecurityID(),
|
|
|
|
$this->render('{$getSecurityID}'),
|
|
|
|
'SecurityToken template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
SecurityToken::getSecurityID(),
|
|
|
|
$this->render('{$SecurityID}'),
|
|
|
|
'SecurityToken template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
Permission::check("ADMIN"),
|
|
|
|
(bool)$this->render('{$HasPerm(\'ADMIN\')}'),
|
|
|
|
'Permissions template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
Permission::check("ADMIN"),
|
|
|
|
(bool)$this->render('{$hasPerm(\'ADMIN\')}'),
|
|
|
|
'Permissions template functions result correct result'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testNonFieldCastingHelpersNotUsedInHasValue()
|
|
|
|
{
|
|
|
|
// check if Link without $ in front of variable
|
|
|
|
$result = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
'A<% if Link %>$Link<% end_if %>B',
|
|
|
|
new SSViewerTest\TestObject()
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals('Asome/url.htmlB', $result, 'casting helper not used for <% if Link %>');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// check if Link with $ in front of variable
|
|
|
|
$result = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
'A<% if $Link %>$Link<% end_if %>B',
|
|
|
|
new SSViewerTest\TestObject()
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals('Asome/url.htmlB', $result, 'casting helper not used for <% if $Link %>');
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testLocalFunctionsTakePriorityOverGlobals()
|
|
|
|
{
|
2017-05-12 05:17:23 +02:00
|
|
|
$data = new ArrayData([
|
|
|
|
'Page' => new SSViewerTest\TestObject()
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//call method with lots of arguments
|
|
|
|
$result = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<% with Page %>$lotsOfArguments11("a","b","c","d","e","f","g","h","i","j","k")<% end_with %>',
|
|
|
|
$data
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals("abcdefghijk", $result, "public function can accept up to 11 arguments");
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//call method that does not exist
|
|
|
|
$result = $this->render('<% with Page %><% if IDoNotExist %>hello<% end_if %><% end_with %>', $data);
|
|
|
|
$this->assertEquals("", $result, "Method does not exist - empty result");
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//call if that does not exist
|
|
|
|
$result = $this->render('<% with Page %>$IDoNotExist("hello")<% end_with %>', $data);
|
|
|
|
$this->assertEquals("", $result, "Method does not exist - empty result");
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//call method with same name as a global method (local call should take priority)
|
|
|
|
$result = $this->render('<% with Page %>$absoluteBaseURL<% end_with %>', $data);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
"testLocalFunctionPriorityCalled",
|
|
|
|
$result,
|
|
|
|
"Local Object's public function called. Did not return the actual baseURL of the current site"
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testCurrentScopeLoopWith()
|
|
|
|
{
|
|
|
|
// Data to run the loop tests on - one sequence of three items, each with a subitem
|
2017-05-12 05:17:23 +02:00
|
|
|
$data = new ArrayData([
|
|
|
|
'Foo' => new ArrayList([
|
|
|
|
'Subocean' => new ArrayData([
|
2017-05-10 06:24:30 +02:00
|
|
|
'Name' => 'Higher'
|
2017-05-12 05:17:23 +02:00
|
|
|
]),
|
|
|
|
new ArrayData([
|
|
|
|
'Sub' => new ArrayData([
|
|
|
|
'Name' => 'SubKid1'
|
|
|
|
])
|
|
|
|
]),
|
|
|
|
new ArrayData([
|
|
|
|
'Sub' => new ArrayData([
|
|
|
|
'Name' => 'SubKid2'
|
|
|
|
])
|
|
|
|
]),
|
|
|
|
new SSViewerTest\TestObject('Number6')
|
|
|
|
])
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<% loop Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %><% end_if %><% end_loop %>',
|
|
|
|
$data
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop works");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<% loop Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %><% end_if %><% end_loop %>',
|
|
|
|
$data
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop works");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $this->render('<% with Foo %>$Count<% end_with %>', $data);
|
|
|
|
$this->assertEquals("4", $result, "4 items in the DataObjectSet");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<% with Foo %><% loop Up.Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %>'
|
|
|
|
. '<% end_if %><% end_loop %><% end_with %>',
|
|
|
|
$data
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop in with Up.Foo scope works");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<% with Foo %><% loop %>$Number<% if Sub %><% with Sub %>$Name<% end_with %>'
|
|
|
|
. '<% end_if %><% end_loop %><% end_with %>',
|
|
|
|
$data
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop in current scope works");
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testObjectDotArguments()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'[out:TestObject.methodWithOneArgument(one)]
|
2009-11-27 01:24:57 +01:00
|
|
|
[out:TestObject.methodWithTwoArguments(one,two)]
|
|
|
|
[out:TestMethod(Arg1,Arg2).Bar.Val]
|
|
|
|
[out:TestMethod(Arg1,Arg2).Bar]
|
|
|
|
[out:TestMethod(Arg1,Arg2)]
|
|
|
|
[out:TestMethod(Arg1).Bar.Val]
|
|
|
|
[out:TestMethod(Arg1).Bar]
|
|
|
|
[out:TestMethod(Arg1)]',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'$TestObject.methodWithOneArgument(one)
|
2009-11-27 01:24:57 +01:00
|
|
|
$TestObject.methodWithTwoArguments(one,two)
|
|
|
|
$TestMethod(Arg1, Arg2).Bar.Val
|
|
|
|
$TestMethod(Arg1, Arg2).Bar
|
|
|
|
$TestMethod(Arg1, Arg2)
|
|
|
|
$TestMethod(Arg1).Bar.Val
|
|
|
|
$TestMethod(Arg1).Bar
|
2016-12-16 05:34:21 +01:00
|
|
|
$TestMethod(Arg1)'
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testEscapedArguments()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'[out:Foo(Arg1,Arg2).Bar.Val].Suffix
|
2009-11-27 01:24:57 +01:00
|
|
|
[out:Foo(Arg1,Arg2).Val]_Suffix
|
|
|
|
[out:Foo(Arg1,Arg2)]/Suffix
|
|
|
|
[out:Foo(Arg1).Bar.Val]textSuffix
|
|
|
|
[out:Foo(Arg1).Bar].Suffix
|
|
|
|
[out:Foo(Arg1)].Suffix
|
|
|
|
[out:Foo.Bar.Val].Suffix
|
|
|
|
[out:Foo.Bar].Suffix
|
|
|
|
[out:Foo].Suffix',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'{$Foo(Arg1, Arg2).Bar.Val}.Suffix
|
2009-11-27 01:24:57 +01:00
|
|
|
{$Foo(Arg1, Arg2).Val}_Suffix
|
|
|
|
{$Foo(Arg1, Arg2)}/Suffix
|
|
|
|
{$Foo(Arg1).Bar.Val}textSuffix
|
|
|
|
{$Foo(Arg1).Bar}.Suffix
|
|
|
|
{$Foo(Arg1)}.Suffix
|
|
|
|
{$Foo.Bar.Val}.Suffix
|
|
|
|
{$Foo.Bar}.Suffix
|
2016-12-16 05:34:21 +01:00
|
|
|
{$Foo}.Suffix'
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testLoopWhitespace()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'before[out:SingleItem.Test]after
|
2009-11-27 01:24:58 +01:00
|
|
|
beforeTestafter',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'before<% loop SingleItem %>$Test<% end_loop %>after
|
2016-12-16 05:34:21 +01:00
|
|
|
before<% loop SingleItem %>Test<% end_loop %>after'
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// The control tags are removed from the output, but no whitespace
|
|
|
|
// This is a quirk that could be changed, but included in the test to make the current
|
|
|
|
// behaviour explicit
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'before
|
2009-11-27 01:24:58 +01:00
|
|
|
|
|
|
|
[out:SingleItem.ItemOnItsOwnLine]
|
|
|
|
|
|
|
|
after',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'before
|
2013-03-19 11:43:23 +01:00
|
|
|
<% loop SingleItem %>
|
2009-11-27 01:24:58 +01:00
|
|
|
$ItemOnItsOwnLine
|
2013-03-19 11:43:23 +01:00
|
|
|
<% end_loop %>
|
2016-12-16 05:34:21 +01:00
|
|
|
after'
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2009-11-27 01:24:58 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// The whitespace within the control tags is preserve in a loop
|
|
|
|
// This is a quirk that could be changed, but included in the test to make the current
|
|
|
|
// behaviour explicit
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'before
|
2009-11-27 01:24:58 +01:00
|
|
|
|
|
|
|
[out:Loop3.ItemOnItsOwnLine]
|
|
|
|
|
|
|
|
[out:Loop3.ItemOnItsOwnLine]
|
|
|
|
|
|
|
|
[out:Loop3.ItemOnItsOwnLine]
|
|
|
|
|
|
|
|
after',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'before
|
2013-03-19 11:43:23 +01:00
|
|
|
<% loop Loop3 %>
|
2009-11-27 01:24:58 +01:00
|
|
|
$ItemOnItsOwnLine
|
2013-03-19 11:43:23 +01:00
|
|
|
<% end_loop %>
|
2016-12-16 05:34:21 +01:00
|
|
|
after'
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-10-05 13:06:58 +02:00
|
|
|
public function typePreservationDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
// Null
|
|
|
|
['NULL:', 'null'],
|
|
|
|
['NULL:', 'NULL'],
|
|
|
|
// Booleans
|
|
|
|
['boolean:1', 'true'],
|
|
|
|
['boolean:1', 'TRUE'],
|
|
|
|
['boolean:', 'false'],
|
|
|
|
['boolean:', 'FALSE'],
|
2018-10-05 13:47:04 +02:00
|
|
|
// Strings which may look like booleans/null to the parser
|
|
|
|
['string:nullish', 'nullish'],
|
|
|
|
['string:notnull', 'notnull'],
|
|
|
|
['string:truethy', 'truethy'],
|
|
|
|
['string:untrue', 'untrue'],
|
|
|
|
['string:falsey', 'falsey'],
|
2018-10-05 13:06:58 +02:00
|
|
|
// Integers
|
|
|
|
['integer:0', '0'],
|
|
|
|
['integer:1', '1'],
|
|
|
|
['integer:15', '15'],
|
|
|
|
['integer:-15', '-15'],
|
|
|
|
// Octal integers
|
|
|
|
['integer:83', '0123'],
|
|
|
|
['integer:-83', '-0123'],
|
|
|
|
// Hexadecimal integers
|
|
|
|
['integer:26', '0x1A'],
|
|
|
|
['integer:-26', '-0x1A'],
|
|
|
|
// Binary integers
|
|
|
|
['integer:255', '0b11111111'],
|
|
|
|
['integer:-255', '-0b11111111'],
|
|
|
|
// Floats (aka doubles)
|
|
|
|
['double:0', '0.0'],
|
|
|
|
['double:1', '1.0'],
|
|
|
|
['double:15.25', '15.25'],
|
|
|
|
['double:-15.25', '-15.25'],
|
|
|
|
['double:1200', '1.2e3'],
|
|
|
|
['double:-1200', '-1.2e3'],
|
|
|
|
['double:0.07', '7E-2'],
|
|
|
|
['double:-0.07', '-7E-2'],
|
|
|
|
// Explicitly quoted strings
|
|
|
|
['string:0', '"0"'],
|
|
|
|
['string:1', '\'1\''],
|
|
|
|
['string:foobar', '"foobar"'],
|
|
|
|
['string:foo bar baz', '"foo bar baz"'],
|
2022-09-09 03:42:06 +02:00
|
|
|
['string:false', '\'false\''],
|
|
|
|
['string:true', '\'true\''],
|
|
|
|
['string:null', '\'null\''],
|
|
|
|
['string:false', '"false"'],
|
|
|
|
['string:true', '"true"'],
|
|
|
|
['string:null', '"null"'],
|
2018-10-05 13:06:58 +02:00
|
|
|
// Implicit strings
|
|
|
|
['string:foobar', 'foobar'],
|
|
|
|
['string:foo bar baz', 'foo bar baz']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider typePreservationDataProvider
|
|
|
|
*/
|
|
|
|
public function testTypesArePreserved($expected, $templateArg)
|
2018-06-08 15:21:27 +02:00
|
|
|
{
|
|
|
|
$data = new ArrayData([
|
|
|
|
'Test' => new TestViewableData()
|
|
|
|
]);
|
|
|
|
|
2018-10-05 13:06:58 +02:00
|
|
|
$this->assertEquals($expected, $this->render("\$Test.Type({$templateArg})", $data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider typePreservationDataProvider
|
|
|
|
*/
|
|
|
|
public function testTypesArePreservedAsIncludeArguments($expected, $templateArg)
|
|
|
|
{
|
|
|
|
$data = new ArrayData([
|
|
|
|
'Test' => new TestViewableData()
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$this->render("<% include SSViewerTestTypePreservation Argument={$templateArg} %>", $data)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTypePreservationInConditionals()
|
|
|
|
{
|
|
|
|
$data = new ArrayData([
|
|
|
|
'Test' => new TestViewableData()
|
|
|
|
]);
|
2018-06-08 15:21:27 +02:00
|
|
|
|
|
|
|
// Types in conditionals
|
|
|
|
$this->assertEquals('pass', $this->render('<% if true %>pass<% else %>fail<% end_if %>', $data));
|
|
|
|
$this->assertEquals('pass', $this->render('<% if false %>fail<% else %>pass<% end_if %>', $data));
|
|
|
|
$this->assertEquals('pass', $this->render('<% if 1 %>pass<% else %>fail<% end_if %>', $data));
|
|
|
|
$this->assertEquals('pass', $this->render('<% if 0 %>fail<% else %>pass<% end_if %>', $data));
|
|
|
|
}
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testControls()
|
|
|
|
{
|
|
|
|
// Single item controls
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'a[out:Foo.Bar.Item]b
|
2009-11-27 01:24:58 +01:00
|
|
|
[out:Foo.Bar(Arg1).Item]
|
|
|
|
[out:Foo(Arg1).Item]
|
|
|
|
[out:Foo(Arg1,Arg2).Item]
|
|
|
|
[out:Foo(Arg1,Arg2,Arg3).Item]',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'<% with Foo.Bar %>a{$Item}b<% end_with %>
|
2013-03-19 11:43:23 +01:00
|
|
|
<% with Foo.Bar(Arg1) %>$Item<% end_with %>
|
|
|
|
<% with Foo(Arg1) %>$Item<% end_with %>
|
|
|
|
<% with Foo(Arg1, Arg2) %>$Item<% end_with %>
|
2016-12-16 05:34:21 +01:00
|
|
|
<% with Foo(Arg1, Arg2, Arg3) %>$Item<% end_with %>'
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Loop controls
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'a[out:Foo.Loop2.Item]ba[out:Foo.Loop2.Item]b',
|
|
|
|
$this->render('<% loop Foo.Loop2 %>a{$Item}b<% end_loop %>')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'[out:Foo.Loop2(Arg1).Item][out:Foo.Loop2(Arg1).Item]',
|
|
|
|
$this->render('<% loop Foo.Loop2(Arg1) %>$Item<% end_loop %>')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'[out:Loop2(Arg1).Item][out:Loop2(Arg1).Item]',
|
|
|
|
$this->render('<% loop Loop2(Arg1) %>$Item<% end_loop %>')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'[out:Loop2(Arg1,Arg2).Item][out:Loop2(Arg1,Arg2).Item]',
|
|
|
|
$this->render('<% loop Loop2(Arg1, Arg2) %>$Item<% end_loop %>')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'[out:Loop2(Arg1,Arg2,Arg3).Item][out:Loop2(Arg1,Arg2,Arg3).Item]',
|
|
|
|
$this->render('<% loop Loop2(Arg1, Arg2, Arg3) %>$Item<% end_loop %>')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testIfBlocks()
|
|
|
|
{
|
|
|
|
// Basic test
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AC',
|
|
|
|
$this->render('A<% if NotSet %>B$NotSet<% end_if %>C')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Nested test
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AB1C',
|
|
|
|
$this->render('A<% if IsSet %>B$NotSet<% if IsSet %>1<% else %>2<% end_if %><% end_if %>C')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// else_if
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if NotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AD',
|
|
|
|
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ADE',
|
|
|
|
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% else_if IsSet %>D<% end_if %>E')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ADE',
|
|
|
|
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% else_if IsSet %>D<% end_if %>E')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Dot syntax
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if Foo.NotSet %>B<% else_if Foo.IsSet %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if Foo.Bar.NotSet %>B<% else_if Foo.Bar.IsSet %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Params
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if NotSet(Param) %>B<% else %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABD',
|
|
|
|
$this->render('A<% if IsSet(Param) %>B<% else %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Negation
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AC',
|
|
|
|
$this->render('A<% if not IsSet %>B<% end_if %>C')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABC',
|
|
|
|
$this->render('A<% if not NotSet %>B<% end_if %>C')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Or
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABD',
|
|
|
|
$this->render('A<% if IsSet || NotSet %>B<% else_if A %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AD',
|
|
|
|
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if NotSet3 %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if IsSet || NotSet %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AD',
|
|
|
|
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if NotSet2 || NotSet3 %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Negated Or
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if not IsSet || AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABD',
|
|
|
|
$this->render('A<% if not NotSet || AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABD',
|
|
|
|
$this->render('A<% if NotSet || not AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// And
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABD',
|
|
|
|
$this->render('A<% if IsSet && AlsoSet %>B<% else_if A %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if IsSet && NotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AD',
|
|
|
|
$this->render('A<% if NotSet && NotSet2 %>B<% else_if NotSet3 %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if IsSet && NotSet %>B<% else_if IsSet && AlsoSet %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AD',
|
|
|
|
$this->render('A<% if NotSet && NotSet2 %>B<% else_if IsSet && NotSet3 %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Equality
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABC',
|
|
|
|
$this->render('A<% if RawVal == RawVal %>B<% end_if %>C')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ACD',
|
|
|
|
$this->render('A<% if Right == Wrong %>B<% else_if RawVal == RawVal %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABC',
|
|
|
|
$this->render('A<% if Right != Wrong %>B<% end_if %>C')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AD',
|
|
|
|
$this->render('A<% if Right == Wrong %>B<% else_if RawVal != RawVal %>C<% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// test inequalities with simple numbers
|
|
|
|
$this->assertEquals('ABD', $this->render('A<% if 5 > 3 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ABD', $this->render('A<% if 5 >= 3 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ACD', $this->render('A<% if 3 > 5 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ACD', $this->render('A<% if 3 >= 5 %>B<% else %>C<% end_if %>D'));
|
|
|
|
|
|
|
|
$this->assertEquals('ABD', $this->render('A<% if 3 < 5 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ABD', $this->render('A<% if 3 <= 5 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ACD', $this->render('A<% if 5 < 3 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ACD', $this->render('A<% if 5 <= 3 %>B<% else %>C<% end_if %>D'));
|
|
|
|
|
|
|
|
$this->assertEquals('ABD', $this->render('A<% if 4 <= 4 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ABD', $this->render('A<% if 4 >= 4 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ACD', $this->render('A<% if 4 > 4 %>B<% else %>C<% end_if %>D'));
|
|
|
|
$this->assertEquals('ACD', $this->render('A<% if 4 < 4 %>B<% else %>C<% end_if %>D'));
|
|
|
|
|
|
|
|
// empty else_if and else tags, if this would not be supported,
|
|
|
|
// the output would stop after A, thereby failing the assert
|
|
|
|
$this->assertEquals('AD', $this->render('A<% if IsSet %><% else %><% end_if %>D'));
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AD',
|
|
|
|
$this->render('A<% if NotSet %><% else_if IsSet %><% else %><% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'AD',
|
|
|
|
$this->render('A<% if NotSet %><% else_if AlsoNotSet %><% else %><% end_if %>D')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Bare words with ending space
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABC',
|
|
|
|
$this->render('A<% if "RawVal" == RawVal %>B<% end_if %>C')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Else
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ADE',
|
|
|
|
$this->render('A<% if Right == Wrong %>B<% else_if RawVal != RawVal %>C<% else %>D<% end_if %>E')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Empty if with else
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'ABC',
|
|
|
|
$this->render('A<% if NotSet %><% else %>B<% end_if %>C')
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testBaseTagGeneration()
|
|
|
|
{
|
2021-12-13 09:05:33 +01:00
|
|
|
// XHTML will have a closed base tag
|
2017-05-11 00:17:54 +02:00
|
|
|
$tmpl1 = '<?xml version="1.0" encoding="UTF-8"?>
|
2012-09-26 23:34:00 +02:00
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
|
2017-05-10 06:24:30 +02:00
|
|
|
. ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
2009-10-31 01:16:54 +01:00
|
|
|
<html>
|
|
|
|
<head><% base_tag %></head>
|
|
|
|
<body><p>test</p><body>
|
2009-11-27 01:24:57 +01:00
|
|
|
</html>';
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertMatchesRegularExpression('/<head><base href=".*" \/><\/head>/', $this->render($tmpl1));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// HTML4 and 5 will only have it for IE
|
|
|
|
$tmpl2 = '<!DOCTYPE html>
|
2009-10-31 01:16:54 +01:00
|
|
|
<html>
|
|
|
|
<head><% base_tag %></head>
|
|
|
|
<body><p>test</p><body>
|
2009-11-27 01:24:57 +01:00
|
|
|
</html>';
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertMatchesRegularExpression(
|
2017-05-12 05:17:23 +02:00
|
|
|
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
|
|
|
|
$this->render($tmpl2)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$tmpl3 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
2009-10-31 01:16:54 +01:00
|
|
|
<html>
|
|
|
|
<head><% base_tag %></head>
|
|
|
|
<body><p>test</p><body>
|
2009-11-27 01:24:57 +01:00
|
|
|
</html>';
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertMatchesRegularExpression(
|
2017-05-12 05:17:23 +02:00
|
|
|
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
|
|
|
|
$this->render($tmpl3)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Check that the content negotiator converts to the equally legal formats
|
|
|
|
$negotiator = new ContentNegotiator();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$response = new HTTPResponse($this->render($tmpl1));
|
|
|
|
$negotiator->html($response);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertMatchesRegularExpression(
|
2017-05-12 05:17:23 +02:00
|
|
|
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
|
|
|
|
$response->getBody()
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$response = new HTTPResponse($this->render($tmpl1));
|
|
|
|
$negotiator->xhtml($response);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertMatchesRegularExpression('/<head><base href=".*" \/><\/head>/', $response->getBody());
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testIncludeWithArguments()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('<% include SSViewerTestIncludeWithArguments %>'),
|
|
|
|
'<p>[out:Arg1]</p><p>[out:Arg2]</p>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('<% include SSViewerTestIncludeWithArguments Arg1=A %>'),
|
|
|
|
'<p>A</p><p>[out:Arg2]</p>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('<% include SSViewerTestIncludeWithArguments Arg1=A, Arg2=B %>'),
|
|
|
|
'<p>A</p><p>B</p>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('<% include SSViewerTestIncludeWithArguments Arg1=A Bare String, Arg2=B Bare String %>'),
|
|
|
|
'<p>A Bare String</p><p>B Bare String</p>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=$B %>',
|
2020-04-20 19:58:09 +02:00
|
|
|
new ArrayData(['B' => 'Bar'])
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
'<p>A</p><p>Bar</p>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'<% include SSViewerTestIncludeWithArguments Arg1="A" %>',
|
2020-04-20 19:58:09 +02:00
|
|
|
new ArrayData(['Arg1' => 'Foo', 'Arg2' => 'Bar'])
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
'<p>A</p><p>Bar</p>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'<% include SSViewerTestIncludeScopeInheritanceWithArgsInLoop Title="SomeArg" %>',
|
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Items' => new ArrayList(
|
|
|
|
[
|
|
|
|
new ArrayData(['Title' => 'Foo']),
|
|
|
|
new ArrayData(['Title' => 'Bar'])
|
|
|
|
]
|
|
|
|
)]
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'SomeArg - Foo - Bar - SomeArg'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'<% include SSViewerTestIncludeScopeInheritanceWithArgsInWith Title="A" %>',
|
2020-04-20 19:58:09 +02:00
|
|
|
new ArrayData(['Item' => new ArrayData(['Title' =>'B'])])
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
'A - B - A'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'<% include SSViewerTestIncludeScopeInheritanceWithArgsInNestedWith Title="A" %>',
|
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Item' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Title' =>'B', 'NestedItem' => new ArrayData(['Title' => 'C'])
|
|
|
|
]
|
|
|
|
)]
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'A - B - C - B - A'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
|
|
|
'<% include SSViewerTestIncludeScopeInheritanceWithUpAndTop Title="A" %>',
|
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Item' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Title' =>'B', 'NestedItem' => new ArrayData(['Title' => 'C'])
|
|
|
|
]
|
|
|
|
)]
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'A - A - A'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$data = new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Nested' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Object' => new ArrayData(['Key' => 'A'])
|
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
'Object' => new ArrayData(['Key' => 'B'])
|
|
|
|
]
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$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');
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testNamespaceInclude()
|
|
|
|
{
|
|
|
|
$data = new ArrayData([]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
"tests:( NamespaceInclude\n )",
|
|
|
|
$this->render('tests:( <% include Namespace\NamespaceInclude %> )', $data),
|
|
|
|
'Backslashes work for namespace references in includes'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-08-29 06:15:46 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
"tests:( NamespaceInclude\n )",
|
|
|
|
$this->render('tests:( <% include Namespace\\NamespaceInclude %> )', $data),
|
|
|
|
'Escaped backslashes work for namespace references in includes'
|
|
|
|
);
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
"tests:( NamespaceInclude\n )",
|
|
|
|
$this->render('tests:( <% include Namespace/NamespaceInclude %> )', $data),
|
|
|
|
'Forward slashes work for namespace references in includes'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-08-29 06:15:46 +02:00
|
|
|
/**
|
|
|
|
* Test search for includes fallback to non-includes folder
|
|
|
|
*/
|
|
|
|
public function testIncludeFallbacks()
|
|
|
|
{
|
|
|
|
$data = new ArrayData([]);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"tests:( Namespace/Includes/IncludedTwice.ss\n )",
|
|
|
|
$this->render('tests:( <% include Namespace\\IncludedTwice %> )', $data),
|
|
|
|
'Prefer Includes in the Includes folder'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"tests:( Namespace/Includes/IncludedOnceSub.ss\n )",
|
|
|
|
$this->render('tests:( <% include Namespace\\IncludedOnceSub %> )', $data),
|
|
|
|
'Includes in only Includes folder can be found'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"tests:( Namespace/IncludedOnceBase.ss\n )",
|
|
|
|
$this->render('tests:( <% include Namespace\\IncludedOnceBase %> )', $data),
|
|
|
|
'Includes outside of Includes folder can be found'
|
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testRecursiveInclude()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$view = new SSViewer(['Includes/SSViewerTestRecursiveInclude']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$data = new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Title' => 'A',
|
|
|
|
'Children' => new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Title' => 'A1',
|
|
|
|
'Children' => new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
new ArrayData([ 'Title' => 'A1 i', ]),
|
|
|
|
new ArrayData([ 'Title' => 'A1 ii', ]),
|
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
new ArrayData([ 'Title' => 'A2', ]),
|
|
|
|
new ArrayData([ 'Title' => 'A3', ]),
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $view->process($data);
|
|
|
|
// We don't care about whitespace
|
2022-04-14 03:12:59 +02:00
|
|
|
$rationalisedResult = trim(preg_replace('/\s+/', ' ', $result ?? '') ?? '');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals('A A1 A1 i A1 ii A2 A3', $rationalisedResult);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function assertEqualIgnoringWhitespace($a, $b, $message = '')
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(preg_replace('/\s+/', '', $a ?? ''), preg_replace('/\s+/', '', $b ?? ''), $message);
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See {@link ViewableDataTest} for more extensive casting tests,
|
|
|
|
* this test just ensures that basic casting is correctly applied during template parsing.
|
|
|
|
*/
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testCastingHelpers()
|
|
|
|
{
|
|
|
|
$vd = new SSViewerTest\TestViewableData();
|
|
|
|
$vd->TextValue = '<b>html</b>';
|
|
|
|
$vd->HTMLValue = '<b>html</b>';
|
|
|
|
$vd->UncastedValue = '<b>html</b>';
|
|
|
|
|
|
|
|
// Value casted as "Text"
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$TextValue')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$TextValue.RAW')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$TextValue.XML')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Value casted as "HTMLText"
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$HTMLValue')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$HTMLValue.RAW')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$HTMLValue.XML')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Uncasted value (falls back to ViewableData::$default_cast="Text")
|
|
|
|
$vd = new SSViewerTest\TestViewableData();
|
|
|
|
$vd->UncastedValue = '<b>html</b>';
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$UncastedValue')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$UncastedValue.RAW')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<b>html</b>',
|
|
|
|
$t = SSViewer::fromString('$UncastedValue.XML')->process($vd)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testSSViewerBasicIteratorSupport()
|
|
|
|
{
|
|
|
|
$data = new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Set' => new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
new SSViewerTest\TestObject("1"),
|
|
|
|
new SSViewerTest\TestObject("2"),
|
|
|
|
new SSViewerTest\TestObject("3"),
|
|
|
|
new SSViewerTest\TestObject("4"),
|
|
|
|
new SSViewerTest\TestObject("5"),
|
|
|
|
new SSViewerTest\TestObject("6"),
|
|
|
|
new SSViewerTest\TestObject("7"),
|
|
|
|
new SSViewerTest\TestObject("8"),
|
|
|
|
new SSViewerTest\TestObject("9"),
|
|
|
|
new SSViewerTest\TestObject("10"),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//base test
|
|
|
|
$result = $this->render('<% loop Set %>$Number<% end_loop %>', $data);
|
|
|
|
$this->assertEquals("12345678910", $result, "Numbers rendered in order");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test First
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $IsFirst %>$Number<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("1", $result, "Only the first number is rendered");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Last
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $IsLast %>$Number<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("10", $result, "Only the last number is rendered");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Even
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $Even() %>$Number<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Even with quotes
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $Even("1") %>$Number<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Even without quotes
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $Even(1) %>$Number<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Even with zero-based start index
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $Even("0") %>$Number<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("13579", $result, "Even (with zero-based index) numbers rendered in order");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Odd
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $Odd %>$Number<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("13579", $result, "Odd numbers rendered in order");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test FirstLast
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $FirstLast %>$Number$FirstLast<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("1first10last", $result, "First and last numbers rendered in order");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Middle
|
2022-11-15 06:20:54 +01:00
|
|
|
$result = $this->render('<% loop Set %><% if $Middle %>$Number<% end_if %><% end_loop %>', $data);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals("23456789", $result, "Middle numbers rendered in order");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test MiddleString
|
|
|
|
$result = $this->render(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<% loop Set %><% if MiddleString == "middle" %>$Number$MiddleString<% end_if %>'
|
|
|
|
. '<% end_loop %>',
|
|
|
|
$data
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
"2middle3middle4middle5middle6middle7middle8middle9middle",
|
|
|
|
$result,
|
|
|
|
"Middle numbers rendered in order"
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test EvenOdd
|
|
|
|
$result = $this->render('<% loop Set %>$EvenOdd<% end_loop %>', $data);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
"oddevenoddevenoddevenoddevenoddeven",
|
|
|
|
$result,
|
|
|
|
"Even and Odd is returned in sequence numbers rendered in order"
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Pos
|
|
|
|
$result = $this->render('<% loop Set %>$Pos<% end_loop %>', $data);
|
|
|
|
$this->assertEquals("12345678910", $result, '$Pos is rendered in order');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Pos
|
|
|
|
$result = $this->render('<% loop Set %>$Pos(0)<% end_loop %>', $data);
|
|
|
|
$this->assertEquals("0123456789", $result, '$Pos(0) is rendered in order');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test FromEnd
|
|
|
|
$result = $this->render('<% loop Set %>$FromEnd<% end_loop %>', $data);
|
|
|
|
$this->assertEquals("10987654321", $result, '$FromEnd is rendered in order');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test FromEnd
|
|
|
|
$result = $this->render('<% loop Set %>$FromEnd(0)<% end_loop %>', $data);
|
|
|
|
$this->assertEquals("9876543210", $result, '$FromEnd(0) rendered in order');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test Total
|
|
|
|
$result = $this->render('<% loop Set %>$TotalItems<% end_loop %>', $data);
|
|
|
|
$this->assertEquals("10101010101010101010", $result, "10 total items X 10 are returned");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//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");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//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");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//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");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//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");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//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");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//test MultipleOf 9 zero-based
|
|
|
|
$result = $this->render('<% loop Set %><% if MultipleOf(9,0) %>$Number<% end_if %><% end_loop %>', $data);
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
"110",
|
|
|
|
$result,
|
|
|
|
"Only numbers that are multiples of 9 with zero-based indexing are returned. (The first and last item)"
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
//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");
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test $Up works when the scope $Up refers to was entered with a "with" block
|
|
|
|
*/
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testUpInWith()
|
|
|
|
{
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Data to run the loop tests on - three levels deep
|
|
|
|
$data = new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Top',
|
|
|
|
'Foo' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Foo',
|
|
|
|
'Bar' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Bar',
|
|
|
|
'Baz' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Baz'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
'Qux' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Qux'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Basic functionality
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'BarFoo',
|
|
|
|
$this->render('<% with Foo %><% with Bar %>{$Name}{$Up.Name}<% end_with %><% end_with %>', $data)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Two level with block, up refers to internally referenced Bar
|
|
|
|
$this->assertEquals(
|
2018-06-07 16:01:17 +02:00
|
|
|
'BarTop',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render('<% with Foo.Bar %>{$Name}{$Up.Name}<% end_with %>', $data)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Stepping up & back down the scope tree
|
|
|
|
$this->assertEquals(
|
2018-06-07 16:01:17 +02:00
|
|
|
'BazFooBar',
|
|
|
|
$this->render('<% with Foo.Bar.Baz %>{$Name}{$Up.Foo.Name}{$Up.Foo.Bar.Name}<% end_with %>', $data)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Using $Up in a with block
|
|
|
|
$this->assertEquals(
|
2018-06-07 16:01:17 +02:00
|
|
|
'BazTopBar',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
2018-06-07 16:01:17 +02:00
|
|
|
'<% with Foo.Bar.Baz %>{$Name}<% with $Up %>{$Name}{$Foo.Bar.Name}<% end_with %>'
|
2018-01-16 19:39:30 +01:00
|
|
|
. '<% end_with %>',
|
2017-05-12 05:17:23 +02:00
|
|
|
$data
|
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Stepping up & back down the scope tree with with blocks
|
|
|
|
$this->assertEquals(
|
2018-06-07 16:01:17 +02:00
|
|
|
'BazTopBarTopBaz',
|
2017-05-12 05:17:23 +02:00
|
|
|
$this->render(
|
2018-06-07 16:01:17 +02:00
|
|
|
'<% with Foo.Bar.Baz %>{$Name}<% with $Up %>{$Name}<% with Foo.Bar %>{$Name}<% end_with %>'
|
2017-05-12 05:17:23 +02:00
|
|
|
. '{$Name}<% end_with %>{$Name}<% end_with %>',
|
|
|
|
$data
|
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Using $Up.Up, where first $Up points to a previous scope entered using $Up, thereby skipping up to Foo
|
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'Foo',
|
|
|
|
$this->render(
|
2018-06-07 16:01:17 +02:00
|
|
|
'<% with Foo %><% with Bar %><% with Baz %>{$Up.Up.Name}<% end_with %><% end_with %>'
|
2017-05-12 05:17:23 +02:00
|
|
|
. '<% end_with %>',
|
|
|
|
$data
|
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-06-07 16:01:17 +02:00
|
|
|
// Using $Up as part of a lookup chain in <% with %>
|
|
|
|
$this->assertEquals(
|
|
|
|
'Top',
|
|
|
|
$this->render('<% with Foo.Bar.Baz.Up.Qux %>{$Up.Name}<% end_with %>', $data)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooManyUps()
|
|
|
|
{
|
2022-09-09 03:38:55 +02:00
|
|
|
$this->expectException(LogicException::class);
|
|
|
|
$this->expectExceptionMessage("Up called when we're already at the top of the scope");
|
2018-06-07 16:01:17 +02:00
|
|
|
$data = new ArrayData([
|
|
|
|
'Foo' => new ArrayData([
|
|
|
|
'Name' => 'Foo',
|
|
|
|
'Bar' => new ArrayData([
|
|
|
|
'Name' => 'Bar'
|
|
|
|
])
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
'Foo',
|
2018-06-07 16:01:17 +02:00
|
|
|
$this->render('<% with Foo.Bar %>{$Up.Up.Name}<% end_with %>', $data)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test $Up works when the scope $Up refers to was entered with a "loop" block
|
|
|
|
*/
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testUpInLoop()
|
|
|
|
{
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Data to run the loop tests on - one sequence of three items, each with a subitem
|
|
|
|
$data = new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Top',
|
|
|
|
'Foo' => new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => '1',
|
|
|
|
'Sub' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Bar'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => '2',
|
|
|
|
'Sub' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Baz'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => '3',
|
|
|
|
'Sub' => new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'Qux'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Make sure inside a loop, $Up refers to the current item of the loop
|
|
|
|
$this->assertEqualIgnoringWhitespace(
|
2017-05-12 05:17:23 +02:00
|
|
|
'111 222 333',
|
|
|
|
$this->render(
|
|
|
|
'<% loop $Foo %>$Name<% with $Sub %>$Up.Name<% end_with %>$Name<% end_loop %>',
|
|
|
|
$data
|
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Make sure inside a loop, looping over $Up uses a separate iterator,
|
|
|
|
// and doesn't interfere with the original iterator
|
|
|
|
$this->assertEqualIgnoringWhitespace(
|
2017-05-12 05:17:23 +02:00
|
|
|
'1Bar123Bar1 2Baz123Baz2 3Qux123Qux3',
|
|
|
|
$this->render(
|
|
|
|
'<% loop $Foo %>
|
2011-02-18 05:06:11 +01:00
|
|
|
$Name
|
|
|
|
<% with $Sub %>
|
2011-02-21 03:24:14 +01:00
|
|
|
$Name
|
|
|
|
<% loop $Up %>$Name<% end_loop %>
|
2011-02-18 05:06:11 +01:00
|
|
|
$Name
|
|
|
|
<% end_with %>
|
2014-08-15 08:53:05 +02:00
|
|
|
$Name
|
|
|
|
<% end_loop %>',
|
2017-05-12 05:17:23 +02:00
|
|
|
$data
|
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// 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(
|
2017-05-12 05:17:23 +02:00
|
|
|
'1 Bar1 123 1Bar 1 2 Baz2 123 2Baz 2 3 Qux3 123 3Qux 3',
|
|
|
|
$this->render(
|
|
|
|
'<% loop $Foo %>
|
2011-02-18 05:06:11 +01:00
|
|
|
$Name
|
|
|
|
<% with $Sub %>
|
|
|
|
{$Name}{$Up.Name}
|
|
|
|
<% loop $Up %>$Name<% end_loop %>
|
|
|
|
{$Up.Name}{$Name}
|
|
|
|
<% end_with %>
|
|
|
|
$Name
|
|
|
|
<% end_loop %>',
|
2017-05-12 05:17:23 +02:00
|
|
|
$data
|
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that nested loops restore the loop variables correctly when pushing and popping states
|
|
|
|
*/
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testNestedLoops()
|
|
|
|
{
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// 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(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Foo' => new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => '1',
|
|
|
|
'Children' => new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'a'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => 'b'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
),
|
2017-05-12 05:17:23 +02:00
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => '2',
|
|
|
|
'Children' => new ArrayList(),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Name' => '3',
|
|
|
|
'Children' => new ArrayList(),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Make sure that including a loop inside a loop will not destroy the internal count of
|
|
|
|
// items, checked by using "Last"
|
|
|
|
$this->assertEqualIgnoringWhitespace(
|
2017-05-12 05:17:23 +02:00
|
|
|
'1ab23last',
|
|
|
|
$this->render(
|
2022-11-15 06:20:54 +01:00
|
|
|
'<% loop $Foo %>$Name<% loop Children %>$Name<% end_loop %><% if $IsLast %>last<% end_if %>'
|
2017-05-12 05:17:23 +02:00
|
|
|
. '<% end_loop %>',
|
|
|
|
$data
|
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testLayout()
|
|
|
|
{
|
|
|
|
$this->useTestTheme(
|
2018-01-16 19:39:30 +01:00
|
|
|
__DIR__ . '/SSViewerTest',
|
2017-05-12 05:17:23 +02:00
|
|
|
'layouttest',
|
|
|
|
function () {
|
2020-04-20 19:58:09 +02:00
|
|
|
$template = new SSViewer(['Page']);
|
|
|
|
$this->assertEquals("Foo\n\n", $template->process(new ArrayData([])));
|
2017-05-12 05:17:23 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$template = new SSViewer(['Shortcodes', 'Page']);
|
|
|
|
$this->assertEquals("[file_link]\n\n", $template->process(new ArrayData([])));
|
2017-05-12 05:17:23 +02:00
|
|
|
}
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\View\SSViewer::get_templates_by_class()
|
|
|
|
*/
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testGetTemplatesByClass()
|
|
|
|
{
|
|
|
|
$this->useTestTheme(
|
2017-05-12 05:17:23 +02:00
|
|
|
__DIR__ . '/SSViewerTest',
|
|
|
|
'layouttest',
|
|
|
|
function () {
|
2017-05-10 06:24:30 +02:00
|
|
|
// Test passing a string
|
2017-05-12 05:17:23 +02:00
|
|
|
$templates = SSViewer::get_templates_by_class(
|
|
|
|
SSViewerTestModelController::class,
|
|
|
|
'',
|
|
|
|
Controller::class
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
2016-12-30 00:27:20 +01:00
|
|
|
SSViewerTestModelController::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'type' => 'Includes',
|
2016-12-30 00:27:20 +01:00
|
|
|
SSViewerTestModelController::class,
|
2017-05-10 06:24:30 +02:00
|
|
|
],
|
|
|
|
SSViewerTestModel::class,
|
|
|
|
Controller::class,
|
|
|
|
[
|
|
|
|
'type' => 'Includes',
|
2016-12-30 00:27:20 +01:00
|
|
|
Controller::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
],
|
2017-05-12 05:17:23 +02:00
|
|
|
],
|
|
|
|
$templates
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-10 06:24:30 +02:00
|
|
|
// Test to ensure we're stopping at the base class.
|
2017-05-12 05:17:23 +02:00
|
|
|
$templates = SSViewer::get_templates_by_class(
|
|
|
|
SSViewerTestModelController::class,
|
|
|
|
'',
|
|
|
|
SSViewerTestModelController::class
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
2016-12-30 00:27:20 +01:00
|
|
|
SSViewerTestModelController::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'type' => 'Includes',
|
2016-12-30 00:27:20 +01:00
|
|
|
SSViewerTestModelController::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
],
|
|
|
|
SSViewerTestModel::class,
|
2017-05-12 05:17:23 +02:00
|
|
|
],
|
|
|
|
$templates
|
|
|
|
);
|
2017-05-10 06:24:30 +02:00
|
|
|
|
|
|
|
// Make sure we can search templates by suffix.
|
2017-05-12 05:17:23 +02:00
|
|
|
$templates = SSViewer::get_templates_by_class(
|
|
|
|
SSViewerTestModel::class,
|
|
|
|
'Controller',
|
|
|
|
DataObject::class
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
SSViewerTestModelController::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'type' => 'Includes',
|
2016-12-30 00:27:20 +01:00
|
|
|
SSViewerTestModelController::class,
|
2017-05-10 06:24:30 +02:00
|
|
|
],
|
|
|
|
DataObject::class . 'Controller',
|
|
|
|
[
|
|
|
|
'type' => 'Includes',
|
2016-12-30 00:27:20 +01:00
|
|
|
DataObject::class . 'Controller',
|
2016-12-16 05:34:21 +01:00
|
|
|
],
|
2017-05-12 05:17:23 +02:00
|
|
|
],
|
|
|
|
$templates
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-12 05:17:23 +02:00
|
|
|
// Let's throw something random in there.
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2017-06-22 12:50:45 +02:00
|
|
|
SSViewer::get_templates_by_class(null);
|
2017-05-12 05:17:23 +02:00
|
|
|
}
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testRewriteHashlinks()
|
|
|
|
{
|
2017-09-21 03:42:28 +02:00
|
|
|
SSViewer::setRewriteHashLinksDefault(true);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$_SERVER['HTTP_HOST'] = 'www.mysite.com';
|
|
|
|
$_SERVER['REQUEST_URI'] = '//file.com?foo"onclick="alert(\'xss\')""';
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// 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\')""');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-10-09 01:41:34 +02:00
|
|
|
$tmplFile = TEMP_PATH . DIRECTORY_SEPARATOR . 'SSViewerTest_testRewriteHashlinks_' . sha1(rand()) . '.ss';
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Note: SSViewer_FromString doesn't rewrite hash links.
|
|
|
|
file_put_contents(
|
2022-04-14 03:12:59 +02:00
|
|
|
$tmplFile ?? '',
|
2017-05-12 05:17:23 +02:00
|
|
|
'<!DOCTYPE html>
|
2011-10-18 11:42:55 +02:00
|
|
|
<html>
|
|
|
|
<head><% base_tag %></head>
|
|
|
|
<body>
|
2015-03-20 06:17:51 +01:00
|
|
|
<a class="external-inline" href="http://google.com#anchor">ExternalInlineLink</a>
|
|
|
|
$ExternalInsertedLink
|
2011-10-18 11:42:55 +02:00
|
|
|
<a class="inline" href="#anchor">InlineLink</a>
|
|
|
|
$InsertedLink
|
2014-09-26 07:41:27 +02:00
|
|
|
<svg><use xlink:href="#sprite"></use></svg>
|
2011-10-18 11:42:55 +02:00
|
|
|
<body>
|
2016-12-16 05:34:21 +01:00
|
|
|
</html>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$tmpl = new SSViewer($tmplFile);
|
|
|
|
$obj = new ViewableData();
|
|
|
|
$obj->InsertedLink = DBField::create_field(
|
2017-05-12 05:17:23 +02:00
|
|
|
'HTMLFragment',
|
|
|
|
'<a class="inserted" href="#anchor">InsertedLink</a>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$obj->ExternalInsertedLink = DBField::create_field(
|
2017-05-12 05:17:23 +02:00
|
|
|
'HTMLFragment',
|
|
|
|
'<a class="external-inserted" href="http://google.com#anchor">ExternalInsertedLink</a>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$result = $tmpl->process($obj);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<a class="inserted" href="' . $base . '#anchor">InsertedLink</a>',
|
|
|
|
$result
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<a class="external-inserted" href="http://google.com#anchor">ExternalInsertedLink</a>',
|
|
|
|
$result
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<a class="inline" href="' . $base . '#anchor">InlineLink</a>',
|
|
|
|
$result
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<a class="external-inline" href="http://google.com#anchor">ExternalInlineLink</a>',
|
|
|
|
$result
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<svg><use xlink:href="#sprite"></use></svg>',
|
|
|
|
$result,
|
|
|
|
'SSTemplateParser should only rewrite anchor hrefs'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
unlink($tmplFile ?? '');
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testRewriteHashlinksInPhpMode()
|
|
|
|
{
|
2017-09-21 03:42:28 +02:00
|
|
|
SSViewer::setRewriteHashLinksDefault('php');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-10-09 01:41:34 +02:00
|
|
|
$tmplFile = TEMP_PATH . DIRECTORY_SEPARATOR . 'SSViewerTest_testRewriteHashlinksInPhpMode_' . sha1(rand()) . '.ss';
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Note: SSViewer_FromString doesn't rewrite hash links.
|
|
|
|
file_put_contents(
|
2022-04-14 03:12:59 +02:00
|
|
|
$tmplFile ?? '',
|
2017-05-12 05:17:23 +02:00
|
|
|
'<!DOCTYPE html>
|
2011-10-18 11:42:55 +02:00
|
|
|
<html>
|
|
|
|
<head><% base_tag %></head>
|
|
|
|
<body>
|
|
|
|
<a class="inline" href="#anchor">InlineLink</a>
|
|
|
|
$InsertedLink
|
2014-09-26 07:41:27 +02:00
|
|
|
<svg><use xlink:href="#sprite"></use></svg>
|
2011-10-18 11:42:55 +02:00
|
|
|
<body>
|
2016-12-16 05:34:21 +01:00
|
|
|
</html>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$tmpl = new SSViewer($tmplFile);
|
|
|
|
$obj = new ViewableData();
|
|
|
|
$obj->InsertedLink = DBField::create_field(
|
2017-05-12 05:17:23 +02:00
|
|
|
'HTMLFragment',
|
|
|
|
'<a class="inserted" href="#anchor">InsertedLink</a>'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
$result = $tmpl->process($obj);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$code = <<<'EOC'
|
2016-08-19 00:51:35 +02:00
|
|
|
<a class="inserted" href="<?php echo \SilverStripe\Core\Convert::raw2att(preg_replace("/^(\/)+/", "/", $_SERVER['REQUEST_URI'])); ?>#anchor">InsertedLink</a>
|
2015-10-28 23:53:44 +01:00
|
|
|
EOC;
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString($code, $result);
|
|
|
|
$this->assertStringContainsString(
|
2017-05-12 05:17:23 +02:00
|
|
|
'<svg><use xlink:href="#sprite"></use></svg>',
|
|
|
|
$result,
|
|
|
|
'SSTemplateParser should only rewrite anchor hrefs'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
unlink($tmplFile ?? '');
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testRenderWithSourceFileComments()
|
|
|
|
{
|
2022-11-15 06:20:54 +01:00
|
|
|
SSViewer::config()->set('source_file_comments', true);
|
2017-05-11 00:17:54 +02:00
|
|
|
$i = __DIR__ . '/SSViewerTest/templates/Includes';
|
|
|
|
$f = __DIR__ . '/SSViewerTest/templates/SSViewerTestComments';
|
2020-04-20 19:58:09 +02:00
|
|
|
$templates = [
|
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'name' => 'SSViewerTestCommentsFullSource',
|
|
|
|
'expected' => ""
|
|
|
|
. "<!doctype html>"
|
|
|
|
. "<!-- template $f/SSViewerTestCommentsFullSource.ss -->"
|
|
|
|
. "<html>"
|
|
|
|
. "\t<head></head>"
|
|
|
|
. "\t<body></body>"
|
|
|
|
. "</html>"
|
|
|
|
. "<!-- end template $f/SSViewerTestCommentsFullSource.ss -->",
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'name' => 'SSViewerTestCommentsFullSourceHTML4Doctype',
|
|
|
|
'expected' => ""
|
|
|
|
. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML "
|
|
|
|
. "4.01//EN\"\t\t\"http://www.w3.org/TR/html4/strict.dtd\">"
|
|
|
|
. "<!-- template $f/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->"
|
|
|
|
. "<html>"
|
|
|
|
. "\t<head></head>"
|
|
|
|
. "\t<body></body>"
|
|
|
|
. "</html>"
|
|
|
|
. "<!-- end template $f/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->",
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'name' => 'SSViewerTestCommentsFullSourceNoDoctype',
|
|
|
|
'expected' => ""
|
|
|
|
. "<html><!-- template $f/SSViewerTestCommentsFullSourceNoDoctype.ss -->"
|
|
|
|
. "\t<head></head>"
|
|
|
|
. "\t<body></body>"
|
|
|
|
. "<!-- end template $f/SSViewerTestCommentsFullSourceNoDoctype.ss --></html>",
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'name' => 'SSViewerTestCommentsFullSourceIfIE',
|
|
|
|
'expected' => ""
|
|
|
|
. "<!doctype html>"
|
|
|
|
. "<!-- template $f/SSViewerTestCommentsFullSourceIfIE.ss -->"
|
|
|
|
. "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->"
|
|
|
|
. "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->"
|
|
|
|
. "<!--[if !IE]><!--> <html class='no-ie'> <!--<![endif]-->"
|
|
|
|
. "\t<head></head>"
|
|
|
|
. "\t<body></body>"
|
|
|
|
. "</html>"
|
|
|
|
. "<!-- end template $f/SSViewerTestCommentsFullSourceIfIE.ss -->",
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'name' => 'SSViewerTestCommentsFullSourceIfIENoDoctype',
|
|
|
|
'expected' => ""
|
|
|
|
. "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->"
|
|
|
|
. "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->"
|
|
|
|
. "<!--[if !IE]><!--> <html class='no-ie'>"
|
|
|
|
. "<!-- template $f/SSViewerTestCommentsFullSourceIfIENoDoctype.ss -->"
|
|
|
|
. " <!--<![endif]-->"
|
|
|
|
. "\t<head></head>"
|
|
|
|
. "\t<body></body>"
|
|
|
|
. "<!-- end template $f/SSViewerTestCommentsFullSourceIfIENoDoctype.ss --></html>",
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'name' => 'SSViewerTestCommentsPartialSource',
|
|
|
|
'expected' =>
|
|
|
|
"<!-- template $f/SSViewerTestCommentsPartialSource.ss -->"
|
|
|
|
. "<div class='typography'></div>"
|
|
|
|
. "<!-- end template $f/SSViewerTestCommentsPartialSource.ss -->",
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
'name' => 'SSViewerTestCommentsWithInclude',
|
|
|
|
'expected' =>
|
|
|
|
"<!-- template $f/SSViewerTestCommentsWithInclude.ss -->"
|
|
|
|
. "<div class='typography'>"
|
|
|
|
. "<!-- include 'SSViewerTestCommentsInclude' -->"
|
|
|
|
. "<!-- template $i/SSViewerTestCommentsInclude.ss -->"
|
|
|
|
. "Included"
|
|
|
|
. "<!-- end template $i/SSViewerTestCommentsInclude.ss -->"
|
|
|
|
. "<!-- end include 'SSViewerTestCommentsInclude' -->"
|
|
|
|
. "</div>"
|
|
|
|
. "<!-- end template $f/SSViewerTestCommentsWithInclude.ss -->",
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
2017-05-11 00:17:54 +02:00
|
|
|
foreach ($templates as $template) {
|
2018-01-16 19:39:30 +01:00
|
|
|
$this->_renderWithSourceFileComments('SSViewerTestComments/' . $template['name'], $template['expected']);
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private function _renderWithSourceFileComments($name, $expected)
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$viewer = new SSViewer([$name]);
|
|
|
|
$data = new ArrayData([]);
|
2017-05-11 00:17:54 +02:00
|
|
|
$result = $viewer->process($data);
|
2022-04-14 03:12:59 +02:00
|
|
|
$expected = str_replace(["\r", "\n"], '', $expected ?? '');
|
|
|
|
$result = str_replace(["\r", "\n"], '', $result ?? '');
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals($result, $expected);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testLoopIteratorIterator()
|
|
|
|
{
|
|
|
|
$list = new PaginatedList(new ArrayList());
|
|
|
|
$viewer = new SSViewer_FromString('<% loop List %>$ID - $FirstName<br /><% end_loop %>');
|
2020-04-20 19:58:09 +02:00
|
|
|
$result = $viewer->process(new ArrayData(['List' => $list]));
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals($result, '');
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testProcessOnlyIncludesRequirementsOnce()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$template = new SSViewer(['SSViewerTestProcess']);
|
2017-05-11 00:17:54 +02:00
|
|
|
$basePath = $this->getCurrentRelativePath() . '/SSViewerTest';
|
2017-05-10 06:24:30 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$backend = Injector::inst()->create(Requirements_Backend::class);
|
|
|
|
$backend->setCombinedFilesEnabled(false);
|
|
|
|
$backend->combineFiles(
|
2017-05-12 05:17:23 +02:00
|
|
|
'RequirementsTest_ab.css',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-10 06:24:30 +02:00
|
|
|
$basePath . '/css/RequirementsTest_a.css',
|
|
|
|
$basePath . '/css/RequirementsTest_b.css'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
Requirements::set_backend($backend);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(1, substr_count($template->process(new ViewableData()) ?? '', "a.css"));
|
|
|
|
$this->assertEquals(1, substr_count($template->process(new ViewableData()) ?? '', "b.css"));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// if we disable the requirements then we should get nothing
|
|
|
|
$template->includeRequirements(false);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(0, substr_count($template->process(new ViewableData()) ?? '', "a.css"));
|
|
|
|
$this->assertEquals(0, substr_count($template->process(new ViewableData()) ?? '', "b.css"));
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testRequireCallInTemplateInclude()
|
|
|
|
{
|
|
|
|
//TODO undo skip test on the event that templates ever obtain the ability to reference MODULE_DIR (or something to that effect)
|
|
|
|
if (FRAMEWORK_DIR === 'framework') {
|
2020-04-20 19:58:09 +02:00
|
|
|
$template = new SSViewer(['SSViewerTestProcess']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
Requirements::set_suffix_requirements(false);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
1,
|
|
|
|
substr_count(
|
2022-04-14 03:12:59 +02:00
|
|
|
$template->process(new ViewableData()) ?? '',
|
2017-05-12 05:17:23 +02:00
|
|
|
"tests/php/View/SSViewerTest/javascript/RequirementsTest_a.js"
|
|
|
|
)
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->markTestSkipped(
|
2018-01-16 19:39:30 +01:00
|
|
|
'Requirement will always fail if the framework dir is not ' .
|
2017-05-12 05:17:23 +02:00
|
|
|
'named \'framework\', since templates require hard coded paths'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testCallsWithArguments()
|
|
|
|
{
|
|
|
|
$data = new ArrayData(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
'Set' => new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-05-12 05:17:23 +02:00
|
|
|
new SSViewerTest\TestObject("1"),
|
|
|
|
new SSViewerTest\TestObject("2"),
|
|
|
|
new SSViewerTest\TestObject("3"),
|
|
|
|
new SSViewerTest\TestObject("4"),
|
|
|
|
new SSViewerTest\TestObject("5"),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-05-12 05:17:23 +02:00
|
|
|
),
|
|
|
|
'Level' => new SSViewerTest\LevelTestData(1),
|
2020-04-20 19:58:09 +02:00
|
|
|
'Nest' => [
|
2017-05-10 06:24:30 +02:00
|
|
|
'Level' => new SSViewerTest\LevelTestData(2),
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
]
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$tests = [
|
2017-05-10 06:24:30 +02:00
|
|
|
'$Level.output(1)' => '1-1',
|
|
|
|
'$Nest.Level.output($Set.First.Number)' => '2-1',
|
|
|
|
'<% with $Set %>$Up.Level.output($First.Number)<% end_with %>' => '1-1',
|
|
|
|
'<% with $Set %>$Top.Nest.Level.output($First.Number)<% end_with %>' => '2-1',
|
|
|
|
'<% loop $Set %>$Up.Nest.Level.output($Number)<% end_loop %>' => '2-12-22-32-42-5',
|
|
|
|
'<% loop $Set %>$Top.Level.output($Number)<% end_loop %>' => '1-11-21-31-41-5',
|
|
|
|
'<% with $Nest %>$Level.output($Top.Set.First.Number)<% end_with %>' => '2-1',
|
|
|
|
'<% with $Level %>$output($Up.Set.Last.Number)<% end_with %>' => '1-5',
|
|
|
|
'<% with $Level.forWith($Set.Last.Number) %>$output("hi")<% end_with %>' => '5-hi',
|
|
|
|
'<% loop $Level.forLoop($Set.First.Number) %>$Number<% end_loop %>' => '!0',
|
|
|
|
'<% with $Nest %>
|
2013-06-25 07:35:16 +02:00
|
|
|
<% with $Level.forWith($Up.Set.First.Number) %>$output("hi")<% end_with %>
|
|
|
|
<% end_with %>' => '1-hi',
|
2017-05-10 06:24:30 +02:00
|
|
|
'<% with $Nest %>
|
2013-06-25 07:35:16 +02:00
|
|
|
<% loop $Level.forLoop($Top.Set.Last.Number) %>$Number<% end_loop %>
|
|
|
|
<% end_with %>' => '!0!1!2!3!4',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2013-06-25 07:35:16 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
foreach ($tests as $template => $expected) {
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals($expected, trim($this->render($template, $data) ?? ''));
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2013-10-24 01:43:20 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testRepeatedCallsAreCached()
|
|
|
|
{
|
|
|
|
$data = new SSViewerTest\CacheTestData();
|
|
|
|
$template = '
|
2016-10-03 17:01:02 +02:00
|
|
|
<% if $TestWithCall %>
|
|
|
|
<% with $TestWithCall %>
|
|
|
|
{$Message}
|
|
|
|
<% end_with %>
|
|
|
|
|
|
|
|
{$TestWithCall.Message}
|
|
|
|
<% end_if %>';
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals('HiHi', preg_replace('/\s+/', '', $this->render($template, $data) ?? ''));
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
1,
|
|
|
|
$data->testWithCalls,
|
|
|
|
'SSViewerTest_CacheTestData::TestWithCall() should only be called once. Subsequent calls should be cached'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-10-03 17:01:02 +02:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$data = new SSViewerTest\CacheTestData();
|
|
|
|
$template = '
|
2016-10-03 17:01:02 +02:00
|
|
|
<% if $TestLoopCall %>
|
|
|
|
<% loop $TestLoopCall %>
|
|
|
|
{$Message}
|
|
|
|
<% end_loop %>
|
|
|
|
<% end_if %>';
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals('OneTwo', preg_replace('/\s+/', '', $this->render($template, $data) ?? ''));
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(
|
2017-05-12 05:17:23 +02:00
|
|
|
1,
|
|
|
|
$data->testLoopCalls,
|
|
|
|
'SSViewerTest_CacheTestData::TestLoopCall() should only be called once. Subsequent calls should be cached'
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testClosedBlockExtension()
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
$parser = new SSTemplateParser();
|
|
|
|
$parser->addClosedBlock(
|
2017-05-12 05:17:23 +02:00
|
|
|
'test',
|
|
|
|
function ($res) use (&$count) {
|
|
|
|
$count++;
|
|
|
|
}
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$template = new SSViewer_FromString("<% test %><% end_test %>", $parser);
|
|
|
|
$template->process(new SSViewerTest\TestFixture());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(1, $count);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testOpenBlockExtension()
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
$parser = new SSTemplateParser();
|
|
|
|
$parser->addOpenBlock(
|
2017-05-12 05:17:23 +02:00
|
|
|
'test',
|
|
|
|
function ($res) use (&$count) {
|
|
|
|
$count++;
|
|
|
|
}
|
2017-05-11 00:17:54 +02:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$template = new SSViewer_FromString("<% test %>", $parser);
|
|
|
|
$template->process(new SSViewerTest\TestFixture());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->assertEquals(1, $count);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if caching for SSViewer_FromString is working
|
|
|
|
*/
|
2017-05-11 00:17:54 +02:00
|
|
|
public function testFromStringCaching()
|
|
|
|
{
|
|
|
|
$content = 'Test content';
|
2022-04-14 03:12:59 +02:00
|
|
|
$cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . '.cache.' . sha1($content ?? '');
|
|
|
|
if (file_exists($cacheFile ?? '')) {
|
|
|
|
unlink($cacheFile ?? '');
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Test global behaviors
|
|
|
|
$this->render($content, null, null);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertFalse(file_exists($cacheFile ?? ''), 'Cache file was created when caching was off');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
SSViewer_FromString::config()->set('cache_template', true);
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->render($content, null, null);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertTrue(file_exists($cacheFile ?? ''), 'Cache file wasn\'t created when it was meant to');
|
|
|
|
unlink($cacheFile ?? '');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
// Test instance behaviors
|
|
|
|
$this->render($content, null, false);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertFalse(file_exists($cacheFile ?? ''), 'Cache file was created when caching was off');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-11 00:17:54 +02:00
|
|
|
$this->render($content, null, true);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertTrue(file_exists($cacheFile ?? ''), 'Cache file wasn\'t created when it was meant to');
|
|
|
|
unlink($cacheFile ?? '');
|
2017-05-11 00:17:54 +02:00
|
|
|
}
|
2009-01-04 23:20:36 +01:00
|
|
|
}
|