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

testFileLinkTracking()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 34
rs 8.8571
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\Assets\Dev\TestAssetStore;
7
use SilverStripe\Assets\File;
8
use SilverStripe\Assets\Filesystem;
9
use SilverStripe\Assets\Folder;
10
use SilverStripe\CMS\Model\SiteTree;
11
use SilverStripe\Dev\CSSContentParser;
12
use SilverStripe\Dev\FunctionalTest;
13
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
14
15
class SiteTreeHTMLEditorFieldTest extends FunctionalTest
16
{
17
    protected static $fixture_file = 'SiteTreeHTMLEditorFieldTest.yml';
18
19
    public function setUp()
20
    {
21
        parent::setUp();
22
        TestAssetStore::activate('SiteTreeHTMLEditorFieldTest');
23
        $this->logInWithPermission('ADMIN');
24
25
        // Write file contents
26
        $files = File::get()->exclude('ClassName', Folder::class);
27
        foreach ($files as $file) {
28
            $destPath = TestAssetStore::getLocalPath($file);
29
            Filesystem::makeFolder(dirname($destPath));
30
            file_put_contents($destPath, str_repeat('x', 1000000));
31
        }
32
33
        // Ensure all pages are published
34
        /** @var Page $page */
35
        foreach (Page::get() as $page) {
36
            $page->publishSingle();
37
        }
38
    }
39
40
    public function tearDown()
41
    {
42
        TestAssetStore::reset();
43
        parent::tearDown();
44
    }
45
46
    public function testLinkTracking()
47
    {
48
        /** @var SiteTree $sitetree */
49
        $sitetree = $this->objFromFixture(SiteTree::class, 'home');
50
        $editor   = new HTMLEditorField('Content');
51
52
        $aboutID   = $this->idFromFixture(SiteTree::class, 'about');
53
        $contactID = $this->idFromFixture(SiteTree::class, 'contact');
54
55
        $editor->setValue("<a href=\"[sitetree_link,id=$aboutID]\">Example Link</a>");
56
        $editor->saveInto($sitetree);
57
        $sitetree->write();
58
        $this->assertEquals(array($aboutID => $aboutID), $sitetree->LinkTracking()->getIdList(), 'Basic link tracking works.');
59
60
        $editor->setValue(
61
            "<a href=\"[sitetree_link,id=$aboutID]\"></a><a href=\"[sitetree_link,id=$contactID]\"></a>"
62
        );
63
        $editor->saveInto($sitetree);
64
        $sitetree->write();
65
        $this->assertEquals(
66
            array($aboutID => $aboutID, $contactID => $contactID),
67
            $sitetree->LinkTracking()->getIdList(),
68
            'Tracking works on multiple links'
69
        );
70
71
        $editor->setValue(null);
72
        $editor->saveInto($sitetree);
73
        $sitetree->write();
74
        $this->assertEquals(array(), $sitetree->LinkTracking()->getIdList(), 'Link tracking is removed when links are.');
75
76
        // Legacy support - old CMS versions added link shortcodes with spaces instead of commas
77
        $editor->setValue("<a href=\"[sitetree_link id=$aboutID]\">Example Link</a>");
78
        $editor->saveInto($sitetree);
79
        $sitetree->write();
80
        $this->assertEquals(
81
            array($aboutID => $aboutID),
82
            $sitetree->LinkTracking()->getIdList(),
83
            'Link tracking with space instead of comma in shortcode works.'
84
        );
85
    }
86
87
    public function testImageInsertion()
88
    {
89
        $sitetree = new SiteTree();
90
        $editor   = new HTMLEditorField('Content');
91
92
        $editor->setValue('<img src="assets/example.jpg" />');
93
        $editor->saveInto($sitetree);
94
        $sitetree->write();
95
96
        $parser = new CSSContentParser($sitetree->Content);
97
        $xml = $parser->getByXpath('//img');
98
        $this->assertEquals('', (string)$xml[0]['alt'], 'Alt tags are added by default.');
99
        $this->assertEquals('', (string)$xml[0]['title'], 'Title tags are added by default.');
100
101
        $editor->setValue('<img src="assets/example.jpg" alt="foo" title="bar" />');
102
        $editor->saveInto($sitetree);
103
        $sitetree->write();
104
105
        $parser = new CSSContentParser($sitetree->Content);
106
        $xml = $parser->getByXpath('//img');
107
        $this->assertEquals('foo', (string)$xml[0]['alt'], 'Alt tags are preserved.');
108
        $this->assertEquals('bar', (string)$xml[0]['title'], 'Title tags are preserved.');
109
    }
110
111
    public function testBrokenSiteTreeLinkTracking()
112
    {
113
        $sitetree = new SiteTree();
114
        $editor   = new HTMLEditorField('Content');
115
116
        $this->assertFalse((bool) $sitetree->HasBrokenLink);
117
118
        $editor->setValue('<p><a href="[sitetree_link,id=0]">Broken Link</a></p>');
119
        $editor->saveInto($sitetree);
120
        $sitetree->write();
121
122
        $this->assertTrue($sitetree->HasBrokenLink);
123
124
        $editor->setValue(sprintf(
125
            '<p><a href="[sitetree_link,id=%d]">Working Link</a></p>',
126
            $this->idFromFixture(SiteTree::class, 'home')
127
        ));
128
        $sitetree->HasBrokenLink = false;
129
        $editor->saveInto($sitetree);
130
        $sitetree->write();
131
132
        $this->assertFalse((bool) $sitetree->HasBrokenLink);
133
    }
134
}
135