function LinkAccessConstraintValidatorTest::testValidate

Same name in other branches
  1. 9 core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkAccessConstraintValidatorTest::testValidate()
  2. 8.9.x core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkAccessConstraintValidatorTest::testValidate()
  3. 10 core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkAccessConstraintValidatorTest::testValidate()

Tests the access validation constraint for links.

@covers ::validate @dataProvider providerValidate

File

core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php, line 26

Class

LinkAccessConstraintValidatorTest
Tests the LinkAccessConstraintValidator validator.

Namespace

Drupal\Tests\link\Unit\Plugin\Validation\Constraint

Code

public function testValidate(bool $mayLinkAnyPage, bool $urlAccess, bool $valid) : void {
    // Mock a Url object that returns a boolean indicating user access.
    $url = $this->getMockBuilder('Drupal\\Core\\Url')
        ->disableOriginalConstructor()
        ->getMock();
    if ($mayLinkAnyPage) {
        $url->expects($this->never())
            ->method('access');
    }
    else {
        $url->expects($this->once())
            ->method('access')
            ->willReturn($urlAccess);
    }
    // Mock a link object that returns the URL object.
    $link = $this->createMock('Drupal\\link\\LinkItemInterface');
    $link->expects($this->any())
        ->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())
        ->method('hasPermission')
        ->with($this->equalTo('link to any page'))
        ->willReturn($mayLinkAnyPage);
    $context = $this->createMock(ExecutionContextInterface::class);
    if ($valid) {
        $context->expects($this->never())
            ->method('addViolation');
    }
    else {
        $context->expects($this->once())
            ->method('addViolation');
    }
    $constraint = new LinkAccessConstraint();
    $validate = new LinkAccessConstraintValidator($user);
    $validate->initialize($context);
    $validate->validate($link, $constraint);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.