[FEATURE] URL validator for Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / UrlValidatorTest.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 /**
19 * Test case
20 */
21 class UrlValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
22 {
23 /**
24 * @var string
25 */
26 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\UrlValidator::class;
27
28 /**
29 * @var \TYPO3\CMS\Extbase\Validation\Validator\UrlValidator
30 */
31 protected $validator;
32
33 protected function setUp()
34 {
35 $this->validator = $this->getMockBuilder($this->validatorClassName)
36 ->setMethods(['translateErrorMessage'])
37 ->getMock();
38 }
39
40 /**
41 * @return array
42 */
43 public function urlDataProvider(): array
44 {
45 return [
46 'Regular URL' => [
47 'value' => 'https://typo3.org/',
48 'isValid' => true,
49 ],
50 'Regular URL with subdomain' => [
51 'value' => 'https://testify.typo3.org/',
52 'isValid' => true,
53 ],
54 'Valid URL with trailing slash and path segment' => [
55 'value' => 'https://testify.typo3.org/testify/',
56 'isValid' => true,
57 ],
58 'Valid URL without trailing slash and path segment' => [
59 'value' => 'https://testify.typo3.org/testify',
60 'isValid' => true,
61 ],
62 'mailto' => [
63 'value' => 'mailto:foobar@example.com',
64 'isValid' => true,
65 ],
66 'mailto with subject' => [
67 'value' => 'mailto:foobar@example.com?subject=Unit+test+results',
68 'isValid' => true,
69 ],
70 'ftp' => [
71 'value' => 'ftp://remotestorage.org',
72 'isValid' => true,
73 ],
74 'tel' => [
75 'value' => 'tel:01189998819991197253',
76 'isValid' => true,
77 ],
78 'Some scheme that most likely does not exist' => [
79 'value' => 'monk://convert.wololo',
80 'isValid' => true,
81 ],
82 'Umlauts in domain' => [
83 'value' => 'https://b├╝rgerkarte.at',
84 'isValid' => true,
85 ],
86 'Domain without protocol' => [
87 'value' => 'typo3.org',
88 'isValid' => false,
89 ],
90 'Empty value' => [
91 'value' => '',
92 'isValid' => true,
93 ],
94 'Null value' => [
95 'value' => null,
96 'isValid' => true,
97 ],
98 'Invalid value is only a string' => [
99 'value' => 'testify',
100 'isValid' => false,
101 ],
102 'Invalid value is integer' => [
103 'value' => 1,
104 'isValid' => false,
105 ],
106 'Invalid value is object' => [
107 'value' => new \stdClass,
108 'isValid' => false,
109 ],
110 'Invalid value is closure' => [
111 'value' => function () {
112 },
113 'isValid' => false,
114 ],
115 ];
116 }
117
118 /**
119 * @test
120 * @dataProvider urlDataProvider
121 */
122 public function urlValidatorDetectsUrlsCorrectly($value, $expected)
123 {
124 $this->assertSame($expected, !$this->validator->validate($value)->hasErrors());
125 }
126 }