2014-08-12 06:39:11 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 04:53:38 +02:00
|
|
|
namespace SilverStripe\CMS\Tests\Model;
|
2017-08-09 03:25:12 +02:00
|
|
|
|
2021-11-03 21:31:34 +01:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTreeLinkTracking_Parser;
|
2017-10-30 02:38:31 +01:00
|
|
|
use SilverStripe\Control\Director;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-04-18 00:52:51 +02:00
|
|
|
use SilverStripe\View\Parsers\HTMLValue;
|
2017-08-09 03:25:12 +02:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
class SiteTreeLinkTrackingTest extends SapphireTest
|
|
|
|
{
|
2018-04-06 05:53:57 +02:00
|
|
|
|
2021-10-27 23:40:52 +02:00
|
|
|
protected function setUp(): void
|
2017-10-30 02:38:31 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Director::config()->set('alternate_base_url', 'http://www.mysite.com/');
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
protected function isBroken($content)
|
|
|
|
{
|
|
|
|
$parser = new SiteTreeLinkTracking_Parser();
|
2017-04-18 00:52:51 +02:00
|
|
|
$htmlValue = HTMLValue::create($content);
|
2017-01-25 21:59:25 +01:00
|
|
|
$links = $parser->process($htmlValue);
|
|
|
|
|
|
|
|
if (empty($links[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $links[0]['Broken'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testParser()
|
|
|
|
{
|
2018-05-04 03:04:18 +02:00
|
|
|
SiteTree::add_extension(SiteTree::class, SiteTreeLinkTracking_Extension::class);
|
2021-11-03 21:31:34 +01:00
|
|
|
|
2017-10-30 02:38:31 +01:00
|
|
|
// Shortcodes
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertTrue($this->isBroken('<a href="[sitetree_link,id=123]">link</a>'));
|
|
|
|
$this->assertTrue($this->isBroken('<a href="[sitetree_link,id=123]#no-such-anchor">link</a>'));
|
2017-10-30 02:38:31 +01:00
|
|
|
|
|
|
|
// Relative urls
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertTrue($this->isBroken('<a href="">link</a>'));
|
|
|
|
$this->assertTrue($this->isBroken('<a href="/">link</a>'));
|
|
|
|
|
2017-10-30 02:38:31 +01:00
|
|
|
// Non-shortcodes, assume non-broken without due reason
|
|
|
|
$this->assertFalse($this->isBroken('<a href="/some-page">link</a>'));
|
|
|
|
$this->assertFalse($this->isBroken('<a href="some-page">link</a>'));
|
|
|
|
|
|
|
|
// Absolute urls
|
|
|
|
$this->assertFalse($this->isBroken('<a href="http://www.mysite.com/some-page">link</a>'));
|
|
|
|
$this->assertFalse($this->isBroken('<a href="http://www.google.com/some-page">link</a>'));
|
|
|
|
|
|
|
|
// Anchors
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertFalse($this->isBroken('<a name="anchor">anchor</a>'));
|
|
|
|
$this->assertFalse($this->isBroken('<a id="anchor">anchor</a>'));
|
|
|
|
$this->assertTrue($this->isBroken('<a href="##anchor">anchor</a>'));
|
|
|
|
|
2018-05-04 03:04:18 +02:00
|
|
|
$page = new SiteTree();
|
2017-01-25 21:59:25 +01:00
|
|
|
$page->Content = '<a name="yes-name-anchor">name</a><a id="yes-id-anchor">id</a>';
|
|
|
|
$page->write();
|
|
|
|
|
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]\">link</a>"));
|
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#yes-name-anchor\">link</a>"));
|
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#yes-id-anchor\">link</a>"));
|
|
|
|
$this->assertTrue($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#http://invalid-anchor.com\"></a>"));
|
2021-11-03 21:31:34 +01:00
|
|
|
|
|
|
|
// Anchors Via updateAnchorsOnPage Extension
|
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#extension-anchor\">link</a>"));
|
|
|
|
$this->assertTrue($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#no-such-anchor\"></a>"));
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function highlight($content)
|
|
|
|
{
|
2018-05-04 03:04:18 +02:00
|
|
|
$page = new SiteTree();
|
2017-01-25 21:59:25 +01:00
|
|
|
$page->Content = $content;
|
|
|
|
$page->write();
|
|
|
|
return $page->Content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHighlighter()
|
|
|
|
{
|
|
|
|
$content = $this->highlight('<a href="[sitetree_link,id=123]" class="existing-class">link</a>');
|
2022-04-13 07:07:59 +02:00
|
|
|
$this->assertEquals(substr_count($content ?? '', 'ss-broken'), 1, 'A ss-broken class is added to the broken link.');
|
|
|
|
$this->assertEquals(substr_count($content ?? '', 'existing-class'), 1, 'Existing class is not removed.');
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
$content = $this->highlight('<a href="[sitetree_link,id=123]">link</a>');
|
2022-04-13 07:07:59 +02:00
|
|
|
$this->assertEquals(substr_count($content ?? '', 'ss-broken'), 1, 'ss-broken class is added to the broken link.');
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-05-04 03:04:18 +02:00
|
|
|
$otherPage = new SiteTree();
|
2017-01-25 21:59:25 +01:00
|
|
|
$otherPage->Content = '';
|
|
|
|
$otherPage->write();
|
|
|
|
|
|
|
|
$content = $this->highlight(
|
|
|
|
"<a href=\"[sitetree_link,id=$otherPage->ID]\" class=\"existing-class ss-broken ss-broken\">link</a>"
|
|
|
|
);
|
2022-04-13 07:07:59 +02:00
|
|
|
$this->assertEquals(substr_count($content ?? '', 'ss-broken'), 0, 'All ss-broken classes are removed from good link');
|
|
|
|
$this->assertEquals(substr_count($content ?? '', 'existing-class'), 1, 'Existing class is not removed.');
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|