class LinkTypeConstraintValidatorTest

Same name and namespace in other branches
  1. 11.x core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkTypeConstraintValidatorTest.php \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkTypeConstraintValidatorTest

Tests LinkTypeConstraintValidator.

Attributes

#[CoversMethod(LinkTypeConstraintValidator::class, 'validate')] #[Group('link')]

Hierarchy

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\Constraint
View source
class LinkTypeConstraintValidatorTest extends UnitTestCase {
  
  /**
   * Validate a good internal link.
   */
  public function testInternal() : void {
    $url = Url::fromRoute('example.existing_route');
    $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->expects($this->once())
      ->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');
    $link = $this->createMock(LinkItemInterface::class);
    $link->expects($this->once())
      ->method('getFieldDefinition')
      ->willReturn($this->getMockFieldDefinition(LinkItemInterface::LINK_EXTERNAL));
    $link->method('getUrl')
      ->willReturn($url);
    $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
    $constraintViolationBuilder->expects($this->once())
      ->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 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->expects($this->once())
      ->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);
  }
  
  /**
   * Tests validating a value that isn't a LinkItemInterface.
   */
  public function testUnexpectedValue() : void {
    $this->expectException(UnexpectedValueException::class);
    $context = $this->createStub(ExecutionContextInterface::class);
    $this->doValidate('bad value', $context);
  }
  
  /**
   * Tests validating an empty Link field.
   */
  public function testEmptyField() : void {
    $link = $this->createMock(LinkItemInterface::class);
    $link->method('getFieldDefinition')
      ->willReturn($this->getMockFieldDefinition(LinkItemInterface::LINK_INTERNAL));
    $link->expects($this->once())
      ->method('isEmpty')
      ->willReturn(TRUE);
    $link->expects($this->never())
      ->method('getUrl');
    $context = $this->createStub(ExecutionContextInterface::class);
    $this->doValidate($link, $context);
  }
  
  /**
   * Validate the link.
   *
   * @param mixed $link
   *   A field value to validate.
   * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
   *   The execution context.
   */
  protected function doValidate($link, ExecutionContextInterface $context) : void {
    $validator = new LinkTypeConstraintValidator();
    $validator->initialize($context);
    $validator->validate($link, new LinkTypeConstraint());
  }
  
  /**
   * Builds a stub Link field definition.
   *
   * @param int $type
   *   The type of Link field as defined in LinkItemInterface.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface&\PHPUnit\Framework\MockObject\Stub
   *   The stub field definition.
   */
  protected function getMockFieldDefinition(int $type) : FieldDefinitionInterface&Stub {
    $definition = $this->createStub(FieldDefinitionInterface::class);
    $definition->method('getSetting')
      ->willReturn($type);
    return $definition;
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCaseTrait::$root protected property The Drupal root directory.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::getDrupalRoot Deprecated protected static function Returns the Drupal root directory. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
DrupalTestCaseTrait::setUpRoot final protected function Ensure that the $root property is set initially.
LinkTypeConstraintValidatorTest::doValidate protected function Validate the link.
LinkTypeConstraintValidatorTest::getMockFieldDefinition protected function Builds a stub 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::testEmptyField public function Tests validating an empty Link field.
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.
LinkTypeConstraintValidatorTest::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::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::setUp protected function 361
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.