From ddaff499afa917cba2af14c075a75456c33bd2cb Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 12:56:17 +0530 Subject: [PATCH 1/5] Add Public Domain Validator --- composer.lock | 165 +++++++++++++------------ src/Domains/Validator/PublicDomain.php | 40 ++++++ tests/Validator/PublicDomainTest.php | 36 ++++++ 3 files changed, 161 insertions(+), 80 deletions(-) create mode 100644 src/Domains/Validator/PublicDomain.php create mode 100644 tests/Validator/PublicDomainTest.php diff --git a/composer.lock b/composer.lock index 4a69919..f23385c 100644 --- a/composer.lock +++ b/composer.lock @@ -8,25 +8,26 @@ "packages": [ { "name": "utopia-php/framework", - "version": "0.28.1", + "version": "0.31.x-dev", "source": { "type": "git", "url": "https://github.com/utopia-php/framework.git", - "reference": "7f22c556fc5991e54e5811a68fb39809b21bda55" + "reference": "32e18aff924149221d660a5387bccd6d2b5e9ee7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/7f22c556fc5991e54e5811a68fb39809b21bda55", - "reference": "7f22c556fc5991e54e5811a68fb39809b21bda55", + "url": "https://api.github.com/repos/utopia-php/framework/zipball/32e18aff924149221d660a5387bccd6d2b5e9ee7", + "reference": "32e18aff924149221d660a5387bccd6d2b5e9ee7", "shasum": "" }, "require": { - "php": ">=8.0.0" + "php": ">=8.0" }, "require-dev": { "laravel/pint": "^1.2", - "phpunit/phpunit": "^9.5.25", - "vimeo/psalm": "4.27.0" + "phpbench/phpbench": "^1.2", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^9.5.25" }, "type": "library", "autoload": { @@ -46,9 +47,9 @@ ], "support": { "issues": "https://github.com/utopia-php/framework/issues", - "source": "https://github.com/utopia-php/framework/tree/0.28.1" + "source": "https://github.com/utopia-php/framework/tree/0.31.x" }, - "time": "2023-03-02T08:16:01+00:00" + "time": "2023-12-08T18:46:26+00:00" } ], "packages-dev": [ @@ -58,25 +59,25 @@ "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", - "reference": "d6eef505a6c46e963e54bf73bb9de43cdea70821" + "reference": "6c0ee619435c5d4f3bc515ab1514cf4cf1006c6e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/d6eef505a6c46e963e54bf73bb9de43cdea70821", - "reference": "d6eef505a6c46e963e54bf73bb9de43cdea70821", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/6c0ee619435c5d4f3bc515ab1514cf4cf1006c6e", + "reference": "6c0ee619435c5d4f3bc515ab1514cf4cf1006c6e", "shasum": "" }, "require": { "php": "^8.1" }, "require-dev": { - "doctrine/coding-standard": "^11", + "doctrine/coding-standard": "^12", "ext-pdo": "*", "ext-phar": "*", "phpbench/phpbench": "^1.2", "phpstan/phpstan": "^1.9.4", "phpstan/phpstan-phpunit": "^1.3", - "phpunit/phpunit": "^9.5.27", + "phpunit/phpunit": "^10.5", "vimeo/psalm": "^5.4" }, "default-branch": true, @@ -121,7 +122,7 @@ "type": "tidelift" } ], - "time": "2023-01-04T15:42:40+00:00" + "time": "2023-12-09T14:19:21+00:00" }, { "name": "laravel/pint", @@ -195,12 +196,12 @@ "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "928a96f585b86224ebc78f8f09d0482cf15b04f5" + "reference": "202aaf6b7c2e1e0a622b0298e9f3f537e4d84018" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/928a96f585b86224ebc78f8f09d0482cf15b04f5", - "reference": "928a96f585b86224ebc78f8f09d0482cf15b04f5", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/202aaf6b7c2e1e0a622b0298e9f3f537e4d84018", + "reference": "202aaf6b7c2e1e0a622b0298e9f3f537e4d84018", "shasum": "" }, "require": { @@ -248,29 +249,31 @@ "type": "tidelift" } ], - "time": "2023-03-08T17:24:01+00:00" + "time": "2023-11-01T08:01:43+00:00" }, { "name": "nikic/php-parser", - "version": "4.x-dev", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "0ffddce52d816f72d0efc4d9b02e276d3309ef01" + "reference": "1eeeb2d5252d6d8706c5a8d9d88b8d1e7ecf2109" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/0ffddce52d816f72d0efc4d9b02e276d3309ef01", - "reference": "0ffddce52d816f72d0efc4d9b02e276d3309ef01", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/1eeeb2d5252d6d8706c5a8d9d88b8d1e7ecf2109", + "reference": "1eeeb2d5252d6d8706c5a8d9d88b8d1e7ecf2109", "shasum": "" }, "require": { + "ext-ctype": "*", + "ext-json": "*", "ext-tokenizer": "*", - "php": ">=7.0" + "php": ">=7.4" }, "require-dev": { "ircmaxell/php-yacc": "^0.0.7", - "phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" }, "default-branch": true, "bin": [ @@ -279,7 +282,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.9-dev" + "dev-master": "5.0-dev" } }, "autoload": { @@ -303,9 +306,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/4.x" + "source": "https://github.com/nikic/PHP-Parser/tree/master" }, - "time": "2023-03-06T22:12:36+00:00" + "time": "2023-12-22T18:57:32+00:00" }, { "name": "phar-io/manifest", @@ -313,12 +316,12 @@ "source": { "type": "git", "url": "https://github.com/phar-io/manifest.git", - "reference": "36d8a21e851a9512db2b086dc5ac2c61308f0138" + "reference": "67729272c564ab9f953c81f48db44e8b1cb1e1c3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phar-io/manifest/zipball/36d8a21e851a9512db2b086dc5ac2c61308f0138", - "reference": "36d8a21e851a9512db2b086dc5ac2c61308f0138", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/67729272c564ab9f953c81f48db44e8b1cb1e1c3", + "reference": "67729272c564ab9f953c81f48db44e8b1cb1e1c3", "shasum": "" }, "require": { @@ -327,7 +330,7 @@ "ext-phar": "*", "ext-xmlwriter": "*", "phar-io/version": "^3.0.1", - "php": "^7.2 || ^8.0" + "php": "^7.3 || ^8.0" }, "default-branch": true, "type": "library", @@ -373,7 +376,7 @@ "type": "github" } ], - "time": "2022-02-21T19:55:33+00:00" + "time": "2023-06-01T14:19:47+00:00" }, { "name": "phar-io/version", @@ -432,19 +435,19 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "443bc6912c9bd5b409254a40f4b0f4ced7c80ea1" + "reference": "ca2bd87d2f9215904682a9cb9bb37dda98e76089" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/443bc6912c9bd5b409254a40f4b0f4ced7c80ea1", - "reference": "443bc6912c9bd5b409254a40f4b0f4ced7c80ea1", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/ca2bd87d2f9215904682a9cb9bb37dda98e76089", + "reference": "ca2bd87d2f9215904682a9cb9bb37dda98e76089", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.15", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3", "phpunit/php-file-iterator": "^3.0.3", "phpunit/php-text-template": "^2.0.2", @@ -493,7 +496,8 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.26" + "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.30" }, "funding": [ { @@ -501,7 +505,7 @@ "type": "github" } ], - "time": "2023-03-06T12:58:08+00:00" + "time": "2023-12-22T06:47:57+00:00" }, { "name": "phpunit/php-file-iterator", @@ -750,12 +754,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "86e761949019ae83f49240b2f2123fb5ab3b2fc5" + "reference": "37dcc921d6b2fdf5c3b8191d6bc9c9431abb05e8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/86e761949019ae83f49240b2f2123fb5ab3b2fc5", - "reference": "86e761949019ae83f49240b2f2123fb5ab3b2fc5", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/37dcc921d6b2fdf5c3b8191d6bc9c9431abb05e8", + "reference": "37dcc921d6b2fdf5c3b8191d6bc9c9431abb05e8", "shasum": "" }, "require": { @@ -770,7 +774,7 @@ "phar-io/manifest": "^2.0.3", "phar-io/version": "^3.0.2", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.13", + "phpunit/php-code-coverage": "^9.2.28", "phpunit/php-file-iterator": "^3.0.5", "phpunit/php-invoker": "^3.1.1", "phpunit/php-text-template": "^2.0.3", @@ -828,7 +832,8 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.5" + "security": "https://github.com/sebastianbergmann/phpunit/security/policy", + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6" }, "funding": [ { @@ -844,7 +849,7 @@ "type": "tidelift" } ], - "time": "2023-03-09T06:34:10+00:00" + "time": "2023-12-22T07:28:31+00:00" }, { "name": "sebastian/cli-parser", @@ -1089,20 +1094,20 @@ }, { "name": "sebastian/complexity", - "version": "2.0.2", + "version": "2.0.x-dev", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/complexity.git", - "reference": "739b35e53379900cc9ac327b2147867b8b6efd88" + "reference": "25f207c40d62b8b7aa32f5ab026c53561964053a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/739b35e53379900cc9ac327b2147867b8b6efd88", - "reference": "739b35e53379900cc9ac327b2147867b8b6efd88", + "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/25f207c40d62b8b7aa32f5ab026c53561964053a", + "reference": "25f207c40d62b8b7aa32f5ab026c53561964053a", "shasum": "" }, "require": { - "nikic/php-parser": "^4.7", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3" }, "require-dev": { @@ -1134,7 +1139,7 @@ "homepage": "https://github.com/sebastianbergmann/complexity", "support": { "issues": "https://github.com/sebastianbergmann/complexity/issues", - "source": "https://github.com/sebastianbergmann/complexity/tree/2.0.2" + "source": "https://github.com/sebastianbergmann/complexity/tree/2.0.3" }, "funding": [ { @@ -1142,20 +1147,20 @@ "type": "github" } ], - "time": "2020-10-26T15:52:27+00:00" + "time": "2023-12-22T06:19:30+00:00" }, { "name": "sebastian/diff", - "version": "4.0.4", + "version": "4.0.x-dev", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "3461e3fccc7cfdfc2720be910d3bd73c69be590d" + "reference": "74be17022044ebaaecfdf0c5cd504fc9cd5a7131" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/3461e3fccc7cfdfc2720be910d3bd73c69be590d", - "reference": "3461e3fccc7cfdfc2720be910d3bd73c69be590d", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/74be17022044ebaaecfdf0c5cd504fc9cd5a7131", + "reference": "74be17022044ebaaecfdf0c5cd504fc9cd5a7131", "shasum": "" }, "require": { @@ -1200,7 +1205,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/diff/issues", - "source": "https://github.com/sebastianbergmann/diff/tree/4.0.4" + "source": "https://github.com/sebastianbergmann/diff/tree/4.0" }, "funding": [ { @@ -1208,7 +1213,7 @@ "type": "github" } ], - "time": "2020-10-26T13:10:38+00:00" + "time": "2023-05-07T05:35:17+00:00" }, { "name": "sebastian/environment", @@ -1356,12 +1361,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "0ca8db5a5fc9c8646244e629625ac486fa286bf2" + "reference": "bde739e7565280bda77be70044ac1047bc007e34" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/0ca8db5a5fc9c8646244e629625ac486fa286bf2", - "reference": "0ca8db5a5fc9c8646244e629625ac486fa286bf2", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bde739e7565280bda77be70044ac1047bc007e34", + "reference": "bde739e7565280bda77be70044ac1047bc007e34", "shasum": "" }, "require": { @@ -1404,7 +1409,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", - "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.5" + "source": "https://github.com/sebastianbergmann/global-state/tree/5.0" }, "funding": [ { @@ -1412,24 +1417,24 @@ "type": "github" } ], - "time": "2022-02-14T08:28:10+00:00" + "time": "2023-08-02T09:26:13+00:00" }, { "name": "sebastian/lines-of-code", - "version": "1.0.3", + "version": "1.0.x-dev", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/lines-of-code.git", - "reference": "c1c2e997aa3146983ed888ad08b15470a2e22ecc" + "reference": "e1e4a170560925c26d424b6a03aed157e7dcc5c5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/c1c2e997aa3146983ed888ad08b15470a2e22ecc", - "reference": "c1c2e997aa3146983ed888ad08b15470a2e22ecc", + "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/e1e4a170560925c26d424b6a03aed157e7dcc5c5", + "reference": "e1e4a170560925c26d424b6a03aed157e7dcc5c5", "shasum": "" }, "require": { - "nikic/php-parser": "^4.6", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3" }, "require-dev": { @@ -1461,7 +1466,7 @@ "homepage": "https://github.com/sebastianbergmann/lines-of-code", "support": { "issues": "https://github.com/sebastianbergmann/lines-of-code/issues", - "source": "https://github.com/sebastianbergmann/lines-of-code/tree/1.0.3" + "source": "https://github.com/sebastianbergmann/lines-of-code/tree/1.0.4" }, "funding": [ { @@ -1469,7 +1474,7 @@ "type": "github" } ], - "time": "2020-11-28T06:42:11+00:00" + "time": "2023-12-22T06:20:34+00:00" }, { "name": "sebastian/object-enumerator", @@ -1652,12 +1657,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "e1157eac767e4dc4ae40dd9aab7fb4de6e56bd32" + "reference": "20bdda85c7c585ab265c0c37ec052a019bae29c4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/e1157eac767e4dc4ae40dd9aab7fb4de6e56bd32", - "reference": "e1157eac767e4dc4ae40dd9aab7fb4de6e56bd32", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/20bdda85c7c585ab265c0c37ec052a019bae29c4", + "reference": "20bdda85c7c585ab265c0c37ec052a019bae29c4", "shasum": "" }, "require": { @@ -1699,7 +1704,7 @@ "type": "github" } ], - "time": "2023-02-08T06:53:39+00:00" + "time": "2023-03-25T08:11:39+00:00" }, { "name": "sebastian/type", @@ -1812,16 +1817,16 @@ }, { "name": "theseer/tokenizer", - "version": "1.2.1", + "version": "1.2.2", "source": { "type": "git", "url": "https://github.com/theseer/tokenizer.git", - "reference": "34a41e998c2183e22995f158c581e7b5e755ab9e" + "reference": "b2ad5003ca10d4ee50a12da31de12a5774ba6b96" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/theseer/tokenizer/zipball/34a41e998c2183e22995f158c581e7b5e755ab9e", - "reference": "34a41e998c2183e22995f158c581e7b5e755ab9e", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/b2ad5003ca10d4ee50a12da31de12a5774ba6b96", + "reference": "b2ad5003ca10d4ee50a12da31de12a5774ba6b96", "shasum": "" }, "require": { @@ -1850,7 +1855,7 @@ "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", "support": { "issues": "https://github.com/theseer/tokenizer/issues", - "source": "https://github.com/theseer/tokenizer/tree/1.2.1" + "source": "https://github.com/theseer/tokenizer/tree/1.2.2" }, "funding": [ { @@ -1858,7 +1863,7 @@ "type": "github" } ], - "time": "2021-07-28T10:34:58+00:00" + "time": "2023-11-20T00:12:19+00:00" } ], "aliases": [], diff --git a/src/Domains/Validator/PublicDomain.php b/src/Domains/Validator/PublicDomain.php new file mode 100644 index 0000000..844642f --- /dev/null +++ b/src/Domains/Validator/PublicDomain.php @@ -0,0 +1,40 @@ +allowedSchemes) && !\in_array(\parse_url($value, PHP_URL_SCHEME), $this->allowedSchemes)) { + return false; + } + + $domain = new Domain(\parse_url($value, PHP_URL_HOST)); + if (!$domain->isKnown()) { + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/tests/Validator/PublicDomainTest.php b/tests/Validator/PublicDomainTest.php new file mode 100644 index 0000000..523d8eb --- /dev/null +++ b/tests/Validator/PublicDomainTest.php @@ -0,0 +1,36 @@ +url = new URL(); + } + + public function tearDown(): void + { + $this->url = null; + } + + public function testIsValid(): void + { + $this->assertEquals('Value must be a valid URL', $this->url->getDescription()); + $this->assertEquals(true, $this->url->isValid('http://example.com')); + $this->assertEquals(true, $this->url->isValid('https://example.com')); + $this->assertEquals(true, $this->url->isValid('htts://example.com')); // does not validate protocol + $this->assertEquals(false, $this->url->isValid('example.com')); // though, requires some kind of protocol + $this->assertEquals(false, $this->url->isValid('http:/example.com')); + $this->assertEquals(true, $this->url->isValid('http://exa-mple.com')); + $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); + $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); + $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); + $this->assertEquals(false, $this->url->isValid('sub.demo.localhost')); + } +} \ No newline at end of file From b6124e5e1de4056e85817b45b8c60ff09c0a8726 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 13:02:53 +0530 Subject: [PATCH 2/5] Add more tests --- tests/Validator/PublicDomainTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Validator/PublicDomainTest.php b/tests/Validator/PublicDomainTest.php index 523d8eb..2a64eaa 100644 --- a/tests/Validator/PublicDomainTest.php +++ b/tests/Validator/PublicDomainTest.php @@ -3,15 +3,14 @@ namespace Utopia\Domains\Validator; use PHPUnit\Framework\TestCase; -use Utopia\Validator\URL; class PublicDomainTest extends TestCase { - protected ?URL $url; + protected ?PublicDomain $url; public function setUp(): void { - $this->url = new URL(); + $this->url = new PublicDomain(); } public function tearDown(): void @@ -31,6 +30,8 @@ public function testIsValid(): void $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); - $this->assertEquals(false, $this->url->isValid('sub.demo.localhost')); + $this->assertEquals(false, $this->url->isValid('http://sub.demo.localhost')); + $this->assertEquals(true, $this->url->isValid('https://sub.example.com.nom.br')); + $this->assertEquals(false, $this->url->isValid('http://localhost')); } } \ No newline at end of file From 86c6cb07a6ef26edf164df5a9984e2728a8a4004 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 13:22:21 +0530 Subject: [PATCH 3/5] Fix formatting --- src/Domains/Validator/PublicDomain.php | 2 +- tests/Validator/PublicDomainTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Domains/Validator/PublicDomain.php b/src/Domains/Validator/PublicDomain.php index 844642f..6510c13 100644 --- a/src/Domains/Validator/PublicDomain.php +++ b/src/Domains/Validator/PublicDomain.php @@ -37,4 +37,4 @@ public function isValid($value): bool return true; } -} \ No newline at end of file +} diff --git a/tests/Validator/PublicDomainTest.php b/tests/Validator/PublicDomainTest.php index 2a64eaa..3a1ad99 100644 --- a/tests/Validator/PublicDomainTest.php +++ b/tests/Validator/PublicDomainTest.php @@ -34,4 +34,4 @@ public function testIsValid(): void $this->assertEquals(true, $this->url->isValid('https://sub.example.com.nom.br')); $this->assertEquals(false, $this->url->isValid('http://localhost')); } -} \ No newline at end of file +} From 8e83a86bd5342625d549c0b591fe0525c02c9908 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 14:23:41 +0530 Subject: [PATCH 4/5] Add more examples --- src/Domains/Validator/PublicDomain.php | 54 +++++++++++++++++++------- tests/Validator/PublicDomainTest.php | 32 +++++++-------- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/Domains/Validator/PublicDomain.php b/src/Domains/Validator/PublicDomain.php index 6510c13..fe7c831 100644 --- a/src/Domains/Validator/PublicDomain.php +++ b/src/Domains/Validator/PublicDomain.php @@ -3,38 +3,66 @@ namespace Utopia\Domains\Validator; use Utopia\Domains\Domain; -use Utopia\Validator\URL; +use Utopia\Validator; /** * PublicDomain * - * Validate that a URL has a public domain + * Validate that a domain is a public domain */ -class PublicDomain extends URL +class PublicDomain extends Validator { + /** + * Get Description + * + * Returns validator description + * + * @return string + */ + public function getDescription(): string + { + return 'Value must be a public domain'; + } + /** * Is valid * - * Validation will pass when $value is valid URL and has a public domain + * Validation will pass when $value is a public domain * * @param mixed $value * @return bool */ public function isValid($value): bool { - if (\filter_var($value, FILTER_VALIDATE_URL) === false) { - return false; - } - - if (!empty($this->allowedSchemes) && !\in_array(\parse_url($value, PHP_URL_SCHEME), $this->allowedSchemes)) { - return false; - } - - $domain = new Domain(\parse_url($value, PHP_URL_HOST)); + $domain = new Domain($value); if (!$domain->isKnown()) { return false; } return true; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/tests/Validator/PublicDomainTest.php b/tests/Validator/PublicDomainTest.php index 3a1ad99..54a47a6 100644 --- a/tests/Validator/PublicDomainTest.php +++ b/tests/Validator/PublicDomainTest.php @@ -6,32 +6,32 @@ class PublicDomainTest extends TestCase { - protected ?PublicDomain $url; + protected ?PublicDomain $domain; public function setUp(): void { - $this->url = new PublicDomain(); + $this->domain = new PublicDomain(); } public function tearDown(): void { - $this->url = null; + $this->domain = null; } public function testIsValid(): void { - $this->assertEquals('Value must be a valid URL', $this->url->getDescription()); - $this->assertEquals(true, $this->url->isValid('http://example.com')); - $this->assertEquals(true, $this->url->isValid('https://example.com')); - $this->assertEquals(true, $this->url->isValid('htts://example.com')); // does not validate protocol - $this->assertEquals(false, $this->url->isValid('example.com')); // though, requires some kind of protocol - $this->assertEquals(false, $this->url->isValid('http:/example.com')); - $this->assertEquals(true, $this->url->isValid('http://exa-mple.com')); - $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); - $this->assertEquals(false, $this->url->isValid('http://sub.demo.localhost')); - $this->assertEquals(true, $this->url->isValid('https://sub.example.com.nom.br')); - $this->assertEquals(false, $this->url->isValid('http://localhost')); + $this->assertEquals('Value must be a public domain', $this->domain->getDescription()); + $this->assertEquals(true, $this->domain->isValid('example.com')); + $this->assertEquals(true, $this->domain->isValid('google.com')); + $this->assertEquals(true, $this->domain->isValid('bbc.co.uk')); + $this->assertEquals(true, $this->domain->isValid('appwrite.io')); + $this->assertEquals(true, $this->domain->isValid('usa.gov')); + $this->assertEquals(true, $this->domain->isValid('stanford.edu')); + $this->assertEquals(false, $this->domain->isValid('localhost')); + $this->assertEquals(false, $this->domain->isValid('sub.demo.localhost')); + $this->assertEquals(false, $this->domain->isValid('test.app.internal')); + $this->assertEquals(false, $this->domain->isValid('home.local')); + $this->assertEquals(false, $this->domain->isValid('qa.testing.internal')); + $this->assertEquals(false, $this->domain->isValid('wiki.team.local')); } } From 86f2b895ef545237dd8bf722200145ca02634e0d Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 14:30:11 +0530 Subject: [PATCH 5/5] Added test for example.test --- tests/Validator/PublicDomainTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Validator/PublicDomainTest.php b/tests/Validator/PublicDomainTest.php index 54a47a6..c24c615 100644 --- a/tests/Validator/PublicDomainTest.php +++ b/tests/Validator/PublicDomainTest.php @@ -33,5 +33,6 @@ public function testIsValid(): void $this->assertEquals(false, $this->domain->isValid('home.local')); $this->assertEquals(false, $this->domain->isValid('qa.testing.internal')); $this->assertEquals(false, $this->domain->isValid('wiki.team.local')); + $this->assertEquals(false, $this->domain->isValid('example.test')); } }