3 namespace Drupal\Tests\link\Kernel;
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FieldItemInterface;
9 use Drupal\entity_test\Entity\EntityTest;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
13 use Drupal\link\LinkItemInterface;
16 * Tests the new entity API for the link field type.
20 class LinkItemTest extends FieldKernelTestBase {
27 public static $modules = ['link'];
29 protected function setUp() {
32 // Create a generic, external, and internal link fields for validation.
33 FieldStorageConfig::create([
34 'field_name' => 'field_test',
35 'entity_type' => 'entity_test',
39 'entity_type' => 'entity_test',
40 'field_name' => 'field_test',
41 'bundle' => 'entity_test',
42 'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC],
44 FieldStorageConfig::create([
45 'field_name' => 'field_test_external',
46 'entity_type' => 'entity_test',
50 'entity_type' => 'entity_test',
51 'field_name' => 'field_test_external',
52 'bundle' => 'entity_test',
53 'settings' => ['link_type' => LinkItemInterface::LINK_EXTERNAL],
55 FieldStorageConfig::create([
56 'field_name' => 'field_test_internal',
57 'entity_type' => 'entity_test',
61 'entity_type' => 'entity_test',
62 'field_name' => 'field_test_internal',
63 'bundle' => 'entity_test',
64 'settings' => ['link_type' => LinkItemInterface::LINK_INTERNAL],
69 * Tests using entity fields of the link field type.
71 public function testLinkItem() {
73 $entity = EntityTest::create();
74 $url = 'https://www.drupal.org?test_param=test_value';
75 $parsed_url = UrlHelper::parse($url);
76 $title = $this->randomMachineName();
77 $class = $this->randomMachineName();
78 $entity->field_test->uri = $parsed_url['path'];
79 $entity->field_test->title = $title;
80 $entity->field_test->first()->get('options')->set('query', $parsed_url['query']);
81 $entity->field_test->first()->get('options')->set('attributes', ['class' => $class]);
83 'query' => $parsed_url['query'],
88 ], $entity->field_test->first()->getUrl()->getOptions());
89 $entity->name->value = $this->randomMachineName();
92 // Verify that the field value is changed.
94 $entity = EntityTest::load($id);
95 $this->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.');
96 $this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
97 $this->assertEqual($entity->field_test->uri, $parsed_url['path']);
98 $this->assertEqual($entity->field_test[0]->uri, $parsed_url['path']);
99 $this->assertEqual($entity->field_test->title, $title);
100 $this->assertEqual($entity->field_test[0]->title, $title);
101 $this->assertEqual($entity->field_test->options['attributes']['class'], $class);
102 $this->assertEqual($entity->field_test->options['query'], $parsed_url['query']);
104 // Update only the entity name property to check if the link field data will
106 $entity->name->value = $this->randomMachineName();
109 $entity = EntityTest::load($id);
110 $this->assertEqual($entity->field_test->uri, $parsed_url['path']);
111 $this->assertEqual($entity->field_test->options['attributes']['class'], $class);
112 $this->assertEqual($entity->field_test->options['query'], $parsed_url['query']);
114 // Verify changing the field value.
115 $new_url = 'https://www.drupal.org';
116 $new_title = $this->randomMachineName();
117 $new_class = $this->randomMachineName();
118 $entity->field_test->uri = $new_url;
119 $entity->field_test->title = $new_title;
120 $entity->field_test->first()->get('options')->set('query', NULL);
121 $entity->field_test->first()->get('options')->set('attributes', ['class' => $new_class]);
122 $this->assertEqual($entity->field_test->uri, $new_url);
123 $this->assertEqual($entity->field_test->title, $new_title);
124 $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class);
125 $this->assertNull($entity->field_test->options['query']);
127 // Read changed entity and assert changed values.
129 $entity = EntityTest::load($id);
130 $this->assertEqual($entity->field_test->uri, $new_url);
131 $this->assertEqual($entity->field_test->title, $new_title);
132 $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class);
134 // Check that if we only set uri the default values for title and options
135 // are also initialized.
136 $entity->field_test = ['uri' => 'internal:/node/add'];
137 $this->assertEqual($entity->field_test->uri, 'internal:/node/add');
138 $this->assertNull($entity->field_test->title);
139 $this->assertIdentical($entity->field_test->options, []);
141 // Check that if set uri and serialize options then the default values are
142 // properly initialized.
143 $entity->field_test = [
144 'uri' => 'internal:/node/add',
145 'options' => serialize(['query' => NULL]),
147 $this->assertEqual($entity->field_test->uri, 'internal:/node/add');
148 $this->assertNull($entity->field_test->title);
149 $this->assertNull($entity->field_test->options['query']);
151 // Check that if we set the direct value of link field it correctly set the
152 // uri and the default values of the field.
153 $entity->field_test = 'internal:/node/add';
154 $this->assertEqual($entity->field_test->uri, 'internal:/node/add');
155 $this->assertNull($entity->field_test->title);
156 $this->assertIdentical($entity->field_test->options, []);
158 // Check that setting options to NULL does not trigger an error when
160 $entity->field_test->options = NULL;
161 $this->assertInstanceOf(Url::class, $entity->field_test[0]->getUrl());
163 // Check that setting LinkItem value NULL doesn't generate any error or
165 $entity->field_test[0] = NULL;
166 $this->assertNull($entity->field_test[0]->getValue());
168 // Test the generateSampleValue() method for generic, external, and internal
170 $entity = EntityTest::create();
171 $entity->field_test->generateSampleItems();
172 $entity->field_test_external->generateSampleItems();
173 $entity->field_test_internal->generateSampleItems();
174 $this->entityValidateAndSave($entity);