Move into nested docroot
This commit is contained in:
parent
83a0d3a149
commit
c8b70abde9
13405 changed files with 0 additions and 0 deletions
177
web/core/modules/link/tests/src/Kernel/LinkItemTest.php
Normal file
177
web/core/modules/link/tests/src/Kernel/LinkItemTest.php
Normal file
|
@ -0,0 +1,177 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\link\Kernel;
|
||||
|
||||
use Drupal\Component\Utility\UrlHelper;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
|
||||
use Drupal\link\LinkItemInterface;
|
||||
|
||||
/**
|
||||
* Tests the new entity API for the link field type.
|
||||
*
|
||||
* @group link
|
||||
*/
|
||||
class LinkItemTest extends FieldKernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('link');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create a generic, external, and internal link fields for validation.
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => 'field_test',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'link',
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test',
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC],
|
||||
])->save();
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => 'field_test_external',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'link',
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_external',
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => ['link_type' => LinkItemInterface::LINK_EXTERNAL],
|
||||
])->save();
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => 'field_test_internal',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'link',
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_internal',
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => ['link_type' => LinkItemInterface::LINK_INTERNAL],
|
||||
])->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests using entity fields of the link field type.
|
||||
*/
|
||||
public function testLinkItem() {
|
||||
// Create entity.
|
||||
$entity = EntityTest::create();
|
||||
$url = 'https://www.drupal.org?test_param=test_value';
|
||||
$parsed_url = UrlHelper::parse($url);
|
||||
$title = $this->randomMachineName();
|
||||
$class = $this->randomMachineName();
|
||||
$entity->field_test->uri = $parsed_url['path'];
|
||||
$entity->field_test->title = $title;
|
||||
$entity->field_test->first()->get('options')->set('query', $parsed_url['query']);
|
||||
$entity->field_test->first()->get('options')->set('attributes', array('class' => $class));
|
||||
$this->assertEquals([
|
||||
'query' => $parsed_url['query'],
|
||||
'attributes' => [
|
||||
'class' => $class,
|
||||
],
|
||||
'external' => TRUE,
|
||||
], $entity->field_test->first()->getUrl()->getOptions());
|
||||
$entity->name->value = $this->randomMachineName();
|
||||
$entity->save();
|
||||
|
||||
// Verify that the field value is changed.
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.');
|
||||
$this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
|
||||
$this->assertEqual($entity->field_test->uri, $parsed_url['path']);
|
||||
$this->assertEqual($entity->field_test[0]->uri, $parsed_url['path']);
|
||||
$this->assertEqual($entity->field_test->title, $title);
|
||||
$this->assertEqual($entity->field_test[0]->title, $title);
|
||||
$this->assertEqual($entity->field_test->options['attributes']['class'], $class);
|
||||
$this->assertEqual($entity->field_test->options['query'], $parsed_url['query']);
|
||||
|
||||
// Update only the entity name property to check if the link field data will
|
||||
// remain intact.
|
||||
$entity->name->value = $this->randomMachineName();
|
||||
$entity->save();
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_test->uri, $parsed_url['path']);
|
||||
$this->assertEqual($entity->field_test->options['attributes']['class'], $class);
|
||||
$this->assertEqual($entity->field_test->options['query'], $parsed_url['query']);
|
||||
|
||||
// Verify changing the field value.
|
||||
$new_url = 'https://www.drupal.org';
|
||||
$new_title = $this->randomMachineName();
|
||||
$new_class = $this->randomMachineName();
|
||||
$entity->field_test->uri = $new_url;
|
||||
$entity->field_test->title = $new_title;
|
||||
$entity->field_test->first()->get('options')->set('query', NULL);
|
||||
$entity->field_test->first()->get('options')->set('attributes', array('class' => $new_class));
|
||||
$this->assertEqual($entity->field_test->uri, $new_url);
|
||||
$this->assertEqual($entity->field_test->title, $new_title);
|
||||
$this->assertEqual($entity->field_test->options['attributes']['class'], $new_class);
|
||||
$this->assertNull($entity->field_test->options['query']);
|
||||
|
||||
// Read changed entity and assert changed values.
|
||||
$entity->save();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_test->uri, $new_url);
|
||||
$this->assertEqual($entity->field_test->title, $new_title);
|
||||
$this->assertEqual($entity->field_test->options['attributes']['class'], $new_class);
|
||||
|
||||
// Check that if we only set uri the default values for title and options
|
||||
// are also initialized.
|
||||
$entity->field_test = ['uri' => 'internal:/node/add'];
|
||||
$this->assertEqual($entity->field_test->uri, 'internal:/node/add');
|
||||
$this->assertNull($entity->field_test->title);
|
||||
$this->assertIdentical($entity->field_test->options, []);
|
||||
|
||||
// Check that if set uri and serialize options then the default values are
|
||||
// properly initialized.
|
||||
$entity->field_test = [
|
||||
'uri' => 'internal:/node/add',
|
||||
'options' => serialize(['query' => NULL]),
|
||||
];
|
||||
$this->assertEqual($entity->field_test->uri, 'internal:/node/add');
|
||||
$this->assertNull($entity->field_test->title);
|
||||
$this->assertNull($entity->field_test->options['query']);
|
||||
|
||||
// Check that if we set the direct value of link field it correctly set the
|
||||
// uri and the default values of the field.
|
||||
$entity->field_test = 'internal:/node/add';
|
||||
$this->assertEqual($entity->field_test->uri, 'internal:/node/add');
|
||||
$this->assertNull($entity->field_test->title);
|
||||
$this->assertIdentical($entity->field_test->options, []);
|
||||
|
||||
// Check that setting options to NULL does not trigger an error when
|
||||
// calling getUrl();
|
||||
$entity->field_test->options = NULL;
|
||||
$this->assertInstanceOf(Url::class, $entity->field_test[0]->getUrl());
|
||||
|
||||
// Check that setting LinkItem value NULL doesn't generate any error or
|
||||
// warning.
|
||||
$entity->field_test[0] = NULL;
|
||||
$this->assertNull($entity->field_test[0]->getValue());
|
||||
|
||||
// Test the generateSampleValue() method for generic, external, and internal
|
||||
// link types.
|
||||
$entity = EntityTest::create();
|
||||
$entity->field_test->generateSampleItems();
|
||||
$entity->field_test_external->generateSampleItems();
|
||||
$entity->field_test_internal->generateSampleItems();
|
||||
$this->entityValidateAndSave($entity);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* 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,111 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\link\Unit\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,108 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\link\Unit\Plugin\Validation\Constraint;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\link\Unit\Plugin\migrate\process\d6;
|
||||
|
||||
use Drupal\link\Plugin\migrate\process\d6\CckLink;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @group Link
|
||||
*/
|
||||
class CckLinkTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Test the url transformations in the CckLink process plugin.
|
||||
*
|
||||
* @dataProvider canonicalizeUriDataProvider
|
||||
*/
|
||||
public function testCanonicalizeUri($url, $expected) {
|
||||
$link_plugin = new CckLink([], '', [], $this->getMock('\Drupal\migrate\Plugin\MigrationInterface'));
|
||||
$transformed = $link_plugin->transform([
|
||||
'url' => $url,
|
||||
'title' => '',
|
||||
'attributes' => serialize([]),
|
||||
], $this->getMock('\Drupal\migrate\MigrateExecutableInterface'), $this->getMockBuilder('\Drupal\migrate\Row')->disableOriginalConstructor()->getMock(), NULL);
|
||||
$this->assertEquals($expected, $transformed['uri']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testCanonicalizeUri.
|
||||
*/
|
||||
public function canonicalizeUriDataProvider() {
|
||||
return [
|
||||
'Simple front-page' => [
|
||||
'<front>',
|
||||
'internal:/',
|
||||
],
|
||||
'Front page with query' => [
|
||||
'<front>?query=1',
|
||||
'internal:/?query=1',
|
||||
],
|
||||
'No leading forward slash' => [
|
||||
'node/10',
|
||||
'internal:/node/10',
|
||||
],
|
||||
'Leading forward slash' => [
|
||||
'/node/10',
|
||||
'internal:/node/10',
|
||||
],
|
||||
'Existing scheme' => [
|
||||
'scheme:test',
|
||||
'scheme:test',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue