class LinkTypeConstraintValidatorTest
Tests LinkTypeConstraintValidator.
Attributes
#[CoversMethod(LinkTypeConstraintValidator::class, 'validate')]
#[Group('link')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkTypeConstraintValidatorTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of LinkTypeConstraintValidatorTest
File
-
core/
modules/ link/ tests/ src/ Unit/ Plugin/ Validation/ Constraint/ LinkTypeConstraintValidatorTest.php, line 23
Namespace
Drupal\Tests\link\Unit\Plugin\Validation\ConstraintView source
class LinkTypeConstraintValidatorTest extends UnitTestCase {
/**
* Validate a good internal link.
*/
public function testInternal() : void {
$url = Url::fromRoute('example.existing_route');
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->any())
->method('generateFromRoute')
->with('example.existing_route', [], [])
->willReturn('/example/existing');
$url->setUrlGenerator($urlGenerator);
$link = $this->createMock(LinkItemInterface::class);
$link->expects($this->once())
->method('getFieldDefinition')
->willReturn($this->getMockFieldDefinition(LinkItemInterface::LINK_INTERNAL));
$link->expects($this->once())
->method('getUrl')
->willReturn($url);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->never())
->method('buildViolation');
$this->doValidate($link, $context);
}
/**
* Validate an external link in an internal-only field.
*/
public function testBadInternal() : void {
$url = Url::fromUri('https://www.drupal.org');
$link = $this->createMock(LinkItemInterface::class);
$link->expects($this->once())
->method('getFieldDefinition')
->willReturn($this->getMockFieldDefinition(LinkItemInterface::LINK_INTERNAL));
$link->expects($this->once())
->method('getUrl')
->willReturn($url);
$constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
$constraintViolationBuilder->method('atPath')
->with('uri')
->willReturn($constraintViolationBuilder);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->once())
->method('buildViolation')
->willReturn($constraintViolationBuilder);
$this->doValidate($link, $context);
}
/**
* Validate a good external link.
*/
public function testExternal() : void {
$url = Url::fromUri('https://www.drupal.org');
$link = $this->createMock(LinkItemInterface::class);
$link->expects($this->once())
->method('getFieldDefinition')
->willReturn($this->getMockFieldDefinition(LinkItemInterface::LINK_EXTERNAL));
$link->expects($this->once())
->method('getUrl')
->willReturn($url);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->never())
->method('buildViolation');
$this->doValidate($link, $context);
}
/**
* Validate an internal link in an external-only field.
*/
public function testBadExternal() : void {
$url = Url::fromRoute('example.existing_route');
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->any())
->method('generateFromRoute')
->with('example.existing_route', [], [])
->willReturn('/example/existing');
$url->setUrlGenerator($urlGenerator);
$link = $this->createMock(LinkItemInterface::class);
$link->expects($this->once())
->method('getFieldDefinition')
->willReturn($this->getMockFieldDefinition(LinkItemInterface::LINK_EXTERNAL));
$link->expects($this->any())
->method('getUrl')
->willReturn($url);
$constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
$constraintViolationBuilder->method('atPath')
->with('uri')
->willReturn($constraintViolationBuilder);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->once())
->method('buildViolation');
$this->doValidate($link, $context);
}
/**
* Validate a URL that throws an exception.
*/
public function testBadUrl() : void {
$link = $this->createMock(LinkItemInterface::class);
$link->expects($this->once())
->method('getFieldDefinition')
->willReturn($this->getMockFieldDefinition(LinkItemInterface::LINK_INTERNAL));
$link->expects($this->once())
->method('getUrl')
->willThrowException(new \InvalidArgumentException());
$constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
$constraintViolationBuilder->method('atPath')
->with('uri')
->willReturn($constraintViolationBuilder);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->once())
->method('buildViolation')
->willReturn($constraintViolationBuilder);
$this->doValidate($link, $context);
}
/**
* Validate a URL in a field that accepts both internal and external URLs.
*/
public function testGeneric() : void {
$url = Url::fromRoute('example.existing_route');
$link = $this->createMock(LinkItemInterface::class);
$link->expects($this->once())
->method('getFieldDefinition')
->willReturn($this->getMockFieldDefinition(LinkItemInterface::LINK_GENERIC));
$link->expects($this->once())
->method('getUrl')
->willReturn($url);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->never())
->method('buildViolation');
$this->doValidate($link, $context);
}
/**
* Validate the link.
*/
protected function doValidate(LinkItemInterface&MockObject $link, ExecutionContextInterface&MockObject $context) : void {
$validator = new LinkTypeConstraintValidator();
$validator->initialize($context);
$validator->validate($link, new LinkTypeConstraint());
}
/**
* Builds a mock Link field definition.
*
* @param int $type
* The type of Link field as defined in LinkItemInterface.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface
* The mock field definition.
*/
protected function getMockFieldDefinition(int $type) : FieldDefinitionInterface {
$definition = $this->createMock(FieldDefinitionInterface::class);
$definition->expects($this->once())
->method('getSetting')
->with('link_type')
->willReturn($type);
return $definition;
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overrides |
|---|---|---|---|---|
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| LinkTypeConstraintValidatorTest::doValidate | protected | function | Validate the link. | |
| LinkTypeConstraintValidatorTest::getMockFieldDefinition | protected | function | Builds a mock Link field definition. | |
| LinkTypeConstraintValidatorTest::testBadExternal | public | function | Validate an internal link in an external-only field. | |
| LinkTypeConstraintValidatorTest::testBadInternal | public | function | Validate an external link in an internal-only field. | |
| LinkTypeConstraintValidatorTest::testBadUrl | public | function | Validate a URL that throws an exception. | |
| LinkTypeConstraintValidatorTest::testExternal | public | function | Validate a good external link. | |
| LinkTypeConstraintValidatorTest::testGeneric | public | function | Validate a URL in a field that accepts both internal and external URLs. | |
| LinkTypeConstraintValidatorTest::testInternal | public | function | Validate a good internal link. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setUp | protected | function | 366 | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.