Skip to content

Commit bec8701

Browse files
committed
feat(HtmlPurifierServiceTest): test cleanHtml
1 parent 8fa3d9d commit bec8701

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace YesWiki\Test\Core\Service;
4+
5+
use Throwable;
6+
use YesWiki\Core\Service\HtmlPurifierService;
7+
use YesWiki\Test\Core\YesWikiTestCase;
8+
9+
require_once 'tests/YesWikiTestCase.php';
10+
11+
class HtmlPurifierServiceTest extends YesWikiTestCase
12+
{
13+
/**
14+
* @covers UserManager::__construct
15+
* @return HtmlPurifierService $htmlPurifierService
16+
*/
17+
public function testHtmlPurifierServiceExisting(): HtmlPurifierService
18+
{
19+
$wiki = $this->getWiki();
20+
$this->assertTrue($wiki->services->has(HtmlPurifierService::class));
21+
return $wiki->services->get(HtmlPurifierService::class);
22+
}
23+
24+
/**
25+
* @depends testHtmlPurifierServiceExisting
26+
* @covers HtmlPurifierService::cleanHTML
27+
* @dataProvider dataProviderTestCleanHTML
28+
* @param string $dirtyHtml
29+
* @param string $waitedCleanedHtml
30+
* @param HtmlPurifierService $htmlPurifierService
31+
*/
32+
public function testCleanHTML(string $dirtyHtml, string $waitedCleanedHtml, HtmlPurifierService $htmlPurifierService)
33+
{
34+
$cleanedHtml = $htmlPurifierService->cleanHTML($dirtyHtml);
35+
$this->assertEquals($cleanedHtml, $waitedCleanedHtml, "'$dirtyHtml' was waited to be cleaned as '$waitedCleanedHtml', but '$cleanedHtml' obtained");
36+
}
37+
38+
public function dataProviderTestCleanHTML()
39+
{
40+
return [
41+
'Only text' => [
42+
'dirtyHtml' => 'This is a test.',
43+
'waitedCleanedHtml' => 'This is a test.'
44+
],
45+
'Text with link' => [
46+
'dirtyHtml' => 'This is a <a href="https://example.com" class="btn btn-primary modalbox">link</a>.',
47+
'waitedCleanedHtml' => 'This is a <a href="https://example.com" class="btn btn-primary modalbox">link</a>.'
48+
],
49+
'Text with link with data' => [
50+
'dirtyHtml' => 'This is a <a href="https://example.com" class="btn btn-primary modalbox" data-iframe="1" data-size="modal-lg">link</a>.',
51+
'waitedCleanedHtml' => 'This is a <a href="https://example.com" class="btn btn-primary modalbox">link</a>.'
52+
],
53+
'Text with link with target' => [
54+
'dirtyHtml' => 'This is a <a href="https://example.com" class="btn btn-primary modalbox" target="_blank">link</a>.',
55+
'waitedCleanedHtml' => 'This is a <a href="https://example.com" class="btn btn-primary modalbox" target="_blank" rel="noreferrer noopener">link</a>.'
56+
],
57+
'Text with link with not authorized target' => [
58+
'dirtyHtml' => 'This is a <a href="https://example.com" class="btn btn-primary modalbox" target="blank">link</a>.',
59+
'waitedCleanedHtml' => 'This is a <a href="https://example.com" class="btn btn-primary modalbox">link</a>.'
60+
],
61+
'Span' => [
62+
'dirtyHtml' => 'This is a <span>word</span>.',
63+
'waitedCleanedHtml' => 'This is a <span>word</span>.'
64+
],
65+
'Span with style : color red' => [
66+
'dirtyHtml' => 'This is a <span style="color:red;">word</span>.',
67+
'waitedCleanedHtml' => 'This is a <span style="color:#FF0000;">word</span>.'
68+
],
69+
'Span with style : color red and font size' => [
70+
'dirtyHtml' => 'This is a <span style="color:red;font-size:16px;">word</span>.',
71+
'waitedCleanedHtml' => 'This is a <span style="color:#FF0000;font-size:16px;">word</span>.'
72+
],
73+
'Span with style : color red and lang' => [
74+
'dirtyHtml' => 'This is a <span style="color:red;" lang="fr">word</span>.',
75+
'waitedCleanedHtml' => 'This is a <span style="color:#FF0000;" lang="fr" xml:lang="fr">word</span>.'
76+
],
77+
'Span with style : color red and data' => [
78+
'dirtyHtml' => 'This is a <span style="color:red;" data-lang="fr">word</span>.',
79+
'waitedCleanedHtml' => 'This is a <span style="color:#FF0000;">word</span>.'
80+
],
81+
'bold, italic, break line' => [
82+
'dirtyHtml' => 'This is <b>a <br /><i>word</i></b>.',
83+
'waitedCleanedHtml' => 'This is <b>a <br /><i>word</i></b>.'
84+
],
85+
'XSS via img' => [
86+
'dirtyHtml' => 'This is an attack <img src="x" onerror="alert(\'Test !\');"/>.',
87+
'waitedCleanedHtml' => 'This is an attack <img src="x" alt="x" />.'
88+
],
89+
'XSS via img injection' => [
90+
'dirtyHtml' => 'This is an attack ><img src="x" onerror="alert(\'Test !\');"/>.',
91+
'waitedCleanedHtml' => 'This is an attack &gt;<img src="x" alt="x" />.'
92+
],
93+
'iframe' => [
94+
'dirtyHtml' => 'This is an iframe :<br /><iframe src="https://yeswiki.net"></iframe>',
95+
'waitedCleanedHtml' => 'This is an iframe :<br />'
96+
],
97+
'dirty iframe' => [
98+
'dirtyHtml' => 'This is a dirty iframe :<br /><iframe src="https://yeswiki.net">.',
99+
'waitedCleanedHtml' => 'This is a dirty iframe :<br />.'
100+
],
101+
];
102+
}
103+
}

0 commit comments

Comments
 (0)