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

HTML5ValueTest::testShortcodeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
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
10
/**
11
 * @package framework
12
 * @subpackage tests
13
 */
14
class HTML5ValueTest extends SapphireTest
15
{
16
    public function testInvalidHTMLParsing()
17
    {
18
        $value = new HTML5Value();
19
20
        $invalid = [
21
            '<p>Enclosed Value</p></p>'          => '<p>Enclosed Value</p>',
22
            '<p><div class="example"></div></p>' => '<p></p><div class="example"></div>'
23
        ];
24
25
        foreach ($invalid as $input => $expected) {
26
            $value->setContent($input);
27
            $this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed');
28
        }
29
    }
30
31
    public function testUtf8Saving()
32
    {
33
        $value = new HTML5Value();
34
35
        $value->setContent('<p>ö ß ā い 家</p>');
36
        $this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent());
37
    }
38
39
    public function testWhitespaceHandling()
40
    {
41
        $value = new HTML5Value();
42
43
        $value->setContent('<p></p> <p></p>');
44
        $this->assertEquals('<p></p> <p></p>', $value->getContent());
45
    }
46
47
    public function testInvalidHTMLTagNames()
48
    {
49
        $value = new HTML5Value();
50
51
        $invalid = [
52
            '<p><div><a href="test-link"></p></div>',
53
            '<html><div><a href="test-link"></a></a></html_>'
54
        ];
55
56
        foreach ($invalid as $input) {
57
            $value->setContent($input);
58
59
            $this->assertEquals(
60
                'test-link',
61
                $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

61
                $value->/** @scrutinizer ignore-call */ 
62
                        getElementsByTagName('a')->item(0)->getAttribute('href'),
Loading history...
62
                'Link data can be extraced from malformed HTML'
63
            );
64
        }
65
    }
66
67
    public function testMixedNewlines()
68
    {
69
        $value = new HTML5Value();
70
71
        $value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>");
72
        $this->assertEquals(
73
            "<p>paragraph</p>\n<ul><li>1</li>\n</ul>",
74
            $value->getContent(),
75
            'Newlines get converted'
76
        );
77
    }
78
79
    public function testShortcodeValue()
80
    {
81
        ShortcodeParser::get('default')->register(
82
            'test_shortcode',
83
            function () {
84
                return 'bit of test shortcode output';
85
            }
86
        );
87
        $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

87
        $content = DBHTMLText::create(/** @scrutinizer ignore-type */ 'Test', ['shortcodes'=>true])
Loading history...
88
            ->setValue('<p>Some content with a [test_shortcode] in it.</p>')
89
            ->forTemplate();
90
        $this->assertContains(
91
            '<p>Some content with a bit of test shortcode output in it.</p>',
92
            $content
93
        );
94
    }
95
}
96