2009-10-11 02:07:13 +02:00
< ? php
/**
2012-04-12 08:02:46 +02:00
* @ package framework
2009-10-11 02:07:13 +02:00
* @ subpackage tests
*/
class HtmlEditorFieldTest extends FunctionalTest {
2013-03-21 19:48:54 +01:00
protected static $fixture_file = 'HtmlEditorFieldTest.yml' ;
2009-10-11 02:07:13 +02:00
2013-03-21 19:48:54 +01:00
protected static $use_draft_site = true ;
2009-10-11 02:07:13 +02:00
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 ();
2012-09-14 07:31:12 +02:00
$editor = new HtmlEditorField ( 'Content' );
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
}
public function testImageInsertion () {
2011-03-23 02:32:38 +01:00
$obj = new HtmlEditorFieldTest_Object ();
2012-09-14 07:31:12 +02:00
$editor = new HtmlEditorField ( 'Content' );
2009-10-11 02:07:13 +02:00
2013-04-06 12:48:00 +02:00
$editor -> setValue ( '<img src="assets/HTMLEditorFieldTest_example.jpg" />' );
2011-03-23 02:32:38 +01:00
$editor -> saveInto ( $obj );
2012-09-14 07:31:12 +02:00
$parser = new CSSContentParser ( $obj -> Content );
$xml = $parser -> getByXpath ( '//img' );
2012-12-17 15:46:09 +01:00
$this -> assertEquals ( '' , ( string ) $xml [ 0 ][ 'alt' ], 'Alt tags are added by default.' );
$this -> assertEquals ( '' , ( string ) $xml [ 0 ][ 'title' ], 'Title tags are added by default.' );
2012-09-14 07:31:12 +02:00
2013-04-06 12:48:00 +02:00
$editor -> setValue ( '<img src="assets/HTMLEditorFieldTest_example.jpg" alt="foo" title="bar" />' );
2011-03-23 02:32:38 +01:00
$editor -> saveInto ( $obj );
2012-09-14 07:31:12 +02:00
$parser = new CSSContentParser ( $obj -> Content );
$xml = $parser -> getByXpath ( '//img' );
2012-12-17 15:46:09 +01:00
$this -> assertEquals ( 'foo' , ( string ) $xml [ 0 ][ 'alt' ], 'Alt tags are preserved.' );
$this -> assertEquals ( 'bar' , ( string ) $xml [ 0 ][ 'title' ], 'Title tags are preserved.' );
2013-04-06 12:48:00 +02:00
$this -> assertEquals ( false , $obj -> HasBrokenFile , 'Referenced image file exists.' );
2009-10-11 02:07:13 +02:00
}
2013-04-06 12:48:00 +02:00
public function testResizedImageInsertion () {
$obj = new HtmlEditorFieldTest_Object ();
$editor = new HtmlEditorField ( 'Content' );
/*
* Following stuff is neccessary to
* a ) use the proper filename for the image we are referencing
* b ) not confuse the " existing " filesystem by our test
*/
$imageFile = $this -> objFromFixture ( 'Image' , 'example_image' );
$imageFile -> Filename = FRAMEWORK_DIR . '/' . $imageFile -> Filename ;
$origUpdateFilesystem = Config :: inst () -> get ( 'File' , 'update_filesystem' );
Config :: inst () -> update ( 'File' , 'update_filesystem' , false );
$imageFile -> write ();
Config :: inst () -> update ( 'File' , 'update_filesystem' , $origUpdateFilesystem );
/*
* End of test bet setting
*/
$editor -> setValue ( '<img src="assets/HTMLEditorFieldTest_example.jpg" width="10" height="20" />' );
$editor -> saveInto ( $obj );
$parser = new CSSContentParser ( $obj -> Content );
$xml = $parser -> getByXpath ( '//img' );
$this -> assertEquals ( '' , ( string ) $xml [ 0 ][ 'alt' ], 'Alt tags are added by default.' );
$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.' );
2013-04-11 11:59:36 +02:00
$this -> assertEquals ( 'assets/_resampled/ResizedImage10x20-HTMLEditorFieldTest_example.jpg' , ( string ) $xml [ 0 ][ 'src' ], 'Correct URL of resized image is set.' );
$this -> assertTrue ( file_exists ( 'assets/_resampled/ResizedImage10x20-HTMLEditorFieldTest_example.jpg' ), 'File for resized image exists' );
2013-04-06 12:48:00 +02:00
$this -> assertEquals ( false , $obj -> HasBrokenFile , 'Referenced image file exists.' );
}
2009-10-11 02:07:13 +02:00
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' );
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
}
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 (
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
2012-02-09 17:17:39 +01:00
public function testHtmlEditorFieldFileLocal () {
$file = new HtmlEditorField_File ( '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
}
2010-05-25 06:56:02 +02:00
2012-02-09 17:17:39 +01:00
public function testHtmlEditorFieldFileRemote () {
$fileFixture = new File ( array ( 'Name' => 'my_local_image.jpg' , 'Filename' => 'folder/my_local_image.jpg' ));
$file = new HtmlEditorField_File ( '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 );
2010-05-25 06:56:02 +02:00
}
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
*/
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 {
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
}