SiteTreeHTMLEditorFieldTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 1
A testLinkTracking() 0 42 1
A testImageInsertion() 0 22 1
A setUp() 0 18 3
A testBrokenSiteTreeLinkTracking() 0 22 1
1
<?php
2
3
namespace SilverStripe\CMS\Tests\Model;
4
5
use SilverStripe\Assets\Dev\TestAssetStore;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Filesystem;
8
use SilverStripe\Assets\Folder;
9
use SilverStripe\CMS\Model\SiteTree;
10
use SilverStripe\Dev\CSSContentParser;
11
use SilverStripe\Dev\FunctionalTest;
12
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
13
14
class SiteTreeHTMLEditorFieldTest extends FunctionalTest
15
{
16
    protected static $fixture_file = 'SiteTreeHTMLEditorFieldTest.yml';
17
18
    protected function setUp() : void
19
    {
20
        parent::setUp();
21
        TestAssetStore::activate('SiteTreeHTMLEditorFieldTest');
22
        $this->logInWithPermission('ADMIN');
23
24
        // Write file contents
25
        $files = File::get()->exclude('ClassName', Folder::class);
26
        foreach ($files as $file) {
27
            $destPath = TestAssetStore::getLocalPath($file);
0 ignored issues
show
Bug introduced by
$file of type SilverStripe\ORM\DataObject is incompatible with the type SilverStripe\Assets\Storage\AssetContainer expected by parameter $asset of SilverStripe\Assets\Dev\...etStore::getLocalPath(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
            $destPath = TestAssetStore::getLocalPath(/** @scrutinizer ignore-type */ $file);
Loading history...
28
            Filesystem::makeFolder(dirname($destPath));
29
            file_put_contents($destPath, str_repeat('x', 1000000));
30
        }
31
32
        // Ensure all pages are published
33
        /** @var SiteTree $page */
34
        foreach (SiteTree::get() as $page) {
35
            $page->publishSingle();
36
        }
37
    }
38
39
    protected function tearDown() : void
40
    {
41
        TestAssetStore::reset();
42
        parent::tearDown();
43
    }
44
45
    public function testLinkTracking()
46
    {
47
        /** @var SiteTree $sitetree */
48
        $sitetree = $this->objFromFixture(SiteTree::class, 'home');
49
        $editor   = new HTMLEditorField('Content');
50
51
        $aboutID   = $this->idFromFixture(SiteTree::class, 'about');
52
        $contactID = $this->idFromFixture(SiteTree::class, 'contact');
53
54
        $editor->setValue("<a href=\"[sitetree_link,id=$aboutID]\">Example Link</a>");
55
        $editor->saveInto($sitetree);
56
        $sitetree->write();
57
        $this->assertEquals(
58
            [$aboutID => $aboutID],
59
            $sitetree->LinkTracking()->getIdList(),
60
            'Basic link tracking works.'
61
        );
62
63
        $editor->setValue(
64
            "<a href=\"[sitetree_link,id=$aboutID]\"></a><a href=\"[sitetree_link,id=$contactID]\"></a>"
65
        );
66
        $editor->saveInto($sitetree);
67
        $sitetree->write();
68
        $this->assertEquals(
69
            [$aboutID => $aboutID, $contactID => $contactID],
70
            $sitetree->LinkTracking()->getIdList(),
71
            'Tracking works on multiple links'
72
        );
73
74
        $editor->setValue(null);
75
        $editor->saveInto($sitetree);
76
        $sitetree->write();
77
        $this->assertEquals([], $sitetree->LinkTracking()->getIdList(), 'Link tracking is removed when links are.');
78
79
        // Legacy support - old CMS versions added link shortcodes with spaces instead of commas
80
        $editor->setValue("<a href=\"[sitetree_link id=$aboutID]\">Example Link</a>");
81
        $editor->saveInto($sitetree);
82
        $sitetree->write();
83
        $this->assertEquals(
84
            [$aboutID => $aboutID],
85
            $sitetree->LinkTracking()->getIdList(),
86
            'Link tracking with space instead of comma in shortcode works.'
87
        );
88
    }
89
90
    public function testImageInsertion()
91
    {
92
        $sitetree = SiteTree::create();
93
        $editor   = new HTMLEditorField('Content');
94
95
        $editor->setValue('<img src="assets/example.jpg" />');
96
        $editor->saveInto($sitetree);
97
        $sitetree->write();
98
99
        $parser = new CSSContentParser($sitetree->Content);
100
        $xml = $parser->getByXpath('//img');
101
        $this->assertEquals('', (string)$xml[0]['alt'], 'Alt tags are added by default.');
102
        $this->assertEquals('', (string)$xml[0]['title'], 'Title tags are added by default.');
103
104
        $editor->setValue('<img src="assets/example.jpg" alt="foo" title="bar" />');
105
        $editor->saveInto($sitetree);
106
        $sitetree->write();
107
108
        $parser = new CSSContentParser($sitetree->Content);
109
        $xml = $parser->getByXpath('//img');
110
        $this->assertEquals('foo', (string)$xml[0]['alt'], 'Alt tags are preserved.');
111
        $this->assertEquals('bar', (string)$xml[0]['title'], 'Title tags are preserved.');
112
    }
113
114
    public function testBrokenSiteTreeLinkTracking()
115
    {
116
        $sitetree = SiteTree::create();
117
        $editor   = new HTMLEditorField('Content');
118
119
        $this->assertFalse((bool) $sitetree->HasBrokenLink);
120
121
        $editor->setValue('<p><a href="[sitetree_link,id=0]">Broken Link</a></p>');
122
        $editor->saveInto($sitetree);
123
        $sitetree->write();
124
125
        $this->assertTrue($sitetree->HasBrokenLink);
126
127
        $editor->setValue(sprintf(
128
            '<p><a href="[sitetree_link,id=%d]">Working Link</a></p>',
129
            $this->idFromFixture(SiteTree::class, 'home')
130
        ));
131
        $sitetree->HasBrokenLink = false;
0 ignored issues
show
Bug introduced by
Accessing HasBrokenLink on the interface SilverStripe\ORM\DataObjectInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
132
        $editor->saveInto($sitetree);
133
        $sitetree->write();
134
135
        $this->assertFalse((bool) $sitetree->HasBrokenLink);
136
    }
137
}
138