Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
allowMediaHosts example
use
Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use
Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
class
HtmlSanitizerAllTest
extends
TestCase
{
private
function
createSanitizer
(
)
: HtmlSanitizer
{
return
new
HtmlSanitizer
(
(
new
HtmlSanitizerConfig
(
)
)
->
allowStaticElements
(
)
->
allowLinkHosts
(
[
'trusted.com', 'external.com'
]
)
->
allowMediaHosts
(
[
'trusted.com', 'external.com'
]
)
->
allowRelativeLinks
(
)
->
allowRelativeMedias
(
)
->
forceHttpsUrls
(
)
)
;
}
/** * @dataProvider provideSanitizeHead */
public
function
testSanitizeHead
(
string
$input
, string
$expected
)
{
$this
->
assertSame
(
[
'http', 'ftp'
]
,
$config
->
getAllowedLinkSchemes
(
)
)
;
$config
=
$config
->
allowLinkHosts
(
[
'symfony.com', 'example.com'
]
)
;
$this
->
assertSame
(
[
'symfony.com', 'example.com'
]
,
$config
->
getAllowedLinkHosts
(
)
)
;
$config
=
$config
->
allowRelativeLinks
(
)
;
$this
->
assertTrue
(
$config
->
getAllowRelativeLinks
(
)
)
;
$config
=
$config
->
allowMediaSchemes
(
[
'https'
]
)
;
$this
->
assertSame
(
[
'https'
]
,
$config
->
getAllowedMediaSchemes
(
)
)
;
$config
=
$config
->
allowMediaHosts
(
[
'symfony.com'
]
)
;
$this
->
assertSame
(
[
'symfony.com'
]
,
$config
->
getAllowedMediaHosts
(
)
)
;
$config
=
$config
->
allowRelativeMedias
(
)
;
$this
->
assertTrue
(
$config
->
getAllowRelativeMedias
(
)
)
;
$config
=
$config
->
forceHttpsUrls
(
)
;
$this
->
assertTrue
(
$config
->
getForceHttpsUrls
(
)
)
;
}
public
function
testAllowElement
(
)
{
$this
->
assertSame
(
'<img />',
$this
->
sanitize
(
$config
, '<img src="/image.png" />'
)
)
;
}
public
function
testAllowMediasHosts
(
)
{
$config
=
(
new
HtmlSanitizerConfig
(
)
)
->
allowElement
(
'img',
[
'src'
]
)
->
allowMediaHosts
(
[
'trusted.com'
]
)
;
$this
->
assertSame
(
'<img src="https://trusted.com" />',
$this
->
sanitize
(
$config
, '<img src="https://trusted.com" />'
)
)
;
$this
->
assertSame
(
'<img />',
$this
->
sanitize
(
$config
, '<img src="https://untrusted.com" />'
)
)
;