diff --git a/code/model/SiteTreeLinkTracking.php b/code/model/SiteTreeLinkTracking.php index c5a5c11f..a58c1e12 100644 --- a/code/model/SiteTreeLinkTracking.php +++ b/code/model/SiteTreeLinkTracking.php @@ -38,24 +38,22 @@ class SiteTreeLinkTracking extends DataExtension { $href = Director::makeRelative($link->getAttribute('href')); if($href) { - if(preg_match('/\[sitetree_link,id=([0-9]+)\]/i', $href, $matches)) { - $ID = $matches[1]; + if(preg_match('/\[(sitetree|file)_link[,\s]id=([0-9]+)\]/i', $href, $matches)) { + $type = $matches[1]; + $id = $matches[2]; - // 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; + if($type === 'sitetree') { + if(SiteTree::get()->byID($id)) { + $linkedPages[] = $id; + } else { + $record->HasBrokenLink = true; + } + } else if($type === 'file') { + if(File::get()->byID($id)) { + $linkedFiles[] = $id; + } else { + $record->HasBrokenFile = true; + } } } else if($href == '' || $href[0] == '/') { $record->HasBrokenLink = true; diff --git a/tests/model/SiteTreeBrokenLinksTest.php b/tests/model/SiteTreeBrokenLinksTest.php index 67ebc029..1051d2c7 100644 --- a/tests/model/SiteTreeBrokenLinksTest.php +++ b/tests/model/SiteTreeBrokenLinksTest.php @@ -46,18 +46,6 @@ class SiteTreeBrokenLinksTest extends SapphireTest { $this->assertTrue($rp->HasBrokenLink, 'Broken redirector page IS marked as such'); } - public function testBrokenAssetLinks() { - $obj = $this->objFromFixture('Page','content'); - - $obj->Content = 'this is a broken link to a pdf file'; - $obj->syncLinkTracking(); - $this->assertTrue($obj->HasBrokenFile, 'Page has a broken file'); - - $obj->Content = 'this is not a broken file link'; - $obj->syncLinkTracking(); - $this->assertFalse($obj->HasBrokenFile, 'Page does NOT have a broken file'); - } - public function testDeletingFileMarksBackedPagesAsBroken() { // Test entry $file = new File(); @@ -65,7 +53,10 @@ class SiteTreeBrokenLinksTest extends SapphireTest { $file->write(); $obj = $this->objFromFixture('Page','content'); - $obj->Content = 'link to a pdf file'; + $obj->Content = sprintf( + '
', + $file->ID + ); $obj->write(); $this->assertTrue($obj->doPublish()); // Confirm that it isn't marked as broken to begin with diff --git a/tests/model/SiteTreeHTMLEditorFieldTest.php b/tests/model/SiteTreeHTMLEditorFieldTest.php index ee289164..daf5c77d 100644 --- a/tests/model/SiteTreeHTMLEditorFieldTest.php +++ b/tests/model/SiteTreeHTMLEditorFieldTest.php @@ -31,6 +31,16 @@ class SiteTreeHtmlEditorFieldTest extends FunctionalTest { $editor->saveInto($sitetree); $sitetree->write(); $this->assertEquals(array(), $sitetree->LinkTracking()->getIdList(), 'Link tracking is removed when links are.'); + + // Legacy support - old CMS versions added link shortcodes with spaces instead of commas + $editor->setValue("Example Link"); + $editor->saveInto($sitetree); + $sitetree->write(); + $this->assertEquals( + array($aboutID => $aboutID), + $sitetree->LinkTracking()->getIdList(), + 'Link tracking with space instead of comma in shortcode works.' + ); } public function testFileLinkTracking() { @@ -38,7 +48,10 @@ class SiteTreeHtmlEditorFieldTest extends FunctionalTest { $editor = new HtmlEditorField('Content'); $fileID = $this->idFromFixture('File', 'example_file'); - $editor->setValue('Example File'); + $editor->setValue(sprintf( + '', + $fileID + )); $editor->saveInto($sitetree); $sitetree->write(); $this->assertEquals ( @@ -49,6 +62,19 @@ class SiteTreeHtmlEditorFieldTest extends FunctionalTest { $editor->saveInto($sitetree); $sitetree->write(); $this->assertEquals(array(), $sitetree->ImageTracking()->getIdList(), 'Asset tracking is removed with links.'); + + // Legacy support - old CMS versions added link shortcodes with spaces instead of commas + $editor->setValue(sprintf( + '', + $fileID + )); + $editor->saveInto($sitetree); + $sitetree->write(); + $this->assertEquals( + array($fileID => $fileID), + $sitetree->ImageTracking()->getIDList(), + 'Link tracking with space instead of comma in shortcode works.' + ); } public function testImageInsertion() { @@ -94,7 +120,7 @@ class SiteTreeHtmlEditorFieldTest extends FunctionalTest { ); } - public function testBrokenLinkTracking() { + public function testBrokenSiteTreeLinkTracking() { $sitetree = new SiteTree(); $editor = new HtmlEditorField('Content'); @@ -117,14 +143,38 @@ class SiteTreeHtmlEditorFieldTest extends FunctionalTest { $this->assertFalse((bool) $sitetree->HasBrokenLink); } + public function testBrokenFileLinkTracking() { + $sitetree = new SiteTree(); + $editor = new HtmlEditorField('Content'); + + $this->assertFalse((bool) $sitetree->HasBrokenFile); + + $editor->setValue(''); + $editor->saveInto($sitetree); + $sitetree->write(); + + $this->assertTrue($sitetree->HasBrokenFile); + + $editor->setValue(sprintf ( + '', + $this->idFromFixture('File', 'example_file') + )); + $sitetree->HasBrokenFile = false; + $editor->saveInto($sitetree); + $sitetree->write(); + + $this->assertFalse((bool) $sitetree->HasBrokenFile); + } + public function testBrokenLinkHighlighting() { $sitetree = new SiteTree(); $editor = new HtmlEditorField('Content'); + // SiteTree link highlighting $editor->setValue('Broken Link'); $element = new SimpleXMLElement(html_entity_decode((string) new SimpleXMLElement($editor->Field()))); - $this->assertContains('ss-broken', (string) $element['class'], 'A broken link class is added to broken links'); + $this->assertContains('ss-broken', (string) $element['class'], 'A broken SiteTree link is highlighted'); $editor->setValue(sprintf ( 'Working Link', @@ -133,6 +183,20 @@ class SiteTreeHtmlEditorFieldTest extends FunctionalTest { $element = new SimpleXMLElement(html_entity_decode((string) new SimpleXMLElement($editor->Field()))); $this->assertNotContains('ss-broken', (string) $element['class']); + + // File link highlighting + $editor->setValue('Broken Link'); + + $element = new SimpleXMLElement(html_entity_decode((string) new SimpleXMLElement($editor->Field()))); + $this->assertContains('ss-broken', (string) $element['class'], 'A broken File link is highlighted'); + + $editor->setValue(sprintf ( + 'Working Link', + $this->idFromFixture('File', 'example_file') + )); + + $element = new SimpleXMLElement(html_entity_decode((string) new SimpleXMLElement($editor->Field()))); + $this->assertNotContains('ss-broken', (string) $element['class']); } }