diff --git a/forms/HtmlEditorField.php b/forms/HtmlEditorField.php index 91f15430e..dd14daf99 100755 --- a/forms/HtmlEditorField.php +++ b/forms/HtmlEditorField.php @@ -72,31 +72,33 @@ class HtmlEditorField extends TextareaField { $htmlValue = new SS_HTMLValue($this->value); - // Populate link tracking for internal links & links to asset files. - if($links = $htmlValue->getElementsByTagName('a')) foreach($links as $link) { - $href = Director::makeRelative($link->getAttribute('href')); - - if($href) { - if(preg_match('/\[sitetree_link id=([0-9]+)\]/i', $href, $matches)) { - $ID = $matches[1]; - - // clear out any broken link classes - if($class = $link->getAttribute('class')) { - $link->setAttribute('class', preg_replace('/(^ss-broken|ss-broken$| ss-broken )/', null, $class)); - } - - $linkedPages[] = $ID; - if(!DataObject::get_by_id('SiteTree', $ID)) $record->HasBrokenLink = true; + if(class_exists('SiteTree')) { + // Populate link tracking for internal links & links to asset files. + if($links = $htmlValue->getElementsByTagName('a')) foreach($links as $link) { + $href = Director::makeRelative($link->getAttribute('href')); - } else if(substr($href, 0, strlen(ASSETS_DIR) + 1) == ASSETS_DIR.'/') { - $candidateFile = File::find(Convert::raw2sql(urldecode($href))); - if($candidateFile) { - $linkedFiles[] = $candidateFile->ID; - } else { - $record->HasBrokenFile = true; + if($href) { + if(preg_match('/\[sitetree_link id=([0-9]+)\]/i', $href, $matches)) { + $ID = $matches[1]; + + // clear out any broken link classes + if($class = $link->getAttribute('class')) { + $link->setAttribute('class', preg_replace('/(^ss-broken|ss-broken$| ss-broken )/', null, $class)); + } + + $linkedPages[] = $ID; + if(!DataObject::get_by_id('SiteTree', $ID)) $record->HasBrokenLink = true; + + } else if(substr($href, 0, strlen(ASSETS_DIR) + 1) == ASSETS_DIR.'/') { + $candidateFile = File::find(Convert::raw2sql(urldecode($href))); + if($candidateFile) { + $linkedFiles[] = $candidateFile->ID; + } else { + $record->HasBrokenFile = true; + } + } else if($href == '' || $href[0] == '/') { + $record->HasBrokenLink = true; } - } else if($href == '' || $href[0] == '/') { - $record->HasBrokenLink = true; } } } @@ -138,24 +140,26 @@ class HtmlEditorField extends TextareaField { } // Save file & link tracking data. - if($record->ID && $record->many_many('LinkTracking') && $tracker = $record->LinkTracking()) { - $filter = sprintf('"FieldName" = \'%s\' AND "SiteTreeID" = %d', $this->name, $record->ID); - DB::query("DELETE FROM \"$tracker->tableName\" WHERE $filter"); + if(class_exists('SiteTree')) { + if($record->ID && $record->many_many('LinkTracking') && $tracker = $record->LinkTracking()) { + $filter = sprintf('"FieldName" = \'%s\' AND "SiteTreeID" = %d', $this->name, $record->ID); + DB::query("DELETE FROM \"$tracker->tableName\" WHERE $filter"); - if($linkedPages) foreach($linkedPages as $item) { - $SQL_fieldName = Convert::raw2sql($this->name); - DB::query("INSERT INTO \"SiteTree_LinkTracking\" (\"SiteTreeID\",\"ChildID\", \"FieldName\") - VALUES ($record->ID, $item, '$SQL_fieldName')"); + if($linkedPages) foreach($linkedPages as $item) { + $SQL_fieldName = Convert::raw2sql($this->name); + DB::query("INSERT INTO \"SiteTree_LinkTracking\" (\"SiteTreeID\",\"ChildID\", \"FieldName\") + VALUES ($record->ID, $item, '$SQL_fieldName')"); + } } - } - - if($record->ID && $record->many_many('ImageTracking') && $tracker = $record->ImageTracking()) { - $filter = sprintf('"FieldName" = \'%s\' AND "SiteTreeID" = %d', $this->name, $record->ID); - DB::query("DELETE FROM \"$tracker->tableName\" WHERE $filter"); - $fieldName = $this->name; - if($linkedFiles) foreach($linkedFiles as $item) { - $tracker->add($item, array('FieldName' => $this->name)); + if($record->ID && $record->many_many('ImageTracking') && $tracker = $record->ImageTracking()) { + $filter = sprintf('"FieldName" = \'%s\' AND "SiteTreeID" = %d', $this->name, $record->ID); + DB::query("DELETE FROM \"$tracker->tableName\" WHERE $filter"); + + $fieldName = $this->name; + if($linkedFiles) foreach($linkedFiles as $item) { + $tracker->add($item, array('FieldName' => $this->name)); + } } } diff --git a/tests/forms/HtmlEditorFieldTest.php b/tests/forms/HtmlEditorFieldTest.php index 48817454b..6fc0efce4 100755 --- a/tests/forms/HtmlEditorFieldTest.php +++ b/tests/forms/HtmlEditorFieldTest.php @@ -13,69 +13,71 @@ class HtmlEditorFieldTest extends FunctionalTest { 'HtmlEditorField_Toolbar' => array('HtmlEditorFieldTest_DummyImageFormFieldExtension') ); + protected $extraDataObjects = array('HtmlEditorFieldTest_Object'); + public function testBasicSaving() { - $sitetree = new SiteTree(); + $obj = new HtmlEditorFieldTest_Object(); $editor = new HtmlEditorField('Content'); $editor->setValue('
Simple Content
'); - $editor->saveInto($sitetree); - $this->assertEquals('Simple Content
', $sitetree->Content, 'Attributes are preserved.'); + $editor->saveInto($obj); + $this->assertEquals('Simple Content
', $obj->Content, 'Attributes are preserved.'); $editor->setValue('Unclosed Tag'); - $editor->saveInto($sitetree); - $this->assertEquals('
Unclosed Tag
', $sitetree->Content, 'Unclosed tags are closed.'); + $editor->saveInto($obj); + $this->assertEquals('Unclosed Tag
', $obj->Content, 'Unclosed tags are closed.'); } public function testNullSaving() { - $sitetree = new SiteTree(); + $obj = new HtmlEditorFieldTest_Object(); $editor = new HtmlEditorField('Content'); $editor->setValue(null); - $editor->saveInto($sitetree); - $this->assertEquals('', $sitetree->Content, "Doesn't choke on empty/null values."); + $editor->saveInto($obj); + $this->assertEquals('', $obj->Content, "Doesn't choke on empty/null values."); } public function testImageInsertion() { - $sitetree = new SiteTree(); + $obj = new HtmlEditorFieldTest_Object(); $editor = new HtmlEditorField('Content'); $editor->setValue(''); - $editor->saveInto($sitetree); + $editor->saveInto($obj); - $xml = new SimpleXMLElement($sitetree->Content); + $xml = new SimpleXMLElement($obj->Content); $this->assertNotNull($xml['alt'], 'Alt tags are added by default.'); $this->assertNotNull($xml['title'], 'Title tags are added by default.'); $editor->setValue(''); - $editor->saveInto($sitetree); + $editor->saveInto($obj); - $xml = new SimpleXMLElement($sitetree->Content); + $xml = new SimpleXMLElement($obj->Content); $this->assertNotNull('foo', $xml['alt'], 'Alt tags are preserved.'); $this->assertNotNull('bar', $xml['title'], 'Title tags are preserved.'); } public function testMultiLineSaving() { - $sitetree = $this->objFromFixture('SiteTree', 'home'); + $obj = $this->objFromFixture('HtmlEditorFieldTest_Object', 'home'); $editor = new HtmlEditorField('Content'); $editor->setValue("First Paragraph
Second Paragraph
"); - $editor->saveInto($sitetree); - $this->assertEquals("First Paragraph
Second Paragraph
", $sitetree->Content); + $editor->saveInto($obj); + $this->assertEquals("First Paragraph
Second Paragraph
", $obj->Content); } public function testSavingLinksWithoutHref() { - $sitetree = $this->objFromFixture('SiteTree', 'home'); + $obj = $this->objFromFixture('HtmlEditorFieldTest_Object', 'home'); $editor = new HtmlEditorField('Content'); $editor->setValue(''); - $editor->saveInto($sitetree); + $editor->saveInto($obj); $this->assertEquals ( - '', $sitetree->Content, 'Saving a link without a href attribute works' + '', $obj->Content, 'Saving a link without a href attribute works' ); } public function testExtendImageFormFields() { - $controller = new ContentController(); + $controller = new Controller(); $toolbar = new HtmlEditorField_Toolbar($controller, 'DummyToolbar'); @@ -101,6 +103,7 @@ class HtmlEditorFieldTest_DummyImageFormFieldExtension extends Extension impleme class HtmlEditorFieldTest_Object extends DataObject implements TestOnly { static $db = array( - 'Title' => 'Varchar' + 'Title' => 'Varchar', + 'Content' => 'HTMLText' ); } \ No newline at end of file diff --git a/tests/forms/HtmlEditorFieldTest.yml b/tests/forms/HtmlEditorFieldTest.yml index 678a0d1a4..20c5e4026 100644 --- a/tests/forms/HtmlEditorFieldTest.yml +++ b/tests/forms/HtmlEditorFieldTest.yml @@ -1,3 +1,11 @@ +File: + example_file: + Name: example.pdf + +Image: + example_image: + Name: example.jpg + HtmlEditorFieldTest_Object: home: Title: Home Page