2009-10-11 02:07:13 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class HtmlEditorFieldTest extends FunctionalTest {
|
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
public static $fixture_file = 'HtmlEditorFieldTest.yml';
|
2009-10-11 02:07:13 +02:00
|
|
|
|
|
|
|
public static $use_draft_site = true;
|
|
|
|
|
2010-05-25 06:56:02 +02:00
|
|
|
protected $requiredExtensions = array(
|
2012-02-08 21:32:25 +01:00
|
|
|
'HtmlEditorField_Toolbar' => array('HtmlEditorFieldTest_DummyMediaFormFieldExtension')
|
2010-05-25 06:56:02 +02:00
|
|
|
);
|
|
|
|
|
2011-03-23 02:32:38 +01:00
|
|
|
protected $extraDataObjects = array('HtmlEditorFieldTest_Object');
|
|
|
|
|
2009-10-11 02:07:13 +02:00
|
|
|
public function testBasicSaving() {
|
2011-03-23 02:32:38 +01:00
|
|
|
$obj = new HtmlEditorFieldTest_Object();
|
2009-10-11 02:07:13 +02:00
|
|
|
$editor = new HtmlEditorField('Content');
|
|
|
|
|
|
|
|
$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.');
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullSaving() {
|
2011-03-23 02:32:38 +01:00
|
|
|
$obj = new HtmlEditorFieldTest_Object();
|
2009-10-11 02:07:13 +02:00
|
|
|
$editor = new HtmlEditorField('Content');
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
public function testImageInsertion() {
|
2011-03-23 02:32:38 +01:00
|
|
|
$obj = new HtmlEditorFieldTest_Object();
|
2009-10-11 02:07:13 +02:00
|
|
|
$editor = new HtmlEditorField('Content');
|
|
|
|
|
|
|
|
$editor->setValue('<img src="assets/example.jpg" />');
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
2009-10-11 02:07:13 +02:00
|
|
|
|
2011-03-23 02:32:38 +01:00
|
|
|
$xml = new SimpleXMLElement($obj->Content);
|
2009-10-11 02:07:13 +02:00
|
|
|
$this->assertNotNull($xml['alt'], 'Alt tags are added by default.');
|
|
|
|
$this->assertNotNull($xml['title'], 'Title tags are added by default.');
|
|
|
|
|
|
|
|
$editor->setValue('<img src="assets/example.jpg" alt="foo" title="bar" />');
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
2009-10-11 02:07:13 +02:00
|
|
|
|
2011-03-23 02:32:38 +01:00
|
|
|
$xml = new SimpleXMLElement($obj->Content);
|
2009-10-11 02:07:13 +02:00
|
|
|
$this->assertNotNull('foo', $xml['alt'], 'Alt tags are preserved.');
|
|
|
|
$this->assertNotNull('bar', $xml['title'], 'Title tags are preserved.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultiLineSaving() {
|
2011-03-23 02:32:38 +01:00
|
|
|
$obj = $this->objFromFixture('HtmlEditorFieldTest_Object', 'home');
|
2009-10-11 02:07:13 +02:00
|
|
|
$editor = new HtmlEditorField('Content');
|
2010-10-15 02:04:47 +02:00
|
|
|
$editor->setValue("<p>First Paragraph</p><p>Second Paragraph</p>");
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
|
|
|
$this->assertEquals("<p>First Paragraph</p><p>Second Paragraph</p>", $obj->Content);
|
2009-10-11 02:07:13 +02:00
|
|
|
}
|
|
|
|
|
2009-10-13 04:44:10 +02:00
|
|
|
public function testSavingLinksWithoutHref() {
|
2011-03-23 02:32:38 +01:00
|
|
|
$obj = $this->objFromFixture('HtmlEditorFieldTest_Object', 'home');
|
2009-10-13 04:44:10 +02:00
|
|
|
$editor = new HtmlEditorField('Content');
|
|
|
|
|
|
|
|
$editor->setValue('<p><a name="example-anchor"></a></p>');
|
2011-03-23 02:32:38 +01:00
|
|
|
$editor->saveInto($obj);
|
2009-10-13 04:44:10 +02:00
|
|
|
|
|
|
|
$this->assertEquals (
|
2011-03-23 02:32:38 +01:00
|
|
|
'<p><a name="example-anchor"/></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
|
|
|
|
2012-02-08 21:32:25 +01:00
|
|
|
public function testExtendMediaFormFields() {
|
2011-03-23 03:38:31 +01:00
|
|
|
if(class_exists('ThumbnailStripField')) {
|
|
|
|
$controller = new Controller();
|
2010-05-25 06:56:02 +02:00
|
|
|
|
2011-03-23 03:38:31 +01:00
|
|
|
$toolbar = new HtmlEditorField_Toolbar($controller, 'DummyToolbar');
|
2010-05-25 06:56:02 +02:00
|
|
|
|
2012-02-08 21:32:25 +01:00
|
|
|
$form = $toolbar->MediaForm();
|
|
|
|
$this->assertTrue(HtmlEditorFieldTest_DummyMediaFormFieldExtension::$update_called);
|
|
|
|
$this->assertEquals($form->Fields(), HtmlEditorFieldTest_DummyMediaFormFieldExtension::$fields);
|
2011-03-23 03:38:31 +01:00
|
|
|
} else {
|
|
|
|
$this->markTestSkipped('Test requires cms module (ThumbnailStripfield class)');
|
|
|
|
}
|
|
|
|
|
2010-05-25 06:56:02 +02:00
|
|
|
}
|
2009-10-11 02:07:13 +02:00
|
|
|
}
|
2010-05-25 06:56:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2012-02-08 21:32:25 +01: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;
|
|
|
|
|
|
|
|
public function updateImageForm($form) {
|
|
|
|
self::$update_called = true;
|
|
|
|
self::$fields = $form->Fields();
|
|
|
|
}
|
2011-03-23 04:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class HtmlEditorFieldTest_Object extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
2011-03-23 02:32:38 +01:00
|
|
|
'Title' => 'Varchar',
|
|
|
|
'Content' => 'HTMLText'
|
2011-03-23 04:32:24 +01:00
|
|
|
);
|
2010-05-25 06:56:02 +02:00
|
|
|
}
|