Passed
Pull Request — master (#17)
by
unknown
01:19
created

SS_HTML5ValueTest::testWhitespaceHandling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

60
                $value->/** @scrutinizer ignore-call */ 
61
                        getElementsByTagName('a')->item(0)->getAttribute('href'),
Loading history...
61
                'Link data can be extraced from malformed HTML'
62
            );
63
        }
64
    }
65
66
    public function testMixedNewlines()
67
    {
68
        $value = new HTML5Value();
69
70
        $value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>");
71
        $this->assertEquals(
72
            "<p>paragraph</p>\n<ul><li>1</li>\n</ul>",
73
            $value->getContent(),
74
            'Newlines get converted'
75
        );
76
    }
77
}
78