Issue #3439079 by mondrake: Change remaining Link module test dataproviders to static

merge-requests/6743/merge
catch 2024-04-08 17:31:43 +01:00
parent f79817a4be
commit 0186f88691
2 changed files with 90 additions and 93 deletions

View File

@ -20,19 +20,39 @@ class LinkAccessConstraintValidatorTest extends UnitTestCase {
/** /**
* Tests the access validation constraint for links. * Tests the access validation constraint for links.
* *
* @param \Drupal\link\LinkItemInterface $value
* The link item.
* @param \Drupal\Core\Session\AccountProxyInterface $user
* The user account.
* @param bool $valid
* A boolean indicating if the combination is expected to be valid.
*
* @covers ::validate * @covers ::validate
* @dataProvider providerValidate * @dataProvider providerValidate
*/ */
public function testValidate($value, $user, $valid) { public function testValidate(bool $mayLinkAnyPage, bool $urlAccess, bool $valid): void {
$context = $this->createMock(ExecutionContextInterface::class); // 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) { if ($valid) {
$context->expects($this->never()) $context->expects($this->never())
->method('addViolation'); ->method('addViolation');
@ -43,10 +63,9 @@ class LinkAccessConstraintValidatorTest extends UnitTestCase {
} }
$constraint = new LinkAccessConstraint(); $constraint = new LinkAccessConstraint();
$validate = new LinkAccessConstraintValidator($user); $validate = new LinkAccessConstraintValidator($user);
$validate->initialize($context); $validate->initialize($context);
$validate->validate($value, $constraint); $validate->validate($link, $constraint);
} }
/** /**
@ -57,41 +76,11 @@ class LinkAccessConstraintValidatorTest extends UnitTestCase {
* *
* @see \Drupal\Tests\link\LinkAccessConstraintValidatorTest::validate() * @see \Drupal\Tests\link\LinkAccessConstraintValidatorTest::validate()
*/ */
public function providerValidate() { public static function providerValidate(): \Generator {
$data = []; yield [TRUE, TRUE, TRUE];
yield [TRUE, FALSE, TRUE];
$cases = [ yield [FALSE, TRUE, TRUE];
['may_link_any_page' => TRUE, 'url_access' => TRUE, 'valid' => TRUE], yield [FALSE, FALSE, FALSE];
['may_link_any_page' => TRUE, 'url_access' => FALSE, 'valid' => TRUE],
['may_link_any_page' => FALSE, 'url_access' => TRUE, 'valid' => TRUE],
['may_link_any_page' => FALSE, 'url_access' => FALSE, 'valid' => FALSE],
];
foreach ($cases as $case) {
// Mock a Url object that returns a boolean indicating user access.
$url = $this->getMockBuilder('Drupal\Core\Url')
->disableOriginalConstructor()
->getMock();
$url->expects($this->once())
->method('access')
->willReturn($case['url_access']);
// 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($case['may_link_any_page']);
$data[] = [$link, $user, $case['valid']];
}
return $data;
} }
} }

View File

@ -4,10 +4,13 @@ declare(strict_types=1);
namespace Drupal\Tests\link\Unit\Plugin\Validation\Constraint; namespace Drupal\Tests\link\Unit\Plugin\Validation\Constraint;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\link\LinkItemInterface;
use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraint; use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraint;
use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator; use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Routing\Exception\RouteNotFoundException; use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Context\ExecutionContextInterface;
@ -19,70 +22,70 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
/** /**
* @covers ::validate * @covers ::validate
* @dataProvider providerValidate
*/ */
public function testValidate($value, $valid) { public function testValidateFromUri(): void {
$context = $this->createMock(ExecutionContextInterface::class); $url = Url::fromUri('https://www.drupal.org');
if ($valid) { $link = $this->createMock(LinkItemInterface::class);
$link->expects($this->any())
->method('getUrl')
->willReturn($url);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->never()) $context->expects($this->never())
->method('addViolation'); ->method('addViolation');
}
else {
$context->expects($this->once())
->method('addViolation');
}
$constraint = new LinkNotExistingInternalConstraint(); $this->validate($link, $context);
$validator = new LinkNotExistingInternalConstraintValidator();
$validator->initialize($context);
$validator->validate($value, $constraint);
} }
/** /**
* Data provider for ::testValidate. * @covers ::validate
*/ */
public function providerValidate() { public function testValidateFromRoute(): void {
$data = [];
// External URL
$data[] = [Url::fromUri('https://www.drupal.org'), TRUE];
// Existing routed URL.
$url = Url::fromRoute('example.existing_route'); $url = Url::fromRoute('example.existing_route');
$url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface'); $urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$url_generator->expects($this->any()) $urlGenerator->expects($this->any())
->method('generateFromRoute') ->method('generateFromRoute')
->with('example.existing_route', [], []) ->with('example.existing_route', [], [])
->willReturn('/example/existing'); ->willReturn('/example/existing');
$url->setUrlGenerator($url_generator); $url->setUrlGenerator($urlGenerator);
$data[] = [$url, TRUE]; $link = $this->createMock(LinkItemInterface::class);
// Non-existent routed URL.
$url = Url::fromRoute('example.not_existing_route');
$url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
$url_generator->expects($this->any())
->method('generateFromRoute')
->with('example.not_existing_route', [], [])
->willThrowException(new RouteNotFoundException());
$url->setUrlGenerator($url_generator);
$data[] = [$url, FALSE];
foreach ($data as &$single_data) {
$link = $this->createMock('Drupal\link\LinkItemInterface');
$link->expects($this->any()) $link->expects($this->any())
->method('getUrl') ->method('getUrl')
->willReturn($single_data[0]); ->willReturn($url);
$single_data[0] = $link; $context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->never())
->method('addViolation');
$this->validate($link, $context);
} }
return $data; /**
* @covers ::validate
*/
public function testValidateFromNonExistingRoute(): void {
$url = Url::fromRoute('example.not_existing_route');
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->any())
->method('generateFromRoute')
->with('example.not_existing_route', [], [])
->willReturn(new RouteNotFoundException());
$url->setUrlGenerator($urlGenerator);
$link = $this->createMock(LinkItemInterface::class);
$link->expects($this->any())
->method('getUrl')
->willReturn($url);
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->never())
->method('addViolation');
$this->validate($link, $context);
} }
/** /**
@ -90,8 +93,8 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
* *
* @see \Drupal\Core\Url::fromUri * @see \Drupal\Core\Url::fromUri
*/ */
public function testValidateWithMalformedUri() { public function testValidateWithMalformedUri(): void {
$link = $this->createMock('Drupal\link\LinkItemInterface'); $link = $this->createMock(LinkItemInterface::class);
$link->expects($this->any()) $link->expects($this->any())
->method('getUrl') ->method('getUrl')
->willThrowException(new \InvalidArgumentException()); ->willThrowException(new \InvalidArgumentException());
@ -100,11 +103,16 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
$context->expects($this->never()) $context->expects($this->never())
->method('addViolation'); ->method('addViolation');
$constraint = new LinkNotExistingInternalConstraint(); $this->validate($link, $context);
}
/**
* Validate the link.
*/
protected function validate(LinkItemInterface&MockObject $link, ExecutionContextInterface&MockObject $context): void {
$validator = new LinkNotExistingInternalConstraintValidator(); $validator = new LinkNotExistingInternalConstraintValidator();
$validator->initialize($context); $validator->initialize($context);
$validator->validate($link, $constraint); $validator->validate($link, new LinkNotExistingInternalConstraint());
} }
} }