3 namespace Drupal\Tests\link\Unit\Plugin\Validation\Constraint;
6 use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraint;
7 use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\Routing\Exception\RouteNotFoundException;
10 use Symfony\Component\Validator\Context\ExecutionContextInterface;
13 * @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator
16 class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
20 * @dataProvider providerValidate
22 public function testValidate($value, $valid) {
23 $context = $this->getMock(ExecutionContextInterface::class);
26 $context->expects($this->never())
27 ->method('addViolation');
30 $context->expects($this->once())
31 ->method('addViolation');
35 $constraint = new LinkNotExistingInternalConstraint();
37 $validator = new LinkNotExistingInternalConstraintValidator();
38 $validator->initialize($context);
39 $validator->validate($value, $constraint);
43 * Data provider for ::testValidate
45 public function providerValidate() {
49 $data[] = [Url::fromUri('https://www.drupal.org'), TRUE];
51 // Existing routed URL.
52 $url = Url::fromRoute('example.existing_route');
54 $url_generator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
55 $url_generator->expects($this->any())
56 ->method('generateFromRoute')
57 ->with('example.existing_route', [], [])
58 ->willReturn('/example/existing');
59 $url->setUrlGenerator($url_generator);
61 $data[] = [$url, TRUE];
63 // Not existing routed URL.
64 $url = Url::fromRoute('example.not_existing_route');
66 $url_generator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
67 $url_generator->expects($this->any())
68 ->method('generateFromRoute')
69 ->with('example.not_existing_route', [], [])
70 ->willThrowException(new RouteNotFoundException());
71 $url->setUrlGenerator($url_generator);
73 $data[] = [$url, FALSE];
75 foreach ($data as &$single_data) {
76 $link = $this->getMock('Drupal\link\LinkItemInterface');
77 $link->expects($this->any())
79 ->willReturn($single_data[0]);
81 $single_data[0] = $link;
90 * @see \Drupal\Core\Url::fromUri
92 public function testValidateWithMalformedUri() {
93 $link = $this->getMock('Drupal\link\LinkItemInterface');
94 $link->expects($this->any())
96 ->willThrowException(new \InvalidArgumentException());
98 $context = $this->getMock(ExecutionContextInterface::class);
99 $context->expects($this->never())
100 ->method('addViolation');
102 $constraint = new LinkNotExistingInternalConstraint();
104 $validator = new LinkNotExistingInternalConstraintValidator();
105 $validator->initialize($context);
106 $validator->validate($link, $constraint);