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