diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php index daaaa45..8444f95 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php @@ -52,7 +52,7 @@ public function validate($value, Constraint $constraint) { // Unfortunately, this cannot use filter_var() with FILTER_VALIDATE_URL, // because it doesn't correctly validate URLs. // @see http://php.net/manual/de/filter.filters.validate.php#110411 - if ($typed_data instanceof UriInterface && parse_url($value, PHP_URL_SCHEME) === FALSE) { + if ($typed_data instanceof UriInterface && parse_url($value, PHP_URL_SCHEME) === NULL) { $valid = FALSE; } // @todo: Move those to separate constraint validators. diff --git a/core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php b/core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php index def43ac..7661838 100644 --- a/core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php +++ b/core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php @@ -7,8 +7,6 @@ namespace Drupal\link\Plugin\Validation\Constraint; -use Drupal\Component\Utility\UrlHelper; -use Drupal\Core\Url; use Drupal\link\LinkItemInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidatorInterface; diff --git a/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php b/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php new file mode 100644 index 0000000..8319d5b --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php @@ -0,0 +1,83 @@ +getMockBuilder('Drupal\Core\TypedData\Validation\Metadata') + ->disableOriginalConstructor() + ->getMock(); + $metadata->expects($this->any()) + ->method('getTypedData') + ->willReturn($typed_data); + + $context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface'); + $context->expects($this->any()) + ->method('getMetadata') + ->willReturn($metadata); + + if ($valid) { + $context->expects($this->never()) + ->method('addViolation'); + } + else { + $context->expects($this->once()) + ->method('addViolation'); + } + + $constraint = new PrimitiveTypeConstraint(); + + $validate = new PrimitiveTypeConstraintValidator(); + $validate->initialize($context); + $validate->validate($value, $constraint); + } + + public function provideTestValidate() { + $data = []; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\BooleanInterface'), NULL, TRUE]; + + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\BooleanInterface'), 1, TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\BooleanInterface'), 'test', FALSE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\FloatInterface'), 1.5, TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\FloatInterface'), 'test', FALSE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\IntegerInterface'), 1, TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\IntegerInterface'), 1.5, FALSE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\IntegerInterface'), 'test', FALSE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), 'test', TRUE]; + // It is odd that 1 is a valid string. + // $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), 1, FALSE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), [], FALSE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'http://www.drupal.org', TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'https://www.drupal.org', TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'Invalid', FALSE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'entity:node/1', TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'base://', TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'user-path:', TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'public://', TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'private://', TRUE]; + $data[] = [$this->getMock('Drupal\Core\TypedData\Type\UriInterface'), 'drupal.org', FALSE]; + + return $data; + } + +}