class LinkTitleRequiredConstraintValidatorTest
Tests LinkTitleRequiredConstraintValidator::validate().
Attributes
#[CoversMethod(LinkTitleRequiredConstraintValidator::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\LinkTitleRequiredConstraintValidatorTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of LinkTitleRequiredConstraintValidatorTest
File
-
core/
modules/ link/ tests/ src/ Unit/ Plugin/ Validation/ Constraint/ LinkTitleRequiredConstraintValidatorTest.php, line 25
Namespace
Drupal\Tests\link\Unit\Plugin\Validation\ConstraintView source
class LinkTitleRequiredConstraintValidatorTest extends UnitTestCase {
/**
* Tests validating a value that isn't a LinkItemInterface.
*/
public function testUnexpectedValue() : void {
$this->expectException(UnexpectedValueException::class);
$context = $this->createMock(ExecutionContextInterface::class);
$this->doValidate('bad value', $context);
}
/**
* Tests passing a value with a non-required title.
*/
public function testTitleNotRequired(int $visibility) : void {
$link = $this->getMockLink($visibility);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->never())
->method('buildViolation');
$this->doValidate($link, $context);
}
/**
* Tests passing a value with an empty URI.
*/
public function testEmptyUri() : void {
$link = $this->getMockLink(LinkTitleVisibility::Required->value);
$link->expects($this->once())
->method('__get')
->with('uri')
->willReturn('');
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->never())
->method('buildViolation');
$this->doValidate($link, $context);
}
/**
* Tests the failure case of a value with a non-empty URI and an empty title.
*/
public function testEmptyTitle() : void {
$link = $this->getMockLink(LinkTitleVisibility::Required->value);
$link->expects($this->exactly(2))
->method('__get')
->willReturnMap([
[
'uri',
Url::fromUri('https://example.com'),
],
[
'title',
'',
],
]);
$constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
$constraintViolationBuilder->method('atPath')
->with('title')
->willReturn($constraintViolationBuilder);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->once())
->method('buildViolation')
->willReturn($constraintViolationBuilder);
$this->doValidate($link, $context);
}
/**
* Validate the field value.
*
* @param mixed $value
* A link field value.
* @param \Symfony\Component\Validator\Context\ExecutionContextInterface&\PHPUnit\Framework\MockObject\MockObject $context
* The validation context.
*/
protected function doValidate($value, ExecutionContextInterface&MockObject $context) : void {
$validator = new LinkTitleRequiredConstraintValidator();
$validator->initialize($context);
$validator->validate($value, new LinkTitleRequiredConstraint());
}
/**
* Builds a mock Link field.
*
* @param int $visibility
* The visibility of the Link title field as defined in LinkTitleVisibility.
*
* @return \Drupal\link\LinkItemInterface&\PHPUnit\Framework\MockObject\MockObject
* The mock LinkItemInterface field item.
*/
protected function getMockLink(int $visibility) : LinkItemInterface&MockObject {
$definition = $this->createMock(FieldDefinitionInterface::class);
$definition->expects($this->once())
->method('getSetting')
->with('title')
->willReturn($visibility);
$link = $this->createMock(LinkItemInterface::class);
$link->expects($this->once())
->method('getFieldDefinition')
->willReturn($definition);
return $link;
}
}
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. | |
| LinkTitleRequiredConstraintValidatorTest::doValidate | protected | function | Validate the field value. | |
| LinkTitleRequiredConstraintValidatorTest::getMockLink | protected | function | Builds a mock Link field. | |
| LinkTitleRequiredConstraintValidatorTest::testEmptyTitle | public | function | Tests the failure case of a value with a non-empty URI and an empty title. | |
| LinkTitleRequiredConstraintValidatorTest::testEmptyUri | public | function | Tests passing a value with an empty URI. | |
| LinkTitleRequiredConstraintValidatorTest::testTitleNotRequired | public | function | Tests passing a value with a non-required title. | |
| LinkTitleRequiredConstraintValidatorTest::testUnexpectedValue | public | function | Tests validating a value that isn't a LinkItemInterface. | |
| 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 | 369 | |
| 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.