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; |
|
|
|
|
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'), |
|
|
|
|
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]) |
|
|
|
|
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 & test & link</a>'; |
101
|
|
|
$output = new HTML5Value($content); |
102
|
|
|
$output = $output->getContent(); |
103
|
|
|
$this->assertEquals( |
104
|
|
|
'<a href="http://domain.test/path?two&vars">ampersand & test & 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&test">test link</a>'; |
124
|
|
|
$output = DBHTMLText::create('Test', ['shortcodes' => true]) |
|
|
|
|
125
|
|
|
->setValue($content) |
126
|
|
|
->forTemplate(); |
127
|
|
|
$this->assertEquals($expected, $output); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths