comparison core/modules/link/tests/src/Kernel/LinkItemTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\link\Kernel;
4
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FieldItemInterface;
8 use Drupal\Core\Url;
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;
14
15 /**
16 * Tests the new entity API for the link field type.
17 *
18 * @group link
19 */
20 class LinkItemTest extends FieldKernelTestBase {
21
22 /**
23 * Modules to enable.
24 *
25 * @var array
26 */
27 public static $modules = ['link'];
28
29 protected function setUp() {
30 parent::setUp();
31
32 // Create a generic, external, and internal link fields for validation.
33 FieldStorageConfig::create([
34 'field_name' => 'field_test',
35 'entity_type' => 'entity_test',
36 'type' => 'link',
37 ])->save();
38 FieldConfig::create([
39 'entity_type' => 'entity_test',
40 'field_name' => 'field_test',
41 'bundle' => 'entity_test',
42 'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC],
43 ])->save();
44 FieldStorageConfig::create([
45 'field_name' => 'field_test_external',
46 'entity_type' => 'entity_test',
47 'type' => 'link',
48 ])->save();
49 FieldConfig::create([
50 'entity_type' => 'entity_test',
51 'field_name' => 'field_test_external',
52 'bundle' => 'entity_test',
53 'settings' => ['link_type' => LinkItemInterface::LINK_EXTERNAL],
54 ])->save();
55 FieldStorageConfig::create([
56 'field_name' => 'field_test_internal',
57 'entity_type' => 'entity_test',
58 'type' => 'link',
59 ])->save();
60 FieldConfig::create([
61 'entity_type' => 'entity_test',
62 'field_name' => 'field_test_internal',
63 'bundle' => 'entity_test',
64 'settings' => ['link_type' => LinkItemInterface::LINK_INTERNAL],
65 ])->save();
66 }
67
68 /**
69 * Tests using entity fields of the link field type.
70 */
71 public function testLinkItem() {
72 // Create entity.
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]);
82 $this->assertEquals([
83 'query' => $parsed_url['query'],
84 'attributes' => [
85 'class' => $class,
86 ],
87 'external' => TRUE,
88 ], $entity->field_test->first()->getUrl()->getOptions());
89 $entity->name->value = $this->randomMachineName();
90 $entity->save();
91
92 // Verify that the field value is changed.
93 $id = $entity->id();
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']);
103
104 // Update only the entity name property to check if the link field data will
105 // remain intact.
106 $entity->name->value = $this->randomMachineName();
107 $entity->save();
108 $id = $entity->id();
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']);
113
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']);
126
127 // Read changed entity and assert changed values.
128 $entity->save();
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);
133
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, []);
140
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]),
146 ];
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']);
150
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, []);
157
158 // Check that setting options to NULL does not trigger an error when
159 // calling getUrl();
160 $entity->field_test->options = NULL;
161 $this->assertInstanceOf(Url::class, $entity->field_test[0]->getUrl());
162
163 // Check that setting LinkItem value NULL doesn't generate any error or
164 // warning.
165 $entity->field_test[0] = NULL;
166 $this->assertNull($entity->field_test[0]->getValue());
167
168 // Test the generateSampleValue() method for generic, external, and internal
169 // link types.
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);
175 }
176
177 }