function LinkExternalProtocolsConstraintValidatorTest::testValidate

Tests validate.

@legacy-covers ::validate

Attributes

#[DataProvider('providerValidate')] #[RunInSeparateProcess]

File

core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidatorTest.php, line 31

Class

LinkExternalProtocolsConstraintValidatorTest
Tests Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator.

Namespace

Drupal\Tests\link\Unit\Plugin\Validation\Constraint

Code

public function testValidate($url, $valid) : void {
  $link = $this->createMock('Drupal\\link\\LinkItemInterface');
  $link->expects($this->any())
    ->method('getUrl')
    ->willReturn(Url::fromUri($url));
  $context = $this->createMock(ExecutionContextInterface::class);
  $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
  $constraintViolationBuilder->method('atPath')
    ->with('uri')
    ->willReturn($constraintViolationBuilder);
  if ($valid) {
    $context->expects($this->never())
      ->method('buildViolation');
  }
  else {
    $context->expects($this->once())
      ->method('buildViolation')
      ->willReturn($constraintViolationBuilder);
  }
  // Setup some more allowed protocols.
  UrlHelper::setAllowedProtocols([
    'http',
    'https',
    'magnet',
  ]);
  $constraint = new LinkExternalProtocolsConstraint();
  $validator = new LinkExternalProtocolsConstraintValidator();
  $validator->initialize($context);
  $validator->validate($link, $constraint);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.