41ecd4a87d80091fca4d185c3de7ae8a4dd727bf
[yaffs-website] / web / core / modules / link / tests / src / Unit / Plugin / Validation / Constraint / LinkNotExistingInternalConstraintValidatorTest.php
1 <?php
2
3 namespace Drupal\Tests\link\Unit\Plugin\Validation\Constraint;
4
5 use Drupal\Core\Url;
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;
11
12 /**
13  * @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator
14  * @group Link
15  */
16 class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
17
18   /**
19    * @covers ::validate
20    * @dataProvider providerValidate
21    */
22   public function testValidate($value, $valid) {
23     $context = $this->getMock(ExecutionContextInterface::class);
24
25     if ($valid) {
26       $context->expects($this->never())
27         ->method('addViolation');
28     }
29     else {
30       $context->expects($this->once())
31         ->method('addViolation');
32     }
33
34
35     $constraint = new LinkNotExistingInternalConstraint();
36
37     $validator = new LinkNotExistingInternalConstraintValidator();
38     $validator->initialize($context);
39     $validator->validate($value, $constraint);
40   }
41
42   /**
43    * Data provider for ::testValidate
44    */
45   public function providerValidate() {
46     $data = [];
47
48     // External URL
49     $data[] = [Url::fromUri('https://www.drupal.org'), TRUE];
50
51     // Existing routed URL.
52     $url = Url::fromRoute('example.existing_route');
53
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);
60
61     $data[] = [$url, TRUE];
62
63     // Not existing routed URL.
64     $url = Url::fromRoute('example.not_existing_route');
65
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);
72
73     $data[] = [$url, FALSE];
74
75     foreach ($data as &$single_data) {
76       $link = $this->getMock('Drupal\link\LinkItemInterface');
77       $link->expects($this->any())
78         ->method('getUrl')
79         ->willReturn($single_data[0]);
80
81       $single_data[0] = $link;
82     }
83
84     return $data;
85   }
86
87   /**
88    * @covers ::validate
89    *
90    * @see \Drupal\Core\Url::fromUri
91    */
92   public function testValidateWithMalformedUri() {
93     $link = $this->getMock('Drupal\link\LinkItemInterface');
94     $link->expects($this->any())
95       ->method('getUrl')
96       ->willThrowException(new \InvalidArgumentException());
97
98     $context = $this->getMock(ExecutionContextInterface::class);
99     $context->expects($this->never())
100       ->method('addViolation');
101
102     $constraint = new LinkNotExistingInternalConstraint();
103
104     $validator = new LinkNotExistingInternalConstraintValidator();
105     $validator->initialize($context);
106     $validator->validate($link, $constraint);
107   }
108
109 }