2013-07-03 10:06:49 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests\HTMLEditor;
|
|
|
|
|
2019-09-25 07:38:21 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorSanitiser;
|
2017-04-18 00:34:18 +02:00
|
|
|
use SilverStripe\View\Parsers\HTMLValue;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
class HTMLEditorSanitiserTest extends FunctionalTest
|
|
|
|
{
|
2013-07-03 10:06:49 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSanitisation()
|
|
|
|
{
|
|
|
|
$tests = array(
|
|
|
|
array(
|
|
|
|
'p,strong',
|
|
|
|
'<p>Leave Alone</p><div>Strip parent<strong>But keep children</strong> in order</div>',
|
|
|
|
'<p>Leave Alone</p>Strip parent<strong>But keep children</strong> in order',
|
|
|
|
'Non-whitelisted elements are stripped, but children are kept'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'p,strong',
|
|
|
|
'<div>A <strong>B <div>Nested elements are still filtered</div> C</strong> D</div>',
|
|
|
|
'A <strong>B Nested elements are still filtered C</strong> D',
|
|
|
|
'Non-whitelisted elements are stripped even when children of non-whitelisted elements'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'p',
|
|
|
|
'<p>Keep</p><script>Strip <strong>including children</strong></script>',
|
|
|
|
'<p>Keep</p>',
|
|
|
|
'Non-whitelisted script elements are totally stripped, including any children'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'p[id]',
|
|
|
|
'<p id="keep" bad="strip">Test</p>',
|
|
|
|
'<p id="keep">Test</p>',
|
|
|
|
'Non-whitelisted attributes are stripped'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'p[default1=default1|default2=default2|force1:force1|force2:force2]',
|
|
|
|
'<p default1="specific1" force1="specific1">Test</p>',
|
|
|
|
'<p default1="specific1" force1="force1" default2="default2" force2="force2">Test</p>',
|
|
|
|
'Default attributes are set when not present in input, forced attributes are always set'
|
2019-09-25 07:38:21 +02:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'a[href|target|rel]',
|
|
|
|
'<a href="/test" target="_blank">Test</a>',
|
|
|
|
'<a href="/test" target="_blank" rel="noopener noreferrer">Test</a>',
|
|
|
|
'noopener rel attribute is added when target attribute is set'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'a[href|target|rel]',
|
|
|
|
'<a href="/test" target="_top">Test</a>',
|
|
|
|
'<a href="/test" target="_top" rel="noopener noreferrer">Test</a>',
|
|
|
|
'noopener rel attribute is added when target is _top instead of _blank'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'a[href|target|rel]',
|
|
|
|
'<a href="/test" rel="noopener noreferrer">Test</a>',
|
|
|
|
'<a href="/test">Test</a>',
|
|
|
|
'noopener rel attribute is removed when target is not set'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'a[href|target|rel]',
|
|
|
|
'<a href="/test" rel="noopener noreferrer" target="_blank">Test</a>',
|
|
|
|
'<a href="/test" target="_blank">Test</a>',
|
|
|
|
'noopener rel attribute is removed when link_rel_value is an empty string'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'a[href|target|rel]',
|
|
|
|
'<a href="/test" target="_blank">Test</a>',
|
|
|
|
'<a href="/test" target="_blank">Test</a>',
|
|
|
|
'noopener rel attribute is unchanged when link_rel_value is null'
|
|
|
|
),
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$config = HTMLEditorConfig::get('htmleditorsanitisertest');
|
|
|
|
|
|
|
|
foreach ($tests as $test) {
|
|
|
|
list($validElements, $input, $output, $desc) = $test;
|
|
|
|
|
|
|
|
$config->setOptions(array('valid_elements' => $validElements));
|
|
|
|
$sanitiser = new HtmlEditorSanitiser($config);
|
|
|
|
|
2019-09-25 07:38:21 +02:00
|
|
|
$value = 'noopener noreferrer';
|
|
|
|
if (strpos($desc, 'link_rel_value is an empty string') !== false) {
|
|
|
|
$value = '';
|
|
|
|
} elseif (strpos($desc, 'link_rel_value is null') !== false) {
|
|
|
|
$value = null;
|
|
|
|
}
|
|
|
|
Config::inst()->set(HTMLEditorSanitiser::class, 'link_rel_value', $value);
|
|
|
|
|
2017-04-18 00:34:18 +02:00
|
|
|
$htmlValue = HTMLValue::create($input);
|
2016-12-16 05:34:21 +01:00
|
|
|
$sanitiser->sanitise($htmlValue);
|
|
|
|
|
|
|
|
$this->assertEquals($output, $htmlValue->getContent(), $desc);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|