Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Classes/FusionObjects/DataTemplateImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,29 @@ public function getDateTimeFormat(): string
return $this->fusionValue('dateTimeFormat') ?? \DateTimeInterface::W3C;
}

/**
* @return string "plaintext" or "html" are supported modes
*/
public function getMode(): string
{
return $this->fusionValue('mode') ?? 'html';
}

public function evaluate()
{
$template = $this->getTemplate();
$data = $this->getData();
$mode = $this->getMode();

return preg_replace_callback(
'/{([a-z0-9\\-\\.]+)}/ium',
function (array $matches) use ($data) {
function (array $matches) use ($data, $mode) {
$value = Arrays::getValueByPath($data, $matches[1]);
return htmlspecialchars(strip_tags($this->stringify($value)));
return match ($mode) {
'plaintext' => strip_tags($this->stringify($value)),
'html' => htmlspecialchars(strip_tags($this->stringify($value))),
default => htmlspecialchars(strip_tags($this->stringify($value)))
};
},
$template
);
Expand Down
8 changes: 6 additions & 2 deletions NodeTypes/Action/Email/Email.Definition.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ prototype(Sitegeist.PaperTiger:Action.Email.Definition) < prototype(Neos.Fusion:
[email protected] = Sitegeist.PaperTiger:Action.DataTemplate
format = ${q(node).property('format')}
plaintext = ${q(node).property('plaintext')}
[email protected] = Sitegeist.PaperTiger:Action.DataTemplate
[email protected] = Sitegeist.PaperTiger:Action.DataTemplate {
mode = 'plaintext'
}
[email protected] = ${this.format != 'html'}
html = ${q(node).property('html')}
[email protected] = ${this.format != 'plaintext'}
[email protected] = Sitegeist.PaperTiger:Action.DataTemplate
[email protected] = Sitegeist.PaperTiger:Action.DataTemplate {
mode = 'html'
}
recipientAddress = ${q(node).property('recipientAddress')}
recipientName = ${q(node).property('recipientName')}
senderAddress = ${q(node).property('senderAddress')}
Expand Down
1 change: 1 addition & 0 deletions Resources/Private/Fusion/Prototypes/DataTemplate.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ prototype(Sitegeist.PaperTiger:Action.DataTemplate) < prototype(Neos.Fusion:Valu
@class = '\\Sitegeist\\PaperTiger\\FusionObjects\\DataTemplateImplementation'
template = ${value}
data = ${data}
mode = 'html'

dateTimeFormat = 'Y-m-d H:i:s'
}
33 changes: 32 additions & 1 deletion Tests/Unit/DataTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class DataTemplateTest extends TestCase

public function setUp(): void
{
$this->dataTemplate = $this->createPartialMock(DataTemplateImplementation::class, ['getTemplate', 'getData', 'getDateTimeFormat']);
$this->dataTemplate = $this->createPartialMock(DataTemplateImplementation::class, ['getTemplate', 'getData', 'getMode', 'getDateTimeFormat']);
$this->dataTemplate->expects($this->any())->method('getDateTimeFormat')->willReturn(\DateTimeImmutable::W3C);
$this->dataTemplate->expects($this->any())->method('getMode')->willReturn('html');

}

public function stringifyConvertsDataProvider(): \Generator
Expand Down Expand Up @@ -76,4 +78,33 @@ public function evaluateAccessesNestedData(mixed $data, string $template, string
$this->dataTemplate->expects($this->once())->method('getData')->willReturn($data);
$this->assertSame($expectedString, $this->dataTemplate->evaluate());
}

public function modeControlsEntityEscapingDataProvider(): \Generator
{
yield 'string with ampersand' => ['foo & bar', 'foo &amp; bar', 'foo & bar'];
yield 'string with script' => ['foo <script>evil</script> bar', 'foo evil bar', 'foo evil bar'];
}

/**
* @test
* @dataProvider modeControlsEntityEscapingDataProvider
*/
public function modeControlsEntityEscaping(mixed $data, string $expectedHtmlString, string $expectedPlaintextString): void
{

$htmlDataTemplate = $this->createPartialMock(DataTemplateImplementation::class, ['getTemplate', 'getData', 'getDateTimeFormat', 'getMode']);
$htmlDataTemplate->expects($this->any())->method('getDateTimeFormat')->willReturn(\DateTimeImmutable::W3C);
$htmlDataTemplate->expects($this->any())->method('getMode')->willReturn('html');
$htmlDataTemplate->expects($this->any())->method('getTemplate')->willReturn('{data}');
$htmlDataTemplate->expects($this->any())->method('getData')->willReturn(['data' => $data]);

$plainte = $this->createPartialMock(DataTemplateImplementation::class, ['getTemplate', 'getData', 'getDateTimeFormat', 'getMode']);
$plainte->expects($this->any())->method('getDateTimeFormat')->willReturn(\DateTimeImmutable::W3C);
$plainte->expects($this->any())->method('getMode')->willReturn('plaintext');
$plainte->expects($this->any())->method('getTemplate')->willReturn('{data}');
$plainte->expects($this->any())->method('getData')->willReturn(['data' => $data]);

$this->assertSame($expectedHtmlString, $htmlDataTemplate->evaluate());
$this->assertSame($expectedPlaintextString, $plainte->evaluate());
}
}