2009-10-11 02:07:13 +02:00
|
|
|
<?php
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests\HTMLEditor;
|
|
|
|
|
2018-03-22 05:20:27 +01:00
|
|
|
use Silverstripe\Assets\Dev\TestAssetStore;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\File;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\FileNameFilter;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\Filesystem;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\Folder;
|
|
|
|
use SilverStripe\Assets\Image;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2023-10-16 01:39:18 +02:00
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
2018-03-22 05:20:27 +01:00
|
|
|
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\HTMLReadonlyField;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\HTMLEditor\HTMLEditorFieldTest\TestObject;
|
2016-07-14 04:43:53 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class HTMLEditorFieldTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'HTMLEditorFieldTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
TestObject::class,
|
|
|
|
];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Set backend root to /HTMLEditorFieldTest
|
|
|
|
TestAssetStore::activate('HTMLEditorFieldTest');
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Set the File Name Filter replacements so files have the expected names
|
2017-05-25 03:43:13 +02:00
|
|
|
Config::modify()->set(
|
2016-12-16 05:34:21 +01:00
|
|
|
FileNameFilter::class,
|
|
|
|
'default_replacements',
|
2017-05-25 03:43:13 +02:00
|
|
|
[
|
|
|
|
'/\s/' => '-', // remove whitespace
|
|
|
|
'/_/' => '-', // underscores to dashes
|
|
|
|
'/[^A-Za-z0-9+.\-]+/' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot
|
|
|
|
'/[\-]{2,}/' => '-', // remove duplicate dashes
|
|
|
|
'/^[\.\-_]+/' => '', // Remove all leading dots, dashes or underscores
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Create a test files for each of the fixture references
|
|
|
|
$files = File::get()->exclude('ClassName', Folder::class);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$fromPath = __DIR__ . '/HTMLEditorFieldTest/images/' . $file->Name;
|
|
|
|
$destPath = TestAssetStore::getLocalPath($file); // Only correct for test asset store
|
2022-04-14 03:12:59 +02:00
|
|
|
Filesystem::makeFolder(dirname($destPath ?? ''));
|
|
|
|
copy($fromPath ?? '', $destPath ?? '');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
TestAssetStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2016-12-28 14:52:49 +01:00
|
|
|
public function testCasting()
|
|
|
|
{
|
2017-05-10 03:18:44 +02:00
|
|
|
// Shim TinyMCE so silverstripe/admin doesn't have to be installed
|
2017-08-16 05:56:20 +02:00
|
|
|
TinyMCEConfig::config()->set(
|
|
|
|
'base_dir',
|
2017-10-10 05:51:47 +02:00
|
|
|
'silverstripe/framework: tests/php/Forms/HTMLEditor/TinyMCECombinedGeneratorTest/tinymce'
|
2017-08-16 05:56:20 +02:00
|
|
|
);
|
2017-05-10 03:18:44 +02:00
|
|
|
|
2016-12-28 14:52:49 +01:00
|
|
|
// Test special characters
|
|
|
|
$inputText = "These are some unicodes: ä, ö, & ü";
|
|
|
|
$field = new HTMLEditorField("Test", "Test");
|
|
|
|
$field->setValue($inputText);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('These are some unicodes: ä, ö, & ü', $field->Field());
|
2016-12-28 14:52:49 +01:00
|
|
|
// Test shortcodes
|
|
|
|
$inputText = "Shortcode: [file_link id=4]";
|
|
|
|
$field = new HTMLEditorField("Test", "Test");
|
|
|
|
$field->setValue($inputText);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('Shortcode: [file_link id=4]', $field->Field());
|
2016-12-28 14:52:49 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testBasicSaving()
|
|
|
|
{
|
|
|
|
$obj = new TestObject();
|
|
|
|
$editor = new HTMLEditorField('Content');
|
|
|
|
|
|
|
|
$editor->setValue('<p class="foo">Simple Content</p>');
|
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals('<p class="foo">Simple Content</p>', $obj->Content, 'Attributes are preserved.');
|
|
|
|
|
|
|
|
$editor->setValue('<p>Unclosed Tag');
|
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals('<p>Unclosed Tag</p>', $obj->Content, 'Unclosed tags are closed.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullSaving()
|
|
|
|
{
|
|
|
|
$obj = new TestObject();
|
|
|
|
$editor = new HTMLEditorField('Content');
|
|
|
|
|
|
|
|
$editor->setValue(null);
|
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals('', $obj->Content, "Doesn't choke on empty/null values.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testResizedImageInsertion()
|
|
|
|
{
|
|
|
|
$obj = new TestObject();
|
|
|
|
$editor = new HTMLEditorField('Content');
|
|
|
|
|
|
|
|
$fileID = $this->idFromFixture(Image::class, 'example_image');
|
|
|
|
$editor->setValue(
|
|
|
|
sprintf(
|
|
|
|
'[image src="assets/example.jpg" width="10" height="20" id="%d"]',
|
|
|
|
$fileID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$editor->saveInto($obj);
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($obj->dbObject('Content')->forTemplate());
|
|
|
|
$xml = $parser->getByXpath('//img');
|
|
|
|
$this->assertEquals(
|
2019-11-14 00:04:37 +01:00
|
|
|
'',
|
2016-12-16 05:34:21 +01:00
|
|
|
(string)$xml[0]['alt'],
|
2019-11-14 00:04:37 +01:00
|
|
|
'Alt attribute is always present, even if empty'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals('', (string)$xml[0]['title'], 'Title tags are added by default.');
|
|
|
|
$this->assertEquals(10, (int)$xml[0]['width'], 'Width tag of resized image is set.');
|
|
|
|
$this->assertEquals(20, (int)$xml[0]['height'], 'Height tag of resized image is set.');
|
|
|
|
|
|
|
|
$neededFilename
|
2017-11-22 02:36:24 +01:00
|
|
|
= '/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWzEwLDIwXQ.jpg';
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals($neededFilename, (string)$xml[0]['src'], 'Correct URL of resized image is set.');
|
2018-01-22 02:57:05 +01:00
|
|
|
$this->assertTrue(file_exists(PUBLIC_PATH . DIRECTORY_SEPARATOR . $neededFilename), 'File for resized image exists');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(false, $obj->HasBrokenFile, 'Referenced image file exists.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultiLineSaving()
|
|
|
|
{
|
|
|
|
$obj = $this->objFromFixture(TestObject::class, 'home');
|
|
|
|
$editor = new HTMLEditorField('Content');
|
|
|
|
$editor->setValue('<p>First Paragraph</p><p>Second Paragraph</p>');
|
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals('<p>First Paragraph</p><p>Second Paragraph</p>', $obj->Content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingLinksWithoutHref()
|
|
|
|
{
|
|
|
|
$obj = $this->objFromFixture(TestObject::class, 'home');
|
|
|
|
$editor = new HTMLEditorField('Content');
|
|
|
|
|
|
|
|
$editor->setValue('<p><a name="example-anchor"></a></p>');
|
|
|
|
$editor->saveInto($obj);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p><a name="example-anchor"></a></p>',
|
|
|
|
$obj->Content,
|
|
|
|
'Saving a link without a href attribute works'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadonlyField()
|
|
|
|
{
|
|
|
|
$editor = new HTMLEditorField('Content');
|
|
|
|
$fileID = $this->idFromFixture(Image::class, 'example_image');
|
|
|
|
$editor->setValue(
|
|
|
|
sprintf(
|
|
|
|
'[image src="assets/example.jpg" width="10" height="20" id="%d"]',
|
|
|
|
$fileID
|
|
|
|
)
|
|
|
|
);
|
2017-05-25 03:43:13 +02:00
|
|
|
/** @var HTMLReadonlyField $readonly */
|
2016-12-16 05:34:21 +01:00
|
|
|
$readonly = $editor->performReadonlyTransformation();
|
2017-05-25 03:43:13 +02:00
|
|
|
/** @var DBHTMLText $readonlyContent */
|
2016-12-16 05:34:21 +01:00
|
|
|
$readonlyContent = $readonly->Field();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
<<<EOS
|
2016-07-14 04:43:53 +02:00
|
|
|
<span class="readonly typography" id="Content">
|
2021-07-06 07:27:54 +02:00
|
|
|
<img src="/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWzEwLDIwXQ.jpg" alt="" width="10" height="20" loading="lazy">
|
2016-07-14 04:43:53 +02:00
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
|
|
EOS
|
2016-12-16 05:34:21 +01:00
|
|
|
,
|
|
|
|
$readonlyContent->getValue()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test with include input tag
|
|
|
|
$readonly = $editor->performReadonlyTransformation()
|
|
|
|
->setIncludeHiddenField(true);
|
2017-05-25 03:43:13 +02:00
|
|
|
/** @var DBHTMLText $readonlyContent */
|
2016-12-16 05:34:21 +01:00
|
|
|
$readonlyContent = $readonly->Field();
|
|
|
|
$this->assertEquals(
|
|
|
|
<<<EOS
|
2016-07-14 04:43:53 +02:00
|
|
|
<span class="readonly typography" id="Content">
|
2021-07-06 07:27:54 +02:00
|
|
|
<img src="/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWzEwLDIwXQ.jpg" alt="" width="10" height="20" loading="lazy">
|
2016-07-14 04:43:53 +02:00
|
|
|
</span>
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
<input type="hidden" name="Content" value="[image src="/assets/HTMLEditorFieldTest/f5c7c2f814/example.jpg" width="10" height="20" id="{$fileID}"]" />
|
2016-07-14 04:43:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
EOS
|
2016-12-16 05:34:21 +01:00
|
|
|
,
|
|
|
|
$readonlyContent->getValue()
|
|
|
|
);
|
|
|
|
}
|
2023-08-07 06:23:46 +02:00
|
|
|
|
|
|
|
public function testValueEntities()
|
|
|
|
{
|
|
|
|
$inputText = "The company & partners";
|
|
|
|
$field = new HTMLEditorField("Content");
|
|
|
|
$field->setValue($inputText);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"The company & partners",
|
|
|
|
$field->obj('ValueEntities')->forTemplate()
|
|
|
|
);
|
|
|
|
|
|
|
|
$inputText = "The company && partners";
|
|
|
|
$field = new HTMLEditorField("Content");
|
|
|
|
$field->setValue($inputText);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"The company && partners",
|
|
|
|
$field->obj('ValueEntities')->forTemplate()
|
|
|
|
);
|
|
|
|
}
|
2023-10-16 01:39:18 +02:00
|
|
|
|
|
|
|
public function testFieldConfigSanitization()
|
|
|
|
{
|
|
|
|
$obj = TestObject::create();
|
|
|
|
$editor = HTMLEditorField::create('Content');
|
|
|
|
$defaultValidElements = [
|
|
|
|
'@[id|class|style|title|data*]',
|
|
|
|
'a[id|rel|dir|tabindex|accesskey|type|name|href|target|title|class]',
|
|
|
|
'-strong/-b[class]',
|
|
|
|
'-em/-i[class]',
|
|
|
|
'-ol[class]',
|
|
|
|
'#p[id|dir|class|align|style]',
|
|
|
|
'-li[class]',
|
|
|
|
'br',
|
|
|
|
'-span[class|align|style]',
|
|
|
|
'-ul[class]',
|
|
|
|
'-h3[id|dir|class|align|style]',
|
|
|
|
'-h2[id|dir|class|align|style]',
|
|
|
|
'hr[class]',
|
|
|
|
];
|
|
|
|
$restrictedConfig = HTMLEditorConfig::get('restricted');
|
|
|
|
$restrictedConfig->setOption('valid_elements', implode(',', $defaultValidElements));
|
|
|
|
$editor->setEditorConfig($restrictedConfig);
|
|
|
|
|
|
|
|
$expectedHtmlString = '<p>standard text</p>Header';
|
|
|
|
$htmlValue = '<p>standard text</p><table><th><tr><td>Header</td></tr></th><tbody></tbody></table>';
|
|
|
|
$editor->setValue($htmlValue);
|
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals($expectedHtmlString, $obj->Content, 'Table is not removed');
|
|
|
|
|
|
|
|
$defaultConfig = HTMLEditorConfig::get('default');
|
|
|
|
$editor->setEditorConfig($defaultConfig);
|
|
|
|
|
|
|
|
$editor->setValue($htmlValue);
|
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals($htmlValue, $obj->Content, 'Table is removed');
|
|
|
|
}
|
2009-10-11 02:07:13 +02:00
|
|
|
}
|