Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\link\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
Chris@0
|
7 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
8 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
9 use Drupal\field_ui\Tests\FieldUiTestTrait;
|
Chris@0
|
10 use Drupal\link\LinkItemInterface;
|
Chris@0
|
11 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Tests link field UI functionality.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @group link
|
Chris@0
|
17 */
|
Chris@0
|
18 class LinkFieldUITest extends BrowserTestBase {
|
Chris@0
|
19
|
Chris@0
|
20 use FieldUiTestTrait;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Modules to enable.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var array
|
Chris@0
|
26 */
|
Chris@0
|
27 public static $modules = ['node', 'link', 'field_ui', 'block'];
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * A user that can edit content types.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\user\UserInterface
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $adminUser;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * A user that should see the help texts.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var \Drupal\user\Entity\User
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $helpTextUser;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * The first content type to add fields to.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var \Drupal\node\Entity\NodeType
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $firstContentType;
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * The second content type to add fields to.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @var \Drupal\node\Entity\NodeType
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $secondContentType;
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * {@inheritdoc}
|
Chris@0
|
59 */
|
Chris@0
|
60 protected function setUp() {
|
Chris@0
|
61 parent::setUp();
|
Chris@0
|
62
|
Chris@0
|
63 $this->firstContentType = $this->drupalCreateContentType();
|
Chris@0
|
64 $this->secondContentType = $this->drupalCreateContentType();
|
Chris@0
|
65 $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']);
|
Chris@0
|
66 $this->helpTextUser = $this->drupalCreateUser(['create ' . $this->secondContentType->id() . ' content']);
|
Chris@0
|
67 $this->drupalPlaceBlock('system_breadcrumb_block');
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Tests the link field UI.
|
Chris@0
|
72 */
|
Chris@0
|
73 public function testFieldUI() {
|
Chris@0
|
74 foreach ($this->providerTestFieldUI() as $item) {
|
Chris@0
|
75 list($cardinality, $link_type, $title, $label, $field_name) = $item;
|
Chris@0
|
76 $this->runFieldUIItem($cardinality, $link_type, $title, $label, $field_name);
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Provides test data for ::testFieldUI().
|
Chris@0
|
82 */
|
Chris@0
|
83 protected function providerTestFieldUI() {
|
Chris@0
|
84 // There are many combinations of field settings: where the description
|
Chris@0
|
85 // should show: variation on internal, external, both; cardinality (where
|
Chris@0
|
86 // the fieldset is hidden or used); and link text shown (required or
|
Chris@0
|
87 // optional) or disabled. There are two descriptions: field and URL help
|
Chris@0
|
88 // text.
|
Chris@0
|
89 $cardinalities = [1, 2];
|
Chris@0
|
90 $title_settings = [
|
Chris@0
|
91 DRUPAL_DISABLED,
|
Chris@0
|
92 DRUPAL_OPTIONAL,
|
Chris@0
|
93 ];
|
Chris@0
|
94 $link_types = [
|
Chris@0
|
95 LinkItemInterface::LINK_EXTERNAL,
|
Chris@0
|
96 LinkItemInterface::LINK_GENERIC,
|
Chris@0
|
97 LinkItemInterface::LINK_INTERNAL,
|
Chris@0
|
98 ];
|
Chris@0
|
99
|
Chris@0
|
100 // Test all variations of link types on all cardinalities.
|
Chris@0
|
101 foreach ($cardinalities as $cardinality) {
|
Chris@0
|
102 foreach ($link_types as $link_type) {
|
Chris@0
|
103 // Now, test this with both the title enabled and disabled.
|
Chris@0
|
104 foreach ($title_settings as $title_setting) {
|
Chris@0
|
105 // Test both empty and non-empty labels.
|
Chris@0
|
106 foreach ([TRUE, FALSE] as $label_provided) {
|
Chris@0
|
107 // Generate a unique machine name for the field so it can be
|
Chris@0
|
108 // identified in the test.
|
Chris@0
|
109 $id = implode('_', [
|
Chris@0
|
110 'link',
|
Chris@0
|
111 $cardinality,
|
Chris@0
|
112 $link_type,
|
Chris@0
|
113 $title_setting,
|
Chris@0
|
114 (int) $label_provided,
|
Chris@0
|
115 ]);
|
Chris@0
|
116
|
Chris@0
|
117 // Use a unique label that contains some HTML.
|
Chris@0
|
118 $label = '<img src="http://example.com">' . $id;
|
Chris@0
|
119
|
Chris@0
|
120 yield [
|
Chris@0
|
121 $cardinality,
|
Chris@0
|
122 $link_type,
|
Chris@0
|
123 $title_setting,
|
Chris@0
|
124 $label_provided ? $label : '',
|
Chris@0
|
125 $id,
|
Chris@0
|
126 ];
|
Chris@0
|
127 }
|
Chris@0
|
128 }
|
Chris@0
|
129 }
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * Tests one link field UI item.
|
Chris@0
|
135 *
|
Chris@0
|
136 * @param int $cardinality
|
Chris@0
|
137 * The field cardinality.
|
Chris@0
|
138 * @param int $link_type
|
Chris@0
|
139 * Determine if the link is external, internal or both.
|
Chris@0
|
140 * @param int $title
|
Chris@0
|
141 * Determine if the field will display the link text field.
|
Chris@0
|
142 * @param string $label
|
Chris@0
|
143 * The field label.
|
Chris@0
|
144 * @param string $field_name
|
Chris@0
|
145 * The unique machine name for the field.
|
Chris@0
|
146 */
|
Chris@0
|
147 public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name) {
|
Chris@0
|
148 $this->drupalLogin($this->adminUser);
|
Chris@0
|
149 $type_path = 'admin/structure/types/manage/' . $this->firstContentType->id();
|
Chris@0
|
150
|
Chris@0
|
151 // Add a link field to the newly-created type. It defaults to allowing both
|
Chris@0
|
152 // internal and external links.
|
Chris@0
|
153 $field_label = str_replace('_', ' ', $field_name);
|
Chris@0
|
154 $description = 'link field description';
|
Chris@0
|
155 $field_edit = [
|
Chris@0
|
156 'description' => $description,
|
Chris@0
|
157 ];
|
Chris@0
|
158 $this->fieldUIAddNewField($type_path, $field_name, $field_label, 'link', [], $field_edit);
|
Chris@0
|
159
|
Chris@0
|
160 // Load the formatter page to check that the settings summary does not
|
Chris@0
|
161 // generate warnings.
|
Chris@0
|
162 // @todo Mess with the formatter settings a bit here.
|
Chris@0
|
163 $this->drupalGet("$type_path/display");
|
Chris@0
|
164 $this->assertText(t('Link text trimmed to @limit characters', ['@limit' => 80]));
|
Chris@0
|
165
|
Chris@0
|
166 $storage = FieldStorageConfig::create([
|
Chris@0
|
167 'field_name' => $field_name,
|
Chris@0
|
168 'entity_type' => 'node',
|
Chris@0
|
169 'type' => 'link',
|
Chris@0
|
170 'cardinality' => $cardinality,
|
Chris@0
|
171 ]);
|
Chris@0
|
172 $storage->save();
|
Chris@0
|
173
|
Chris@0
|
174 FieldConfig::create([
|
Chris@0
|
175 'field_storage' => $storage,
|
Chris@0
|
176 'label' => $label,
|
Chris@0
|
177 'bundle' => $this->secondContentType->id(),
|
Chris@0
|
178 'settings' => [
|
Chris@0
|
179 'title' => $title,
|
Chris@0
|
180 'link_type' => $link_type,
|
Chris@0
|
181 ],
|
Chris@0
|
182 ])->save();
|
Chris@0
|
183
|
Chris@0
|
184 // Make the fields visible in the form display.
|
Chris@0
|
185 $form_display_id = implode('.', ['node', $this->secondContentType->id(), 'default']);
|
Chris@0
|
186 $form_display = EntityFormDisplay::load($form_display_id);
|
Chris@0
|
187 $form_display->setComponent($field_name, ['region' => 'content']);
|
Chris@0
|
188 $form_display->save();
|
Chris@0
|
189
|
Chris@0
|
190 // Log in a user that is allowed to create this content type, see if
|
Chris@0
|
191 // the user can see the expected help text.
|
Chris@0
|
192 $this->drupalLogin($this->helpTextUser);
|
Chris@0
|
193
|
Chris@0
|
194 $add_path = 'node/add/' . $this->secondContentType->id();
|
Chris@0
|
195 $this->drupalGet($add_path);
|
Chris@0
|
196
|
Chris@0
|
197 $expected_help_texts = [
|
Chris@0
|
198 LinkItemInterface::LINK_EXTERNAL => 'This must be an external URL such as <em class="placeholder">http://example.com</em>.',
|
Chris@0
|
199 LinkItemInterface::LINK_GENERIC => 'You can also enter an internal path such as <em class="placeholder">/node/add</em> or an external URL such as <em class="placeholder">http://example.com</em>. Enter <em class="placeholder"><front></em> to link to the front page.',
|
Chris@0
|
200 LinkItemInterface::LINK_INTERNAL => rtrim(\Drupal::url('<front>', [], ['absolute' => TRUE]), '/'),
|
Chris@0
|
201 ];
|
Chris@0
|
202
|
Chris@0
|
203 // Check that the help texts we assume should be there, is there.
|
Chris@0
|
204 $this->assertFieldContainsRawText($field_name, $expected_help_texts[$link_type]);
|
Chris@0
|
205 if ($link_type === LinkItemInterface::LINK_INTERNAL) {
|
Chris@0
|
206 // Internal links have no "system" description. Test that none
|
Chris@0
|
207 // of the other help texts show here.
|
Chris@0
|
208 $this->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_EXTERNAL]);
|
Chris@0
|
209 $this->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_GENERIC]);
|
Chris@0
|
210 }
|
Chris@0
|
211 // Also assert that the description we made is here, no matter what the
|
Chris@0
|
212 // cardinality or link setting.
|
Chris@0
|
213 if (!empty($label)) {
|
Chris@0
|
214 $this->assertFieldContainsRawText($field_name, $label);
|
Chris@0
|
215 }
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 /**
|
Chris@0
|
219 * Checks that given field contains the given raw text.
|
Chris@0
|
220 *
|
Chris@0
|
221 * @param string $field_name
|
Chris@0
|
222 * The name of the field to check.
|
Chris@0
|
223 * @param string $text
|
Chris@0
|
224 * The text to check.
|
Chris@0
|
225 */
|
Chris@0
|
226 protected function assertFieldContainsRawText($field_name, $text) {
|
Chris@0
|
227 $this->assertTrue((bool) preg_match('/' . preg_quote($text, '/') . '/ui', $this->getFieldHtml($field_name)));
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 /**
|
Chris@0
|
231 * Checks that given field does not contain the given raw text.
|
Chris@0
|
232 *
|
Chris@0
|
233 * @param string $field_name
|
Chris@0
|
234 * The name of the field to check.
|
Chris@0
|
235 * @param string $text
|
Chris@0
|
236 * The text to check.
|
Chris@0
|
237 */
|
Chris@0
|
238 protected function assertNoFieldContainsRawText($field_name, $text) {
|
Chris@0
|
239 $this->assertFalse((bool) preg_match('/' . preg_quote($text, '/') . '/ui', $this->getFieldHtml($field_name)));
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 /**
|
Chris@0
|
243 * Returns the raw HTML for the given field.
|
Chris@0
|
244 *
|
Chris@0
|
245 * @param $field_name
|
Chris@0
|
246 * The name of the field for which to return the HTML.
|
Chris@0
|
247 *
|
Chris@0
|
248 * @return string
|
Chris@0
|
249 * The raw HTML.
|
Chris@0
|
250 */
|
Chris@0
|
251 protected function getFieldHtml($field_name) {
|
Chris@0
|
252 $css_id = Html::cleanCssIdentifier('edit-' . $field_name . '-wrapper');
|
Chris@0
|
253 return $this->xpath('//*[@id=:id]', [':id' => $css_id])[0]->getHtml();
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 }
|