LinkAccessConstraintValidatorTest.php in Drupal 8
File
core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php
View source
<?php
namespace Drupal\Tests\link\Unit\Plugin\Validation\Constraint;
use Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraint;
use Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class LinkAccessConstraintValidatorTest extends UnitTestCase {
public function testValidate($value, $user, $valid) {
$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($value, $constraint);
}
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) {
$url = $this
->getMockBuilder('Drupal\\Core\\Url')
->disableOriginalConstructor()
->getMock();
$url
->expects($this
->once())
->method('access')
->willReturn($case['url_access']);
$link = $this
->createMock('Drupal\\link\\LinkItemInterface');
$link
->expects($this
->any())
->method('getUrl')
->willReturn($url);
$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;
}
}