Passed
Pull Request — master (#23)
by
unknown
02:23
created

HTML5ValueTest::testEntities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\HTML5\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\HTML5\HTML5Value;
7
use SilverStripe\ORM\FieldType\DBHTMLText;
8
use SilverStripe\View\Parsers\ShortcodeParser;
9
use SilverStripe\CMS\Model\RedirectorPage;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\RedirectorPage 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...
10
11
/**
12
 * @package framework
13
 * @subpackage tests
14
 */
15
class HTML5ValueTest extends SapphireTest
16
{
17
    public function testInvalidHTMLParsing()
18
    {
19
        $value = new HTML5Value();
20
21
        $invalid = [
22
            '<p>Enclosed Value</p></p>'          => '<p>Enclosed Value</p>',
23
            '<p><div class="example"></div></p>' => '<p></p><div class="example"></div>'
24
        ];
25
26
        foreach ($invalid as $input => $expected) {
27
            $value->setContent($input);
28
            $this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed');
29
        }
30
    }
31
32
    public function testUtf8Saving()
33
    {
34
        $value = new HTML5Value();
35
36
        $value->setContent('<p>ö ß ā い 家</p>');
37
        $this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent());
38
    }
39
40
    public function testWhitespaceHandling()
41
    {
42
        $value = new HTML5Value();
43
44
        $value->setContent('<p></p> <p></p>');
45
        $this->assertEquals('<p></p> <p></p>', $value->getContent());
46
    }
47
48
    public function testInvalidHTMLTagNames()
49
    {
50
        $value = new HTML5Value();
51
52
        $invalid = [
53
            '<p><div><a href="test-link"></p></div>',
54
            '<html><div><a href="test-link"></a></a></html_>'
55
        ];
56
57
        foreach ($invalid as $input) {
58
            $value->setContent($input);
59
60
            $this->assertEquals(
61
                'test-link',
62
                $value->getElementsByTagName('a')->item(0)->getAttribute('href'),
0 ignored issues
show
Bug introduced by
The method getElementsByTagName() does not exist on SilverStripe\HTML5\HTML5Value. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

62
                $value->/** @scrutinizer ignore-call */ 
63
                        getElementsByTagName('a')->item(0)->getAttribute('href'),
Loading history...
63
                'Link data can be extraced from malformed HTML'
64
            );
65
        }
66
    }
67
68
    public function testMixedNewlines()
69
    {
70
        $value = new HTML5Value();
71
72
        $value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>");
73
        $this->assertEquals(
74
            "<p>paragraph</p>\n<ul><li>1</li>\n</ul>",
75
            $value->getContent(),
76
            'Newlines get converted'
77
        );
78
    }
79
80
    public function testShortcodeValue()
81
    {
82
        ShortcodeParser::get('default')->register(
83
            'test_shortcode',
84
            function () {
85
                return 'bit of test shortcode output';
86
            }
87
        );
88
        $content = DBHTMLText::create('Test', ['shortcodes' => true])
0 ignored issues
show
Bug introduced by
'Test' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

88
        $content = DBHTMLText::create(/** @scrutinizer ignore-type */ 'Test', ['shortcodes' => true])
Loading history...
89
            ->setValue('<p>Some content with a [test_shortcode] and a <br /> followed by an <hr> in it.</p>')
90
            ->forTemplate();
91
        $this->assertContains(
92
            // hr is flow content, not phrasing content, so must be corrected to be outside the p tag.
93
            '<p>Some content with a bit of test shortcode output and a <br> followed by an </p><hr> in it.',
94
            $content
95
        );
96
    }
97
98
    public function testEntities()
99
    {
100
        $content = '<a href="http://domain.test/path?two&vars">ampersand &amp; test & link</a>';
101
        $output = new HTML5Value($content);
102
        $output = $output->getContent();
103
        $this->assertEquals(
104
            '<a href="http://domain.test/path?two&amp;vars">ampersand &amp; test &amp; link</a>',
105
            $output
106
        );
107
    }
108
109
    public function testShortcodeEntities()
110
    {
111
        if (!class_exists(RedirectorPage::class)) {
112
            $this->markTestSkipped('RedirectorPage comes via silverstripe/cms, which is not installed');
113
        }
114
115
        $page = RedirectorPage::create()->update([
116
            'Title' => 'Ampersand test',
117
            'URLSegment' => 'and-amp-semicolon',
118
            'RedirectionType' => 'External',
119
            'ExternalURL' => 'https://google.com/search?q=unit&test'
120
        ]);
121
        $page->write();
122
        $content = '[sitetree_link,id=' . $page->ID . ']test link[/sitetree_link]';
123
        $expected = '<a href="https://google.com/search?q=unit&amp;test">test link</a>';
124
        $output = DBHTMLText::create('Test', ['shortcodes' => true])
0 ignored issues
show
Bug introduced by
'Test' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

124
        $output = DBHTMLText::create(/** @scrutinizer ignore-type */ 'Test', ['shortcodes' => true])
Loading history...
125
            ->setValue($content)
126
            ->forTemplate();
127
        $this->assertEquals($expected, $output);
128
    }
129
}
130