Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\link\Plugin\Validation\Constraint\LinkAccessConstraintValidatorTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraint;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* Tests the LinkAccessConstraintValidator validator.
|
||||
*
|
||||
* @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator
|
||||
* @group validation
|
||||
*/
|
||||
class LinkAccessConstraintValidatorTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Tests the \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator::validate()
|
||||
* method.
|
||||
*
|
||||
* @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->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for LinkAccessConstraintValidator::validate().
|
||||
*
|
||||
* @return array
|
||||
* An array of tests, matching the parameter inputs for testValidate.
|
||||
*
|
||||
* @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->getMock('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->getMock('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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidatorTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Component\Utility\UrlHelper;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraint;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator
|
||||
* @group Link
|
||||
*/
|
||||
class LinkExternalProtocolsConstraintValidatorTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
* @dataProvider providerValidate
|
||||
*/
|
||||
public function testValidate($value, $valid) {
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
|
||||
if ($valid) {
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
}
|
||||
else {
|
||||
$context->expects($this->once())
|
||||
->method('addViolation');
|
||||
}
|
||||
|
||||
// Setup some more allowed protocols.
|
||||
UrlHelper::setAllowedProtocols(['http', 'https', 'magnet']);
|
||||
|
||||
$constraint = new LinkExternalProtocolsConstraint();
|
||||
|
||||
$validator = new LinkExternalProtocolsConstraintValidator();
|
||||
$validator->initialize($context);
|
||||
$validator->validate($value, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for ::testValidate
|
||||
*/
|
||||
public function providerValidate() {
|
||||
$data = [];
|
||||
|
||||
// Test allowed protocols.
|
||||
$data[] = ['http://www.drupal.org', TRUE];
|
||||
$data[] = ['https://www.drupal.org', TRUE];
|
||||
$data[] = ['magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C', TRUE];
|
||||
|
||||
// Invalid protocols.
|
||||
$data[] = ['ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt', FALSE];
|
||||
|
||||
foreach ($data as &$single_data) {
|
||||
$url = Url::fromUri($single_data[0]);
|
||||
$link = $this->getMock('Drupal\link\LinkItemInterface');
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willReturn($url);
|
||||
$single_data[0] = $link;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
*
|
||||
* @see \Drupal\Core\Url::fromUri
|
||||
*/
|
||||
public function testValidateWithMalformedUri() {
|
||||
$link = $this->getMock('Drupal\link\LinkItemInterface');
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willThrowException(new \InvalidArgumentException());
|
||||
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$constraint = new LinkExternalProtocolsConstraint();
|
||||
|
||||
$validator = new LinkExternalProtocolsConstraintValidator();
|
||||
$validator->initialize($context);
|
||||
$validator->validate($link, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
*/
|
||||
public function testValidateIgnoresInternalUrls() {
|
||||
$link = $this->getMock('Drupal\link\LinkItemInterface');
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willReturn(Url::fromRoute('example.test'));
|
||||
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$constraint = new LinkExternalProtocolsConstraint();
|
||||
|
||||
$validator = new LinkExternalProtocolsConstraintValidator();
|
||||
$validator->initialize($context);
|
||||
$validator->validate($link, $constraint);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidatorTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraint;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator
|
||||
* @group Link
|
||||
*/
|
||||
class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
* @dataProvider providerValidate
|
||||
*/
|
||||
public function testValidate($value, $valid) {
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for ::testValidate
|
||||
*/
|
||||
public function providerValidate() {
|
||||
$data = [];
|
||||
|
||||
// External URL
|
||||
$data[] = [Url::fromUri('https://www.drupal.org'), TRUE];
|
||||
|
||||
// Existing routed URL.
|
||||
$url = Url::fromRoute('example.existing_route');
|
||||
|
||||
$url_generator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
|
||||
$url_generator->expects($this->any())
|
||||
->method('generateFromRoute')
|
||||
->with('example.existing_route', [], [])
|
||||
->willReturn('/example/existing');
|
||||
$url->setUrlGenerator($url_generator);
|
||||
|
||||
$data[] = [$url, TRUE];
|
||||
|
||||
// Not existing routed URL.
|
||||
$url = Url::fromRoute('example.not_existing_route');
|
||||
|
||||
$url_generator = $this->getMock('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->getMock('Drupal\link\LinkItemInterface');
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willReturn($single_data[0]);
|
||||
|
||||
$single_data[0] = $link;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
*
|
||||
* @see \Drupal\Core\Url::fromUri
|
||||
*/
|
||||
public function testValidateWithMalformedUri() {
|
||||
$link = $this->getMock('Drupal\link\LinkItemInterface');
|
||||
$link->expects($this->any())
|
||||
->method('getUrl')
|
||||
->willThrowException(new \InvalidArgumentException());
|
||||
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$constraint = new LinkNotExistingInternalConstraint();
|
||||
|
||||
$validator = new LinkNotExistingInternalConstraintValidator();
|
||||
$validator->initialize($context);
|
||||
$validator->validate($link, $constraint);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue