set( FileNameFilter::class, 'default_replacements', [ '/\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 ] ); // 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 Filesystem::makeFolder(dirname($destPath)); copy($fromPath, $destPath); } } protected function tearDown(): void { TestAssetStore::reset(); parent::tearDown(); } public function testCasting() { // Shim TinyMCE so silverstripe/admin doesn't have to be installed TinyMCEConfig::config()->set( 'base_dir', 'silverstripe/framework: tests/php/Forms/HTMLEditor/TinyMCECombinedGeneratorTest/tinymce' ); // Test special characters $inputText = "These are some unicodes: ä, ö, & ü"; $field = new HTMLEditorField("Test", "Test"); $field->setValue($inputText); $this->assertStringContainsString('These are some unicodes: ä, ö, & ü', $field->Field()); // Test shortcodes $inputText = "Shortcode: [file_link id=4]"; $field = new HTMLEditorField("Test", "Test"); $field->setValue($inputText); $this->assertStringContainsString('Shortcode: [file_link id=4]', $field->Field()); } public function testBasicSaving() { $obj = new TestObject(); $editor = new HTMLEditorField('Content'); $editor->setValue('
Simple Content
'); $editor->saveInto($obj); $this->assertEquals('Simple Content
', $obj->Content, 'Attributes are preserved.'); $editor->setValue('Unclosed Tag'); $editor->saveInto($obj); $this->assertEquals('
Unclosed Tag
', $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( '', (string)$xml[0]['alt'], 'Alt attribute is always present, even if empty' ); $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 = '/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWzEwLDIwXQ.jpg'; $this->assertEquals($neededFilename, (string)$xml[0]['src'], 'Correct URL of resized image is set.'); $this->assertTrue(file_exists(PUBLIC_PATH . DIRECTORY_SEPARATOR . $neededFilename), 'File for resized image exists'); $this->assertEquals(false, $obj->HasBrokenFile, 'Referenced image file exists.'); } public function testMultiLineSaving() { $obj = $this->objFromFixture(TestObject::class, 'home'); $editor = new HTMLEditorField('Content'); $editor->setValue('First Paragraph
Second Paragraph
'); $editor->saveInto($obj); $this->assertEquals('First Paragraph
Second Paragraph
', $obj->Content); } public function testSavingLinksWithoutHref() { $obj = $this->objFromFixture(TestObject::class, 'home'); $editor = new HTMLEditorField('Content'); $editor->setValue(''); $editor->saveInto($obj); $this->assertEquals( '', $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 ) ); /** @var HTMLReadonlyField $readonly */ $readonly = $editor->performReadonlyTransformation(); /** @var DBHTMLText $readonlyContent */ $readonlyContent = $readonly->Field(); $this->assertEquals( <<