-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2590 from acrobat/external-url-validation-break
[NodeBundle] Fix url chooser to allow # and #! urls
- Loading branch information
Showing
5 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Kunstmaan/NodeBundle/Tests/unit/Validator/Constraint/ValidExternalUrlValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\NodeBundle\Tests\unit\Validator\Constraint; | ||
|
||
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrl; | ||
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrlValidator; | ||
use Symfony\Component\Validator\Constraints\Url; | ||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
|
||
class ValidExternalUrlValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
protected function createValidator() | ||
{ | ||
return new ValidExternalUrlValidator(); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidUrls | ||
*/ | ||
public function testValidUrls($url) | ||
{ | ||
$this->validator->validate($url, new ValidExternalUrl()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
/** | ||
* @dataProvider getInvalidUrls | ||
*/ | ||
public function testInvalidUrls($url) | ||
{ | ||
$this->validator->validate($url, new ValidExternalUrl()); | ||
|
||
$this->buildViolation('This value is not a valid URL.') | ||
->setParameter('{{ value }}', '"'.$url.'"') | ||
->setCode(Url::INVALID_URL_ERROR) | ||
->assertRaised(); | ||
} | ||
|
||
public function getValidUrls() | ||
{ | ||
return [ | ||
['http://www.example.com'], | ||
['https://example.com'], | ||
['#'], | ||
['#anchor-name'], | ||
['#!'], | ||
]; | ||
} | ||
|
||
public function getInvalidUrls() | ||
{ | ||
return [ | ||
['example.com'], | ||
['www.example.com'], | ||
['!#'], | ||
['abc#anchor-name'], | ||
]; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Kunstmaan/NodeBundle/Validator/Constraint/ValidExternalUrl.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\NodeBundle\Validator\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class ValidExternalUrl extends Constraint | ||
{ | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Kunstmaan/NodeBundle/Validator/Constraint/ValidExternalUrlValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\NodeBundle\Validator\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\Url; | ||
use Symfony\Component\Validator\Constraints\UrlValidator; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
final class ValidExternalUrlValidator extends ConstraintValidator | ||
{ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (strpos($value, '#') === 0) { | ||
return; | ||
} | ||
|
||
$urlValidator = new UrlValidator(); | ||
$urlValidator->initialize($this->context); | ||
$urlValidator->validate($value, new Url()); | ||
} | ||
} |