silverstripe-framework/tests/php/Forms/UrlFieldTest.php

93 lines
2.6 KiB
PHP
Raw Normal View History

2024-02-02 01:43:26 +01:00
<?php
namespace SilverStripe\Forms\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\UrlField;
use SilverStripe\Forms\RequiredFields;
2024-09-18 03:53:44 +02:00
use PHPUnit\Framework\Attributes\DataProvider;
2024-02-02 01:43:26 +01:00
class UrlFieldTest extends SapphireTest
{
2024-09-18 03:53:44 +02:00
public static function provideValidate(): array
2024-02-02 01:43:26 +01:00
{
return [
[
'url' => '',
'valid' => true,
],
[
'url' => '',
'valid' => true,
],
[
'url' => 'http://example-123.com',
'valid' => true,
],
[
'url' => 'https://example-123.com',
'valid' => true,
],
[
'url' => 'ftp://example-123.com',
'valid' => false,
],
[
'url' => 'http://example-123.com:8080',
'valid' => true,
],
[
'url' => 'http://example_with_underscore_in_host.com',
'valid' => true,
],
[
'url' => 'http://subdomain.example-123.com',
'valid' => true,
],
[
'url' => 'http://subdomain_with_underscores.example-123.com',
'valid' => true,
],
[
'url' => 'http://subdomain-with-dashes.example-123.com',
'valid' => true,
],
[
'url' => 'http://example-123.com:8080/path_with_underscores_(and)_parens-and-dashes',
'valid' => true,
],
[
'url' => 'http://example-123.com:8080/path/?query=string&some=1#fragment',
'valid' => true,
],
[
'url' => 'http://a/b/c/g;x?y#s',
'valid' => true,
],
[
'url' => 'http://a:123/b/c/g;x?y#s',
'valid' => true,
],
[
'url' => 'example-123.com',
'valid' => false,
],
[
'url' => 'nope',
'valid' => false,
],
];
}
2024-09-18 03:53:44 +02:00
#[DataProvider('provideValidate')]
public function testValidate(string $url, bool $valid)
2024-02-02 01:43:26 +01:00
{
$field = new UrlField('MyUrl');
2024-09-18 03:53:44 +02:00
$field->setValue($url);
2024-02-02 01:43:26 +01:00
$validator = new RequiredFields();
$field->validate($validator);
$expectedCount = $valid ? 0 : 1;
$this->assertEquals($expectedCount, count($validator->getErrors()));
}
}