Passed
Push — master ( 45ae27...9313e5 )
by Damian
03:54
created

SiteTreeLinkTrackingTest::pageIsBrokenFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CMS\Tests\Model;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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