Skip to content

Commit cfead2f

Browse files
committed
OXDEV-5017 Add tests
1 parent d78550f commit cfead2f

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

src/Extensions/Filters/SanitizeHtmlExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace OxidEsales\Twig\Extensions\Filters;
1111

12-
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
12+
use OxidEsales\EshopCommunity\Internal\Utility\Html\HtmlSanitizerInterface;
1313
use Twig\Extension\AbstractExtension;
1414
use Twig\TwigFilter;
1515

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\Twig\Extensions\Filters;
11+
12+
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
13+
use OxidEsales\EshopCommunity\Internal\Utility\Html\HtmlSanitizerInterface;
14+
use OxidEsales\EshopCommunity\Tests\ContainerTrait;
15+
use OxidEsales\Twig\Tests\Integration\Extensions\AbstractExtensionTestCase;
16+
17+
class SanitizeHtmlExtensionTest extends AbstractExtensionTestCase
18+
{
19+
use ContainerTrait;
20+
21+
private string $unsafeHtml = '<div><script> alert("SPAM MESSAGE") </script></div>';
22+
private string $safeHtml = '<div></div>';
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
$this->createContainer();
28+
}
29+
30+
public function testSanitizerShouldEliminateUnsafeTags(): void
31+
{
32+
$this->setParameter('oxid_esales.html_sanitizer_enabled', true);
33+
$this->attachContainerToContainerFactory();
34+
$this->extension = new SanitizeHtmlExtension(ContainerFacade::get(HtmlSanitizerInterface::class));
35+
$template = "{{ '" . $this->unsafeHtml . "' | sanitize_html }}";
36+
37+
$result = $this->getTemplate($template)->render([]);
38+
39+
$this->assertEquals($this->safeHtml, $result);
40+
}
41+
42+
public function testSanitizerShouldPassEverythingWhenDisabled(): void
43+
{
44+
$this->setParameter('oxid_esales.html_sanitizer_enabled', false);
45+
$this->attachContainerToContainerFactory();
46+
$this->extension = new SanitizeHtmlExtension(ContainerFacade::get(HtmlSanitizerInterface::class));
47+
$template = "{{ '" . $this->unsafeHtml . "' | sanitize_html }}";
48+
49+
$result = $this->getTemplate($template)->render([]);
50+
51+
$this->assertEquals($this->unsafeHtml, $result);
52+
}
53+
}

0 commit comments

Comments
 (0)