config/schema/cdn.data_types.schema.yml | 2 + .../Constraint/CdnStreamWrapperConstraint.php | 21 +++++++ .../CdnStreamWrapperConstraintValidator.php | 50 ++++++++++++++++ .../CdnStreamWrapperConstraintValidatorTest.php | 66 ++++++++++++++++++++++ 4 files changed, 139 insertions(+) diff --git a/config/schema/cdn.data_types.schema.yml b/config/schema/cdn.data_types.schema.yml index 77cd0dd..9f2006d 100644 --- a/config/schema/cdn.data_types.schema.yml +++ b/config/schema/cdn.data_types.schema.yml @@ -10,3 +10,5 @@ cdn.domain: cdn.stream_wrapper_scheme: type: string label: 'Stream wrapper scheme' + constraints: + CdnStreamWrapper: [] diff --git a/src/Plugin/Validation/Constraint/CdnStreamWrapperConstraint.php b/src/Plugin/Validation/Constraint/CdnStreamWrapperConstraint.php new file mode 100644 index 0000000..2c26a0a --- /dev/null +++ b/src/Plugin/Validation/Constraint/CdnStreamWrapperConstraint.php @@ -0,0 +1,21 @@ +context->buildViolation($constraint->message) + ->setParameter('%stream_wrapper', $streamWrapper) + ->setInvalidValue($streamWrapper) + ->addViolation(); + } + } + + /** + * Validates the given stream wrapper, with an exception for private. + * + * @param string $streamWrapper + * A stream wrapper configured for use in Drupal. + * + * @return bool + */ + protected static function isValidCdnStreamWrapper($streamWrapper) { + /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */ + $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager'); + $forbiddenWrappers = ['private']; + return !in_array($streamWrapper, $forbiddenWrappers) + && in_array($streamWrapper, array_keys($stream_wrapper_manager->getNames()), TRUE); + } + +} diff --git a/tests/src/Functional/Plugin/Validation/Constraint/CdnStreamWrapperConstraintValidatorTest.php b/tests/src/Functional/Plugin/Validation/Constraint/CdnStreamWrapperConstraintValidatorTest.php new file mode 100644 index 0000000..db205d3 --- /dev/null +++ b/tests/src/Functional/Plugin/Validation/Constraint/CdnStreamWrapperConstraintValidatorTest.php @@ -0,0 +1,66 @@ +prophesize(ConstraintViolationBuilderInterface::class); + $constraint_violation_builder->setParameter('%stream_wrapper', $value) + ->willReturn($constraint_violation_builder->reveal()); + $constraint_violation_builder->setInvalidValue($value) + ->willReturn($constraint_violation_builder->reveal()); + $constraint_violation_builder->addViolation() + ->willReturn($constraint_violation_builder->reveal()); + if ($valid) { + $constraint_violation_builder->addViolation()->shouldNotBeCalled(); + } + else { + $constraint_violation_builder->addViolation()->shouldBeCalled(); + } + $context = $this->prophesize(ExecutionContextInterface::class); + $context->buildViolation(Argument::type('string')) + ->willReturn($constraint_violation_builder->reveal()); + + $constraint = new CdnStreamWrapperConstraint(); + + $validate = new CdnStreamWrapperConstraintValidator(); + $validate->initialize($context->reveal()); + $validate->validate($value, $constraint); + } + + public function provideTestValidate() { + $data = []; + + $data['public, registered by default'] = ['public', TRUE]; + $data['private, prohibited by rule'] = ['private', FALSE]; + $data['unregistered'] = ['unregistered', FALSE]; + $data['test-custom'] = ['dummy-remote', TRUE]; + + return $data; + } + +}