silverstripe-framework/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php

243 lines
8.0 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\Forms\Tests\HTMLEditor;
use Page;
use SilverStripe\Assets\File;
2016-10-14 03:30:05 +02:00
use SilverStripe\Assets\FileNameFilter;
use SilverStripe\Assets\Filesystem;
2016-10-14 03:30:05 +02:00
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
use SilverStripe\Control\Controller;
2016-09-09 08:43:05 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField_Toolbar;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField_Image;
use SilverStripe\Forms\HTMLReadonlyField;
2016-10-14 03:30:05 +02:00
use SilverStripe\Forms\Tests\HTMLEditor\HTMLEditorFieldTest\DummyMediaFormFieldExtension;
use SilverStripe\Forms\Tests\HTMLEditor\HTMLEditorFieldTest\TestObject;
use SilverStripe\ORM\FieldType\DBHTMLText;
class HTMLEditorFieldTest extends FunctionalTest {
2014-08-15 08:53:05 +02:00
protected static $fixture_file = 'HTMLEditorFieldTest.yml';
2014-08-15 08:53:05 +02:00
protected static $use_draft_site = true;
2014-08-15 08:53:05 +02:00
protected $requiredExtensions = array(
2016-10-14 03:30:05 +02:00
HTMLEditorField_Toolbar::class => array(
DummyMediaFormFieldExtension::class
)
);
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02:00
protected $extraDataObjects = array(TestObject::class);
2014-08-15 08:53:05 +02:00
public function setUp() {
parent::setUp();
// Set backend root to /HTMLEditorFieldTest
2016-10-14 03:30:05 +02:00
TestAssetStore::activate('HTMLEditorFieldTest');
// Set the File Name Filter replacements so files have the expected names
2016-10-14 03:30:05 +02:00
Config::inst()->update(FileNameFilter::class, 'default_replacements', array(
'/\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
2016-10-14 03:30:05 +02:00
$files = File::get()->exclude('ClassName', Folder::class);
foreach($files as $file) {
2016-10-14 03:30:05 +02:00
$fromPath = __DIR__ . '/HTMLEditorFieldTest/images/' . $file->Name;
$destPath = TestAssetStore::getLocalPath($file); // Only correct for test asset store
Filesystem::makeFolder(dirname($destPath));
copy($fromPath, $destPath);
}
}
public function tearDown() {
2016-10-14 03:30:05 +02:00
TestAssetStore::reset();
parent::tearDown();
}
public function testBasicSaving() {
2016-10-14 03:30:05 +02:00
$obj = new TestObject();
$editor = new HTMLEditorField('Content');
2014-08-15 08:53:05 +02:00
$editor->setValue('<p class="foo">Simple Content</p>');
$editor->saveInto($obj);
$this->assertEquals('<p class="foo">Simple Content</p>', $obj->Content, 'Attributes are preserved.');
2014-08-15 08:53:05 +02:00
$editor->setValue('<p>Unclosed Tag');
$editor->saveInto($obj);
$this->assertEquals('<p>Unclosed Tag</p>', $obj->Content, 'Unclosed tags are closed.');
}
2014-08-15 08:53:05 +02:00
public function testNullSaving() {
2016-10-14 03:30:05 +02:00
$obj = new TestObject();
$editor = new HTMLEditorField('Content');
2014-08-15 08:53:05 +02:00
$editor->setValue(null);
$editor->saveInto($obj);
$this->assertEquals('', $obj->Content, "Doesn't choke on empty/null values.");
}
2014-08-15 08:53:05 +02:00
public function testResizedImageInsertion() {
2016-10-14 03:30:05 +02:00
$obj = new TestObject();
$editor = new HTMLEditorField('Content');
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02:00
$fileID = $this->idFromFixture(Image::class, 'example_image');
$editor->setValue(sprintf(
2016-10-14 03:30:05 +02:00
'[image src="assets/example.jpg" width="10" height="20" id="%d"]',
$fileID
));
$editor->saveInto($obj);
2014-08-15 08:53:05 +02:00
2016-03-08 12:20:51 +01:00
$parser = new CSSContentParser($obj->dbObject('Content')->forTemplate());
$xml = $parser->getByXpath('//img');
2016-03-08 12:20:51 +01:00
$this->assertEquals(
2016-10-14 03:30:05 +02:00
'example',
2016-03-08 12:20:51 +01:00
(string)$xml[0]['alt'],
'Alt tags are added by default based on filename'
);
$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.');
2014-08-15 08:53:05 +02:00
$neededFilename
2016-10-14 03:30:05 +02:00
= '/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWyIxMCIsIjIwIl0.jpg';
$this->assertEquals($neededFilename, (string)$xml[0]['src'], 'Correct URL of resized image is set.');
$this->assertTrue(file_exists(BASE_PATH.DIRECTORY_SEPARATOR.$neededFilename), 'File for resized image exists');
$this->assertEquals(false, $obj->HasBrokenFile, 'Referenced image file exists.');
2014-08-15 08:53:05 +02:00
}
public function testMultiLineSaving() {
2016-10-14 03:30:05 +02:00
$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);
}
2014-08-15 08:53:05 +02:00
public function testSavingLinksWithoutHref() {
2016-10-14 03:30:05 +02:00
$obj = $this->objFromFixture(TestObject::class, 'home');
$editor = new HTMLEditorField('Content');
2014-08-15 08:53:05 +02:00
$editor->setValue('<p><a name="example-anchor"></a></p>');
$editor->saveInto($obj);
2014-08-15 08:53:05 +02:00
$this->assertEquals (
'<p><a name="example-anchor"></a></p>', $obj->Content, 'Saving a link without a href attribute works'
);
}
2015-11-18 17:26:26 +01:00
public function testGetAnchors() {
if (!class_exists('Page')) {
$this->markTestSkipped();
}
$linkedPage = new Page();
$linkedPage->Title = 'Dummy';
$linkedPage->write();
$html = <<<EOS
<div name="foo"></div>
<div name='bar'></div>
2015-11-18 17:26:26 +01:00
<div id="baz"></div>
[sitetree_link id="{$linkedPage->ID}"]
<div id='bam'></div>
2015-11-18 17:26:26 +01:00
<div id = "baz"></div>
<div id = ""></div>
<div id="some'id"></div>
<div id=bar></div>
EOS
;
2015-11-18 17:26:26 +01:00
$expected = array(
'foo',
'bar',
'baz',
'bam',
2016-01-19 05:08:40 +01:00
"some&#039;id",
2015-11-18 17:26:26 +01:00
);
$page = new Page();
$page->Title = 'Test';
$page->Content = $html;
$page->write();
$this->useDraftSite(true);
2016-09-09 08:43:05 +02:00
$request = new HTTPRequest('GET', '/', array(
2015-11-18 17:26:26 +01:00
'PageID' => $page->ID,
2016-01-19 05:08:40 +01:00
));
2015-11-18 17:26:26 +01:00
$toolBar = new HTMLEditorField_Toolbar(new Controller(), 'test');
2016-01-19 05:08:40 +01:00
$toolBar->setRequest($request);
2015-11-18 17:26:26 +01:00
2016-01-19 05:08:40 +01:00
$results = json_decode($toolBar->getanchors(), true);
$this->assertEquals($expected, $results);
2015-11-18 17:26:26 +01:00
}
public function testHTMLEditorFieldFileLocal() {
$file = new HTMLEditorField_Image('http://domain.com/folder/my_image.jpg?foo=bar');
$this->assertEquals('http://domain.com/folder/my_image.jpg?foo=bar', $file->URL);
$this->assertEquals('my_image.jpg', $file->Name);
$this->assertEquals('jpg', $file->Extension);
// TODO Can't easily test remote file dimensions
}
public function testHTMLEditorFieldFileRemote() {
$fileFixture = new File(array('Name' => 'my_local_image.jpg', 'Filename' => 'folder/my_local_image.jpg'));
$file = new HTMLEditorField_Image('http://localdomain.com/folder/my_local_image.jpg', $fileFixture);
$this->assertEquals('http://localdomain.com/folder/my_local_image.jpg', $file->URL);
$this->assertEquals('my_local_image.jpg', $file->Name);
$this->assertEquals('jpg', $file->Extension);
}
public function testReadonlyField() {
$editor = new HTMLEditorField('Content');
2016-10-14 03:30:05 +02:00
$fileID = $this->idFromFixture(Image::class, 'example_image');
$editor->setValue(sprintf(
2016-10-14 03:30:05 +02:00
'[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( <<<EOS
<span class="readonly typography" id="Content">
2016-10-14 03:30:05 +02:00
<img src="/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWyIxMCIsIjIwIl0.jpg" alt="example" width="10" height="20">
</span>
EOS
,
$readonlyContent->getValue()
);
// Test with include input tag
$readonly = $editor->performReadonlyTransformation()
->setIncludeHiddenField(true);
/** @var DBHTMLText $readonlyContent */
$readonlyContent = $readonly->Field();
$this->assertEquals( <<<EOS
<span class="readonly typography" id="Content">
2016-10-14 03:30:05 +02:00
<img src="/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWyIxMCIsIjIwIl0.jpg" alt="example" width="10" height="20">
</span>
2016-10-14 03:30:05 +02:00
<input type="hidden" name="Content" value="[image src=&quot;/assets/HTMLEditorFieldTest/f5c7c2f814/example.jpg&quot; width=&quot;10&quot; height=&quot;20&quot; id=&quot;{$fileID}&quot;]" />
EOS
,
$readonlyContent->getValue()
);
}
}