View source
<?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;
class LinkItemTest extends FieldKernelTestBase {
public static $modules = [
'link',
];
protected function setUp() {
parent::setUp();
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();
}
public function testLinkItem() {
$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', [
'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();
$id = $entity
->id();
$entity = EntityTest::load($id);
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->field_test);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_test[0]);
$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']);
$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']);
$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', [
'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']);
$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);
$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, []);
$entity->field_test = [
'uri' => 'internal:/node/add',
'options' => [
'query' => NULL,
],
];
$this
->assertEqual($entity->field_test->uri, 'internal:/node/add');
$this
->assertNull($entity->field_test->title);
$this
->assertNull($entity->field_test->options['query']);
$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, []);
$entity->field_test->options = NULL;
$this
->assertInstanceOf(Url::class, $entity->field_test[0]
->getUrl());
$entity->field_test[0] = NULL;
$this
->assertNull($entity->field_test[0]
->getValue());
$entity = EntityTest::create();
$entity->field_test
->generateSampleItems();
$entity->field_test_external
->generateSampleItems();
$entity->field_test_internal
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
public function testSerializedOptions() {
$entity = EntityTest::create();
$entity
->set('field_test', [
'uri' => 'internal:/node/add',
'options' => serialize([
'query' => NULL,
]),
]);
$this
->assertEquals('internal:/node/add', $entity
->get('field_test')->uri);
$this
->assertNull($entity
->get('field_test')->title);
$this
->assertNull($entity
->get('field_test')->options['query']);
}
}