diff --git a/code/Model/SiteTreeLinkTracking_Parser.php b/code/Model/SiteTreeLinkTracking_Parser.php index ae45b0fb..16ac823b 100644 --- a/code/Model/SiteTreeLinkTracking_Parser.php +++ b/code/Model/SiteTreeLinkTracking_Parser.php @@ -68,8 +68,7 @@ class SiteTreeLinkTracking_Parser $broken = true; } elseif (!empty($matches['anchor'])) { // Ensure anchor isn't broken on target page - $anchor = preg_quote($matches['anchor'], '/'); - $broken = !preg_match("/(name|id)=\"{$anchor}\"/", $page->Content); + $broken = !in_array($matches['anchor'], $page->getAnchorsOnPage()); } else { $broken = false; } diff --git a/tests/php/Model/SiteTreeLinkTrackingTest.php b/tests/php/Model/SiteTreeLinkTrackingTest.php index 2f720f99..5d0c50c8 100644 --- a/tests/php/Model/SiteTreeLinkTrackingTest.php +++ b/tests/php/Model/SiteTreeLinkTrackingTest.php @@ -3,6 +3,7 @@ namespace SilverStripe\CMS\Tests\Model; use Page; +use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTreeLinkTracking_Parser; use SilverStripe\Control\Director; use SilverStripe\Dev\SapphireTest; @@ -31,6 +32,8 @@ class SiteTreeLinkTrackingTest extends SapphireTest public function testParser() { + SiteTree::add_extension(Page::class, SiteTreeLinkTracking_Extension::class); + // Shortcodes $this->assertTrue($this->isBroken('link')); $this->assertTrue($this->isBroken('link')); @@ -52,6 +55,7 @@ class SiteTreeLinkTrackingTest extends SapphireTest $this->assertFalse($this->isBroken('anchor')); $this->assertTrue($this->isBroken('anchor')); + $page = new Page(); $page->Content = 'nameid'; $page->write(); @@ -60,6 +64,10 @@ class SiteTreeLinkTrackingTest extends SapphireTest $this->assertFalse($this->isBroken("ID]#yes-name-anchor\">link")); $this->assertFalse($this->isBroken("ID]#yes-id-anchor\">link")); $this->assertTrue($this->isBroken("ID]#http://invalid-anchor.com\">")); + + // Anchors Via updateAnchorsOnPage Extension + $this->assertFalse($this->isBroken("ID]#extension-anchor\">link")); + $this->assertTrue($this->isBroken("ID]#no-such-anchor\">")); } protected function highlight($content) diff --git a/tests/php/Model/SiteTreeLinkTracking_Extension.php b/tests/php/Model/SiteTreeLinkTracking_Extension.php new file mode 100644 index 00000000..ab745a1a --- /dev/null +++ b/tests/php/Model/SiteTreeLinkTracking_Extension.php @@ -0,0 +1,18 @@ +