View source
<?php
namespace Drupal\Tests\link\Kernel;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
class LinkItemUrlValidationTest extends FieldKernelTestBase {
protected static $modules = [
'link',
];
public function testExternalLinkValidation() {
$definition = \Drupal::typedDataManager()
->createDataDefinition('field_item:link');
$link_item = \Drupal::typedDataManager()
->create($definition);
$test_links = $this
->getTestLinks();
foreach ($test_links as $data) {
[
$value,
$expected_violations,
] = $data;
$link_item
->setValue($value);
$violations = $link_item
->validate();
$expected_count = count($expected_violations);
$this
->assertCount($expected_count, $violations, sprintf('Violation message count error for %s', $value));
if ($expected_count) {
$i = 0;
foreach ($expected_violations as $error_msg) {
if (strpos($error_msg, '%')) {
$error_msg = sprintf($error_msg, $value);
}
$this
->assertEquals($error_msg, $violations[$i++]
->getMessage());
}
}
}
}
protected function getTestLinks() {
$violation_0 = "The path '%s' is invalid.";
$violation_1 = 'This value should be of the correct primitive type.';
return [
[
'invalid://not-a-valid-protocol',
[
$violation_0,
],
],
[
'http://www.example.com/',
[],
],
[
'http://www.example.com/strings_(string_within_parenthesis)',
[],
],
[
'http://www.example.com/numbers_(9999)',
[],
],
[
'http://www.example.com/?name=ferret&color=purple',
[],
],
[
'http://www.example.com/page?name=ferret&color=purple',
[],
],
[
'http://www.example.com?a=&b[]=c&d[]=e&d[]=f&h==',
[],
],
[
'http://www.example.com#colors',
[],
],
[
"http://foo.com/blah_blah",
[],
],
[
"http://foo.com/blah_blah/",
[],
],
[
"http://foo.com/blah_blah_(wikipedia)",
[],
],
[
"http://foo.com/blah_blah_(wikipedia)_(again)",
[],
],
[
"http://www.example.com/wpstyle/?p=364",
[],
],
[
"https://www.example.com/foo/?bar=baz&inga=42&quux",
[],
],
[
"http://✪df.ws/123",
[],
],
[
"http://userid:password@example.com:8080",
[],
],
[
"http://userid:password@example.com:8080/",
[],
],
[
"http://userid@example.com",
[],
],
[
"http://userid@example.com/",
[],
],
[
"http://userid@example.com:8080",
[],
],
[
"http://userid@example.com:8080/",
[],
],
[
"http://userid:password@example.com",
[],
],
[
"http://userid:password@example.com/",
[],
],
[
"http://➡.ws/䨹",
[],
],
[
"http://⌘.ws",
[],
],
[
"http://⌘.ws/",
[],
],
[
"http://foo.com/blah_(wikipedia)#cite-1",
[],
],
[
"http://foo.com/blah_(wikipedia)_blah#cite-1",
[],
],
[
"http://foo.com/unicode_(✪)_in_parens",
[],
],
[
"http://foo.com/(something)?after=parens",
[],
],
[
"http://☺.damowmow.com/",
[],
],
[
"http://code.google.com/events/#&product=browser",
[],
],
[
"http://j.mp",
[],
],
[
"ftp://foo.bar/baz",
[],
],
[
"http://foo.bar/?q=Test%20URL-encoded%20stuff",
[],
],
[
"http://مثال.إختبار",
[],
],
[
"http://例子.测试",
[],
],
[
"http://-.~_!\$&'()*+,;=:%40:80%2f::::::@example.com",
[],
],
[
"http://1337.net",
[],
],
[
"http://a.b-c.de",
[],
],
[
"radar://1234",
[
$violation_0,
],
],
[
"h://test",
[
$violation_0,
],
],
[
"ftps://foo.bar/",
[
$violation_0,
],
],
[
'http://',
[
$violation_0,
$violation_1,
],
],
[
"http://?",
[
$violation_0,
$violation_1,
],
],
[
"http://??",
[
$violation_0,
$violation_1,
],
],
[
"http://??/",
[
$violation_0,
$violation_1,
],
],
[
"http://#",
[
$violation_0,
$violation_1,
],
],
[
"http://##",
[
$violation_0,
$violation_1,
],
],
[
"http://##/",
[
$violation_0,
$violation_1,
],
],
[
"//",
[
$violation_0,
$violation_1,
],
],
[
"///a",
[
$violation_0,
$violation_1,
],
],
[
"///",
[
$violation_0,
$violation_1,
],
],
[
"http:///a",
[
$violation_0,
$violation_1,
],
],
];
}
}