File size: 10,472 Bytes
cb29db0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
diff --git a/src/Oro/Bundle/EmailBundle/Provider/EmailRenderer.php b/src/Oro/Bundle/EmailBundle/Provider/EmailRenderer.php
index 7ed98a24484..f2c601262d5 100644
--- a/src/Oro/Bundle/EmailBundle/Provider/EmailRenderer.php
+++ b/src/Oro/Bundle/EmailBundle/Provider/EmailRenderer.php
@@ -7,6 +7,7 @@
 use Oro\Bundle\EntityBundle\Twig\Sandbox\TemplateRenderer;
 use Oro\Bundle\EntityBundle\Twig\Sandbox\TemplateRendererConfigProviderInterface;
 use Oro\Bundle\EntityBundle\Twig\Sandbox\VariableProcessorRegistry;
+use Oro\Bundle\UIBundle\Tools\HtmlTagHelper;
 use Symfony\Contracts\Translation\TranslatorInterface;
 use Twig\Environment;
 
@@ -20,6 +21,9 @@ class EmailRenderer extends TemplateRenderer
     /** @var TranslatorInterface */
     private $translator;
 
+    /** @var HtmlTagHelper|null */
+    private $htmlTagHelper;
+
     public function __construct(
         Environment $environment,
         TemplateRendererConfigProviderInterface $configProvider,
@@ -31,6 +35,11 @@ public function __construct(
         $this->translator = $translator;
     }
 
+    public function setHtmlTagHelper(HtmlTagHelper $htmlTagHelper): void
+    {
+        $this->htmlTagHelper = $htmlTagHelper;
+    }
+
     /**
      * Compiles the given email template.
      *
@@ -61,8 +70,14 @@ public function compilePreview(EmailTemplateInterface $template): string
     {
         $this->ensureSandboxConfigured();
 
+        if ($this->htmlTagHelper) {
+            $content = $this->htmlTagHelper->sanitize($template->getContent(), 'default', false);
+        } else {
+            $content = $template->getContent();
+        }
+
         return $this->environment
-            ->createTemplate('{% verbatim %}' . $template->getContent() . '{% endverbatim %}')
+            ->createTemplate('{% verbatim %}' . $content . '{% endverbatim %}')
             ->render();
     }
 
diff --git a/src/Oro/Bundle/EmailBundle/Resources/config/services.yml b/src/Oro/Bundle/EmailBundle/Resources/config/services.yml
index dd9e337ecdd..253161e2ad4 100644
--- a/src/Oro/Bundle/EmailBundle/Resources/config/services.yml
+++ b/src/Oro/Bundle/EmailBundle/Resources/config/services.yml
@@ -338,6 +338,7 @@ services:
             - '@Doctrine\Inflector\Inflector'
         calls:
             - [addSystemVariableDefaultFilter, ['userSignature', 'oro_html_sanitize']]
+            - [setHtmlTagHelper, ['@oro_ui.html_tag_helper']]
         lazy: true
 
     oro_email.email_renderer_configuration:
diff --git a/src/Oro/Bundle/EmailBundle/Tests/Unit/Provider/EmailRendererTest.php b/src/Oro/Bundle/EmailBundle/Tests/Unit/Provider/EmailRendererTest.php
index cf108b8969f..196a6aa5d1f 100644
--- a/src/Oro/Bundle/EmailBundle/Tests/Unit/Provider/EmailRendererTest.php
+++ b/src/Oro/Bundle/EmailBundle/Tests/Unit/Provider/EmailRendererTest.php
@@ -22,22 +22,9 @@
 
 class EmailRendererTest extends \PHPUnit\Framework\TestCase
 {
-    private const ENTITY_VARIABLE_TEMPLATE =
-        '{% if %val% is defined %}'
-        . '{{ _entity_var("%name%", %val%, %parent%) }}'
-        . '{% else %}'
-        . '{{ "oro.email.variable.not.found" }}'
-        . '{% endif %}';
-
     /** @var TemplateRendererConfigProviderInterface|\PHPUnit\Framework\MockObject\MockObject */
     private $configProvider;
 
-    /** @var VariableProcessorRegistry|\PHPUnit\Framework\MockObject\MockObject */
-    private $variablesProcessorRegistry;
-
-    /** @var TranslatorInterface|\PHPUnit\Framework\MockObject\MockObject */
-    private $translation;
-
     /** @var ContainerInterface|\PHPUnit\Framework\MockObject\MockObject */
     private $container;
 
@@ -46,16 +33,25 @@ class EmailRendererTest extends \PHPUnit\Framework\TestCase
 
     protected function setUp(): void
     {
-        $environment = new Environment(new ArrayLoader(), ['strict_variables' => true]);
         $this->configProvider = $this->createMock(TemplateRendererConfigProviderInterface::class);
-        $this->variablesProcessorRegistry = $this->createMock(VariableProcessorRegistry::class);
-        $this->translation = $this->createMock(TranslatorInterface::class);
         $this->container = $this->createMock(ContainerInterface::class);
 
-        $this->translation->expects($this->any())
+        $variablesProcessorRegistry = $this->createMock(VariableProcessorRegistry::class);
+
+        $htmlTagHelper = $this->createMock(HtmlTagHelper::class);
+        $htmlTagHelper->expects(self::any())
+            ->method('sanitize')
+            ->with(self::isType(\PHPUnit\Framework\Constraint\IsType::TYPE_STRING), 'default', false)
+            ->willReturnCallback(static function (string $content) {
+                return $content . '(sanitized)';
+            });
+
+        $translation = $this->createMock(TranslatorInterface::class);
+        $translation->expects(self::any())
             ->method('trans')
             ->willReturnArgument(0);
 
+        $environment = new Environment(new ArrayLoader(), ['strict_variables' => true]);
         $environment->addExtension(new SandboxExtension(new SecurityPolicy()));
         $environment->addExtension(new HttpKernelExtension());
         $environment->addExtension(new HtmlTagExtension($this->container));
@@ -63,10 +59,12 @@ protected function setUp(): void
         $this->renderer = new EmailRenderer(
             $environment,
             $this->configProvider,
-            $this->variablesProcessorRegistry,
-            $this->translation,
+            $variablesProcessorRegistry,
+            $translation,
             (new InflectorFactory())->build()
         );
+
+        $this->renderer->setHtmlTagHelper($htmlTagHelper);
     }
 
     private function getEmailTemplate(string $content, string $subject = ''): EmailTemplateInterface
@@ -78,37 +76,25 @@ private function getEmailTemplate(string $content, string $subject = ''): EmailT
         return $emailTemplate;
     }
 
-    private function getEntityVariableTemplate(string $propertyName, string $path, string $parentPath): string
-    {
-        return strtr(
-            self::ENTITY_VARIABLE_TEMPLATE,
-            [
-                '%name%' => $propertyName,
-                '%val%' => $path,
-                '%parent%' => $parentPath,
-            ]
-        );
-    }
-
     private function expectVariables(array $entityVariableProcessors = [], array $systemVariableValues = []): void
     {
         $entityVariableProcessorsMap = [];
         foreach ($entityVariableProcessors as $entityClass => $processors) {
             $entityVariableProcessorsMap[] = [$entityClass, $processors];
         }
-        $this->configProvider->expects($this->any())
+        $this->configProvider->expects(self::any())
             ->method('getEntityVariableProcessors')
             ->willReturnMap($entityVariableProcessorsMap);
-        $this->configProvider->expects($this->any())
+        $this->configProvider->expects(self::any())
             ->method('getSystemVariableValues')
             ->willReturn($systemVariableValues);
     }
 
-    public function testCompilePreview()
+    public function testCompilePreview(): void
     {
         $entity = new TestEntityForVariableProvider();
 
-        $this->configProvider->expects($this->any())
+        $this->configProvider->expects(self::any())
             ->method('getConfiguration')
             ->willReturn([
                 'properties' => [],
@@ -123,10 +109,10 @@ public function testCompilePreview()
         $emailTemplate->setContent($template);
         $emailTemplate->setSubject('');
 
-        $this->assertSame($template, $this->renderer->compilePreview($emailTemplate));
+        self::assertSame($template.'(sanitized)', $this->renderer->compilePreview($emailTemplate));
     }
 
-    public function testCompileMessage()
+    public function testCompileMessage(): void
     {
         $entity = new TestEntityForVariableProvider();
         $entity->setField1('Test');
@@ -144,7 +130,7 @@ public function testCompileMessage()
             . ' {{ system.testVar }}'
             . ' N/A';
 
-        $this->configProvider->expects($this->any())
+        $this->configProvider->expects(self::any())
             ->method('getConfiguration')
             ->willReturn([
                 'properties' => [],
@@ -155,7 +141,7 @@ public function testCompileMessage()
         $this->expectVariables($entityVariableProcessors, $systemVars);
 
         $htmlTagHelper = $this->createMock(HtmlTagHelper::class);
-        $this->container->expects($this->once())
+        $this->container->expects(self::once())
             ->method('get')
             ->with('oro_ui.html_tag_helper')
             ->willReturn($htmlTagHelper);
@@ -169,13 +155,13 @@ public function testCompileMessage()
         $result = $this->renderer->compileMessage($emailTemplate, $templateParams);
         $expectedContent = 'test <a href="http://example.com">test</a>   2 test_system N/A';
 
-        $this->assertIsArray($result);
-        $this->assertCount(2, $result);
-        $this->assertSame($subject, $result[0]);
-        $this->assertSame($expectedContent, $result[1]);
+        self::assertIsArray($result);
+        self::assertCount(2, $result);
+        self::assertSame($subject, $result[0]);
+        self::assertSame($expectedContent, $result[1]);
     }
 
-    public function testRenderTemplate()
+    public function testRenderTemplate(): void
     {
         $template = 'test '
             . "\n"
@@ -184,7 +170,7 @@ public function testRenderTemplate()
             . '{{ system.currentDate }}';
 
         $entity = new TestEntityForVariableProvider();
-        $this->configProvider->expects($this->any())
+        $this->configProvider->expects(self::any())
             ->method('getConfiguration')
             ->willReturn([
                 'properties' => [],
@@ -197,7 +183,7 @@ public function testRenderTemplate()
         ], ['currentDate' => '10-12-2019']);
 
         $htmlTagHelper = $this->createMock(HtmlTagHelper::class);
-        $this->container->expects($this->once())
+        $this->container->expects(self::once())
             ->method('get')
             ->with('oro_ui.html_tag_helper')
             ->willReturn($htmlTagHelper);
@@ -208,6 +194,6 @@ public function testRenderTemplate()
             'test '
             . "\n"
             . '10-12-2019';
-        $this->assertSame($expectedRenderedResult, $result);
+        self::assertSame($expectedRenderedResult, $result);
     }
 }