1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CMS\Tests\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTreeLinkTracking_Parser; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\View\Parsers\HTMLValue; |
9
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
10
|
|
|
|
11
|
|
|
class SiteTreeLinkTrackingTest extends SapphireTest |
12
|
|
|
{ |
13
|
|
|
protected function setUp() : void |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
Director::config()->set('alternate_base_url', 'http://www.mysite.com/'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function isBroken($content) |
20
|
|
|
{ |
21
|
|
|
$parser = new SiteTreeLinkTracking_Parser(); |
22
|
|
|
$htmlValue = HTMLValue::create($content); |
23
|
|
|
$links = $parser->process($htmlValue); |
24
|
|
|
|
25
|
|
|
if (empty($links[0])) { |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
return $links[0]['Broken']; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testParser() |
32
|
|
|
{ |
33
|
|
|
// Shortcodes |
34
|
|
|
$this->assertTrue($this->isBroken('<a href="[sitetree_link,id=123]">link</a>')); |
35
|
|
|
$this->assertTrue($this->isBroken('<a href="[sitetree_link,id=123]#no-such-anchor">link</a>')); |
36
|
|
|
|
37
|
|
|
// Relative urls |
38
|
|
|
$this->assertTrue($this->isBroken('<a href="">link</a>')); |
39
|
|
|
$this->assertTrue($this->isBroken('<a href="/">link</a>')); |
40
|
|
|
|
41
|
|
|
// Non-shortcodes, assume non-broken without due reason |
42
|
|
|
$this->assertFalse($this->isBroken('<a href="/some-page">link</a>')); |
43
|
|
|
$this->assertFalse($this->isBroken('<a href="some-page">link</a>')); |
44
|
|
|
|
45
|
|
|
// Absolute urls |
46
|
|
|
$this->assertFalse($this->isBroken('<a href="http://www.mysite.com/some-page">link</a>')); |
47
|
|
|
$this->assertFalse($this->isBroken('<a href="http://www.google.com/some-page">link</a>')); |
48
|
|
|
|
49
|
|
|
// Anchors |
50
|
|
|
$this->assertFalse($this->isBroken('<a name="anchor">anchor</a>')); |
51
|
|
|
$this->assertFalse($this->isBroken('<a id="anchor">anchor</a>')); |
52
|
|
|
$this->assertTrue($this->isBroken('<a href="##anchor">anchor</a>')); |
53
|
|
|
|
54
|
|
|
$page = SiteTree::create(); |
55
|
|
|
$page->Content = '<a name="yes-name-anchor">name</a><a id="yes-id-anchor">id</a>'; |
56
|
|
|
$page->write(); |
57
|
|
|
|
58
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]\">link</a>")); |
59
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#yes-name-anchor\">link</a>")); |
60
|
|
|
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#yes-id-anchor\">link</a>")); |
61
|
|
|
$this->assertTrue($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#http://invalid-anchor.com\"></a>")); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function highlight($content) |
65
|
|
|
{ |
66
|
|
|
$page = SiteTree::create(); |
67
|
|
|
$page->Content = $content; |
68
|
|
|
$page->write(); |
69
|
|
|
return $page->Content; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testHighlighter() |
73
|
|
|
{ |
74
|
|
|
$content = $this->highlight('<a href="[sitetree_link,id=123]" class="existing-class">link</a>'); |
75
|
|
|
$this->assertEquals(substr_count($content, 'ss-broken'), 1, 'A ss-broken class is added to the broken link.'); |
76
|
|
|
$this->assertEquals(substr_count($content, 'existing-class'), 1, 'Existing class is not removed.'); |
77
|
|
|
|
78
|
|
|
$content = $this->highlight('<a href="[sitetree_link,id=123]">link</a>'); |
79
|
|
|
$this->assertEquals(substr_count($content, 'ss-broken'), 1, 'ss-broken class is added to the broken link.'); |
80
|
|
|
|
81
|
|
|
$otherPage = SiteTree::create(); |
82
|
|
|
$otherPage->Content = ''; |
83
|
|
|
$otherPage->write(); |
84
|
|
|
|
85
|
|
|
$content = $this->highlight( |
86
|
|
|
"<a href=\"[sitetree_link,id=$otherPage->ID]\" class=\"existing-class ss-broken ss-broken\">link</a>" |
87
|
|
|
); |
88
|
|
|
$this->assertEquals(substr_count($content, 'ss-broken'), 0, 'All ss-broken classes are removed from good link'); |
89
|
|
|
$this->assertEquals(substr_count($content, 'existing-class'), 1, 'Existing class is not removed.'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|