Verified Commit 43c31284 authored by Dave Long's avatar Dave Long
Browse files

test: #3566476 Convert expectation-less test mocks to stubs - Link module

By: dcam
(cherry picked from commit c34374f1)
parent 5f9de462
Loading
Loading
Loading
Loading
Loading
+8 −37
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@
namespace Drupal\Tests\link\Unit;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
@@ -29,21 +28,11 @@ class LinkFormatterTest extends UnitTestCase {
   * LinkItem::getUrl will throw \InvalidArgumentException.
   */
  public function testFormatterLinkItemUrlMalformed(): void {
    $entity = $this->createMock(EntityInterface::class);

    $linkItem = $this->createMock(LinkItemInterface::class);
    $exception = new \InvalidArgumentException();
    $linkItem->expects($this->any())
      ->method('getParent')
      ->willReturn($entity);
    $linkItem->expects($this->once())
      ->method('getUrl')
      ->willThrowException($exception);
    $linkItem->expects($this->any())
      ->method('__get')
      ->with('options')
      ->willReturn([]);
    $fieldDefinition = $this->createMock(FieldDefinitionInterface::class);
      ->willThrowException(new \InvalidArgumentException());
    $fieldDefinition = $this->createStub(FieldDefinitionInterface::class);
    $fieldList = new FieldItemList($fieldDefinition, '', $linkItem);

    $fieldTypePluginManager = $this->createMock(FieldTypePluginManagerInterface::class);
@@ -61,7 +50,7 @@ public function testFormatterLinkItemUrlMalformed(): void {
    \Drupal::setContainer($container);
    $fieldList->setValue([$linkItem]);

    $pathValidator = $this->createMock(PathValidatorInterface::class);
    $pathValidator = $this->createStub(PathValidatorInterface::class);
    $linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
    $elements = $linkFormatter->viewElements($fieldList, 'es');
    $this->assertEquals('link', $elements[0]['#type']);
@@ -71,21 +60,11 @@ public function testFormatterLinkItemUrlMalformed(): void {
   * Tests when LinkItem::getUrl throws an unexpected exception.
   */
  public function testFormatterLinkItemUrlUnexpectedException(): void {
    $exception = new \Exception('Unexpected!!!');

    $linkItem = $this->createMock(LinkItemInterface::class);
    $entity = $this->createMock(EntityInterface::class);
    $linkItem->expects($this->any())
      ->method('getParent')
      ->willReturn($entity);
    $linkItem->expects($this->once())
      ->method('getUrl')
      ->willThrowException($exception);
    $linkItem->expects($this->any())
      ->method('__get')
      ->with('options')
      ->willReturn([]);
    $fieldDefinition = $this->createMock(FieldDefinitionInterface::class);
      ->willThrowException(new \Exception('Unexpected!!!'));
    $fieldDefinition = $this->createStub(FieldDefinitionInterface::class);
    $fieldList = new FieldItemList($fieldDefinition, '', $linkItem);

    $fieldTypePluginManager = $this->createMock(FieldTypePluginManagerInterface::class);
@@ -97,7 +76,7 @@ public function testFormatterLinkItemUrlUnexpectedException(): void {
    \Drupal::setContainer($container);
    $fieldList->setValue([$linkItem]);

    $pathValidator = $this->createMock(PathValidatorInterface::class);
    $pathValidator = $this->createStub(PathValidatorInterface::class);
    $linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
    $this->expectException(\Exception::class);
    $this->expectExceptionMessage('Unexpected!!!');
@@ -111,18 +90,10 @@ public function testFormatterLinkItem(): void {
    $expectedUrl = Url::fromUri('route:<front>');

    $linkItem = $this->createMock(LinkItemInterface::class);
    $entity = $this->createMock(EntityInterface::class);
    $linkItem->expects($this->any())
      ->method('getParent')
      ->willReturn($entity);
    $linkItem->expects($this->once())
      ->method('getUrl')
      ->willReturn($expectedUrl);
    $linkItem->expects($this->any())
      ->method('__get')
      ->with('options')
      ->willReturn([]);
    $fieldDefinition = $this->createMock(FieldDefinitionInterface::class);
    $fieldDefinition = $this->createStub(FieldDefinitionInterface::class);
    $fieldList = new FieldItemList($fieldDefinition, '', $linkItem);

    $fieldTypePluginManager = $this->createMock(FieldTypePluginManagerInterface::class);
@@ -140,7 +111,7 @@ public function testFormatterLinkItem(): void {
    \Drupal::setContainer($container);
    $fieldList->setValue([$linkItem]);

    $pathValidator = $this->createMock(PathValidatorInterface::class);
    $pathValidator = $this->createStub(PathValidatorInterface::class);
    $linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
    $elements = $linkFormatter->viewElements($fieldList, 'zh');
    $this->assertEquals([
+10 −10
Original line number Diff line number Diff line
@@ -43,15 +43,14 @@ public function testValidate(bool $mayLinkAnyPage, bool $urlAccess, bool $valid)
    }

    // Mock a link object that returns the URL object.
    $link = $this->createMock('Drupal\link\LinkItemInterface');
    $link->expects($this->any())
      ->method('getUrl')
    $link = $this->createStub(LinkItemInterface::class);
    $link->method('getUrl')
      ->willReturn($url);

    // Mock a user object that returns a boolean indicating user access to all
    // links.
    $user = $this->createMock('Drupal\Core\Session\AccountProxyInterface');
    $user->expects($this->any())
    $user = $this->createMock(AccountProxyInterface::class);
    $user->expects($this->once())
      ->method('hasPermission')
      ->with($this->equalTo('link to any page'))
      ->willReturn($mayLinkAnyPage);
@@ -59,7 +58,8 @@ public function testValidate(bool $mayLinkAnyPage, bool $urlAccess, bool $valid)
    $context = $this->createMock(ExecutionContextInterface::class);

    $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
    $constraintViolationBuilder->method('atPath')
    $constraintViolationBuilder->expects($valid ? $this->never() : $this->once())
      ->method('atPath')
      ->with('uri')
      ->willReturn($constraintViolationBuilder);

@@ -99,9 +99,9 @@ public static function providerValidate(): \Generator {
   */
  public function testUnexpectedValue(): void {
    $this->expectException(UnexpectedValueException::class);
    $user = $this->createMock(AccountProxyInterface::class);
    $user = $this->createStub(AccountProxyInterface::class);
    $validator = new LinkAccessConstraintValidator($user);
    $context = $this->createMock(ExecutionContextInterface::class);
    $context = $this->createStub(ExecutionContextInterface::class);
    $validator->initialize($context);
    $constraint = new LinkAccessConstraint();
    $validator->validate('bad value', $constraint);
@@ -118,9 +118,9 @@ public function testEmptyField(): void {
    $link->expects($this->never())
      ->method('getUrl');

    $user = $this->createMock(AccountProxyInterface::class);
    $user = $this->createStub(AccountProxyInterface::class);
    $validator = new LinkAccessConstraintValidator($user);
    $context = $this->createMock(ExecutionContextInterface::class);
    $context = $this->createStub(ExecutionContextInterface::class);
    $validator->initialize($context);
    $constraint = new LinkAccessConstraint();
    $validator->validate($link, $constraint);
+10 −13
Original line number Diff line number Diff line
@@ -31,15 +31,14 @@ class LinkExternalProtocolsConstraintValidatorTest extends UnitTestCase {
  #[DataProvider('providerValidate')]
  #[RunInSeparateProcess]
  public function testValidate($url, $valid): void {
    $link = $this->createMock('Drupal\link\LinkItemInterface');
    $link->expects($this->any())
      ->method('getUrl')
    $link = $this->createStub(LinkItemInterface::class);
    $link->method('getUrl')
      ->willReturn(Url::fromUri($url));
    $context = $this->createMock(ExecutionContextInterface::class);

    $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
    $constraintViolationBuilder->method('atPath')
      ->with('uri')
    $constraintViolationBuilder->expects($valid ? $this->never() : $this->once())
      ->method('atPath')
      ->willReturn($constraintViolationBuilder);

    if ($valid) {
@@ -86,9 +85,8 @@ public static function providerValidate() {
   * @see \Drupal\Core\Url::fromUri
   */
  public function testValidateWithMalformedUri(): void {
    $link = $this->createMock('Drupal\link\LinkItemInterface');
    $link->expects($this->any())
      ->method('getUrl')
    $link = $this->createStub(LinkItemInterface::class);
    $link->method('getUrl')
      ->willThrowException(new \InvalidArgumentException());

    $context = $this->createMock(ExecutionContextInterface::class);
@@ -106,9 +104,8 @@ public function testValidateWithMalformedUri(): void {
   * Tests validate ignores internal urls.
   */
  public function testValidateIgnoresInternalUrls(): void {
    $link = $this->createMock('Drupal\link\LinkItemInterface');
    $link->expects($this->any())
      ->method('getUrl')
    $link = $this->createStub(LinkItemInterface::class);
    $link->method('getUrl')
      ->willReturn(Url::fromRoute('example.test'));

    $context = $this->createMock(ExecutionContextInterface::class);
@@ -128,7 +125,7 @@ public function testValidateIgnoresInternalUrls(): void {
  public function testUnexpectedValue(): void {
    $this->expectException(UnexpectedValueException::class);
    $validator = new LinkExternalProtocolsConstraintValidator();
    $context = $this->createMock(ExecutionContextInterface::class);
    $context = $this->createStub(ExecutionContextInterface::class);
    $validator->initialize($context);
    $constraint = new LinkExternalProtocolsConstraint();
    $validator->validate('bad value', $constraint);
@@ -146,7 +143,7 @@ public function testEmptyField(): void {
      ->method('getUrl');

    $validator = new LinkExternalProtocolsConstraintValidator();
    $context = $this->createMock(ExecutionContextInterface::class);
    $context = $this->createStub(ExecutionContextInterface::class);
    $validator->initialize($context);
    $constraint = new LinkExternalProtocolsConstraint();
    $validator->validate($link, $constraint);
+16 −18
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
@@ -31,9 +32,8 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
  public function testValidateFromUri(): void {
    $url = Url::fromUri('https://www.drupal.org');

    $link = $this->createMock(LinkItemInterface::class);
    $link->expects($this->any())
      ->method('getUrl')
    $link = $this->createStub(LinkItemInterface::class);
    $link->method('getUrl')
      ->willReturn($url);

    $context = $this->createMock(ExecutionContextInterface::class);
@@ -50,15 +50,14 @@ public function testValidateFromRoute(): void {
    $url = Url::fromRoute('example.existing_route');

    $urlGenerator = $this->createMock(UrlGeneratorInterface::class);
    $urlGenerator->expects($this->any())
    $urlGenerator->expects($this->once())
      ->method('generateFromRoute')
      ->with('example.existing_route', [], [])
      ->willReturn('/example/existing');
    $url->setUrlGenerator($urlGenerator);

    $link = $this->createMock(LinkItemInterface::class);
    $link->expects($this->any())
      ->method('getUrl')
    $link = $this->createStub(LinkItemInterface::class);
    $link->method('getUrl')
      ->willReturn($url);

    $context = $this->createMock(ExecutionContextInterface::class);
@@ -75,19 +74,19 @@ public function testValidateFromNonExistingRoute(): void {
    $url = Url::fromRoute('example.not_existing_route');

    $urlGenerator = $this->createMock(UrlGeneratorInterface::class);
    $urlGenerator->expects($this->any())
    $urlGenerator->expects($this->once())
      ->method('generateFromRoute')
      ->with('example.not_existing_route', [], [])
      ->willThrowException(new RouteNotFoundException());
    $url->setUrlGenerator($urlGenerator);

    $link = $this->createMock(LinkItemInterface::class);
    $link->expects($this->any())
      ->method('getUrl')
    $link = $this->createStub(LinkItemInterface::class);
    $link->method('getUrl')
      ->willReturn($url);

    $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
    $constraintViolationBuilder->method('atPath')
    $constraintViolationBuilder->expects($this->once())
      ->method('atPath')
      ->with('uri')
      ->willReturn($constraintViolationBuilder);
    $context = $this->createMock(ExecutionContextInterface::class);
@@ -104,9 +103,8 @@ public function testValidateFromNonExistingRoute(): void {
   * @see \Drupal\Core\Url::fromUri
   */
  public function testValidateWithMalformedUri(): void {
    $link = $this->createMock(LinkItemInterface::class);
    $link->expects($this->any())
      ->method('getUrl')
    $link = $this->createStub(LinkItemInterface::class);
    $link->method('getUrl')
      ->willThrowException(new \InvalidArgumentException());

    $context = $this->createMock(ExecutionContextInterface::class);
@@ -119,7 +117,7 @@ public function testValidateWithMalformedUri(): void {
  /**
   * Validate the link.
   */
  protected function validate(LinkItemInterface&MockObject $link, ExecutionContextInterface&MockObject $context): void {
  protected function validate(LinkItemInterface&Stub $link, ExecutionContextInterface&MockObject $context): void {
    $validator = new LinkNotExistingInternalConstraintValidator();
    $validator->initialize($context);
    $validator->validate($link, new LinkNotExistingInternalConstraint());
@@ -131,7 +129,7 @@ protected function validate(LinkItemInterface&MockObject $link, ExecutionContext
  public function testUnexpectedValue(): void {
    $this->expectException(UnexpectedValueException::class);
    $validator = new LinkNotExistingInternalConstraintValidator();
    $context = $this->createMock(ExecutionContextInterface::class);
    $context = $this->createStub(ExecutionContextInterface::class);
    $validator->initialize($context);
    $constraint = new LinkNotExistingInternalConstraint();
    $validator->validate('bad value', $constraint);
@@ -149,7 +147,7 @@ public function testEmptyField(): void {
      ->method('getUrl');

    $validator = new LinkNotExistingInternalConstraintValidator();
    $context = $this->createMock(ExecutionContextInterface::class);
    $context = $this->createStub(ExecutionContextInterface::class);
    $validator->initialize($context);
    $constraint = new LinkNotExistingInternalConstraint();
    $validator->validate($link, $constraint);
+5 −4
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ class LinkTitleRequiredConstraintValidatorTest extends UnitTestCase {
   */
  public function testUnexpectedValue(): void {
    $this->expectException(UnexpectedValueException::class);
    $context = $this->createMock(ExecutionContextInterface::class);
    $context = $this->createStub(ExecutionContextInterface::class);
    $this->doValidate('bad value', $context);
  }

@@ -75,7 +75,8 @@ public function testEmptyTitle(): void {
        ['title', ''],
      ]);
    $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
    $constraintViolationBuilder->method('atPath')
    $constraintViolationBuilder->expects($this->once())
      ->method('atPath')
      ->with('title')
      ->willReturn($constraintViolationBuilder);
    $context = $this->createMock(ExecutionContextInterface::class);
@@ -90,10 +91,10 @@ public function testEmptyTitle(): void {
   *
   * @param mixed $value
   *   A link field value.
   * @param \Symfony\Component\Validator\Context\ExecutionContextInterface&\PHPUnit\Framework\MockObject\MockObject $context
   * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
   *   The validation context.
   */
  protected function doValidate($value, ExecutionContextInterface&MockObject $context): void {
  protected function doValidate($value, ExecutionContextInterface $context): void {
    $validator = new LinkTitleRequiredConstraintValidator();
    $validator->initialize($context);
    $validator->validate($value, new LinkTitleRequiredConstraint());
Loading