Issue #3439079 by mondrake: Change remaining Link module test dataproviders to static
parent
f79817a4be
commit
0186f88691
|
@ -20,19 +20,39 @@ class LinkAccessConstraintValidatorTest extends UnitTestCase {
|
|||
/**
|
||||
* 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
|
||||
* @dataProvider providerValidate
|
||||
*/
|
||||
public function testValidate($value, $user, $valid) {
|
||||
$context = $this->createMock(ExecutionContextInterface::class);
|
||||
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');
|
||||
|
@ -43,10 +63,9 @@ class LinkAccessConstraintValidatorTest extends UnitTestCase {
|
|||
}
|
||||
|
||||
$constraint = new LinkAccessConstraint();
|
||||
|
||||
$validate = new LinkAccessConstraintValidator($user);
|
||||
$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()
|
||||
*/
|
||||
public function providerValidate() {
|
||||
$data = [];
|
||||
|
||||
$cases = [
|
||||
['may_link_any_page' => TRUE, 'url_access' => TRUE, 'valid' => TRUE],
|
||||
['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;
|
||||
public static function providerValidate(): \Generator {
|
||||
yield [TRUE, TRUE, TRUE];
|
||||
yield [TRUE, FALSE, TRUE];
|
||||
yield [FALSE, TRUE, TRUE];
|
||||
yield [FALSE, FALSE, FALSE];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,10 +4,13 @@ declare(strict_types=1);
|
|||
|
||||
namespace Drupal\Tests\link\Unit\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Core\Routing\UrlGeneratorInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\link\LinkItemInterface;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraint;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
|
@ -19,70 +22,70 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
|
|||
|
||||
/**
|
||||
* @covers ::validate
|
||||
* @dataProvider providerValidate
|
||||
*/
|
||||
public function testValidate($value, $valid) {
|
||||
public function testValidateFromUri(): void {
|
||||
$url = Url::fromUri('https://www.drupal.org');
|
||||
|
||||
$link = $this->createMock(LinkItemInterface::class);
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willReturn($url);
|
||||
|
||||
$context = $this->createMock(ExecutionContextInterface::class);
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
if ($valid) {
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
}
|
||||
else {
|
||||
$context->expects($this->once())
|
||||
->method('addViolation');
|
||||
}
|
||||
|
||||
$constraint = new LinkNotExistingInternalConstraint();
|
||||
|
||||
$validator = new LinkNotExistingInternalConstraintValidator();
|
||||
$validator->initialize($context);
|
||||
$validator->validate($value, $constraint);
|
||||
$this->validate($link, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for ::testValidate.
|
||||
* @covers ::validate
|
||||
*/
|
||||
public function providerValidate() {
|
||||
$data = [];
|
||||
|
||||
// External URL
|
||||
$data[] = [Url::fromUri('https://www.drupal.org'), TRUE];
|
||||
|
||||
// Existing routed URL.
|
||||
public function testValidateFromRoute(): void {
|
||||
$url = Url::fromRoute('example.existing_route');
|
||||
|
||||
$url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
|
||||
$url_generator->expects($this->any())
|
||||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||||
$urlGenerator->expects($this->any())
|
||||
->method('generateFromRoute')
|
||||
->with('example.existing_route', [], [])
|
||||
->willReturn('/example/existing');
|
||||
$url->setUrlGenerator($url_generator);
|
||||
$url->setUrlGenerator($urlGenerator);
|
||||
|
||||
$data[] = [$url, TRUE];
|
||||
$link = $this->createMock(LinkItemInterface::class);
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willReturn($url);
|
||||
|
||||
// Non-existent routed URL.
|
||||
$context = $this->createMock(ExecutionContextInterface::class);
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validate($link, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
*/
|
||||
public function testValidateFromNonExistingRoute(): void {
|
||||
$url = Url::fromRoute('example.not_existing_route');
|
||||
|
||||
$url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
|
||||
$url_generator->expects($this->any())
|
||||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||||
$urlGenerator->expects($this->any())
|
||||
->method('generateFromRoute')
|
||||
->with('example.not_existing_route', [], [])
|
||||
->willThrowException(new RouteNotFoundException());
|
||||
$url->setUrlGenerator($url_generator);
|
||||
->willReturn(new RouteNotFoundException());
|
||||
$url->setUrlGenerator($urlGenerator);
|
||||
|
||||
$data[] = [$url, FALSE];
|
||||
$link = $this->createMock(LinkItemInterface::class);
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willReturn($url);
|
||||
|
||||
foreach ($data as &$single_data) {
|
||||
$link = $this->createMock('Drupal\link\LinkItemInterface');
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willReturn($single_data[0]);
|
||||
$context = $this->createMock(ExecutionContextInterface::class);
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$single_data[0] = $link;
|
||||
}
|
||||
|
||||
return $data;
|
||||
$this->validate($link, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,8 +93,8 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
|
|||
*
|
||||
* @see \Drupal\Core\Url::fromUri
|
||||
*/
|
||||
public function testValidateWithMalformedUri() {
|
||||
$link = $this->createMock('Drupal\link\LinkItemInterface');
|
||||
public function testValidateWithMalformedUri(): void {
|
||||
$link = $this->createMock(LinkItemInterface::class);
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willThrowException(new \InvalidArgumentException());
|
||||
|
@ -100,11 +103,16 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
|
|||
$context->expects($this->never())
|
||||
->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->initialize($context);
|
||||
$validator->validate($link, $constraint);
|
||||
$validator->validate($link, new LinkNotExistingInternalConstraint());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue