2009-10-11 02:07:13 +02:00
|
|
|
<?php
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Filesystem;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\SS_HTTPRequest;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Extension;
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField_Toolbar;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField_Image;
|
|
|
|
use SilverStripe\Forms\HTMLReadonlyField;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-07-14 04:43:53 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2009-10-11 02:07:13 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-10-11 02:07:13 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2016-05-06 01:43:47 +02:00
|
|
|
class HTMLEditorFieldTest extends FunctionalTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-06 01:43:47 +02:00
|
|
|
protected static $fixture_file = 'HTMLEditorFieldTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $use_draft_site = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 06:56:02 +02:00
|
|
|
protected $requiredExtensions = array(
|
2016-08-19 00:51:35 +02:00
|
|
|
'SilverStripe\\Forms\\HTMLEditor\\HTMLEditorField_Toolbar' => array(
|
|
|
|
'HTMLEditorFieldTest_DummyMediaFormFieldExtension'
|
|
|
|
)
|
2010-05-25 06:56:02 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-06 01:43:47 +02:00
|
|
|
protected $extraDataObjects = array('HTMLEditorFieldTest_Object');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-05-06 01:43:47 +02:00
|
|
|
// Set backend root to /HTMLEditorFieldTest
|
|
|
|
AssetStoreTest_SpyStore::activate('HTMLEditorFieldTest');
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-05-09 10:12:29 +02:00
|
|
|
// Set the File Name Filter replacements so files have the expected names
|
2016-08-19 00:51:35 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Assets\\FileNameFilter', 'default_replacements', array(
|
2016-05-09 10:12:29 +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
|
|
|
|
));
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Create a test files for each of the fixture references
|
2016-08-19 00:51:35 +02:00
|
|
|
$files = File::get()->exclude('ClassName', 'SilverStripe\\Assets\\Folder');
|
2015-09-15 04:52:02 +02:00
|
|
|
foreach($files as $file) {
|
|
|
|
$fromPath = BASE_PATH . '/framework/tests/forms/images/' . $file->Name;
|
2015-10-19 06:27:27 +02:00
|
|
|
$destPath = AssetStoreTest_SpyStore::getLocalPath($file); // Only correct for test asset store
|
2016-08-19 00:51:35 +02:00
|
|
|
Filesystem::makeFolder(dirname($destPath));
|
2015-09-15 04:52:02 +02:00
|
|
|
copy($fromPath, $destPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
AssetStoreTest_SpyStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:13 +02:00
|
|
|
public function testBasicSaving() {
|
2016-05-06 01:43:47 +02:00
|
|
|
$obj = new HTMLEditorFieldTest_Object();
|
|
|
|
$editor = new HTMLEditorField('Content');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:13 +02:00
|
|
|
$editor->setValue('<p class="foo">Simple Content</p>');
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals('<p class="foo">Simple Content</p>', $obj->Content, 'Attributes are preserved.');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:13 +02:00
|
|
|
$editor->setValue('<p>Unclosed Tag');
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals('<p>Unclosed Tag</p>', $obj->Content, 'Unclosed tags are closed.');
|
2009-10-11 02:07:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:13 +02:00
|
|
|
public function testNullSaving() {
|
2016-05-06 01:43:47 +02:00
|
|
|
$obj = new HTMLEditorFieldTest_Object();
|
|
|
|
$editor = new HTMLEditorField('Content');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:13 +02:00
|
|
|
$editor->setValue(null);
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals('', $obj->Content, "Doesn't choke on empty/null values.");
|
2009-10-11 02:07:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:48:00 +02:00
|
|
|
public function testResizedImageInsertion() {
|
2016-05-06 01:43:47 +02:00
|
|
|
$obj = new HTMLEditorFieldTest_Object();
|
|
|
|
$editor = new HTMLEditorField('Content');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$fileID = $this->idFromFixture('SilverStripe\\Assets\\Image', 'example_image');
|
2015-09-15 04:52:02 +02:00
|
|
|
$editor->setValue(sprintf(
|
2016-03-08 12:20:51 +01:00
|
|
|
'[image src="assets/HTMLEditorFieldTest_example.jpg" width="10" height="20" id="%d"]',
|
2015-09-15 04:52:02 +02:00
|
|
|
$fileID
|
|
|
|
));
|
2013-04-06 12:48:00 +02:00
|
|
|
$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());
|
2013-04-06 12:48:00 +02:00
|
|
|
$xml = $parser->getByXpath('//img');
|
2016-03-08 12:20:51 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'HTMLEditorFieldTest example',
|
|
|
|
(string)$xml[0]['alt'],
|
|
|
|
'Alt tags are added by default based on filename'
|
|
|
|
);
|
2013-04-06 12:48:00 +02: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.');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$neededFilename
|
2016-05-06 01:43:47 +02:00
|
|
|
= '/assets/HTMLEditorFieldTest/f5c7c2f814/HTMLEditorFieldTest-example__ResizedImageWyIxMCIsIjIwIl0.jpg';
|
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
$this->assertEquals($neededFilename, (string)$xml[0]['src'], 'Correct URL of resized image is set.');
|
2013-10-02 06:31:06 +02:00
|
|
|
$this->assertTrue(file_exists(BASE_PATH.DIRECTORY_SEPARATOR.$neededFilename), 'File for resized image exists');
|
2013-04-06 12:48:00 +02:00
|
|
|
$this->assertEquals(false, $obj->HasBrokenFile, 'Referenced image file exists.');
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:13 +02:00
|
|
|
public function testMultiLineSaving() {
|
2016-05-06 01:43:47 +02:00
|
|
|
$obj = $this->objFromFixture('HTMLEditorFieldTest_Object', 'home');
|
|
|
|
$editor = new HTMLEditorField('Content');
|
2012-09-14 07:31:12 +02:00
|
|
|
$editor->setValue('<p>First Paragraph</p><p>Second Paragraph</p>');
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
2012-09-14 07:31:12 +02:00
|
|
|
$this->assertEquals('<p>First Paragraph</p><p>Second Paragraph</p>', $obj->Content);
|
2009-10-11 02:07:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-13 04:44:10 +02:00
|
|
|
public function testSavingLinksWithoutHref() {
|
2016-05-06 01:43:47 +02:00
|
|
|
$obj = $this->objFromFixture('HTMLEditorFieldTest_Object', 'home');
|
|
|
|
$editor = new HTMLEditorField('Content');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-13 04:44:10 +02:00
|
|
|
$editor->setValue('<p><a name="example-anchor"></a></p>');
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-13 04:44:10 +02:00
|
|
|
$this->assertEquals (
|
2012-09-14 07:31:12 +02:00
|
|
|
'<p><a name="example-anchor"></a></p>', $obj->Content, 'Saving a link without a href attribute works'
|
2009-10-13 04:44:10 +02:00
|
|
|
);
|
|
|
|
}
|
2010-05-25 06:56:02 +02:00
|
|
|
|
2015-11-18 17:26:26 +01:00
|
|
|
public function testGetAnchors() {
|
|
|
|
if (!class_exists('Page')) {
|
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
2016-06-17 06:28:58 +02:00
|
|
|
$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>
|
2016-06-17 06:28:58 +02:00
|
|
|
[sitetree_link id="{$linkedPage->ID}"]
|
|
|
|
<div id='bam'></div>
|
2015-11-18 17:26:26 +01:00
|
|
|
<div id = "baz"></div>
|
|
|
|
<div id = ""></div>
|
2016-06-17 06:28:58 +02:00
|
|
|
<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'id",
|
2015-11-18 17:26:26 +01:00
|
|
|
);
|
|
|
|
$page = new Page();
|
|
|
|
$page->Title = 'Test';
|
|
|
|
$page->Content = $html;
|
|
|
|
$page->write();
|
|
|
|
$this->useDraftSite(true);
|
|
|
|
|
2016-01-19 05:08:40 +01:00
|
|
|
$request = new SS_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
|
|
|
|
2016-05-06 01:43:47 +02: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
|
|
|
}
|
|
|
|
|
2016-05-06 01:43:47 +02:00
|
|
|
public function testHTMLEditorFieldFileLocal() {
|
|
|
|
$file = new HTMLEditorField_Image('http://domain.com/folder/my_image.jpg?foo=bar');
|
2012-02-09 17:17:39 +01:00
|
|
|
$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
|
|
|
|
}
|
2010-05-25 06:56:02 +02:00
|
|
|
|
2016-05-06 01:43:47 +02:00
|
|
|
public function testHTMLEditorFieldFileRemote() {
|
2012-02-09 17:17:39 +01:00
|
|
|
$fileFixture = new File(array('Name' => 'my_local_image.jpg', 'Filename' => 'folder/my_local_image.jpg'));
|
2016-05-06 01:43:47 +02:00
|
|
|
$file = new HTMLEditorField_Image('http://localdomain.com/folder/my_local_image.jpg', $fileFixture);
|
2012-02-09 17:17:39 +01:00
|
|
|
$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);
|
2010-05-25 06:56:02 +02:00
|
|
|
}
|
2016-07-14 04:43:53 +02:00
|
|
|
|
|
|
|
public function testReadonlyField() {
|
|
|
|
$editor = new HTMLEditorField('Content');
|
2016-08-19 00:51:35 +02:00
|
|
|
$fileID = $this->idFromFixture('SilverStripe\\Assets\\Image', 'example_image');
|
2016-07-14 04:43:53 +02:00
|
|
|
$editor->setValue(sprintf(
|
|
|
|
'[image src="assets/HTMLEditorFieldTest_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">
|
|
|
|
<img src="/assets/HTMLEditorFieldTest/f5c7c2f814/HTMLEditorFieldTest-example__ResizedImageWyIxMCIsIjIwIl0.jpg" alt="HTMLEditorFieldTest 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">
|
|
|
|
<img src="/assets/HTMLEditorFieldTest/f5c7c2f814/HTMLEditorFieldTest-example__ResizedImageWyIxMCIsIjIwIl0.jpg" alt="HTMLEditorFieldTest example" width="10" height="20">
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<input type="hidden" name="Content" value="[image src="/assets/HTMLEditorFieldTest/f5c7c2f814/HTMLEditorFieldTest-example.jpg" width="10" height="20" id="{$fileID}"]" />
|
|
|
|
|
|
|
|
|
|
|
|
EOS
|
|
|
|
,
|
|
|
|
$readonlyContent->getValue()
|
|
|
|
);
|
|
|
|
}
|
2009-10-11 02:07:13 +02:00
|
|
|
}
|
2010-05-25 06:56:02 +02:00
|
|
|
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-05-25 06:56:02 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2016-05-06 01:43:47 +02:00
|
|
|
class HTMLEditorFieldTest_DummyMediaFormFieldExtension extends Extension implements TestOnly {
|
2010-05-25 06:56:02 +02:00
|
|
|
public static $fields = null;
|
|
|
|
public static $update_called = false;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @param Form $form
|
|
|
|
*/
|
2010-05-25 06:56:02 +02:00
|
|
|
public function updateImageForm($form) {
|
|
|
|
self::$update_called = true;
|
|
|
|
self::$fields = $form->Fields();
|
|
|
|
}
|
2011-03-23 04:32:24 +01:00
|
|
|
}
|
|
|
|
|
2016-05-06 01:43:47 +02:00
|
|
|
class HTMLEditorFieldTest_Object extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2011-03-23 02:32:38 +01:00
|
|
|
'Title' => 'Varchar',
|
2013-04-06 12:48:00 +02:00
|
|
|
'Content' => 'HTMLText',
|
|
|
|
'HasBrokenFile' => 'Boolean'
|
2011-03-23 04:32:24 +01:00
|
|
|
);
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|